Skip to content

Commit

Permalink
use v2 moderation endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Aug 2, 2024
1 parent 8d93df4 commit 0c1a97b
Show file tree
Hide file tree
Showing 8 changed files with 3,453 additions and 369 deletions.
9 changes: 5 additions & 4 deletions __tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,14 @@ describe('messages API', () => {
});

it('flag', async () => {
const response = await client.flag({
target_message_id: messageId!,
const response = await client.moderation.v2Flag({
entity_type: 'stream:chat:v1:message',
entity_id: messageId!,
user_id: user.id,
reason: 'disrespectful',
reason: 'hate',
});

expect(response.flag?.target_message_id).toBe(messageId!);
expect(response.item_id).toBeDefined();
});

it('truncate', async () => {
Expand Down
15 changes: 9 additions & 6 deletions __tests__/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ describe('user API', () => {
});

it('ban and unban', async () => {
await client.ban({
await client.moderation.v2Ban({
target_user_id: newUser.id,
user_id: user.id,
banned_by_id: user.id,
timeout: 5,
});

let queryResponse = await client.queryBannedUsers({
Expand All @@ -111,7 +112,10 @@ describe('user API', () => {
queryResponse.bans.find((b) => b.user?.id === newUser.id),
).toBeDefined();

await client.unban({ target_user_id: newUser.id, created_by: user.id });
await client.moderation.v2Unban({
target_user_id: newUser.id,
created_by: user.id,
});

queryResponse = await client.queryBannedUsers({
payload: { filter_conditions: {} },
Expand All @@ -123,18 +127,17 @@ describe('user API', () => {
});

it('mute and unmute', async () => {
const muteResponse = await client.muteUser({
const muteResponse = await client.moderation.v2Mute({
target_ids: [newUser.id],
user_id: user.id,
timeout: 5,
});

expect(muteResponse.mutes?.[0]?.target?.id).toBe(newUser.id);

const unmuteResponse = await client.unmuteUser({
const unmuteResponse = await client.moderation.v2Unmute({
target_ids: [newUser.id],
user_id: user.id,
timeout: 5,
});

expect(unmuteResponse).toBeDefined();
Expand Down
8 changes: 8 additions & 0 deletions src/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import crypto from 'crypto';
import { StreamChatClient } from './StreamChatClient';
import { CallTokenPayload, RateLimit, UserTokenPayload } from './types';
import { QueryBannedUsersRequest, UserRequest } from './gen/models';
import { StreamModerationClient } from './StreamModerationClient';

export interface StreamClientOptions {
timeout?: number;
Expand All @@ -14,6 +15,7 @@ export interface StreamClientOptions {
export class StreamClient extends CommonApi {
public readonly video: StreamVideoClient;
public readonly chat: StreamChatClient;
public readonly moderation: StreamModerationClient;
public readonly options: StreamClientOptions = {};

private static readonly DEFAULT_TIMEOUT = 3000;
Expand Down Expand Up @@ -47,6 +49,12 @@ export class StreamClient extends CommonApi {
timeout,
baseUrl: chatBaseUrl,
});
this.moderation = new StreamModerationClient({
apiKey,
token,
timeout,
baseUrl: chatBaseUrl,
});
}

upsertUsers = (users: UserRequest[]) => {
Expand Down
3 changes: 3 additions & 0 deletions src/StreamModerationClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ModerationApi } from './gen/moderation/ModerationApi';

export class StreamModerationClient extends ModerationApi {}
149 changes: 0 additions & 149 deletions src/gen/common/CommonApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BaseApi } from '../../BaseApi';
import { StreamResponse } from '../../types';
import {
BanRequest,
BlockUsersRequest,
BlockUsersResponse,
CheckExternalStorageResponse,
Expand Down Expand Up @@ -33,8 +32,6 @@ import {
ExportUserResponse,
ExportUsersRequest,
ExportUsersResponse,
FlagRequest,
FlagResponse,
GetApplicationResponse,
GetBlockListResponse,
GetBlockedUsersResponse,
Expand All @@ -50,8 +47,6 @@ import {
ListPermissionsResponse,
ListPushProvidersResponse,
ListRolesResponse,
MuteUserRequest,
MuteUserResponse,
QueryUsersPayload,
QueryUsersResponse,
ReactivateUserRequest,
Expand All @@ -62,8 +57,6 @@ import {
RestoreUsersRequest,
UnblockUsersRequest,
UnblockUsersResponse,
UnmuteResponse,
UnmuteUserRequest,
UpdateAppRequest,
UpdateBlockListRequest,
UpdateExternalStorageRequest,
Expand All @@ -75,7 +68,6 @@ import {
UpsertPushProviderResponse,
} from '../models';
import {
encodeBanRequest,
encodeBlockUsersRequest,
decodeBlockUsersResponse,
decodeCheckExternalStorageResponse,
Expand Down Expand Up @@ -107,8 +99,6 @@ import {
decodeExportUserResponse,
encodeExportUsersRequest,
decodeExportUsersResponse,
encodeFlagRequest,
decodeFlagResponse,
decodeGetApplicationResponse,
decodeGetBlockListResponse,
decodeGetBlockedUsersResponse,
Expand All @@ -124,8 +114,6 @@ import {
decodeListPermissionsResponse,
decodeListPushProvidersResponse,
decodeListRolesResponse,
encodeMuteUserRequest,
decodeMuteUserResponse,
encodeQueryUsersPayload,
decodeQueryUsersResponse,
encodeReactivateUserRequest,
Expand All @@ -136,8 +124,6 @@ import {
encodeRestoreUsersRequest,
encodeUnblockUsersRequest,
decodeUnblockUsersResponse,
decodeUnmuteResponse,
encodeUnmuteUserRequest,
encodeUpdateAppRequest,
encodeUpdateBlockListRequest,
encodeUpdateExternalStorageRequest,
Expand Down Expand Up @@ -684,141 +670,6 @@ export class CommonApi extends BaseApi {
return { ...response.body, responseHeaders: response.headers };
};

unban = async (request: {
target_user_id: string;
channel_cid?: string;
created_by?: string;
}): Promise<StreamResponse<Response>> => {
const requestCopy = JSON.parse(JSON.stringify(request ?? {}));

const queryParams = {
target_user_id: requestCopy.target_user_id,
channel_cid: requestCopy.channel_cid,
created_by: requestCopy.created_by,
};

const response = await this.sendRequest<StreamResponse<Response>>(
'DELETE',
'/api/v2/moderation/ban',
undefined,
queryParams,
);

decodeResponse(response.body);

return { ...response.body, responseHeaders: response.headers };
};

ban = async (request: BanRequest): Promise<StreamResponse<Response>> => {
const requestCopy = JSON.parse(JSON.stringify(request ?? {}));

const body = {
target_user_id: requestCopy.target_user_id,
banned_by_id: requestCopy.banned_by_id,
channel_cid: requestCopy.channel_cid,
ip_ban: requestCopy.ip_ban,
reason: requestCopy.reason,
shadow: requestCopy.shadow,
timeout: requestCopy.timeout,
user_id: requestCopy.user_id,
banned_by: requestCopy.banned_by,
user: requestCopy.user,
};
encodeBanRequest(body);

const response = await this.sendRequest<StreamResponse<Response>>(
'POST',
'/api/v2/moderation/ban',
undefined,
undefined,
body,
);

decodeResponse(response.body);

return { ...response.body, responseHeaders: response.headers };
};

flag = async (
request?: FlagRequest,
): Promise<StreamResponse<FlagResponse>> => {
const requestCopy = JSON.parse(JSON.stringify(request ?? {}));

const body = {
reason: requestCopy.reason,
target_message_id: requestCopy.target_message_id,
target_user_id: requestCopy.target_user_id,
user_id: requestCopy.user_id,
custom: requestCopy.custom,
user: requestCopy.user,
};
encodeFlagRequest(body);

const response = await this.sendRequest<StreamResponse<FlagResponse>>(
'POST',
'/api/v2/moderation/flag',
undefined,
undefined,
body,
);

decodeFlagResponse(response.body);

return { ...response.body, responseHeaders: response.headers };
};

muteUser = async (
request: MuteUserRequest,
): Promise<StreamResponse<MuteUserResponse>> => {
const requestCopy = JSON.parse(JSON.stringify(request ?? {}));

const body = {
timeout: requestCopy.timeout,
user_id: requestCopy.user_id,
target_ids: requestCopy.target_ids,
user: requestCopy.user,
};
encodeMuteUserRequest(body);

const response = await this.sendRequest<StreamResponse<MuteUserResponse>>(
'POST',
'/api/v2/moderation/mute',
undefined,
undefined,
body,
);

decodeMuteUserResponse(response.body);

return { ...response.body, responseHeaders: response.headers };
};

unmuteUser = async (
request: UnmuteUserRequest,
): Promise<StreamResponse<UnmuteResponse>> => {
const requestCopy = JSON.parse(JSON.stringify(request ?? {}));

const body = {
timeout: requestCopy.timeout,
user_id: requestCopy.user_id,
target_ids: requestCopy.target_ids,
user: requestCopy.user,
};
encodeUnmuteUserRequest(body);

const response = await this.sendRequest<StreamResponse<UnmuteResponse>>(
'POST',
'/api/v2/moderation/unmute',
undefined,
undefined,
body,
);

decodeUnmuteResponse(response.body);

return { ...response.body, responseHeaders: response.headers };
};

getOG = async (request: {
url: string;
}): Promise<StreamResponse<GetOGResponse>> => {
Expand Down
Loading

0 comments on commit 0c1a97b

Please sign in to comment.