Skip to content

Commit bf14a53

Browse files
authored
refactor: Custom notification sound from streams (RocketChat#36774)
1 parent e7aacb2 commit bf14a53

5 files changed

Lines changed: 44 additions & 7 deletions

File tree

apps/meteor/app/lib/server/functions/notifications/desktop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ export async function notifyDesktopUser({
2222
room,
2323
duration,
2424
notificationMessage,
25+
audioNotificationValue,
2526
}: {
2627
userId: string;
2728
user: AtLeast<IUser, '_id' | 'name' | 'username'>;
2829
message: IMessage | Pick<IMessage, 'u'>;
2930
room: IRoom;
3031
duration?: number;
3132
notificationMessage: string;
33+
audioNotificationValue?: string;
3234
}): Promise<void> {
3335
const { title, text, name } = await roomCoordinator
3436
.getRoomDirectives(room.t)
@@ -57,6 +59,7 @@ export async function notifyDesktopUser({
5759
}),
5860
},
5961
name,
62+
audioNotificationValue,
6063
},
6164
};
6265

apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,15 @@ type SubscriptionAggregation = {
2828
receiver: [Pick<IUser, 'active' | 'emails' | 'language' | 'status' | 'statusConnection' | 'username' | 'settings'> | null];
2929
} & Pick<
3030
ISubscription,
31-
'desktopNotifications' | 'emailNotifications' | 'mobilePushNotifications' | 'muteGroupMentions' | 'name' | 'rid' | 'userHighlights' | 'u'
31+
| 'desktopNotifications'
32+
| 'emailNotifications'
33+
| 'mobilePushNotifications'
34+
| 'muteGroupMentions'
35+
| 'name'
36+
| 'rid'
37+
| 'userHighlights'
38+
| 'u'
39+
| 'audioNotificationValue'
3240
>;
3341

3442
type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
@@ -134,6 +142,7 @@ export const sendNotification = async ({
134142
user: sender,
135143
message,
136144
room,
145+
audioNotificationValue: subscription.audioNotificationValue,
137146
});
138147
}
139148

@@ -244,6 +253,7 @@ const project = {
244253
'receiver.statusConnection': 1,
245254
'receiver.username': 1,
246255
'receiver.settings.preferences.enableMobileRinging': 1,
256+
'audioNotificationValue': 1,
247257
},
248258
} as const;
249259

apps/meteor/client/hooks/notification/useNewMessageNotification.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import type { AtLeast, ISubscription } from '@rocket.chat/core-typings';
22
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
33
import { useCustomSound } from '@rocket.chat/ui-contexts';
44

5-
import { Subscriptions } from '../../stores';
6-
75
export const useNewMessageNotification = () => {
86
const { notificationSounds } = useCustomSound();
97

@@ -12,10 +10,8 @@ export const useNewMessageNotification = () => {
1210
return;
1311
}
1412

15-
const subscription = Subscriptions.state.find((record) => record.rid === sub.rid);
16-
17-
if (subscription?.audioNotificationValue) {
18-
return notificationSounds.playNewMessageCustom(subscription.audioNotificationValue);
13+
if (sub?.audioNotificationValue) {
14+
return notificationSounds.playNewMessageCustom(sub.audioNotificationValue);
1915
}
2016

2117
notificationSounds.playNewMessage();

apps/meteor/tests/e2e/notification-sounds.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,30 @@ test.describe.serial('Notification Sounds', () => {
127127
expect(audioCalls.played).toBe(true);
128128
});
129129
});
130+
131+
test.describe('none sound notification preferences', () => {
132+
test.beforeEach(async ({ api }) => {
133+
await api.post('/rooms.saveNotification', {
134+
roomId: targetChannelId,
135+
notifications: {
136+
audioNotificationValue: 'none',
137+
},
138+
});
139+
});
140+
141+
test('should not play any notification sound', async ({ page }) => {
142+
await user1Page.goto(`/channel/${targetChannel}`);
143+
const user1PoHomeChannel = new HomeChannel(user1Page);
144+
await user1PoHomeChannel.content.waitForChannel();
145+
146+
await poHomeChannel.sidenav.sidebarHomeAction.click();
147+
148+
await user1PoHomeChannel.content.sendMessage(`Hello @${Users.admin.data.username} from User 1`);
149+
150+
await page.waitForTimeout(100); // wait for the sound to play
151+
152+
const audioCalls = await page.evaluate(() => window.__audioCalls);
153+
expect(audioCalls).toBeUndefined();
154+
});
155+
});
130156
});

packages/core-typings/src/INotification.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ICalendarEvent } from './ICalendarEvent';
22
import type { IMessage } from './IMessage';
33
import type { IRoom } from './IRoom';
4+
import type { ISubscription } from './ISubscription';
45

56
export interface INotificationItemPush {
67
type: 'push';
@@ -64,6 +65,7 @@ export interface INotificationDesktop {
6465
msg: IMessage['msg'];
6566
t?: IMessage['t'];
6667
};
68+
audioNotificationValue: ISubscription['audioNotificationValue'];
6769
};
6870
}
6971

0 commit comments

Comments
 (0)