Skip to content

Commit

Permalink
feat(lib): moderation restriction methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Salet committed Oct 16, 2023
1 parent 86132d6 commit 9157623
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const MESSAGE_THREAD_ID_PREFIX = "PUBNUB_INTERNAL_THREAD"
export const INTERNAL_MODERATION_PREFIX = "PUBNUB_INTERNAL_MODERATION_"
export const INTERNAL_ADMIN_CHANNEL = "PUBNUB_INTERNAL_ADMIN_CHANNEL"
export const ERROR_LOGGER_KEY_PREFIX = "PUBNUB_INTERNAL_ERROR_LOGGER"
55 changes: 55 additions & 0 deletions lib/src/entities/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { User } from "./user"
import { MentionsUtils } from "../mentions-utils"
import { MessageDraft } from "./message-draft"
import { getErrorProxiedEntity } from "../error-logging"
import { INTERNAL_MODERATION_PREFIX } from "../constants"

export type ChannelFields = Pick<
Channel,
Expand Down Expand Up @@ -644,4 +645,58 @@ export class Channel {
async deleteFile(params: { id: string; name: string }) {
return this.chat.sdk.deleteFile({ channel: this.id, ...params })
}

/**
* Moderation restrictions
*/

async setResctrictions(user: User, params: { ban?: boolean; mute?: boolean }) {
if (!(this.chat.sdk as any)._config.secretKey)
throw "Moderation restrictions can only be set by clients initialized with a Secret Key."
return this.chat.setResctrictions(user.id, this.id, params)
}

/* @internal */
private async getRestrictions(
user?: User,
params?: Pick<PubNub.GetChannelMembersParameters, "limit" | "page" | "sort">
) {
return await this.chat.sdk.objects.getChannelMembers({
channel: `${INTERNAL_MODERATION_PREFIX}${this.id}`,
include: {
totalCount: true,
customFields: true,
},
...(user && { filter: `uuid.id == '${user.id}'` }),
...params,
})
}

async getUserRestrictions(user: User) {
const response = await this.getRestrictions(user)
const restrictions = response && response.data[0]?.custom
return {
ban: !!restrictions?.ban,
mute: !!restrictions?.mute,
}
}

async getUsersRestrictions(
params?: Pick<PubNub.GetChannelMembersParameters, "limit" | "page" | "sort">
) {
const response = await this.getRestrictions(undefined, params)
return {
page: {
next: response.next,
prev: response.prev,
},
total: response.totalCount,
status: response.status,
restrictions: response.data.map(({ custom, uuid }) => ({
ban: !!custom?.ban,
mute: !!custom?.mute,
userId: uuid.id,
})),
}
}
}
17 changes: 16 additions & 1 deletion lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { Message } from "./message"
import { Event } from "./event"
import { Membership } from "./membership"
import { MESSAGE_THREAD_ID_PREFIX } from "../constants"
import { MESSAGE_THREAD_ID_PREFIX, INTERNAL_MODERATION_PREFIX } from "../constants"
import { ThreadChannel } from "./thread-channel"
import { MentionsUtils } from "../mentions-utils"
import { getErrorProxiedEntity, ErrorLogger } from "../error-logging"
Expand Down Expand Up @@ -1067,4 +1067,19 @@ export class Chat {
),
}
}

/**
* Moderation restrictions
*/

async setResctrictions(
userId: string,
channelId: string,
params: { ban?: boolean; mute?: boolean }
) {
await this.sdk.objects.setChannelMembers({
channel: `${INTERNAL_MODERATION_PREFIX}${channelId}`,
uuids: [{ id: userId, custom: params }],
})
}
}
61 changes: 60 additions & 1 deletion lib/src/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import PubNub, { UUIDMetadataObject, ObjectCustom, GetMembershipsParametersv2 } from "pubnub"
import { Chat } from "./chat"
import { DeleteParameters, OptionalAllBut } from "../types"
import { Channel } from "./channel"
import { Membership } from "./membership"
import { INTERNAL_ADMIN_CHANNEL } from "../constants"
import { INTERNAL_ADMIN_CHANNEL, INTERNAL_MODERATION_PREFIX } from "../constants"
import { getErrorProxiedEntity } from "../error-logging"

export type UserFields = Pick<
Expand Down Expand Up @@ -122,6 +123,7 @@ export class User {
channelFields: true,
customChannelFields: true,
},
filter: `!(channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*')`,
})

return {
Expand All @@ -137,6 +139,63 @@ export class User {
}
}

/**
* Moderation restrictions
*/

async setResctrictions(channel: Channel, params: { ban?: boolean; mute?: boolean }) {
if (!(this.chat.sdk as any)._config.secretKey)
throw "Moderation restrictions can only be set by clients initialized with a Secret Key."
return this.chat.setResctrictions(this.id, channel.id, params)
}

/* @internal */
private async getRestrictions(
channel?: Channel,
params?: Pick<PubNub.GetMembershipsParametersv2, "limit" | "page" | "sort">
) {
const filter = channel
? `channel.id == '${INTERNAL_MODERATION_PREFIX}${channel.id}'`
: `channel.id LIKE '${INTERNAL_MODERATION_PREFIX}*'`
return await this.chat.sdk.objects.getMemberships({
uuid: this.id,
include: {
totalCount: true,
customFields: true,
},
filter,
...params,
})
}

async getChannelRestrictions(channel: Channel) {
const response = await this.getRestrictions(channel)
const restrictions = response && response.data[0]?.custom
return {
ban: !!restrictions?.ban,
mute: !!restrictions?.mute,
}
}

async getChannelsRestrictions(
params?: Pick<PubNub.GetChannelMembersParameters, "limit" | "page" | "sort">
) {
const response = await this.getRestrictions(undefined, params)
return {
page: {
next: response.next,
prev: response.prev,
},
total: response.totalCount,
status: response.status,
restrictions: response.data.map(({ custom, channel }) => ({
ban: !!custom?.ban,
mute: !!custom?.mute,
channelId: channel.id.replace(INTERNAL_MODERATION_PREFIX, ""),
})),
}
}

/*
* Other
*/
Expand Down

0 comments on commit 9157623

Please sign in to comment.