Skip to content

Commit

Permalink
feat(lib): add new events related to moderation (#156)
Browse files Browse the repository at this point in the history
* feat(lib): add new events related to moderation

* feat(lib): add tests to moderation events
  • Loading branch information
piotr-suwala authored Dec 11, 2023
1 parent 992e3d0 commit dbbe034
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,24 @@ export class Chat {

if (!params.ban && !params.mute) {
await this.sdk.objects.removeChannelMembers({ channel, uuids: [userId] })
await this.emitEvent({
type: "moderation",
channel: userId,
payload: {
channelId: channel,
restriction: "lifted",
},
})
} else {
await this.sdk.objects.setChannelMembers({ channel, uuids: [{ id: userId, custom: params }] })
await this.emitEvent({
type: "moderation",
channel: userId,
payload: {
channelId: channel,
restriction: params.ban ? "banned" : "muted",
},
})
}
}
}
22 changes: 20 additions & 2 deletions lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type InviteEventParams = {
type: "invite"
channel: string
}
type ModerationEventParams = {
type: "moderation"
channel: string
}
type CustomEventParams = {
type: "custom"
method: "signal" | "publish"
Expand All @@ -59,6 +63,7 @@ export type EventParams = {
mention: MentionEventParams
invite: InviteEventParams
custom: CustomEventParams
moderation: ModerationEventParams
}

type TypingEventPayload = {
Expand All @@ -82,6 +87,10 @@ type InviteEventPayload = {
channelType: ChannelType | "unknown"
channelId: string
}
type ModerationEventPayload = {
channelId: string
restriction: "muted" | "banned" | "lifted"
}
type CustomEventPayload = any

export type EventPayloads = {
Expand All @@ -90,6 +99,7 @@ export type EventPayloads = {
receipt: ReceiptEventPayload
mention: MentionEventPayload
invite: InviteEventPayload
moderation: ModerationEventPayload
custom: CustomEventPayload
}

Expand All @@ -100,8 +110,16 @@ export type EmitEventParams =
| (MentionEventParams & { payload: MentionEventPayload })
| (InviteEventParams & { payload: InviteEventPayload })
| (CustomEventParams & { payload: CustomEventPayload })

export type EventType = "typing" | "report" | "receipt" | "mention" | "invite" | "custom"
| (ModerationEventParams & { payload: ModerationEventPayload })

export type EventType =
| "typing"
| "report"
| "receipt"
| "mention"
| "invite"
| "custom"
| "moderation"
export type GenericEventParams<T extends keyof EventParams> = EventParams[T]

export type MessageActions = {
Expand Down
59 changes: 59 additions & 0 deletions lib/tests/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,4 +1069,63 @@ describe("Channel test", () => {

await publicChannel.delete()
})

test("should set (or lift) restrictions on a user", async () => {
const moderationEventCallback = jest.fn()

const removeModerationListener = chat.listenForEvents({
channel: chat.currentUser.id,
type: "moderation",
callback: moderationEventCallback,
})

await chat.setRestrictions(chat.currentUser.id, "some-channel", { mute: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "muted",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", { ban: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "banned",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", { ban: true, mute: true })
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "banned",
},
})
)
moderationEventCallback.mockReset()

await chat.setRestrictions(chat.currentUser.id, "some-channel", {})
await sleep(150) // Wait for the message to be sent and cached
expect(moderationEventCallback).toHaveBeenCalledWith(
expect.objectContaining({
payload: {
channelId: "PUBNUB_INTERNAL_MODERATION_some-channel",
restriction: "lifted",
},
})
)

removeModerationListener()
})
})

0 comments on commit dbbe034

Please sign in to comment.