Skip to content

Commit 22d1cae

Browse files
renatobeckersampaiodiego
authored andcommitted
[FIX] Livechat not sending desktop notifications (RocketChat#11266)
1 parent 830607d commit 22d1cae

9 files changed

Lines changed: 102 additions & 19 deletions

File tree

‎packages/rocketchat-lib/lib/RoomTypeConfig.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ export class RoomTypeConfig {
150150
return true;
151151
}
152152

153+
/**
154+
* Return a room's name
155+
*
156+
* @abstract
157+
* @return {string} Room's name according to it's type
158+
*/
159+
roomName(/* room */) {
160+
return '';
161+
}
162+
153163
canBeCreated() {
154164
return Meteor.isServer ?
155165
RocketChat.authz.hasAtLeastOnePermission(Meteor.userId(), [`create-${ this._identifier }`]) :
@@ -203,4 +213,33 @@ export class RoomTypeConfig {
203213
getUiText(/* context */) {
204214
return '';
205215
}
216+
217+
/**
218+
* Returns the full object of message sender
219+
* @param {string} senderId Sender's _id
220+
* @return {object} Sender's object from db
221+
*/
222+
getMsgSender(senderId) {
223+
return Meteor.isServer ? RocketChat.models.Users.findOneById(senderId) : {};
224+
}
225+
226+
/**
227+
* Returns details to use on notifications
228+
*
229+
* @param {object} room
230+
* @param {object} user
231+
* @param {string} notificationMessage
232+
* @return {object} Notification details
233+
*/
234+
getNotificationDetails(room, user, notificationMessage) {
235+
if (!Meteor.isServer) {
236+
return {};
237+
}
238+
239+
const title = `#${ this.roomName(room) }`;
240+
241+
const text = `${ RocketChat.settings.get('UI_Use_Real_Name') ? user.name : user.username }: ${ notificationMessage }`;
242+
243+
return { title, text };
244+
}
206245
}

‎packages/rocketchat-lib/lib/RoomTypesCommon.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,12 @@ export class RoomTypesCommon {
9090

9191
return FlowRouter.go(this.roomTypes[roomType].route.name, routeData, queryParams);
9292
}
93+
94+
/**
95+
* @param {string} roomType room type (e.g.: c (for channels), d (for direct channels))
96+
* @param {RoomTypeConfig} roomConfig room's type configuration
97+
*/
98+
getConfig(roomType) {
99+
return this.roomTypes[roomType];
100+
}
93101
}

‎packages/rocketchat-lib/lib/roomTypes/direct.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,23 @@ export class DirectMessageRoomType extends RoomTypeConfig {
111111
return '';
112112
}
113113
}
114+
115+
/**
116+
* Returns details to use on notifications
117+
*
118+
* @param {object} room
119+
* @param {object} user
120+
* @param {string} notificationMessage
121+
* @return {object} Notification details
122+
*/
123+
getNotificationDetails(room, user, notificationMessage) {
124+
if (!Meteor.isServer) {
125+
return {};
126+
}
127+
128+
const title = RocketChat.settings.get('UI_Use_Real_Name') ? user.name : `@${ user.username }`;
129+
const text = notificationMessage;
130+
131+
return { title, text };
132+
}
114133
}

‎packages/rocketchat-lib/server/functions/notifications/desktop.js‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,7 @@ export function notifyDesktopUser({
1616
duration,
1717
notificationMessage
1818
}) {
19-
const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;
20-
21-
let title = '';
22-
let text = '';
23-
if (room.t === 'd') {
24-
title = UI_Use_Real_Name ? user.name : `@${ user.username }`;
25-
text = notificationMessage;
26-
} else if (room.name) {
27-
title = `#${ room.name }`;
28-
text = `${ UI_Use_Real_Name ? user.name : user.username }: ${ notificationMessage }`;
29-
} else {
30-
return;
31-
}
19+
const { title, text } = RocketChat.roomTypes.getConfig(room.t).getNotificationDetails(room, user, notificationMessage);
3220

3321
RocketChat.metrics.notificationsSent.inc({ notification_type: 'desktop' });
3422
RocketChat.Notifications.notifyUser(userId, 'notification', {

‎packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function sendAllNotifications(message, room) {
157157
return message;
158158
}
159159

160-
const sender = (room.t !== 'l') ? RocketChat.models.Users.findOneById(message.u._id) : room.v;
160+
const sender = RocketChat.roomTypes.getConfig(room.t).getMsgSender(message.u._id);
161161
if (!sender) {
162162
return message;
163163
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import LivechatRoomType from '../imports/LivechatRoomType';
2+
3+
RocketChat.roomTypes.add(new LivechatRoomType());

packages/rocketchat-livechat/roomType.js renamed to packages/rocketchat-livechat/imports/LivechatRoomType.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* globals openRoom, LivechatInquiry */
2-
import {RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext} from 'meteor/rocketchat:lib';
2+
import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from 'meteor/rocketchat:lib';
33

44
class LivechatRoomRoute extends RoomTypeRouteConfig {
55
constructor() {
@@ -20,7 +20,7 @@ class LivechatRoomRoute extends RoomTypeRouteConfig {
2020
}
2121
}
2222

23-
class LivechatRoomType extends RoomTypeConfig {
23+
export default class LivechatRoomType extends RoomTypeConfig {
2424
constructor() {
2525
super({
2626
identifier: 'l',
@@ -82,5 +82,3 @@ class LivechatRoomType extends RoomTypeConfig {
8282
}
8383
}
8484
}
85-
86-
RocketChat.roomTypes.add(new LivechatRoomType());

‎packages/rocketchat-livechat/package.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ Package.onUse(function(api) {
4646
api.addFiles('server/visitorStatus.js', 'server');
4747
api.addFiles('permissions.js', 'server');
4848
api.addFiles('messageTypes.js');
49-
api.addFiles('roomType.js');
5049

5150
api.addFiles('config.js', 'server');
5251

52+
api.addFiles('client/roomType.js', 'client');
5353
api.addFiles('client/ui.js', 'client');
5454
api.addFiles('client/route.js', 'client');
5555

@@ -131,6 +131,9 @@ Package.onUse(function(api) {
131131
api.addFiles('client/views/app/triggers/livechatTriggerCondition.html', 'client');
132132
api.addFiles('client/views/app/triggers/livechatTriggerCondition.js', 'client');
133133

134+
// startup
135+
api.addFiles('server/roomType.js', 'server');
136+
134137
// hooks
135138
api.addFiles('server/hooks/externalMessage.js', 'server');
136139
api.addFiles('server/hooks/leadCapture.js', 'server');
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import LivechatRoomType from '../imports/LivechatRoomType';
2+
import LivechatVisitors from './models/LivechatVisitors';
3+
4+
class LivechatRoomTypeServer extends LivechatRoomType {
5+
getMsgSender(senderId) {
6+
return LivechatVisitors.findOneById(senderId);
7+
}
8+
9+
/**
10+
* Returns details to use on notifications
11+
*
12+
* @param {object} room
13+
* @param {object} user
14+
* @param {string} notificationMessage
15+
* @return {object} Notification details
16+
*/
17+
getNotificationDetails(room, user, notificationMessage) {
18+
const title = `[livechat] ${ this.roomName(room) }`;
19+
const text = notificationMessage;
20+
21+
return { title, text };
22+
}
23+
}
24+
25+
RocketChat.roomTypes.add(new LivechatRoomTypeServer());

0 commit comments

Comments
 (0)