-
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.
Revert "Merge branch 'main' of https://github.com/Cameri/nostream"
- Loading branch information
Showing
34 changed files
with
1,252 additions
and
1,466 deletions.
There are no files selected for viewing
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
18 changes: 0 additions & 18 deletions
18
migrations/20240111204900_remove_delegator_from_events_table.js
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
70 changes: 0 additions & 70 deletions
70
src/controllers/admission/get-admission-check-controller.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/factories/controllers/get-admission-check-controller-factory.ts
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
import { isDeleteEvent, isEphemeralEvent, isReplaceableEvent } from '../utils/event' | ||
import { DefaultEventStrategy } from '../handlers/event-strategies/default-event-strategy' | ||
import { EphemeralEventStrategy } from '../handlers/event-strategies/ephemeral-event-strategy' | ||
import { Event } from '../@types/event' | ||
import { Factory } from '../@types/base' | ||
import { IEventRepository } from '../@types/repositories' | ||
import { IEventStrategy } from '../@types/message-handlers' | ||
import { IWebSocketAdapter } from '../@types/adapters' | ||
|
||
export const delegatedEventStrategyFactory = ( | ||
eventRepository: IEventRepository, | ||
): Factory<IEventStrategy<Event, Promise<void>>, [Event, IWebSocketAdapter]> => | ||
([event, adapter]: [Event, IWebSocketAdapter]) => { | ||
if (isEphemeralEvent(event)) { | ||
return new EphemeralEventStrategy(adapter) | ||
} else if (isReplaceableEvent(event) || isDeleteEvent(event)) { | ||
return | ||
} | ||
|
||
return new DefaultEventStrategy(adapter, eventRepository) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { EventDelegatorMetadataKey, EventTags } from '../constants/base' | ||
import { createCommandResult } from '../utils/messages' | ||
import { createLogger } from '../factories/logger-factory' | ||
import { DelegatedEvent } from '../@types/event' | ||
import { EventMessageHandler } from './event-message-handler' | ||
import { IMessageHandler } from '../@types/message-handlers' | ||
import { IncomingEventMessage } from '../@types/messages' | ||
import { isDelegatedEventValid } from '../utils/event' | ||
import { WebSocketAdapterEvent } from '../constants/adapter' | ||
|
||
const debug = createLogger('delegated-event-message-handler') | ||
|
||
export class DelegatedEventMessageHandler extends EventMessageHandler implements IMessageHandler { | ||
public async handleMessage(message: IncomingEventMessage): Promise<void> { | ||
const [, event] = message | ||
|
||
let reason = await this.isEventValid(event) | ||
if (reason) { | ||
debug('event %s rejected: %s', event.id, reason) | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, reason)) | ||
return | ||
} | ||
|
||
if (await this.isRateLimited(event)) { | ||
debug('event %s rejected: rate-limited') | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, 'rate-limited: slow down')) | ||
return | ||
} | ||
|
||
reason = this.canAcceptEvent(event) | ||
if (reason) { | ||
debug('event %s rejected: %s', event.id, reason) | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, reason)) | ||
return | ||
} | ||
|
||
reason = await this.isUserAdmitted(event) | ||
if (reason) { | ||
debug('event %s rejected: %s', event.id, reason) | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, reason)) | ||
return | ||
} | ||
|
||
const [, delegator] = event.tags.find((tag) => tag.length === 4 && tag[0] === EventTags.Delegation) | ||
const delegatedEvent: DelegatedEvent = { | ||
...event, | ||
[EventDelegatorMetadataKey]: delegator, | ||
} | ||
|
||
const strategy = this.strategyFactory([delegatedEvent, this.webSocket]) | ||
|
||
if (typeof strategy?.execute !== 'function') { | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, 'error: event not supported')) | ||
return | ||
} | ||
|
||
try { | ||
await strategy.execute(delegatedEvent) | ||
} catch (error) { | ||
console.error('error handling message', message, error) | ||
this.webSocket.emit(WebSocketAdapterEvent.Message, createCommandResult(event.id, false, 'error: unable to process event')) | ||
} | ||
} | ||
|
||
protected async isEventValid(event: DelegatedEvent): Promise<string | undefined> { | ||
const reason = await super.isEventValid(event) | ||
if (reason) { | ||
return reason | ||
} | ||
|
||
if (!await isDelegatedEventValid(event)) { | ||
return 'invalid: delegation verification failed' | ||
} | ||
} | ||
} |
Oops, something went wrong.