-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathvanish-event-strategy.ts
More file actions
36 lines (29 loc) · 1.27 KB
/
vanish-event-strategy.ts
File metadata and controls
36 lines (29 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { IEventRepository, IUserRepository } from '../../@types/repositories'
import { createCommandResult } from '../../utils/messages'
import { createLogger } from '../../factories/logger-factory'
import { Event } from '../../@types/event'
import { EventKinds } from '../../constants/base'
import { IEventStrategy } from '../../@types/message-handlers'
import { IWebSocketAdapter } from '../../@types/adapters'
import { WebSocketAdapterEvent } from '../../constants/adapter'
const debug = createLogger('vanish-event-strategy')
export class VanishEventStrategy implements IEventStrategy<Event, Promise<void>> {
public constructor(
private readonly webSocket: IWebSocketAdapter,
private readonly eventRepository: IEventRepository,
private readonly userRepository: IUserRepository,
) {}
public async execute(event: Event): Promise<void> {
debug('received request to vanish event: %o', event)
await this.eventRepository.deleteByPubkeyExceptKinds(
event.pubkey,
[EventKinds.REQUEST_TO_VANISH],
)
const count = await this.eventRepository.create(event)
await this.userRepository.setVanished(event.pubkey, true)
this.webSocket.emit(
WebSocketAdapterEvent.Message,
createCommandResult(event.id, true, count ? '' : 'duplicate:')
)
}
}