Skip to content

Commit d72665a

Browse files
committed
fix: importer took multiple D tags
1 parent f5093cb commit d72665a

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/services/event-import-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ const enrichEventMetadata = (event: Event): Event => {
2626
}
2727

2828
if (isParameterizedReplaceableEvent(event)) {
29-
const [, ...deduplication] = event.tags.find((tag) => tag.length >= 2 && tag[0] === EventTags.Deduplication) ?? [
29+
const [, deduplication] = event.tags.find((tag) => tag.length >= 2 && tag[0] === EventTags.Deduplication) ?? [
3030
null,
3131
'',
3232
]
33-
enriched = { ...enriched, [EventDeduplicationMetadataKey]: deduplication }
33+
enriched = { ...enriched, [EventDeduplicationMetadataKey]: deduplication ? [deduplication] : [''] }
3434
}
3535

3636
return enriched as Event

test/unit/services/event-import-service.spec.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { join } from 'path'
33
import fs from 'fs'
44
import os from 'os'
55

6-
import { EventImportLineError, EventImportService, EventImportStats } from '../../../src/services/event-import-service'
6+
import {
7+
createEventBatchPersister,
8+
EventImportLineError,
9+
EventImportService,
10+
EventImportStats,
11+
} from '../../../src/services/event-import-service'
12+
import { EventDeduplicationMetadataKey, EventKinds, EventTags } from '../../../src/constants/base'
713
import { Event } from '../../../src/@types/event'
814
import { expect } from 'chai'
915
import { getEvents } from '../data/events'
@@ -170,4 +176,36 @@ describe('EventImportService', () => {
170176
expect(lineErrors.length).to.equal(0)
171177
}
172178
})
179+
180+
it('normalizes parameterized replaceable deduplication to first d tag value', async () => {
181+
const parameterizedEvent: Event = {
182+
id: 'a'.repeat(64),
183+
pubkey: 'b'.repeat(64),
184+
created_at: 1,
185+
kind: EventKinds.PARAMETERIZED_REPLACEABLE_FIRST,
186+
tags: [[EventTags.Deduplication, 'one', 'two']],
187+
content: 'hello',
188+
sig: 'c'.repeat(128),
189+
}
190+
191+
let upsertedEvents: Event[] = []
192+
193+
const eventRepository = {
194+
create: async () => 0,
195+
createMany: async () => 0,
196+
upsert: async () => 0,
197+
upsertMany: async (events: Event[]) => {
198+
upsertedEvents = events
199+
return events.length
200+
},
201+
deleteByPubkeyAndIds: async () => 0,
202+
} as any
203+
204+
const persistBatch = createEventBatchPersister(eventRepository)
205+
const inserted = await persistBatch([parameterizedEvent])
206+
207+
expect(inserted).to.equal(1)
208+
expect(upsertedEvents).to.have.length(1)
209+
expect((upsertedEvents[0] as any)[EventDeduplicationMetadataKey]).to.deep.equal(['one'])
210+
})
173211
})

0 commit comments

Comments
 (0)