diff --git a/lib/src/entities/channel.ts b/lib/src/entities/channel.ts index 416975a..f29f159 100644 --- a/lib/src/entities/channel.ts +++ b/lib/src/entities/channel.ts @@ -318,10 +318,10 @@ export class Channel { /* * Streaming messages */ - private INTERNAL_connect(callback: (message: Message) => void, channelId: string) { + connect(callback: (message: Message) => void) { const listener = { message: (event: MessageEvent) => { - if (event.channel !== channelId) return + if (event.channel !== this.id) return const getMessageResponseBody = this.chat.config.customPayloads.getMessageResponseBody || defaultGetMessageResponseBody if (getMessageResponseBody(event).type !== "text") return @@ -330,7 +330,7 @@ export class Channel { } const removeListener = this.chat.addListener(listener) - const unsubscribe = this.chat.subscribe(channelId) + const unsubscribe = this.chat.subscribe(this.id) return () => { removeListener() @@ -338,10 +338,6 @@ export class Channel { } } - connect(callback: (message: Message) => void) { - return this.INTERNAL_connect(callback, this.id) - } - /* * Presence */ @@ -378,15 +374,12 @@ export class Channel { /* * Messages */ - private async INTERNAL_getHistory(params: { - startTimetoken?: string - endTimetoken?: string - count?: number - channelId: string - }) { + async getHistory( + params: { startTimetoken?: string; endTimetoken?: string; count?: number } = {} + ) { try { const options = { - channels: [params.channelId], + channels: [this.id], count: params.count || 25, start: params.startTimetoken, end: params.endTimetoken, @@ -398,20 +391,16 @@ export class Channel { return { messages: - response.channels[params.channelId]?.map((messageObject) => + response.channels[this.id]?.map((messageObject) => Message.fromDTO(this.chat, messageObject) ) || [], - isMore: response.channels[params.channelId]?.length === (params.count || 25), + isMore: response.channels[this.id]?.length === (params.count || 25), } } catch (error) { throw error } } - getHistory(params: { startTimetoken?: string; endTimetoken?: string; count?: number } = {}) { - return this.INTERNAL_getHistory({ ...params, channelId: this.id }) - } - async getMessage(timetoken: string) { const previousTimetoken = String(BigInt(timetoken) + BigInt(1)) const response = await this.getHistory({ @@ -764,15 +753,22 @@ export class Channel { * Flagged messages */ - async getFlaggedMessages( + async getFlaggedMessagesHistory( params: { startTimetoken?: string; endTimetoken?: string; count?: number } = {} ) { const channel = `${INTERNAL_MODERATION_PREFIX}${this.id}` - return this.INTERNAL_getHistory({ ...params, channelId: channel }) + return this.chat.getEventsHistory({ + ...params, + channel, + }) } - streamFlaggedMessages(callback: (message: Message) => void) { + listenForFlaggedMessages(callback: (event: Event<"report">) => void) { const channel = `${INTERNAL_MODERATION_PREFIX}${this.id}` - return this.INTERNAL_connect(callback, channel) + return this.chat.listenForEvents({ + channel, + callback, + type: "report", + }) } } diff --git a/lib/src/entities/message.ts b/lib/src/entities/message.ts index 9e4572a..06e1eb4 100644 --- a/lib/src/entities/message.ts +++ b/lib/src/entities/message.ts @@ -369,6 +369,18 @@ export class Message { } async report(reason: string) { + const channel = INTERNAL_ADMIN_CHANNEL + const payload = { + text: this.text, + reason, + reportedMessageChannelId: this.channelId, + reportedMessageTimetoken: this.timetoken, + reportedUserId: this.userId, + } + return await this.chat.emitEvent({ channel, type: "report", payload }) + } + + async reportOnChannel(reason: string) { const channel = `${INTERNAL_MODERATION_PREFIX}${this.channelId}` const payload = { text: this.text, diff --git a/lib/tests/message.test.ts b/lib/tests/message.test.ts index 07390aa..f98673f 100644 --- a/lib/tests/message.test.ts +++ b/lib/tests/message.test.ts @@ -1,6 +1,7 @@ import { Channel, Chat, + INTERNAL_ADMIN_CHANNEL, Message, MessageDraft, CryptoUtils, @@ -473,7 +474,10 @@ describe("Send message test", () => { await reportedMessage.report(reportReason) await sleep(150) // history calls have around 130ms of cache time - const adminChannelHistory = await channel.getFlaggedMessages({ count: 1 }) + const adminChannel = await chat.getChannel(INTERNAL_ADMIN_CHANNEL) + expect(adminChannel).toBeDefined() + + const adminChannelHistory = await adminChannel.getHistory({ count: 1 }) const reportMessage = adminChannelHistory.messages[0] expect(reportMessage?.content.type).toBe("report")