Skip to content

Commit

Permalink
add tests for message.{flagged,unflagged} webhook events (#328)
Browse files Browse the repository at this point in the history
* add tests for message.{flagged,unflagged} webhook events
update flag function signature to allow server side flag

* make options optional

* set default options value
  • Loading branch information
thesyncim authored May 15, 2020
1 parent 5af9de7 commit 05c2281
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1184,27 +1184,31 @@ export class StreamChat {
});
}

async flagMessage(messageID) {
async flagMessage(messageID, options = {}) {
return await this.post(this.baseURL + '/moderation/flag', {
target_message_id: messageID,
...options,
});
}

async flagUser(userID) {
async flagUser(userID, options = {}) {
return await this.post(this.baseURL + '/moderation/flag', {
target_user_id: userID,
...options,
});
}

async unflagMessage(messageID) {
async unflagMessage(messageID, options = {}) {
return await this.post(this.baseURL + '/moderation/unflag', {
target_message_id: messageID,
...options,
});
}

async unflagUser(userID) {
async unflagUser(userID, options = {}) {
return await this.post(this.baseURL + '/moderation/unflag', {
target_user_id: userID,
...options,
});
}

Expand Down
43 changes: 43 additions & 0 deletions test/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,49 @@ describe('Webhooks', function() {
expect(event.channel_id).to.eq(chan.id);
});

it('should receive message flagged/unflagged event', async function() {
await chan.create();

// send a message
let sendMessageResp;
let [events] = await Promise.all([
promises.waitForEvents('message.new'),
(sendMessageResp = await chan.sendMessage({
text: 'flag candidate',
user: { id: tommasoID },
})),
]);
const msgNewEvent = events.pop();
expect(msgNewEvent).to.not.be.null;
expect(msgNewEvent.type).to.eq('message.new');
expect(msgNewEvent.channel_type).to.eq(chan.type);
expect(msgNewEvent.channel_id).to.eq(chan.id);

// expect message.flagged event
[events] = await Promise.all([
promises.waitForEvents('message.flagged'),
client.flagMessage(sendMessageResp.message.id, { user_id: tommasoID }),
]);
const msgFlagEvent = events.pop();
expect(msgFlagEvent).to.not.be.null;
expect(msgFlagEvent.type).to.eq('message.flagged');
expect(msgFlagEvent.channel_type).to.eq(chan.type);
expect(msgFlagEvent.channel_id).to.eq(chan.id);
expect(msgFlagEvent.message.id).to.eq(sendMessageResp.message.id);

// expect message.unflagged event
[events] = await Promise.all([
promises.waitForEvents('message.unflagged'),
client.unflagMessage(sendMessageResp.message.id, { user_id: tommasoID }),
]);
const msgUnFlagEvent = events.pop();
expect(msgUnFlagEvent).to.not.be.null;
expect(msgUnFlagEvent.type).to.eq('message.unflagged');
expect(msgUnFlagEvent.channel_type).to.eq(chan.type);
expect(msgUnFlagEvent.channel_id).to.eq(chan.id);
expect(msgUnFlagEvent.message.id).to.eq(sendMessageResp.message.id);
});

it('should receive new message event with members included', async function() {
await Promise.all([chan.addMembers([thierryID]), chan.addMembers([tommasoID])]);
const [events] = await Promise.all([
Expand Down
8 changes: 4 additions & 4 deletions types/stream-chat/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ export class StreamChat {
muteUser(targetUserID: string): Promise<MuteAPIResponse>;
unmuteUser(targetUserID: string): Promise<UnmuteAPIResponse>;

flagUser(userID: string): Promise<FlagAPIResponse>;
unflagUser(userID: string): Promise<UnflagAPIResponse>;
flagMessage(messageID: string): Promise<FlagAPIResponse>;
unflagMessage(messageID: string): Promise<UnflagAPIResponse>;
flagUser(userID: string, options?: object): Promise<FlagAPIResponse>;
unflagUser(userID: string, options?: object): Promise<UnflagAPIResponse>;
flagMessage(messageID: string, options?: object): Promise<FlagAPIResponse>;
unflagMessage(messageID: string, options?: object): Promise<UnflagAPIResponse>;

createChannelType(data: ChannelData): Promise<CreateChannelTypeAPIResponse>;
getChannelType(channelType: string): Promise<GetChannelTypeAPIResponse>;
Expand Down

0 comments on commit 05c2281

Please sign in to comment.