-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
[NEW] Improvements to notifications logic #10686
Changes from 15 commits
7d53503
06d74e2
bde1ee2
dae18bf
1884e99
b7124f1
f538d83
bdc6130
de6d492
2bddb06
6ab4f88
b529103
a9edf7a
ad07123
743839b
46f73bc
c4815d2
1c8200b
d71d48a
d6a9905
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
RocketChat.getUserNotificationPreference = function _getUserNotificationPreference(user, pref) { | ||
if (typeof user === 'string') { | ||
user = RocketChat.models.Users.findOneById(user); | ||
} | ||
|
||
let preferenceKey; | ||
switch (pref) { | ||
case 'desktop': preferenceKey = 'desktopNotifications'; break; | ||
case 'mobile': preferenceKey = 'mobileNotifications'; break; | ||
case 'email': preferenceKey = 'emailNotificationMode'; break; | ||
} | ||
|
||
if (user && user.settings && user.settings.preferences && | ||
user.settings.preferences.hasOwnProperty(preferenceKey) && user.settings.preferences[preferenceKey] !== 'default') { | ||
return { | ||
value: user.settings.preferences[preferenceKey], | ||
origin: 'user' | ||
}; | ||
} | ||
const serverValue = RocketChat.settings.get(`Accounts_Default_User_Preferences_${ preferenceKey }`); | ||
if (serverValue) { | ||
return { | ||
value: serverValue, | ||
origin: 'server' | ||
}; | ||
} | ||
|
||
return null; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function shouldNotifyAudio({ disableAllMessageNotifications, status, audioNotifications, hasMentionToAll, hasMentionToHere, isHighlighted, hasMentionToUser}) { | ||
if (disableAllMessageNotifications && audioNotifications == null) { | ||
return false; | ||
} | ||
|
||
if (status === 'busy' || audioNotifications === 'nothing') { | ||
return false; | ||
} | ||
|
||
if (!audioNotifications && RocketChat.settings.get('Accounts_Default_User_Preferences_audioNotifications') === 'all') { | ||
return true; | ||
} | ||
|
||
return hasMentionToAll || hasMentionToHere || isHighlighted || audioNotifications === 'all' || hasMentionToUser; | ||
} | ||
|
||
export function notifyAudioUser(userId, message, room) { | ||
RocketChat.Notifications.notifyUser(userId, 'audioNotification', { | ||
payload: { | ||
_id: message._id, | ||
rid: message.rid, | ||
sender: message.u, | ||
type: room.t, | ||
name: room.name | ||
} | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import s from 'underscore.string'; | ||
|
||
let contentHeader; | ||
RocketChat.settings.get('Email_Header', (key, value) => { | ||
contentHeader = RocketChat.placeholders.replace(value || ''); | ||
}); | ||
|
||
let contentFooter; | ||
RocketChat.settings.get('Email_Footer', (key, value) => { | ||
contentFooter = RocketChat.placeholders.replace(value || ''); | ||
}); | ||
|
||
const divisorMessage = '<hr style="margin: 20px auto; border: none; border-bottom: 1px solid #dddddd;">'; | ||
|
||
function getEmailContent({ message, user, room }) { | ||
const lng = user && user.language || RocketChat.settings.get('language') || 'en'; | ||
|
||
const roomName = s.escapeHTML(`#${ RocketChat.roomTypes.getRoomName(room.t, room) }`); | ||
const userName = s.escapeHTML(RocketChat.settings.get('UI_Use_Real_Name') ? message.u.name || message.u.username : message.u.username); | ||
|
||
const header = TAPi18n.__(room.t === 'd' ? 'User_sent_a_message_to_you' : 'User_sent_a_message_on_channel', { | ||
username: userName, | ||
channel: roomName, | ||
lng | ||
}); | ||
|
||
if (message.msg !== '') { | ||
let messageContent = s.escapeHTML(message.msg); | ||
message = RocketChat.callbacks.run('renderMessage', message); | ||
if (message.tokens && message.tokens.length > 0) { | ||
message.tokens.forEach((token) => { | ||
token.text = token.text.replace(/([^\$])(\$[^\$])/gm, '$1$$$2'); | ||
messageContent = messageContent.replace(token.token, token.text); | ||
}); | ||
} | ||
return `${ header }<br/><br/>${ messageContent.replace(/\n/gm, '<br/>') }`; | ||
} | ||
|
||
if (message.file) { | ||
const fileHeader = TAPi18n.__(room.t === 'd' ? 'User_uploaded_a_file_to_you' : 'User_uploaded_a_file_on_channel', { | ||
username: userName, | ||
channel: roomName, | ||
lng | ||
}); | ||
|
||
let content = `${ TAPi18n.__('Attachment_File_Uploaded') }: ${ s.escapeHTML(message.file.name) }`; | ||
|
||
if (message.attachments && message.attachments.length === 1 && message.attachments[0].description !== '') { | ||
content += `<br/><br/>${ s.escapeHTML(message.attachments[0].description) }`; | ||
} | ||
|
||
return `${ fileHeader }<br/><br/>${ content }`; | ||
} | ||
|
||
if (message.attachments.length > 0) { | ||
const [ attachment ] = message.attachments; | ||
|
||
let content = ''; | ||
|
||
if (attachment.title) { | ||
content += `${ s.escapeHTML(attachment.title) }<br/>`; | ||
} | ||
if (attachment.text) { | ||
content += `${ s.escapeHTML(attachment.text) }<br/>`; | ||
} | ||
|
||
return `${ header }<br/><br/>${ content }`; | ||
} | ||
|
||
return header; | ||
} | ||
|
||
function getMessageLink(room, sub) { | ||
const roomPath = RocketChat.roomTypes.getRouteLink(room.t, sub); | ||
const path = Meteor.absoluteUrl(roomPath ? roomPath.replace(/^\//, '') : ''); | ||
const style = [ | ||
'color: #fff;', | ||
'padding: 9px 12px;', | ||
'border-radius: 4px;', | ||
'background-color: #04436a;', | ||
'text-decoration: none;' | ||
].join(' '); | ||
const message = TAPi18n.__('Offline_Link_Message'); | ||
return `<p style="text-align:center;margin-bottom:8px;"><a style="${ style }" href="${ path }">${ message }</a>`; | ||
} | ||
|
||
export function sendEmail({ message, user, subscription, room, emailAddress, toAll }) { | ||
let emailSubject; | ||
const username = RocketChat.settings.get('UI_Use_Real_Name') ? message.u.name : message.u.username; | ||
|
||
if (room.t === 'd') { | ||
emailSubject = RocketChat.placeholders.replace(RocketChat.settings.get('Offline_DM_Email'), { | ||
user: username, | ||
room: RocketChat.roomTypes.getRoomName(room.t, room) | ||
}); | ||
} else if (toAll) { | ||
emailSubject = RocketChat.placeholders.replace(RocketChat.settings.get('Offline_Mention_All_Email'), { | ||
user: username, | ||
room: RocketChat.roomTypes.getRoomName(room.t, room) | ||
}); | ||
} else { | ||
emailSubject = RocketChat.placeholders.replace(RocketChat.settings.get('Offline_Mention_Email'), { | ||
user: username, | ||
room: RocketChat.roomTypes.getRoomName(room.t, room) | ||
}); | ||
} | ||
const content = getEmailContent({ | ||
message, | ||
user, | ||
room | ||
}); | ||
|
||
const link = getMessageLink(room, subscription); | ||
|
||
if (RocketChat.settings.get('Direct_Reply_Enable')) { | ||
contentFooter = RocketChat.placeholders.replace(RocketChat.settings.get('Email_Footer_Direct_Reply') || ''); | ||
} | ||
|
||
const email = { | ||
to: emailAddress, | ||
subject: emailSubject, | ||
html: contentHeader + content + divisorMessage + link + contentFooter | ||
}; | ||
|
||
// using user full-name/channel name in from address | ||
if (room.t === 'd') { | ||
email.from = `${ String(message.u.name).replace(/@/g, '%40').replace(/[<>,]/g, '') } <${ RocketChat.settings.get('From_Email') }>`; | ||
} else { | ||
email.from = `${ String(room.name).replace(/@/g, '%40').replace(/[<>,]/g, '') } <${ RocketChat.settings.get('From_Email') }>`; | ||
} | ||
// If direct reply enabled, email content with headers | ||
if (RocketChat.settings.get('Direct_Reply_Enable')) { | ||
email.headers = { | ||
// Reply-To header with format "username+messageId@domain" | ||
'Reply-To': `${ RocketChat.settings.get('Direct_Reply_Username').split('@')[0].split(RocketChat.settings.get('Direct_Reply_Separator'))[0] }${ RocketChat.settings.get('Direct_Reply_Separator') }${ message._id }@${ RocketChat.settings.get('Direct_Reply_Username').split('@')[1] }` | ||
}; | ||
} | ||
|
||
Meteor.defer(() => { | ||
Email.send(email); | ||
}); | ||
} | ||
|
||
export function shouldNotifyEmail({ disableAllMessageNotifications, statusConnection, emailNotifications, isHighlighted, hasMentionToUser }) { | ||
|
||
// no user or room preference | ||
if (emailNotifications == null) { | ||
if (disableAllMessageNotifications) { | ||
return false; | ||
} | ||
|
||
// default server preference is disabled | ||
if (RocketChat.settings.get('Accounts_Default_User_Preferences_emailNotificationMode') === 'disabled') { | ||
return false; | ||
} | ||
} | ||
|
||
// user/room preference to nothing | ||
if (emailNotifications === 'nothing') { | ||
return false; | ||
} | ||
|
||
// use connected (don't need to send him an email) | ||
if (statusConnection === 'online') { | ||
return false; | ||
} | ||
|
||
return isHighlighted || emailNotifications === 'all' || hasMentionToUser; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is now easy to see that email notifications are not triggered by |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import s from 'underscore.string'; | ||
|
||
/** | ||
* This function returns a string ready to be shown in the notification | ||
* | ||
* @param {object} message the message to be parsed | ||
*/ | ||
export function parseMessageText(message, userId) { | ||
const user = RocketChat.models.Users.findOneById(userId); | ||
const lng = user && user.language || RocketChat.settings.get('language') || 'en'; | ||
|
||
if (!message.msg && message.attachments && message.attachments[0]) { | ||
message.msg = message.attachments[0].image_type ? TAPi18n.__('User_uploaded_image', {lng}) : TAPi18n.__('User_uploaded_file', {lng}); | ||
} | ||
message.msg = RocketChat.callbacks.run('beforeNotifyUser', message.msg); | ||
|
||
return message.msg; | ||
} | ||
|
||
/** | ||
* Checks if a message contains a user highlight | ||
* | ||
* @param {string} message | ||
* @param {array|undefined} highlights | ||
* | ||
* @returns {boolean} | ||
*/ | ||
export function messageContainsHighlight(message, highlights) { | ||
if (! highlights || highlights.length === 0) { return false; } | ||
|
||
return highlights.some(function(highlight) { | ||
const regexp = new RegExp(s.escapeRegExp(highlight), 'i'); | ||
if (regexp.test(message.msg)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return true; | ||
} | ||
}); | ||
} | ||
|
||
export function callJoinRoom(user, rid) { | ||
return new Promise((resolve, reject) => { | ||
Meteor.runAsUser(user._id, () => Meteor.call('joinRoom', rid, (error, result) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
return resolve(result); | ||
})); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
hasOwnProperty