-
Notifications
You must be signed in to change notification settings - Fork 231
feat: add NIP-45 Event Count support #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3379f1
feat: add COUNT/CLOSED message types
a-khushal 756783c
feat: add COUNT message validation, response helpers, repository coun…
a-khushal 0eebf10
feat: add schema, factory, handler, and repository tests
a-khushal 70d654d
docs: mark NIP-45 as supported
a-khushal f3f9cd2
refactor: share filter-building logic between findByFilters and count…
a-khushal 2339f69
feat: add nip45.enabled toggle for COUNT
a-khushal 3677472
feat: add nip45.enabled test and docs
a-khushal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| added NIP-45 COUNT support with end-to-end handling (validation, handler routing, DB counting, and tests). |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| 28, | ||
| 33, | ||
| 40, | ||
| 44 | ||
| 44, | ||
| 45 | ||
| ], | ||
| "supportedNipExtensions": [ | ||
| "11a" | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| import { equals, uniqWith } from 'ramda' | ||
|
|
||
| import { IWebSocketAdapter } from '../@types/adapters' | ||
| import { IMessageHandler } from '../@types/message-handlers' | ||
| import { CountMessage } from '../@types/messages' | ||
| import { IEventRepository } from '../@types/repositories' | ||
| import { Settings } from '../@types/settings' | ||
| import { SubscriptionFilter, SubscriptionId } from '../@types/subscription' | ||
| import { WebSocketAdapterEvent } from '../constants/adapter' | ||
| import { createLogger } from '../factories/logger-factory' | ||
| import { createClosedMessage, createCountResultMessage } from '../utils/messages' | ||
|
|
||
| const debug = createLogger('count-message-handler') | ||
|
|
||
| export class CountMessageHandler implements IMessageHandler { | ||
| public constructor( | ||
| private readonly webSocket: IWebSocketAdapter, | ||
| private readonly eventRepository: IEventRepository, | ||
| private readonly settings: () => Settings, | ||
| ) {} | ||
|
|
||
| public async handleMessage(message: CountMessage): Promise<void> { | ||
| const queryId = message[1] | ||
| const countEnabled = this.settings().nip45?.enabled ?? true | ||
| if (!countEnabled) { | ||
| this.webSocket.emit(WebSocketAdapterEvent.Message, createClosedMessage(queryId, 'COUNT is disabled by relay configuration')) | ||
| return | ||
| } | ||
|
|
||
| // Some clients send the same filter more than once. | ||
| // We remove duplicates so we do less DB work. | ||
| const filters = uniqWith(equals, message.slice(2)) as SubscriptionFilter[] | ||
|
|
||
| const reason = this.canCount(queryId, filters) | ||
| if (reason) { | ||
| debug('count request %s with %o rejected: %s', queryId, filters, reason) | ||
| // NIP-45 says we should close rejected COUNT requests with a reason. | ||
| this.webSocket.emit(WebSocketAdapterEvent.Message, createClosedMessage(queryId, reason)) | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| const count = await this.eventRepository.countByFilters(filters) | ||
| this.webSocket.emit(WebSocketAdapterEvent.Message, createCountResultMessage(queryId, { count })) | ||
| } catch (error) { | ||
| debug('count request %s failed: %o', queryId, error) | ||
| // Keep this message generic so internal errors are not leaked to clients. | ||
| this.webSocket.emit(WebSocketAdapterEvent.Message, createClosedMessage(queryId, 'error: unable to count events')) | ||
| } | ||
| } | ||
|
|
||
| private canCount(queryId: SubscriptionId, filters: SubscriptionFilter[]): string | undefined { | ||
| const subscriptionLimits = this.settings().limits?.client?.subscription | ||
| const maxFilters = subscriptionLimits?.maxFilters ?? 0 | ||
|
|
||
| if (maxFilters > 0 && filters.length > maxFilters) { | ||
| return `Too many filters: Number of filters per count query must be less than or equal to ${maxFilters}` | ||
| } | ||
|
|
||
| if ( | ||
| typeof subscriptionLimits?.maxSubscriptionIdLength === 'number' && | ||
| queryId.length > subscriptionLimits.maxSubscriptionIdLength | ||
| ) { | ||
| return `Query ID too long: Query ID must be less than or equal to ${subscriptionLimits.maxSubscriptionIdLength}` | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a setting to enable or disable COUNT. We can have it enabled by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed settings to toggle COUNT here; 666f8a6, 1de0a5b