mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Webhooks v2 (and other fixes) (#10265)
* begin implementing global webhooks * add checklist item scored webhook * add pet hatched and mount raised webhooks (no tests) * fix typo * add lvl up webhooks, remove corrupt notifications and reorganize pre-save hook * fix typo * add some tests, globalActivity webhook * fix bug in global activiy webhook and add more tests * add tests and fix typo for petHatched and mountRaised webhooks * fix errors and add tests for level up webhook * wip: add default data to all webhooks, change signature for WebhookSender.send (missing tests) * remove unused code * fix unit tests * fix chat webhooks * remove console * fix lint * add and fix webhook tests * add questStarted webhook and questActivity type * add unit tests * add finial tests and features
This commit is contained in:
@@ -33,11 +33,20 @@ export class WebhookSender {
|
||||
return true;
|
||||
}
|
||||
|
||||
send (webhooks, data) {
|
||||
attachDefaultData (user, body) {
|
||||
body.webhookType = this.type;
|
||||
body.user = body.user || {};
|
||||
body.user._id = user._id;
|
||||
}
|
||||
|
||||
send (user, data) {
|
||||
const webhooks = user.webhooks;
|
||||
|
||||
let hooks = webhooks.filter((hook) => {
|
||||
return isValidWebhook(hook) &&
|
||||
this.type === hook.type &&
|
||||
this.webhookFilter(hook, data);
|
||||
if (!isValidWebhook(hook)) return false;
|
||||
if (hook.type === 'globalActivity') return true;
|
||||
|
||||
return this.type === hook.type && this.webhookFilter(hook, data);
|
||||
});
|
||||
|
||||
if (hooks.length < 1) {
|
||||
@@ -45,6 +54,7 @@ export class WebhookSender {
|
||||
}
|
||||
|
||||
let body = this.transformData(data);
|
||||
this.attachDefaultData(user, body);
|
||||
|
||||
hooks.forEach((hook) => {
|
||||
sendWebhook(hook.url, body);
|
||||
@@ -65,7 +75,7 @@ export let taskScoredWebhook = new WebhookSender({
|
||||
let extendedStats = user.addComputedStatsToJSONObj(user.stats.toJSON());
|
||||
|
||||
let userData = {
|
||||
_id: user._id,
|
||||
// _id: user._id, added automatically when the webhook is sent
|
||||
_tmp: user._tmp,
|
||||
stats: extendedStats,
|
||||
};
|
||||
@@ -90,6 +100,38 @@ export let taskActivityWebhook = new WebhookSender({
|
||||
},
|
||||
});
|
||||
|
||||
export let userActivityWebhook = new WebhookSender({
|
||||
type: 'userActivity',
|
||||
webhookFilter (hook, data) {
|
||||
let { type } = data;
|
||||
return hook.options[type];
|
||||
},
|
||||
});
|
||||
|
||||
export let questActivityWebhook = new WebhookSender({
|
||||
type: 'questActivity',
|
||||
webhookFilter (hook, data) {
|
||||
let { type } = data;
|
||||
return hook.options[type];
|
||||
},
|
||||
transformData (data) {
|
||||
let { group, quest, type } = data;
|
||||
|
||||
let dataToSend = {
|
||||
type,
|
||||
group: {
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
},
|
||||
quest: {
|
||||
key: quest.key,
|
||||
},
|
||||
};
|
||||
|
||||
return dataToSend;
|
||||
},
|
||||
});
|
||||
|
||||
export let groupChatReceivedWebhook = new WebhookSender({
|
||||
type: 'groupChatReceived',
|
||||
webhookFilter (hook, data) {
|
||||
|
||||
Reference in New Issue
Block a user