|
| 1 | +import { |
| 2 | + APIResponse, |
| 3 | + ModerationConfig, |
| 4 | + DefaultGenerics, |
| 5 | + ExtendableGenerics, |
| 6 | + GetConfigResponse, |
| 7 | + GetUserModerationReportResponse, |
| 8 | + MuteUserResponse, |
| 9 | + ReviewQueueFilters, |
| 10 | + ReviewQueuePaginationOptions, |
| 11 | + ReviewQueueResponse, |
| 12 | + ReviewQueueSort, |
| 13 | + UpsertConfigResponse, |
| 14 | + ModerationFlagOptions, |
| 15 | + ModerationMuteOptions, |
| 16 | + GetUserModerationReportOptions, |
| 17 | +} from './types'; |
| 18 | +import { StreamChat } from './client'; |
| 19 | +import { normalizeQuerySort } from './utils'; |
| 20 | + |
| 21 | +export const MODERATION_ENTITY_TYPES = { |
| 22 | + user: 'stream:user', |
| 23 | + message: 'stream:chat:v1:message', |
| 24 | +}; |
| 25 | + |
| 26 | +// Moderation class provides all the endpoints related to moderation v2. |
| 27 | +export class Moderation<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> { |
| 28 | + client: StreamChat<StreamChatGenerics>; |
| 29 | + |
| 30 | + constructor(client: StreamChat<StreamChatGenerics>) { |
| 31 | + this.client = client; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Flag a user |
| 36 | + * |
| 37 | + * @param {string} flaggedUserID User ID to be flagged |
| 38 | + * @param {string} reason Reason for flagging the user |
| 39 | + * @param {Object} options Additional options for flagging the user |
| 40 | + * @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target user |
| 41 | + * @param {Object} options.custom Additional data to be stored with the flag |
| 42 | + * @returns |
| 43 | + */ |
| 44 | + async flagUser(flaggedUserID: string, reason: string, options: ModerationFlagOptions = {}) { |
| 45 | + return this.flag(MODERATION_ENTITY_TYPES.user, flaggedUserID, '', reason, options); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Flag a message |
| 50 | + * |
| 51 | + * @param {string} messageID Message ID to be flagged |
| 52 | + * @param {string} reason Reason for flagging the message |
| 53 | + * @param {Object} options Additional options for flagging the message |
| 54 | + * @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target message |
| 55 | + * @param {Object} options.custom Additional data to be stored with the flag |
| 56 | + * @returns |
| 57 | + */ |
| 58 | + async flagMessage(messageID: string, reason: string, options: ModerationFlagOptions = {}) { |
| 59 | + return this.flag(MODERATION_ENTITY_TYPES.message, messageID, '', reason, options); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Flag a user |
| 64 | + * |
| 65 | + * @param {string} entityType Entity type to be flagged |
| 66 | + * @param {string} entityId Entity ID to be flagged |
| 67 | + * @param {string} entityCreatorID User ID of the entity creator |
| 68 | + * @param {string} reason Reason for flagging the entity |
| 69 | + * @param {Object} options Additional options for flagging the entity |
| 70 | + * @param {string} options.user_id (For server side usage) User ID of the user who is flagging the target entity |
| 71 | + * @param {Object} options.moderation_payload Content to be flagged e.g., { texts: ['text1', 'text2'], images: ['image1', 'image2']} |
| 72 | + * @param {Object} options.custom Additional data to be stored with the flag |
| 73 | + * @returns |
| 74 | + */ |
| 75 | + async flag( |
| 76 | + entityType: string, |
| 77 | + entityId: string, |
| 78 | + entityCreatorID: string, |
| 79 | + reason: string, |
| 80 | + options: ModerationFlagOptions = {}, |
| 81 | + ) { |
| 82 | + return await this.client.post<{ item_id: string } & APIResponse>(this.client.baseURL + '/api/v2/moderation/flag', { |
| 83 | + entity_type: entityType, |
| 84 | + entity_id: entityId, |
| 85 | + entity_creator_id: entityCreatorID, |
| 86 | + reason, |
| 87 | + ...options, |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Mute a user |
| 93 | + * @param {string} targetID User ID to be muted |
| 94 | + * @param {Object} options Additional options for muting the user |
| 95 | + * @param {string} options.user_id (For server side usage) User ID of the user who is muting the target user |
| 96 | + * @param {number} options.timeout Timeout for the mute in minutes |
| 97 | + * @returns |
| 98 | + */ |
| 99 | + async muteUser(targetID: string, options: ModerationMuteOptions = {}) { |
| 100 | + return await this.client.post<MuteUserResponse<StreamChatGenerics> & APIResponse>( |
| 101 | + this.client.baseURL + '/api/v2/moderation/mute', |
| 102 | + { |
| 103 | + target_ids: [targetID], |
| 104 | + ...options, |
| 105 | + }, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Unmute a user |
| 111 | + * @param {string} targetID User ID to be unmuted |
| 112 | + * @param {Object} options Additional options for unmuting the user |
| 113 | + * @param {string} options.user_id (For server side usage) User ID of the user who is unmuting the target user |
| 114 | + * @returns |
| 115 | + */ |
| 116 | + async unmuteUser( |
| 117 | + targetID: string, |
| 118 | + options: { |
| 119 | + user_id?: string; |
| 120 | + }, |
| 121 | + ) { |
| 122 | + return await this.client.post<{ item_id: string } & APIResponse>( |
| 123 | + this.client.baseURL + '/api/v2/moderation/unmute', |
| 124 | + { |
| 125 | + target_ids: [targetID], |
| 126 | + ...options, |
| 127 | + }, |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get moderation report for a user |
| 133 | + * @param {string} userID User ID for which moderation report is to be fetched |
| 134 | + * @param {Object} options Additional options for fetching the moderation report |
| 135 | + * @param {boolean} options.create_user_if_not_exists Create user if not exists |
| 136 | + * @param {boolean} options.include_user_blocks Include user blocks |
| 137 | + * @param {boolean} options.include_user_mutes Include user mutes |
| 138 | + */ |
| 139 | + async getUserModerationReport(userID: string, options: GetUserModerationReportOptions = {}) { |
| 140 | + return await this.client.get<GetUserModerationReportResponse<StreamChatGenerics>>( |
| 141 | + this.client.baseURL + `/api/v2/moderation/user_report`, |
| 142 | + { |
| 143 | + user_id: userID, |
| 144 | + ...options, |
| 145 | + }, |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Query review queue |
| 151 | + * @param {Object} filterConditions Filter conditions for querying review queue |
| 152 | + * @param {Object} sort Sort conditions for querying review queue |
| 153 | + * @param {Object} options Pagination options for querying review queue |
| 154 | + */ |
| 155 | + async queryReviewQueue( |
| 156 | + filterConditions: ReviewQueueFilters = {}, |
| 157 | + sort: ReviewQueueSort = [], |
| 158 | + options: ReviewQueuePaginationOptions = {}, |
| 159 | + ) { |
| 160 | + return await this.client.post<ReviewQueueResponse>(this.client.baseURL + '/api/v2/moderation/review_queue', { |
| 161 | + filter: filterConditions, |
| 162 | + sort: normalizeQuerySort(sort), |
| 163 | + ...options, |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Upsert moderation config |
| 169 | + * @param {Object} config Moderation config to be upserted |
| 170 | + */ |
| 171 | + async upsertConfig(config: ModerationConfig = {}) { |
| 172 | + return await this.client.post<UpsertConfigResponse>(this.client.baseURL + '/api/v2/moderation/config', config); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get moderation config |
| 177 | + * @param {string} key Key for which moderation config is to be fetched |
| 178 | + */ |
| 179 | + async getConfig(key: string) { |
| 180 | + return await this.client.get<GetConfigResponse>(this.client.baseURL + '/api/v2/moderation/config/' + key); |
| 181 | + } |
| 182 | +} |
0 commit comments