mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
groupChatReceived webhook fix (#10802)
* Moved sendGroupChatReceivedWebhooks to group.sendChat function. * Added test for new functionality.
This commit is contained in:
committed by
Phillip Thelen
parent
15e2e2ab07
commit
f5a2cf6726
@@ -1843,6 +1843,62 @@ describe('Group Model', () => {
|
|||||||
expect(options.chat).to.eql(chat);
|
expect(options.chat).to.eql(chat);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('sends webhooks for users with webhooks triggered by system messages', async () => {
|
||||||
|
let guild = new Group({
|
||||||
|
name: 'some guild',
|
||||||
|
type: 'guild',
|
||||||
|
});
|
||||||
|
|
||||||
|
let memberWithWebhook = new User({
|
||||||
|
guilds: [guild._id],
|
||||||
|
webhooks: [{
|
||||||
|
type: 'groupChatReceived',
|
||||||
|
url: 'http://someurl.com',
|
||||||
|
options: {
|
||||||
|
groupId: guild._id,
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
let memberWithoutWebhook = new User({
|
||||||
|
guilds: [guild._id],
|
||||||
|
});
|
||||||
|
let nonMemberWithWebhooks = new User({
|
||||||
|
webhooks: [{
|
||||||
|
type: 'groupChatReceived',
|
||||||
|
url: 'http://a-different-url.com',
|
||||||
|
options: {
|
||||||
|
groupId: generateUUID(),
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
memberWithWebhook.save(),
|
||||||
|
memberWithoutWebhook.save(),
|
||||||
|
nonMemberWithWebhooks.save(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
guild.leader = memberWithWebhook._id;
|
||||||
|
|
||||||
|
await guild.save();
|
||||||
|
|
||||||
|
const groupMessage = guild.sendChat('Test message.');
|
||||||
|
await groupMessage.save();
|
||||||
|
|
||||||
|
await sleep();
|
||||||
|
|
||||||
|
expect(groupChatReceivedWebhook.send).to.be.calledOnce;
|
||||||
|
|
||||||
|
let args = groupChatReceivedWebhook.send.args[0];
|
||||||
|
let webhooks = args[0].webhooks;
|
||||||
|
let options = args[1];
|
||||||
|
|
||||||
|
expect(webhooks).to.have.a.lengthOf(1);
|
||||||
|
expect(webhooks[0].id).to.eql(memberWithWebhook.webhooks[0].id);
|
||||||
|
expect(options.group).to.eql(guild);
|
||||||
|
expect(options.chat).to.eql(groupMessage);
|
||||||
|
});
|
||||||
|
|
||||||
it('sends webhooks for each user with webhooks in group', async () => {
|
it('sends webhooks for each user with webhooks in group', async () => {
|
||||||
let guild = new Group({
|
let guild = new Group({
|
||||||
name: 'some guild',
|
name: 'some guild',
|
||||||
|
|||||||
@@ -192,8 +192,6 @@ api.postChat = {
|
|||||||
} else {
|
} else {
|
||||||
res.respond(200, {message: newChatMessage});
|
res.respond(200, {message: newChatMessage});
|
||||||
}
|
}
|
||||||
|
|
||||||
group.sendGroupChatReceivedWebhooks(newChatMessage);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -494,6 +494,10 @@ schema.methods.sendChat = function sendChat (message, user, metaData) {
|
|||||||
newChatMessage._meta = metaData;
|
newChatMessage._meta = metaData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Activate the webhook for receiving group chat messages before
|
||||||
|
// newChatMessage is possibly returned
|
||||||
|
this.sendGroupChatReceivedWebhooks(newChatMessage);
|
||||||
|
|
||||||
// do not send notifications for guilds with more than 5000 users and for the tavern
|
// do not send notifications for guilds with more than 5000 users and for the tavern
|
||||||
if (NO_CHAT_NOTIFICATIONS.indexOf(this._id) !== -1 || this.memberCount > LARGE_GROUP_COUNT_MESSAGE_CUTOFF) {
|
if (NO_CHAT_NOTIFICATIONS.indexOf(this._id) !== -1 || this.memberCount > LARGE_GROUP_COUNT_MESSAGE_CUTOFF) {
|
||||||
return newChatMessage;
|
return newChatMessage;
|
||||||
|
|||||||
Reference in New Issue
Block a user