Ensured that only one like or flag is updated during query

This commit is contained in:
Keith Holliday
2015-12-28 14:08:10 -06:00
parent b3feb997d3
commit cb388eea40

View File

@@ -133,16 +133,22 @@ api.likeChat = {
if (message.uuid === user._id) throw new NotFound(res.t('messageGroupChatLikeOwnMessage')); if (message.uuid === user._id) throw new NotFound(res.t('messageGroupChatLikeOwnMessage'));
let update;
if (!message.likes) message.likes = {}; if (!message.likes) message.likes = {};
if (message.likes[user._id]) { if (message.likes[user._id]) {
delete message.likes[user._id]; delete message.likes[user._id];
update = {'$unset': {}};
update.$unset[`chat.$.likes.${user._id}`] = '';
} else { } else {
message.likes[user._id] = true; message.likes[user._id] = true;
update = {'$set': {}};
update.$set[`chat.$.likes.${user._id}`] = true;
} }
return Group.update( return Group.update(
{_id: group._id, 'chat.id': message.id}, {_id: group._id, 'chat.id': message.id},
{$set: {'chat.$.likes': message.likes}} update
); );
}) })
.then((groupSaved) => { .then((groupSaved) => {
@@ -196,10 +202,13 @@ api.flagChat = {
.then((foundAuthor) => { .then((foundAuthor) => {
author = foundAuthor; author = foundAuthor;
let update = {$set: {}};
// Log user ids that have flagged the message // Log user ids that have flagged the message
if (!message.flags) message.flags = {}; if (!message.flags) message.flags = {};
if (message.flags[user._id] && !user.contributor.admin) throw new NotFound(res.t('messageGroupChatFlagAlreadyReported')); if (message.flags[user._id] && !user.contributor.admin) throw new NotFound(res.t('messageGroupChatFlagAlreadyReported'));
message.flags[user._id] = true; message.flags[user._id] = true;
update.$set[`chat.$.flags.${user._id}`] = true;
// Log total number of flags (publicly viewable) // Log total number of flags (publicly viewable)
if (!message.flagCount) message.flagCount = 0; if (!message.flagCount) message.flagCount = 0;
@@ -209,14 +218,12 @@ api.flagChat = {
} else { } else {
message.flagCount++; message.flagCount++;
} }
update.$set['chat.$.flagCount'] = message.flagCount;
// return group.save();
return Group.update( return Group.update(
{_id: group._id, 'chat.id': message.id}, {_id: group._id, 'chat.id': message.id},
{$set: { update
'chat.$.flags': message.flags, );
'chat.$.flagCount': message.flagCount,
}});
}) })
.then((group2) => { .then((group2) => {
if (!group2) throw new NotFound(res.t('groupNotFound')); if (!group2) throw new NotFound(res.t('groupNotFound'));