-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
desktop.js
77 lines (69 loc) · 2.19 KB
/
desktop.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
import { parseMessageText } from './index';
/**
* Replaces @username with full name
*
* @param {string} message The message to replace
* @param {object[]} mentions Array of mentions used to make replacements
*
* @returns {string}
*/
function replaceMentionedUsernamesWithFullNames(message, mentions) {
if (!mentions || !mentions.length) {
return message;
}
mentions.forEach((mention) => {
const user = RocketChat.models.Users.findOneById(mention._id);
if (user && user.name) {
message = message.replace(`@${ mention.username }`, user.name);
}
});
return message;
}
/**
* Send notification to user
*
* @param {string} userId The user to notify
* @param {object} user The sender
* @param {object} room The room send from
* @param {number} duration Duration of notification
*/
export function notifyDesktopUser(userId, user, message, room, duration) {
const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;
message.msg = parseMessageText(message, userId);
if (UI_Use_Real_Name) {
message.msg = replaceMentionedUsernamesWithFullNames(message.msg, message.mentions);
}
let title = UI_Use_Real_Name ? user.name : `@${ user.username }`;
if (room.t !== 'd' && room.name) {
title += ` @ #${ room.name }`;
}
RocketChat.Notifications.notifyUser(userId, 'notification', {
title,
text: message.msg,
duration,
payload: {
_id: message._id,
rid: message.rid,
sender: message.u,
type: room.t,
name: room.name
}
});
}
export function shouldNotifyDesktop({ disableAllMessageNotifications, status, desktopNotifications, hasMentionToAll, hasMentionToHere, isHighlighted, hasMentionToUser}) {
if (disableAllMessageNotifications && desktopNotifications == null) {
return false;
}
if (status === 'busy' || desktopNotifications === 'nothing') {
return false;
}
if (!desktopNotifications) {
if (RocketChat.settings.get('Accounts_Default_User_Preferences_desktopNotifications') === 'all') {
return true;
}
if (RocketChat.settings.get('Accounts_Default_User_Preferences_desktopNotifications') === 'nothing') {
return false;
}
}
return hasMentionToAll || hasMentionToHere || isHighlighted || desktopNotifications === 'all' || hasMentionToUser;
}