-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7d53503
Notification logic improvements
sampaiodiego 06d74e2
Use rawCollection for slow operations
sampaiodiego bde1ee2
Denormalize user notification preferences
sampaiodiego dae18bf
Split notification functions into multiple files
sampaiodiego 1884e99
Code cleanup
sampaiodiego b7124f1
Remove rawCollection
sampaiodiego f538d83
Move specific code to their respective files
sampaiodiego bdc6130
Respect server's default preferences
sampaiodiego de6d492
Add default email preference to user's preferences
sampaiodiego 2bddb06
Change default value of auto away to true
sampaiodiego 6ab4f88
Save notifications accordingly
sampaiodiego b529103
Use correct room name for email notifications
sampaiodiego a9edf7a
Merge branch 'develop' into improvements-to-notifcations-logic
sampaiodiego ad07123
Preserve email notification fixes
sampaiodiego 743839b
Fix auto away default value test
sampaiodiego 46f73bc
Remove duplicated index and add some comments
sampaiodiego c4815d2
Honor Notifications_Max_Room_Members for all notifications
sampaiodiego 1c8200b
Remove unread index
sampaiodiego d71d48a
Minor code improvements
sampaiodiego d6a9905
Merge branch 'develop' into improvements-to-notifcations-logic
rodrigok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
packages/rocketchat-lib/lib/getUserNotificationPreference.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/rocketchat-lib/server/functions/notifications/audio.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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 (!disableAllMessageNotifications && (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 | ||
} | ||
}); | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/rocketchat-lib/server/functions/notifications/desktop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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 (!disableAllMessageNotifications && (hasMentionToAll || hasMentionToHere)) || isHighlighted || desktopNotifications === 'all' || hasMentionToUser; | ||
} |
176 changes: 176 additions & 0 deletions
176
packages/rocketchat-lib/server/functions/notifications/email.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
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, | ||
hasMentionToAll | ||
}) { | ||
|
||
// 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 || (!disableAllMessageNotifications && hasMentionToAll); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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