Skip to content

Commit

Permalink
fix: DM notifications when preference set to mentions (#31759)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Feb 21, 2024
1 parent 5ff4110 commit c18f3e3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-fishes-sniff.md
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fix notifications specially for DMs when preference is set to mentions.
4 changes: 2 additions & 2 deletions apps/meteor/app/lib/server/functions/notifications/desktop.ts
Expand Up @@ -81,7 +81,7 @@ export function shouldNotifyDesktop({
disableAllMessageNotifications: boolean;
status: string;
statusConnection: string;
desktopNotifications: string;
desktopNotifications: string | undefined;
hasMentionToAll: boolean;
hasMentionToHere: boolean;
isHighlighted: boolean;
Expand All @@ -90,7 +90,7 @@ export function shouldNotifyDesktop({
roomType: string;
isThread: boolean;
}): boolean {
if (disableAllMessageNotifications && desktopNotifications == null && !isHighlighted && !hasMentionToUser && !hasReplyToThread) {
if (disableAllMessageNotifications && !desktopNotifications && !isHighlighted && !hasMentionToUser && !hasReplyToThread) {
return false;
}

Expand Down
27 changes: 13 additions & 14 deletions apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts
Expand Up @@ -10,7 +10,7 @@ import {
import { Subscriptions, Users } from '@rocket.chat/models';
import emojione from 'emojione';
import moment from 'moment';
import type { Filter, RootFilterOperators } from 'mongodb';
import type { RootFilterOperators } from 'mongodb';

import { callbacks } from '../../../../lib/callbacks';
import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator';
Expand Down Expand Up @@ -117,7 +117,7 @@ export const sendNotification = async ({
disableAllMessageNotifications,
status: receiver.status ?? 'offline',
statusConnection: receiver.statusConnection ?? 'offline',
desktopNotifications: desktopNotifications ?? 'default',
desktopNotifications,
hasMentionToAll,
hasMentionToHere,
isHighlighted,
Expand Down Expand Up @@ -304,33 +304,32 @@ export async function sendMessageNotifications(message: IMessage, room: IRoom, u
} as const;

(['desktop', 'mobile', 'email'] as const).forEach((kind) => {
const notificationField = `${kind === 'mobile' ? 'mobilePush' : kind}Notifications`;
const notificationField = kind === 'mobile' ? 'mobilePush' : `${kind}Notifications`;

const filter: Filter<ISubscription> = { [notificationField]: 'all' };
query.$or.push({
[notificationField]: 'all',
...(disableAllMessageNotifications ? { [`${kind}PrefOrigin`]: { $ne: 'user' } } : {}),
});

if (disableAllMessageNotifications) {
filter[`${kind}PrefOrigin`] = { $ne: 'user' };
return;
}

query.$or.push(filter);

if (mentionIdsWithoutGroups.length > 0) {
if (room.t === 'd') {
query.$or.push({
[notificationField]: 'mentions',
'u._id': { $in: mentionIdsWithoutGroups },
});
} else if (!disableAllMessageNotifications && (hasMentionToAll || hasMentionToHere)) {
} else if (mentionIdsWithoutGroups.length > 0) {
query.$or.push({
[notificationField]: 'mentions',
'u._id': { $in: mentionIdsWithoutGroups },
});
}

const serverField = kind === 'email' ? 'emailNotificationMode' : `${kind}Notifications`;
const serverPreference = settings.get(`Accounts_Default_User_Preferences_${serverField}`);
if (
(room.t === 'd' && serverPreference !== 'nothing') ||
(!disableAllMessageNotifications && (serverPreference === 'all' || hasMentionToAll || hasMentionToHere))
) {

if (serverPreference === 'all' || hasMentionToAll || hasMentionToHere || room.t === 'd') {
query.$or.push({
[notificationField]: { $exists: false },
});
Expand Down

0 comments on commit c18f3e3

Please sign in to comment.