Skip to content
Snippets Groups Projects
Commit f2699e49 authored by Yash Rajpal's avatar Yash Rajpal Committed by Guilherme Gazzo
Browse files

fix: Desktop notification routing for direct rooms (#30816)

parent 055cb0fa
No related branches found
No related tags found
No related merge requests found
---
'@rocket.chat/meteor': patch
---
Fix desktop notification routing for direct rooms
......@@ -30,7 +30,9 @@ export async function notifyDesktopUser({
duration: number;
notificationMessage: string;
}): Promise<void> {
const { title, text } = await roomCoordinator.getRoomDirectives(room.t).getNotificationDetails(room, user, notificationMessage, userId);
const { title, text, name } = await roomCoordinator
.getRoomDirectives(room.t)
.getNotificationDetails(room, user, notificationMessage, userId);
const payload = {
title: title || '',
......@@ -42,11 +44,11 @@ export async function notifyDesktopUser({
tmid: message.tmid,
sender: message.u,
type: room.t,
name: room.name,
message: {
msg: message.msg,
t: message.t,
},
name,
},
};
......
import type { IMessage, IRoom, IUser, RoomType } from '@rocket.chat/core-typings';
import type { INotificationDesktop, IRoom, IUser } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
......@@ -22,25 +22,6 @@ declare global {
}
}
export type NotificationEvent = {
icon?: string;
title: string;
text: string;
duration?: number;
payload: {
_id?: IMessage['_id'];
rid?: IRoom['_id'];
tmid?: IMessage['_id'];
sender?: Pick<IUser, '_id' | 'username'>;
type?: RoomType;
name?: string;
message?: {
msg: string;
t?: string;
};
};
};
class KonchatNotification {
public notificationStatus = new ReactiveVar<NotificationPermission | undefined>(undefined);
......@@ -52,7 +33,7 @@ class KonchatNotification {
}
}
public async notify(notification: NotificationEvent) {
public async notify(notification: INotificationDesktop) {
if (typeof window.Notification === 'undefined' || Notification.permission !== 'granted') {
return;
}
......@@ -146,11 +127,20 @@ class KonchatNotification {
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
case 'l':
return router.navigate({
pattern: '/live/:id/:tab?/:context?',
params: {
id: notification.payload.rid,
tab: 'room-info',
},
search: { ...router.getSearchParameters(), jump: notification.payload._id },
});
}
};
}
public async showDesktop(notification: NotificationEvent) {
public async showDesktop(notification: INotificationDesktop) {
if (!notification.payload.rid) {
return;
}
......
import type { INotificationDesktop } from '@rocket.chat/core-typings';
import type { SelectOption } from '@rocket.chat/fuselage';
import { Accordion, Field, FieldLabel, FieldRow, FieldHint, Select, FieldGroup, ToggleSwitch, Button, Box } from '@rocket.chat/fuselage';
import { useUniqueId } from '@rocket.chat/fuselage-hooks';
......@@ -45,7 +46,7 @@ const PreferencesNotificationsSection = () => {
const onSendNotification = useCallback(() => {
KonchatNotification.notify({
payload: { sender: { _id: 'rocket.cat', username: 'rocket.cat' } },
payload: { sender: { _id: 'rocket.cat', username: 'rocket.cat' }, rid: 'GENERAL' } as INotificationDesktop['payload'],
title: t('Desktop_Notification_Test'),
text: t('This_is_a_desktop_notification'),
});
......
......@@ -105,7 +105,7 @@ export interface IRoomTypeServerDirectives {
sender: AtLeast<IUser, '_id' | 'name' | 'username'>,
notificationMessage: string,
userId: string,
) => Promise<{ title: string | undefined; text: string }>;
) => Promise<{ title: string | undefined; text: string; name: string | undefined }>;
getMsgSender: (senderId: IUser['_id']) => Promise<IUser | null>;
includeInRoomSearch: () => boolean;
getReadReceiptsExtraData: (message: IMessage) => Partial<ReadReceipt>;
......
......@@ -3,6 +3,7 @@ import { Users } from '@rocket.chat/models';
import { settings } from '../../../app/settings/server';
import type { IRoomTypeConfig, IRoomTypeServerDirectives, RoomSettingsEnum, RoomMemberActions } from '../../../definition/IRoomTypeConfig';
import { getUserDisplayName } from '../../../lib/getUserDisplayName';
import { RoomCoordinator } from '../../../lib/rooms/coordinator';
class RoomCoordinatorServer extends RoomCoordinator {
......@@ -40,13 +41,14 @@ class RoomCoordinatorServer extends RoomCoordinator {
sender: AtLeast<IUser, '_id' | 'name' | 'username'>,
notificationMessage: string,
userId: string,
): Promise<{ title: string | undefined; text: string }> {
): Promise<{ title: string | undefined; text: string; name: string | undefined }> {
const title = `#${await this.roomName(room, userId)}`;
const name = settings.get<boolean>('UI_Use_Real_Name') ? sender.name : sender.username;
const useRealName = settings.get<boolean>('UI_Use_Real_Name');
const senderName = getUserDisplayName(sender.name, sender.username, useRealName);
const text = `${name}: ${notificationMessage}`;
const text = `${senderName}: ${notificationMessage}`;
return { title, text };
return { title, text, name: room.name };
},
getMsgSender(senderId: IUser['_id']): Promise<IUser | null> {
return Users.findOneById(senderId);
......
......@@ -94,16 +94,20 @@ roomCoordinator.add(DirectMessageRoomType, {
async getNotificationDetails(room, sender, notificationMessage, userId) {
const useRealName = settings.get<boolean>('UI_Use_Real_Name');
const displayRoomName = await this.roomName(room, userId);
if (this.isGroupChat(room)) {
return {
title: await this.roomName(room, userId),
title: displayRoomName,
text: `${(useRealName && sender.name) || sender.username}: ${notificationMessage}`,
name: room.name || displayRoomName,
};
}
return {
title: (useRealName && sender.name) || sender.username,
text: notificationMessage,
name: room.name || displayRoomName,
};
},
......
......@@ -35,7 +35,7 @@ roomCoordinator.add(LivechatRoomType, {
const title = `[Omnichannel] ${roomName}`;
const text = notificationMessage;
return { title, text };
return { title, text, name: roomName };
},
async getMsgSender(senderId) {
......
......@@ -16,7 +16,7 @@ roomCoordinator.add(VoipRoomType, {
const title = `[Omnichannel] ${this.roomName(room, userId)}`;
const text = notificationMessage;
return { title, text };
return { title, text, name: room.name };
},
async getMsgSender(senderId) {
......
......@@ -51,6 +51,7 @@ export interface INotification {
export interface INotificationDesktop {
title: string;
text: string;
icon?: string;
duration?: number;
payload: {
_id: IMessage['_id'];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment