-
Notifications
You must be signed in to change notification settings - Fork 13.1k
fix: desktop notifications not respecting privacy settings #36156
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
Merged
kodiakhq
merged 4 commits into
develop
from
fix/desktop-notifications-not-respecting-privacy-settings
Jun 18, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ffed11e
fix: desktop notifications not respecting privacy settings
abhinavkrin 040285b
added changeset
abhinavkrin 08d6065
requested changes with new tests
abhinavkrin b644f4d
Merge branch 'develop' into fix/desktop-notifications-not-respecting-…
kodiakhq[bot] 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
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes an issue where the `Show Message in Notification` and `Show Channel/Group/Username in Notification` setting was ignored in desktop notifications. Also ensures users are redirected to the correct room on interacting with the notifications. |
This file contains hidden or 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
231 changes: 231 additions & 0 deletions
231
apps/meteor/app/lib/server/functions/notifications/desktop.spec.ts
This file contains hidden or 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,231 @@ | ||
| import type { IRoom } from '@rocket.chat/core-typings'; | ||
| import { UserStatus } from '@rocket.chat/core-typings'; | ||
| import { expect } from 'chai'; | ||
| import proxyquire from 'proxyquire'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| const broadcastStub = sinon.stub(); | ||
| const settingsGetStub = sinon.stub(); | ||
|
|
||
| const { buildNotificationDetails } = proxyquire.noCallThru().load('../../../../../server/lib/rooms/buildNotificationDetails.ts', { | ||
| '../../../app/settings/server': { settings: { get: settingsGetStub } }, | ||
| }); | ||
| const { roomCoordinator } = proxyquire.noCallThru().load('../../../../../server/lib/rooms/roomCoordinator.ts', { | ||
| '../../../app/settings/server': { settings: { get: settingsGetStub } }, | ||
| './buildNotificationDetails': { buildNotificationDetails }, | ||
| }); | ||
|
|
||
| ['public', 'private', 'voip', 'livechat'].forEach((type) => { | ||
| proxyquire.noCallThru().load(`../../../../../server/lib/rooms/roomTypes/${type}.ts`, { | ||
| '../../../../app/settings/server': { settings: { get: settingsGetStub } }, | ||
| '../roomCoordinator': { roomCoordinator }, | ||
| '../buildNotificationDetails': { buildNotificationDetails }, | ||
| }); | ||
| }); | ||
|
|
||
| proxyquire.noCallThru().load('../../../../../server/lib/rooms/roomTypes/direct.ts', { | ||
| '../../../../app/settings/server': { settings: { get: settingsGetStub } }, | ||
| '../roomCoordinator': { roomCoordinator }, | ||
| '../buildNotificationDetails': { buildNotificationDetails }, | ||
| 'meteor/meteor': { Meteor: { userId: () => 'user123' } }, | ||
| '@rocket.chat/models': { | ||
| Subscription: { | ||
| findOneByRoomIdAndUserId: () => ({ name: 'general', fname: 'general' }), | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const { notifyDesktopUser } = proxyquire.noCallThru().load('./desktop', { | ||
| '../../../../settings/server': { settings: { get: settingsGetStub } }, | ||
| '../../../../metrics/server': { | ||
| metrics: { | ||
| notificationsSent: { inc: sinon.stub() }, | ||
| }, | ||
| }, | ||
| '@rocket.chat/core-services': { api: { broadcast: broadcastStub } }, | ||
| '../../lib/sendNotificationsOnMessage': {}, | ||
| '../../../../../server/lib/rooms/roomCoordinator': { roomCoordinator }, | ||
| }); | ||
|
|
||
| const fakeUserId = 'user123'; | ||
|
|
||
| const createTestData = (t: IRoom['t'] = 'c', showPushMessage = false, showUserOrRoomName = false, groupDM = false) => { | ||
| const sender = { _id: 'sender123', name: 'Alice', username: 'alice' }; | ||
| let uids: string[] | undefined; | ||
| if (t === 'd') { | ||
| uids = groupDM ? ['sender123', 'user123', 'otherUser123'] : ['sender123', 'user123']; | ||
| } | ||
|
|
||
| const room: Partial<IRoom> = { | ||
| t, | ||
| _id: 'room123', | ||
| msgs: 0, | ||
| _updatedAt: new Date(), | ||
| u: sender, | ||
| usersCount: uids ? uids.length : 2, | ||
| fname: uids?.length === 2 ? sender.name : 'general', | ||
| name: uids?.length === 2 ? sender.username : 'general', | ||
| uids, | ||
| }; | ||
|
|
||
| const message = { | ||
| _id: 'msg123', | ||
| rid: 'room123', | ||
| tmid: null, | ||
| u: sender, | ||
| msg: 'Fake message here', | ||
| }; | ||
|
|
||
| const receiver = { | ||
| _id: 'user123', | ||
| language: 'en', | ||
| username: 'receiver-username', | ||
| emails: [{ address: '[email protected]', verified: true }], | ||
| active: true, | ||
| status: UserStatus.OFFLINE, | ||
| statusConnection: 'offline', | ||
| }; | ||
|
|
||
| let expectedTitle: string | undefined; | ||
| if (showUserOrRoomName) { | ||
| switch (t) { | ||
| case 'c': | ||
| case 'p': | ||
| expectedTitle = `#${room.name}`; | ||
| break; | ||
| case 'l': | ||
| case 'v': | ||
| expectedTitle = `[Omnichannel] ${room.name}`; | ||
| break; | ||
| case 'd': | ||
| expectedTitle = room.name; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| let expectedNotificationMessage: string; | ||
|
|
||
| if (!showPushMessage) { | ||
| expectedNotificationMessage = 'You have a new message'; | ||
| } else if (!showUserOrRoomName) { | ||
| // No prefix if showUserOrRoomName is false | ||
| expectedNotificationMessage = message.msg; | ||
| } else if (t === 'd' && uids && uids.length > 2) { | ||
| expectedNotificationMessage = `${sender.username}: ${message.msg}`; | ||
| } else { | ||
| switch (t) { | ||
| case 'c': | ||
| case 'p': | ||
| expectedNotificationMessage = `${sender.username}: ${message.msg}`; | ||
| break; | ||
| case 'l': | ||
| case 'v': | ||
| case 'd': | ||
| expectedNotificationMessage = message.msg; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| room: room as IRoom, | ||
| user: sender, | ||
| message, | ||
| receiver, | ||
| expectedTitle, | ||
| expectedNotificationMessage, | ||
| }; | ||
| }; | ||
|
|
||
| describe('notifyDesktopUser privacy settings across all room types', () => { | ||
| const roomTypes: Array<{ t: IRoom['t']; isGroupDM?: boolean }> = [ | ||
| { t: 'c' }, | ||
| { t: 'p' }, | ||
| { t: 'l' }, | ||
| { t: 'v' }, | ||
| { t: 'd', isGroupDM: false }, | ||
| { t: 'd', isGroupDM: true }, | ||
| ]; | ||
|
|
||
| afterEach(() => { | ||
| broadcastStub.resetHistory(); | ||
| settingsGetStub.resetHistory(); | ||
| }); | ||
|
|
||
| roomTypes.forEach(({ t, isGroupDM = false }) => { | ||
| let roomLabel: string; | ||
| if (t === 'c') { | ||
| roomLabel = 'channel'; | ||
| } else if (t === 'p') { | ||
| roomLabel = 'private'; | ||
| } else if (t === 'l') { | ||
| roomLabel = 'livechat'; | ||
| } else if (t === 'v') { | ||
| roomLabel = 'voip'; | ||
| } else if (t === 'd' && isGroupDM) { | ||
| roomLabel = 'direct (group DM)'; | ||
| } else { | ||
| roomLabel = 'direct (1:1 DM)'; | ||
| } | ||
|
|
||
| describe(`when room type is "${roomLabel}"`, () => { | ||
| [ | ||
| { showPushMessage: false, showUserOrRoomName: true }, | ||
| { showPushMessage: true, showUserOrRoomName: false }, | ||
| { showPushMessage: false, showUserOrRoomName: false }, | ||
| { showPushMessage: true, showUserOrRoomName: true }, | ||
| ].forEach(({ showPushMessage, showUserOrRoomName }) => { | ||
| const label = `Push_show_message=${ | ||
| showPushMessage ? 'true' : 'false' | ||
| } and Push_show_username_room=${showUserOrRoomName ? 'true' : 'false'}`; | ||
|
|
||
| it(`should handle settings: ${label}`, async () => { | ||
| const { room, user, message, receiver, expectedTitle, expectedNotificationMessage } = createTestData( | ||
| t, | ||
| showPushMessage, | ||
| showUserOrRoomName, | ||
| isGroupDM, | ||
| ); | ||
|
|
||
| settingsGetStub.withArgs('Push_show_message').returns(showPushMessage); | ||
| settingsGetStub.withArgs('Push_show_username_room').returns(showUserOrRoomName); | ||
| settingsGetStub.withArgs('UI_Use_Real_Name').returns(false); | ||
|
|
||
| const duration = 4000; | ||
| const notificationMessage = message.msg; | ||
|
|
||
| await notifyDesktopUser({ | ||
| userId: fakeUserId, | ||
| user, | ||
| room, | ||
| message, | ||
| duration, | ||
| notificationMessage, | ||
| receiver, | ||
| }); | ||
|
|
||
| expect(broadcastStub.calledOnce).to.be.true; | ||
| const [eventName, targetUserId, payload] = broadcastStub.firstCall.args as [string, string, any]; | ||
|
|
||
| expect(eventName).to.equal('notify.desktop'); | ||
| expect(targetUserId).to.equal(fakeUserId); | ||
|
|
||
| expect(payload.text).to.equal(expectedNotificationMessage); | ||
|
|
||
| if (showPushMessage) { | ||
| expect(payload.payload.message?.msg).to.equal(message.msg); | ||
| } else { | ||
| expect(!!payload.payload.message?.msg).to.equal(false); | ||
| } | ||
|
|
||
| if (showUserOrRoomName) { | ||
| expect(payload.title).to.equal(expectedTitle); | ||
| expect(payload.payload.name).to.equal(room.name); | ||
| } else { | ||
| expect(!!payload.title).to.equal(false, `Found title to be ${payload.title} when expected falsy`); | ||
| expect(!!payload.payload.name).to.equal(false, `Found name to be ${payload.name} when expected falsy`); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.