Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flag options #462

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
Device,
Event,
EventHandler,
FlagMessageOptions,
FlagMessageResponse,
FlagUserResponse,
GetChannelTypeResponse,
Expand Down Expand Up @@ -338,11 +337,7 @@ export class StreamChat<
this.anonymous = false;

this.setUserPromise = Promise.all([setTokenPromise, wsPromise])
.then(
(result) =>
// We only return connection promise;
result[1],
)
.then((result) => result[1]) // We only return connection promise;
.catch((e) => {
throw e;
});
Expand Down Expand Up @@ -1663,7 +1658,13 @@ export class StreamChat<
});
}

async flagMessage(targetMessageID: string, options: FlagMessageOptions<UserType> = {}) {
/**
* flagMessage - flag a message
* @param {string} targetMessageID
* @param {string} [options.user_id] currentUserID, only used with serverside auth
* @returns {Promise<APIResponse>}
*/
async flagMessage(targetMessageID: string, options: { user_id?: string } = {}) {
return await this.post<FlagMessageResponse<UserType>>(
this.baseURL + '/moderation/flag',
{
Expand All @@ -1673,31 +1674,49 @@ export class StreamChat<
);
}

async flagUser(userID: string, options: FlagMessageOptions<UserType> = {}) {
/**
* flagUser - flag a user
* @param {string} targetID
* @param {string} [options.user_id] currentUserID, only used with serverside auth
* @returns {Promise<APIResponse>}
*/
async flagUser(targetID: string, options: { user_id?: string } = {}) {
return await this.post<FlagUserResponse<UserType>>(
this.baseURL + '/moderation/flag',
{
target_user_id: userID,
target_user_id: targetID,
...options,
},
);
}

async unflagMessage(messageID: string, options: FlagMessageOptions<UserType> = {}) {
/**
* unflagMessage - unflag a message
* @param {string} targetMessageID
* @param {string} [options.user_id] currentUserID, only used with serverside auth
* @returns {Promise<APIResponse>}
*/
async unflagMessage(targetMessageID: string, options: { user_id?: string } = {}) {
return await this.post<FlagMessageResponse<UserType>>(
this.baseURL + '/moderation/unflag',
{
target_message_id: messageID,
target_message_id: targetMessageID,
...options,
},
);
}

async unflagUser(userID: string, options: FlagMessageOptions<UserType> = {}) {
/**
* unflagUser - unflag a user
* @param {string} targetID
* @param {string} [options.user_id] currentUserID, only used with serverside auth
* @returns {Promise<APIResponse>}
*/
async unflagUser(targetID: string, options: { user_id?: string } = {}) {
return await this.post<FlagUserResponse<UserType>>(
this.baseURL + '/moderation/unflag',
{
target_user_id: userID,
target_user_id: targetID,
...options,
},
);
Expand Down
10 changes: 0 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,6 @@ export type ChannelQueryOptions<
watchers?: PaginationOptions;
};

export type FlagMessageOptions<UserType = UnknownType> = {
client_id?: string;
connection_id?: string;
created_by?: string;
target_message_id?: string;
target_user_id?: string;
user?: UserResponse<UserType>;
user_id?: string;
};
Comment on lines -689 to -697
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this specific type overcomplicates the functions for users, the only thing that is really needed here is user_id?: string


export type InviteOptions<
AttachmentType = UnknownType,
ChannelType = UnknownType,
Expand Down
39 changes: 39 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2810,6 +2810,28 @@ describe('Chat', () => {
expect(e).not.to.be.null;
}
});

it('Flag and Unflag a message server side', async () => {
//flag the message
const text = 'Flag me, i dare you mods!';
const { message } = await channel.sendMessage({ text });
const data = await serverAuthClient.flagMessage(message.id, {
user_id: modUserID,
});

expect(data.flag.target_message_id.toString()).to.equal(
message.id.toString(),
);
expect(data.flag.user.id).to.equal(modUserID);

//unflag the message
const unflagData = await serverAuthClient.unflagMessage(message.id, {
user_id: modUserID,
});
expect(unflagData.flag.target_message_id).to.equal(message.id);
expect(unflagData.flag.user.id).to.equal(modUserID);
});

it('Flag and Unflag a user ', async () => {
//flag the user
const data = await authClient.flagUser('eviluser');
Expand All @@ -2832,6 +2854,23 @@ describe('Chat', () => {
expect(e).not.to.be.null;
}
});

it('Flag and Unflag a user server side ', async () => {
//flag the user
const data = await serverAuthClient.flagUser('eviluser', {
user_id: modUserID,
});
expect(data.flag.target_user.id).to.equal('eviluser');
expect(data.flag.user.id).to.equal(modUserID);

//unflag the user
const unflagData = await serverAuthClient.unflagUser('eviluser', {
user_id: modUserID,
});
expect(unflagData.flag.target_user.id).to.equal('eviluser');
expect(unflagData.flag.user.id).to.equal(modUserID);
});

it.skip('Automod Simple', async () => {
const text = 'MongoDB is such a fucking piece of shit';
const data = await channel.sendMessage({ text });
Expand Down