-
Notifications
You must be signed in to change notification settings - Fork 10.1k
/
sendNotificationsOnMessage.js
180 lines (145 loc) · 5.68 KB
/
sendNotificationsOnMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import moment from 'moment';
import { callJoinRoom, messageContainsHighlight } from '../functions/notifications/';
import { sendEmail, shouldNotifyEmail } from '../functions/notifications/email';
import { sendSinglePush, shouldNotifyMobile } from '../functions/notifications/mobile';
import { notifyDesktopUser, shouldNotifyDesktop } from '../functions/notifications/desktop';
import { notifyAudioUser, shouldNotifyAudio } from '../functions/notifications/audio';
const sendNotification = ({
subscription,
sender,
hasMentionToAll,
hasMentionToHere,
message,
room,
mentionIds,
disableAllMessageNotifications
}) => {
// don't notify the sender
if (subscription.u._id === sender._id) {
return;
}
// notifications disabled
if (subscription.disableNotifications) {
return;
}
// dont send notification to users who ignored the sender
if (Array.isArray(subscription.ignored) && subscription.ignored.find(sender._id)) {
return;
}
// mute group notifications (@here and @all)
if (subscription.muteGroupMentions && (hasMentionToAll || hasMentionToHere)) {
return;
}
const receiver = RocketChat.models.Users.findOneById(subscription.u._id);
if (!receiver || !receiver.active) {
return;
}
const isHighlighted = messageContainsHighlight(message, receiver.settings && receiver.settings.preferences && receiver.settings.preferences.highlights);
const hasMentionToUser = mentionIds.includes(subscription.u._id);
const {
audioNotifications,
desktopNotifications,
mobilePushNotifications,
emailNotifications
} = subscription;
let notificationSent = false;
// busy users don't receive audio notification
if (shouldNotifyAudio({ disableAllMessageNotifications, status: receiver.status, audioNotifications, hasMentionToAll, hasMentionToHere, isHighlighted, hasMentionToUser})) {
notifyAudioUser(subscription.u._id, message, room);
}
// busy users don't receive desktop notification
if (shouldNotifyDesktop({ disableAllMessageNotifications, status: receiver.status, desktopNotifications, hasMentionToAll, hasMentionToHere, isHighlighted, hasMentionToUser})) {
notificationSent = true;
notifyDesktopUser(subscription.u._id, sender, message, room, subscription.desktopNotificationDuration);
}
if (shouldNotifyMobile({ disableAllMessageNotifications, mobilePushNotifications, hasMentionToAll, isHighlighted, hasMentionToUser, statusConnection: receiver.statusConnection })) {
notificationSent = true;
sendSinglePush({
room,
message,
userId: subscription.u._id,
senderUsername: sender.username,
receiverUsername: receiver.username
});
}
if (receiver.emails && shouldNotifyEmail({ disableAllMessageNotifications, statusConnection: receiver.statusConnection, emailNotifications, isHighlighted, hasMentionToUser })) {
receiver.emails.some((email) => {
if (email.verified) {
sendEmail({ message, receiver, subscription, room, emailAddress: email.address });
return true;
}
});
}
if (notificationSent) {
RocketChat.Sandstorm.notify(message, [subscription.u._id], `@${ sender.username }: ${ message.msg }`, room.t === 'p' ? 'privateMessage' : 'message');
}
};
function sendAllNotifications(message, room) {
// skips this callback if the message was edited
if (message.editedAt) {
return message;
}
if (message.ts && Math.abs(moment(message.ts).diff()) > 60000) {
return message;
}
if (!room || room.t == null) {
return message;
}
const sender = (room.t !== 'l') ? RocketChat.models.Users.findOneById(message.u._id) : room.v;
if (!sender) {
return message;
}
// Don't fetch all users if room exceeds max members
const maxMembersForNotification = RocketChat.settings.get('Notifications_Max_Room_Members');
const disableAllMessageNotifications = room.usernames.length > maxMembersForNotification && maxMembersForNotification !== 0;
let subscriptions;
if (disableAllMessageNotifications) {
subscriptions = RocketChat.models.Subscriptions.findAllMessagesNotificationPreferencesByRoom(room._id);
} else {
const mentionsFilter = { $in: ['all', 'mentions'] };
const excludesNothingFilter = { $ne: 'nothing' };
subscriptions = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom({
roomId: room._id,
desktopFilter: RocketChat.settings.get('Accounts_Default_User_Preferences_desktopNotifications') === 'nothing' ? mentionsFilter : excludesNothingFilter,
emailFilter: RocketChat.settings.get('Accounts_Default_User_Preferences_emailNotificationMode') === 'disabled' ? mentionsFilter : excludesNothingFilter,
mobileFilter: RocketChat.settings.get('Accounts_Default_User_Preferences_mobileNotifications') === 'nothing' ? mentionsFilter : excludesNothingFilter
});
}
const mentionIds = (message.mentions || []).map(({_id}) => _id);
const hasMentionToAll = mentionIds.includes('all');
const hasMentionToHere = mentionIds.includes('here');
subscriptions.forEach((subscription) => sendNotification({
subscription,
sender,
hasMentionToAll,
hasMentionToHere,
message,
room,
mentionIds,
disableAllMessageNotifications
}));
if (room.t === 'c') {
Promise.all(message.mentions
.filter(({ _id, username }) => _id !== 'here' && _id !== 'all' && !room.usernames.includes(username))
.map(async(user) => {
await callJoinRoom(user, room._id);
return user._id;
})
).then((users) => {
users.forEach((userId) => {
const subscription = RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, userId);
sendNotification({
subscription,
sender,
hasMentionToAll,
hasMentionToHere,
message,
room,
mentionIds
});
});
});
}
return message;
}
RocketChat.callbacks.add('afterSaveMessage', sendAllNotifications, RocketChat.callbacks.priority.LOW, 'sendNotificationsOnMessage');