Hotfix for webhooks bus in models/group (#10323)

* remove new webhooks code from group model

* disable chat webhooks as well
This commit is contained in:
Matteo Pagliazzi 2018-05-03 22:40:42 +02:00 committed by GitHub
parent 33628a0a6a
commit f85e1c2dc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,10 +12,7 @@ import * as Tasks from './task';
import validator from 'validator'; import validator from 'validator';
import { removeFromArray } from '../libs/collectionManipulators'; import { removeFromArray } from '../libs/collectionManipulators';
import payments from '../libs/payments/payments'; import payments from '../libs/payments/payments';
import { import { groupChatReceivedWebhook } from '../libs/webhook';
groupChatReceivedWebhook,
questActivityWebhook,
} from '../libs/webhook';
import { import {
InternalServerError, InternalServerError,
BadRequest, BadRequest,
@ -651,24 +648,20 @@ schema.methods.startQuest = async function startQuest (user) {
removeFromArray(nonUserQuestMembers, user._id); removeFromArray(nonUserQuestMembers, user._id);
// remove any users from quest.members who aren't in the party // remove any users from quest.members who aren't in the party
// and get the data necessary to send webhooks let partyId = this._id;
const members = []; let questMembers = this.quest.members;
await Promise.all(Object.keys(this.quest.members).map(memberId => {
await User.find({ return User.findOne({_id: memberId, 'party._id': partyId})
_id: {$in: Object.keys(this.quest.members)}, .select('_id')
}) .lean()
.select('party.quest party._id items.quests auth preferences.emailNotifications preferences.pushNotifications pushDevices profile.name webhooks') .exec()
.lean() .then((member) => {
.exec() if (!member) {
.then(partyMembers => { delete questMembers[memberId];
partyMembers.forEach(member => {
if (!member.party || member.party._id !== this._id) {
delete this.quest.members[member._id];
} else {
members.push(member);
} }
return;
}); });
}); }));
if (userIsParticipating) { if (userIsParticipating) {
user.party.quest.key = this.quest.key; user.party.quest.key = this.quest.key;
@ -677,23 +670,20 @@ schema.methods.startQuest = async function startQuest (user) {
user.markModified('party.quest'); user.markModified('party.quest');
} }
const promises = [];
// Remove the quest from the quest leader items (if they are the current user) // Remove the quest from the quest leader items (if they are the current user)
if (this.quest.leader === user._id) { if (this.quest.leader === user._id) {
user.items.quests[this.quest.key] -= 1; user.items.quests[this.quest.key] -= 1;
user.markModified('items.quests'); user.markModified('items.quests');
promises.push(user.save());
} else { // another user is starting the quest, update the leader separately } else { // another user is starting the quest, update the leader separately
promises.push(User.update({_id: this.quest.leader}, { await User.update({_id: this.quest.leader}, {
$inc: { $inc: {
[`items.quests.${this.quest.key}`]: -1, [`items.quests.${this.quest.key}`]: -1,
}, },
}).exec()); }).exec();
} }
// update the remaining users // update the remaining users
promises.push(User.update({ await User.update({
_id: { $in: nonUserQuestMembers }, _id: { $in: nonUserQuestMembers },
}, { }, {
$set: { $set: {
@ -701,9 +691,7 @@ schema.methods.startQuest = async function startQuest (user) {
'party.quest.progress.down': 0, 'party.quest.progress.down': 0,
'party.quest.completed': null, 'party.quest.completed': null,
}, },
}, { multi: true }).exec()); }, { multi: true }).exec();
await Promise.all(promises);
// update the users who are not participating // update the users who are not participating
// Do not block updates // Do not block updates
@ -715,45 +703,38 @@ schema.methods.startQuest = async function startQuest (user) {
}, },
}, { multi: true }).exec(); }, { multi: true }).exec();
// send notifications in the background without blocking
User.find(
{ _id: { $in: nonUserQuestMembers } },
'party.quest items.quests auth.facebook auth.local preferences.emailNotifications preferences.pushNotifications pushDevices profile.name'
).exec().then((membersToNotify) => {
let membersToEmail = _.filter(membersToNotify, (member) => {
// send push notifications and filter users that disabled emails
return member.preferences.emailNotifications.questStarted !== false &&
member._id !== user._id;
});
sendTxnEmail(membersToEmail, 'quest-started', [
{ name: 'PARTY_URL', content: '/party' },
]);
let membersToPush = _.filter(membersToNotify, (member) => {
// send push notifications and filter users that disabled emails
return member.preferences.pushNotifications.questStarted !== false &&
member._id !== user._id;
});
_.each(membersToPush, (member) => {
sendPushNotification(member,
{
title: quest.text(),
message: `${shared.i18n.t('questStarted')}: ${quest.text()}`,
identifier: 'questStarted',
});
});
});
const newMessage = this.sendChat(`\`Your quest, ${quest.text('en')}, has started.\``, null, { const newMessage = this.sendChat(`\`Your quest, ${quest.text('en')}, has started.\``, null, {
participatingMembers: this.getParticipatingQuestMembers().join(', '), participatingMembers: this.getParticipatingQuestMembers().join(', '),
}); });
await newMessage.save(); await newMessage.save();
const membersToEmail = [];
const pushTitle = quest.text();
const pushMessage = `${shared.i18n.t('questStarted')}: ${quest.text()}`;
// send notifications and webhooks in the background without blocking
members.forEach(member => {
if (member._id !== user._id) {
// send push notifications and filter users that disabled emails
if (member.preferences.emailNotifications.questStarted !== false) {
membersToEmail.push(member);
}
// send push notifications and filter users that disabled emails
if (member.preferences.pushNotifications.questStarted !== false) {
sendPushNotification(member, {
title: pushTitle,
message: pushMessage,
identifier: 'questStarted',
});
}
}
// Send webhooks
questActivityWebhook.send(member, {
type: 'questStarted',
group: this,
quest,
});
});
// Send emails in bulk
sendTxnEmail(membersToEmail, 'quest-started', [
{ name: 'PARTY_URL', content: '/party' },
]);
}; };
schema.methods.sendGroupChatReceivedWebhooks = function sendGroupChatReceivedWebhooks (chat) { schema.methods.sendGroupChatReceivedWebhooks = function sendGroupChatReceivedWebhooks (chat) {
@ -772,14 +753,15 @@ schema.methods.sendGroupChatReceivedWebhooks = function sendGroupChatReceivedWeb
query.guilds = this._id; query.guilds = this._id;
} }
User.find(query).select({webhooks: 1}).lean().exec().then((users) => { /* User.find(query).select({webhooks: 1}).lean().exec().then((users) => {
users.forEach((user) => { users.forEach((user) => {
groupChatReceivedWebhook.send(user, { let { webhooks } = user;
groupChatReceivedWebhook.send(webhooks, {
group: this, group: this,
chat, chat,
}); });
}); });
}); }); */
}; };
schema.statics.cleanQuestProgress = _cleanQuestProgress; schema.statics.cleanQuestProgress = _cleanQuestProgress;
@ -925,31 +907,6 @@ schema.methods.finishQuest = async function finishQuest (quest) {
})); }));
} }
// Send webhooks in background
// @TODO move the find users part to a worker as well, not just the http request
User.find({
_id: {$in: participants},
webhooks: {
$elemMatch: {
type: 'questActivity',
'options.questFinished': true,
},
},
})
.select('_id webhooks')
.lean()
.exec()
.then(participantsWithWebhook => {
participantsWithWebhook.forEach(participantWithWebhook => {
// Send webhooks
questActivityWebhook.send(participantWithWebhook, {
type: 'questFinished',
group: this,
quest,
});
});
});
return await Promise.all(promises); return await Promise.all(promises);
}; };
@ -1537,4 +1494,4 @@ if (!nconf.get('IS_TEST')) {
privacy: 'public', privacy: 'public',
}).save(); }).save();
}); });
} }