-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support parameterized replaceable evts
- Loading branch information
Showing
10 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
migrations/20221030_134400_add_deduplication_to_events_table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
exports.up = function (knex) { | ||
return knex.schema.alterTable('events', function (table) { | ||
table.jsonb('event_deduplication').nullable() | ||
}) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema.alterTable('events', function (table) { | ||
table.dropColumn('event_deduplication') | ||
}) | ||
} |
25 changes: 25 additions & 0 deletions
25
migrations/20221030_134500_add_dedulication_to_replaceable_events_unique_index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
exports.up = async function (knex) { | ||
// NIP-33: Parameterized Replaceable Events | ||
|
||
return knex.schema | ||
.raw('DROP INDEX IF EXISTS replaceable_events_idx') | ||
.raw( | ||
`CREATE UNIQUE INDEX replaceable_events_idx | ||
ON events ( event_pubkey, event_kind, event_deduplication ) | ||
WHERE | ||
( | ||
event_kind = 0 | ||
OR event_kind = 3 | ||
OR (event_kind >= 10000 AND event_kind < 20000) | ||
) | ||
OR (event_kind >= 30000 AND event_kind < 40000);`, | ||
) | ||
} | ||
|
||
exports.down = function (knex) { | ||
return knex.schema | ||
.raw('DROP INDEX IF EXISTS replaceable_events_idx') | ||
.raw( | ||
'CREATE UNIQUE INDEX replaceable_events_idx ON events ( event_pubkey, event_kind ) WHERE event_kind = 0 OR event_kind = 3 OR (event_kind >= 10000 AND event_kind < 20000);', | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
15, | ||
16, | ||
22, | ||
26 | ||
26, | ||
33 | ||
], | ||
"main": "src/index.ts", | ||
"scripts": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/handlers/event-strategies/parameterized-replaceable-event-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Event, ParameterizedReplaceableEvent } from '../../@types/event' | ||
import { EventDeduplicationMetadataKey, EventTags } from '../../constants/base' | ||
import { createLogger } from '../../factories/logger-factory' | ||
import { IEventRepository } from '../../@types/repositories' | ||
import { IEventStrategy } from '../../@types/message-handlers' | ||
import { IWebSocketAdapter } from '../../@types/adapters' | ||
import { WebSocketAdapterEvent } from '../../constants/adapter' | ||
|
||
const debug = createLogger('parameterized-replaceable-event-strategy') | ||
|
||
export class ParameterizedReplaceableEventStrategy implements IEventStrategy<Event, Promise<void>> { | ||
public constructor( | ||
private readonly webSocket: IWebSocketAdapter, | ||
private readonly eventRepository: IEventRepository, | ||
) { } | ||
|
||
public async execute(event: Event): Promise<void> { | ||
debug('received event: %o', event) | ||
|
||
const [, ...deduplication] = event.tags.find((tag) => tag.length >= 2 && tag[0] === EventTags.Deduplication) ?? [null, ''] | ||
|
||
const parameterizedReplaceableEvent: ParameterizedReplaceableEvent = { | ||
...event, | ||
[EventDeduplicationMetadataKey]: deduplication, | ||
} | ||
|
||
const count = await this.eventRepository.upsert(parameterizedReplaceableEvent) | ||
if (!count) { | ||
return | ||
} | ||
|
||
this.webSocket.emit(WebSocketAdapterEvent.Broadcast, event) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters