Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Duplicate email and auto-join on mentions #12168

Merged
merged 1 commit into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export function messageContainsHighlight(message, highlights) {
});
}

export function callJoinRoom(user, rid) {
export function callJoinRoom(userId, rid) {
return new Promise((resolve, reject) => {
Meteor.runAsUser(user._id, () => Meteor.call('joinRoom', rid, (error, result) => {
Meteor.runAsUser(userId, () => Meteor.call('joinRoom', rid, (error, result) => {
if (error) {
return reject(error);
}
Expand Down
50 changes: 33 additions & 17 deletions packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,39 @@ function sendAllNotifications(message, room) {

// on public channels, if a mentioned user is not member of the channel yet, he will first join the channel and then be notified based on his preferences.
if (room.t === 'c') {
const mentions = message.mentions.filter(({ _id }) => _id !== 'here' && _id !== 'all').map(({ _id }) => _id);
Promise.all(RocketChat.models.Subscriptions.findByRoomIdAndUserIds(room._id, mentions)
.fetch()
.map(async(subscription) => {
await callJoinRoom(subscription.u, room._id);
return subscription;
})).then((subscriptions) => subscriptions.forEach((subscription) =>
sendNotification({
subscription,
sender,
hasMentionToAll,
hasMentionToHere,
message,
notificationMessage,
room,
mentionIds,
})));
// get subscriptions from users already in room (to not send them a notification)
const mentions = [...mentionIdsWithoutGroups];
RocketChat.models.Subscriptions.findByRoomIdAndUserIds(room._id, mentionIdsWithoutGroups, { fields: { 'u._id': 1 } }).forEach((subscription) => {
const index = mentions.indexOf(subscription.u._id);
if (index !== -1) {
mentions.splice(index, 1);
}
});

Promise.all(mentions
.map(async(userId) => {
await callJoinRoom(userId, room._id);

return userId;
})
).then((users) => {
users.forEach((userId) => {
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, userId);

sendNotification({
subscription,
sender,
hasMentionToAll,
hasMentionToHere,
message,
notificationMessage,
room,
mentionIds,
});
});
}).catch((error) => {
throw new Meteor.Error(error);
});
}

return message;
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-lib/server/models/Subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ class ModelSubscriptions extends RocketChat.models._Base {
return subscription && subscription.ls;
}

findByRoomIdAndUserIds(roomId, userIds) {
findByRoomIdAndUserIds(roomId, userIds, options) {
const query = {
rid: roomId,
'u._id': {
$in: userIds,
},
};

return this.find(query);
return this.find(query, options);
}

findByRoomIdAndUserIdsOrAllMessages(roomId, userIds) {
Expand Down