From dbbe034a41ab2fe15b0d3780c7ce26db70ea216a Mon Sep 17 00:00:00 2001 From: piotr-suwala <112620304+piotr-suwala@users.noreply.github.com> Date: Mon, 11 Dec 2023 14:00:00 +0100 Subject: [PATCH] feat(lib): add new events related to moderation (#156) * feat(lib): add new events related to moderation * feat(lib): add tests to moderation events --- lib/src/entities/chat.ts | 16 +++++++++++ lib/src/types.ts | 22 +++++++++++++-- lib/tests/channel.test.ts | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/lib/src/entities/chat.ts b/lib/src/entities/chat.ts index 6a48bfc..76cd112 100644 --- a/lib/src/entities/chat.ts +++ b/lib/src/entities/chat.ts @@ -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", + }, + }) } } } diff --git a/lib/src/types.ts b/lib/src/types.ts index 07af1f0..75984a6 100644 --- a/lib/src/types.ts +++ b/lib/src/types.ts @@ -46,6 +46,10 @@ type InviteEventParams = { type: "invite" channel: string } +type ModerationEventParams = { + type: "moderation" + channel: string +} type CustomEventParams = { type: "custom" method: "signal" | "publish" @@ -59,6 +63,7 @@ export type EventParams = { mention: MentionEventParams invite: InviteEventParams custom: CustomEventParams + moderation: ModerationEventParams } type TypingEventPayload = { @@ -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 = { @@ -90,6 +99,7 @@ export type EventPayloads = { receipt: ReceiptEventPayload mention: MentionEventPayload invite: InviteEventPayload + moderation: ModerationEventPayload custom: CustomEventPayload } @@ -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 = EventParams[T] export type MessageActions = { diff --git a/lib/tests/channel.test.ts b/lib/tests/channel.test.ts index 534bfe4..b30d0a5 100644 --- a/lib/tests/channel.test.ts +++ b/lib/tests/channel.test.ts @@ -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() + }) })