Skip to content

Commit

Permalink
feat(lib): next iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Jul 16, 2024
1 parent 3924c6a commit 91bef3c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
44 changes: 20 additions & 24 deletions lib/src/entities/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -330,18 +330,14 @@ export class Channel {
}

const removeListener = this.chat.addListener(listener)
const unsubscribe = this.chat.subscribe(channelId)
const unsubscribe = this.chat.subscribe(this.id)

return () => {
removeListener()
unsubscribe()
}
}

connect(callback: (message: Message) => void) {
return this.INTERNAL_connect(callback, this.id)
}

/*
* Presence
*/
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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",
})
}
}
12 changes: 12 additions & 0 deletions lib/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion lib/tests/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Channel,
Chat,
INTERNAL_ADMIN_CHANNEL,
Message,
MessageDraft,
CryptoUtils,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 91bef3c

Please sign in to comment.