-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathmoderation.ts
More file actions
360 lines (339 loc) · 12.3 KB
/
moderation.ts
File metadata and controls
360 lines (339 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import {
APIResponse,
ModerationConfig,
DefaultGenerics,
ExtendableGenerics,
GetConfigResponse,
GetUserModerationReportResponse,
MuteUserResponse,
ReviewQueueFilters,
ReviewQueuePaginationOptions,
ReviewQueueResponse,
ReviewQueueSort,
UpsertConfigResponse,
ModerationFlagOptions,
ModerationMuteOptions,
GetUserModerationReportOptions,
SubmitActionOptions,
QueryModerationConfigsFilters,
QueryModerationConfigsSort,
Pager,
CustomCheckFlag,
ReviewQueueItem,
QueryConfigsResponse,
RequireAtLeastOne,
} from './types';
import { StreamChat } from './client';
import { normalizeQuerySort } from './utils';
export const MODERATION_ENTITY_TYPES = {
user: 'stream:user',
message: 'stream:chat:v1:message',
userprofile: 'stream:v1:user_profile',
};
// Moderation class provides all the endpoints related to moderation v2.
export class Moderation<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> {
client: StreamChat<StreamChatGenerics>;
constructor(client: StreamChat<StreamChatGenerics>) {
this.client = client;
}
/**
* Flag a user
*
* @param {string} flaggedUserID User ID to be flagged
* @param {string} reason Reason for flagging the user
* @param {Object} options Additional options for flagging the user
* @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target user
* @param {Object} options.custom Additional data to be stored with the flag
* @returns
*/
async flagUser(flaggedUserID: string, reason: string, options: ModerationFlagOptions = {}) {
return this.flag(MODERATION_ENTITY_TYPES.user, flaggedUserID, '', reason, options);
}
/**
* Flag a message
*
* @param {string} messageID Message ID to be flagged
* @param {string} reason Reason for flagging the message
* @param {Object} options Additional options for flagging the message
* @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target message
* @param {Object} options.custom Additional data to be stored with the flag
* @returns
*/
async flagMessage(messageID: string, reason: string, options: ModerationFlagOptions = {}) {
return this.flag(MODERATION_ENTITY_TYPES.message, messageID, '', reason, options);
}
/**
* Flag a user
*
* @param {string} entityType Entity type to be flagged
* @param {string} entityId Entity ID to be flagged
* @param {string} entityCreatorID User ID of the entity creator
* @param {string} reason Reason for flagging the entity
* @param {Object} options Additional options for flagging the entity
* @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target entity
* @param {Object} options.moderation_payload Content to be flagged e.g., { texts: ['text1', 'text2'], images: ['image1', 'image2']}
* @param {Object} options.custom Additional data to be stored with the flag
* @returns
*/
async flag(
entityType: string,
entityId: string,
entityCreatorID: string,
reason: string,
options: ModerationFlagOptions = {},
) {
return await this.client.post<{ item_id: string } & APIResponse>(this.client.baseURL + '/api/v2/moderation/flag', {
entity_type: entityType,
entity_id: entityId,
entity_creator_id: entityCreatorID,
reason,
...options,
});
}
/**
* Mute a user
* @param {string} targetID User ID to be muted
* @param {Object} options Additional options for muting the user
* @param {string} options.user_id (For server side usage) User ID of the user who is muting the target user
* @param {number} options.timeout Timeout for the mute in minutes
* @returns
*/
async muteUser(targetID: string, options: ModerationMuteOptions = {}) {
return await this.client.post<MuteUserResponse<StreamChatGenerics> & APIResponse>(
this.client.baseURL + '/api/v2/moderation/mute',
{
target_ids: [targetID],
...options,
},
);
}
/**
* Unmute a user
* @param {string} targetID User ID to be unmuted
* @param {Object} options Additional options for unmuting the user
* @param {string} options.user_id (For server side usage) User ID of the user who is unmuting the target user
* @returns
*/
async unmuteUser(
targetID: string,
options: {
user_id?: string;
},
) {
return await this.client.post<{ item_id: string } & APIResponse>(
this.client.baseURL + '/api/v2/moderation/unmute',
{
target_ids: [targetID],
...options,
},
);
}
/**
* Get moderation report for a user
* @param {string} userID User ID for which moderation report is to be fetched
* @param {Object} options Additional options for fetching the moderation report
* @param {boolean} options.create_user_if_not_exists Create user if not exists
* @param {boolean} options.include_user_blocks Include user blocks
* @param {boolean} options.include_user_mutes Include user mutes
*/
async getUserModerationReport(userID: string, options: GetUserModerationReportOptions = {}) {
return await this.client.get<GetUserModerationReportResponse<StreamChatGenerics>>(
this.client.baseURL + `/api/v2/moderation/user_report`,
{
user_id: userID,
...options,
},
);
}
/**
* Query review queue
* @param {Object} filterConditions Filter conditions for querying review queue
* @param {Object} sort Sort conditions for querying review queue
* @param {Object} options Pagination options for querying review queue
*/
async queryReviewQueue(
filterConditions: ReviewQueueFilters = {},
sort: ReviewQueueSort = [],
options: ReviewQueuePaginationOptions = {},
) {
return await this.client.post<ReviewQueueResponse>(this.client.baseURL + '/api/v2/moderation/review_queue', {
filter: filterConditions,
sort: normalizeQuerySort(sort),
...options,
});
}
/**
* Upsert moderation config
* @param {Object} config Moderation config to be upserted
*/
async upsertConfig(config: ModerationConfig) {
return await this.client.post<UpsertConfigResponse>(this.client.baseURL + '/api/v2/moderation/config', config);
}
/**
* Get moderation config
* @param {string} key Key for which moderation config is to be fetched
*/
async getConfig(key: string, data?: { team?: string }) {
return await this.client.get<GetConfigResponse>(this.client.baseURL + '/api/v2/moderation/config/' + key, data);
}
async deleteConfig(key: string, data?: { team?: string }) {
return await this.client.delete(this.client.baseURL + '/api/v2/moderation/config/' + key, data);
}
/**
* Query moderation configs
* @param {Object} filterConditions Filter conditions for querying moderation configs
* @param {Object} sort Sort conditions for querying moderation configs
* @param {Object} options Additional options for querying moderation configs
*/
async queryConfigs(
filterConditions: QueryModerationConfigsFilters,
sort: QueryModerationConfigsSort,
options: Pager = {},
) {
return await this.client.post<QueryConfigsResponse>(this.client.baseURL + '/api/v2/moderation/configs', {
filter: filterConditions,
sort,
...options,
});
}
async submitAction(actionType: string, itemID: string, options: SubmitActionOptions = {}) {
return await this.client.post<{ item_id: string } & APIResponse>(
this.client.baseURL + '/api/v2/moderation/submit_action',
{
action_type: actionType,
item_id: itemID,
...options,
},
);
}
/**
*
* @param {string} entityType string Type of entity to be checked E.g., stream:user, stream:chat:v1:message, or any custom string
* @param {string} entityID string ID of the entity to be checked. This is mainly for tracking purposes
* @param {string} entityCreatorID string ID of the entity creator
* @param {object} moderationPayload object Content to be checked for moderation. E.g., { texts: ['text1', 'text2'], images: ['image1', 'image2']}
* @param {Array} moderationPayload.texts array Array of texts to be checked for moderation
* @param {Array} moderationPayload.images array Array of images to be checked for moderation
* @param {Array} moderationPayload.videos array Array of videos to be checked for moderation
* @param configKey
* @param options
* @returns
*/
async check(
entityType: string,
entityID: string,
entityCreatorID: string,
moderationPayload: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
custom?: Record<string, any>;
images?: string[];
texts?: string[];
videos?: string[];
},
configKey: string,
options?: {
force_sync?: boolean;
test_mode?: boolean;
},
) {
return await this.client.post(this.client.baseURL + `/api/v2/moderation/check`, {
entity_type: entityType,
entity_id: entityID,
entity_creator_id: entityCreatorID,
moderation_payload: moderationPayload,
config_key: configKey,
options,
});
}
/**
* Experimental: Check user profile
*
* Warning: This is an experimental feature and the API is subject to change.
*
* This function is used to check a user profile for moderation.
* This will not create any review queue items for the user profile.
* You can just use this to check whether to allow a certain user profile to be created or not.
*
* Example:
*
* ```ts
* const res = await client.moderation.checkUserProfile(userId, { username: "fuck_boy_001", image: "https://example.com/profile.jpg" });
* if (res.recommended_action === "remove") {
* // Block the user profile from being created
* } else {
* // Allow the user profile to be created
* }
* ```
*
* @param userId
* @param profile.username
* @param profile.image
* @returns
*/
async checkUserProfile(userId: string, profile: RequireAtLeastOne<{ image?: string; username?: string }>) {
if (!profile.username && !profile.image) {
throw new Error('Either username or image must be provided');
}
const moderationPayload: { images?: string[]; texts?: string[] } = {};
if (profile.username) {
moderationPayload.texts = [profile.username];
}
if (profile.image) {
moderationPayload.images = [profile.image];
}
return await this.check(
MODERATION_ENTITY_TYPES.userprofile,
userId,
userId,
moderationPayload,
'user_profile:default',
{
force_sync: true,
test_mode: true,
},
);
}
/**
*
* @param {string} entityType string Type of entity to be checked E.g., stream:user, stream:chat:v1:message, or any custom string
* @param {string} entityID string ID of the entity to be checked. This is mainly for tracking purposes
* @param {string} entityCreatorID string ID of the entity creator
* @param {object} moderationPayload object Content to be checked for moderation. E.g., { texts: ['text1', 'text2'], images: ['image1', 'image2']}
* @param {Array} moderationPayload.texts array Array of texts to be checked for moderation
* @param {Array} moderationPayload.images array Array of images to be checked for moderation
* @param {Array} moderationPayload.videos array Array of videos to be checked for moderation
* @param {Array<CustomCheckFlag>} flags Array of CustomCheckFlag to be passed to flag the entity
* @returns
*/
async addCustomFlags(
entityType: string,
entityID: string,
entityCreatorID: string,
moderationPayload: {
images?: string[];
texts?: string[];
videos?: string[];
},
flags: CustomCheckFlag[],
) {
return await this.client.post<{ id: string; item: ReviewQueueItem; status: string } & APIResponse>(
this.client.baseURL + `/api/v2/moderation/custom_check`,
{
entity_type: entityType,
entity_id: entityID,
entity_creator_id: entityCreatorID,
moderation_payload: moderationPayload,
flags,
},
);
}
/**
* Add custom flags to a message
* @param {string} messageID Message ID to be flagged
* @param {Array<CustomCheckFlag>} flags Array of CustomCheckFlag to be passed to flag the message
* @returns
*/
async addCustomMessageFlags(messageID: string, flags: CustomCheckFlag[]) {
return await this.addCustomFlags(MODERATION_ENTITY_TYPES.message, messageID, '', {}, flags);
}
}