Skip to content

Commit

Permalink
Fix livechat desktop notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Jul 5, 2018
1 parent 3c27397 commit 5a691ca
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 18 deletions.
29 changes: 29 additions & 0 deletions packages/rocketchat-lib/lib/RoomTypeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,33 @@ export class RoomTypeConfig {
getUiText(/* context */) {
return '';
}

/**
* Returns the full object of message sender
* @param {string} senderId Sender's _id
* @return {object} Sender's object from db
*/
getMsgSender(senderId) {
return Meteor.isServer ? RocketChat.models.Users.findOneById(senderId) : {};
}

/**
* Returns details to use on notifications
*
* @param {object} room
* @param {object} user
* @param {string} notificationMessage
* @return {object} Notification details
*/
getNotificationDetails(room, user, notificationMessage) {
if (!Meteor.isServer) {
return {};
}

const title = `#${ this.getRoomName(room.t, room) }`;

const text = `${ RocketChat.settings.get('UI_Use_Real_Name') ? user.name : user.username }: ${ notificationMessage }`;

return { title, text };
}
}
8 changes: 8 additions & 0 deletions packages/rocketchat-lib/lib/RoomTypesCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ export class RoomTypesCommon {

return FlowRouter.go(this.roomTypes[roomType].route.name, routeData, queryParams);
}

/**
* @param {string} roomType room type (e.g.: c (for channels), d (for direct channels))
* @param {RoomTypeConfig} roomConfig room's type configuration
*/
getConfig(roomType) {
return this.roomTypes[roomType];
}
}
19 changes: 19 additions & 0 deletions packages/rocketchat-lib/lib/roomTypes/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,23 @@ export class DirectMessageRoomType extends RoomTypeConfig {
return '';
}
}

/**
* Returns details to use on notifications
*
* @param {object} room
* @param {object} user
* @param {string} notificationMessage
* @return {object} Notification details
*/
getNotificationDetails(room, user, notificationMessage) {
if (!Meteor.isServer) {
return {};
}

const title = RocketChat.settings.get('UI_Use_Real_Name') ? user.name : `@${ user.username }`;
const text = notificationMessage;

return { title, text };
}
}
14 changes: 2 additions & 12 deletions packages/rocketchat-lib/server/functions/notifications/desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,9 @@ export function notifyDesktopUser({
duration,
notificationMessage
}) {
const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;
const roomConfig = RocketChat.roomTypes.getConfig(room.t);

let title = '';
let text = '';
if (room.t === 'd') {
title = UI_Use_Real_Name ? user.name : `@${ user.username }`;
text = notificationMessage;
} else if (RocketChat.roomTypes.getRoomName(room.t, room)) {
title = `#${ RocketChat.roomTypes.getRoomName(room.t, room) }`;
text = `${ UI_Use_Real_Name ? user.name : user.username }: ${ notificationMessage }`;
} else {
return;
}
const { title, text } = roomConfig.getNotificationDetails(room, user, notificationMessage);

RocketChat.metrics.notificationsSent.inc({ notification_type: 'desktop' });
RocketChat.Notifications.notifyUser(userId, 'notification', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ function sendAllNotifications(message, room) {
return message;
}

const sender = (room.t !== 'l') ? RocketChat.models.Users.findOneById(message.u._id) : room.v;
const roomConfig = RocketChat.roomTypes.getConfig(room.t);

const sender = roomConfig.getMsgSender(message.u._id);
if (!sender) {
return message;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-livechat/client/roomType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import LivechatRoomType from '../imports/LivechatRoomType';

RocketChat.roomTypes.add(new LivechatRoomType());
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals openRoom, LivechatInquiry */
import {RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext} from 'meteor/rocketchat:lib';
import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from 'meteor/rocketchat:lib';

class LivechatRoomRoute extends RoomTypeRouteConfig {
constructor() {
Expand All @@ -20,7 +20,7 @@ class LivechatRoomRoute extends RoomTypeRouteConfig {
}
}

class LivechatRoomType extends RoomTypeConfig {
export default class LivechatRoomType extends RoomTypeConfig {
constructor() {
super({
identifier: 'l',
Expand Down Expand Up @@ -82,5 +82,3 @@ class LivechatRoomType extends RoomTypeConfig {
}
}
}

RocketChat.roomTypes.add(new LivechatRoomType());
5 changes: 4 additions & 1 deletion packages/rocketchat-livechat/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Package.onUse(function(api) {
api.addFiles('server/visitorStatus.js', 'server');
api.addFiles('permissions.js', 'server');
api.addFiles('messageTypes.js');
api.addFiles('roomType.js');

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

api.addFiles('client/roomType.js', 'client');
api.addFiles('client/ui.js', 'client');
api.addFiles('client/route.js', 'client');

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

// startup
api.addFiles('server/roomType.js', 'server');

// hooks
api.addFiles('server/hooks/externalMessage.js', 'server');
api.addFiles('server/hooks/leadCapture.js', 'server');
Expand Down
25 changes: 25 additions & 0 deletions packages/rocketchat-livechat/server/roomType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import LivechatRoomType from '../imports/LivechatRoomType';
import LivechatVisitors from './models/LivechatVisitors';

class LivechatRoomTypeServer extends LivechatRoomType {
getMsgSender(senderId) {
return LivechatVisitors.findOneById(senderId);
}

/**
* Returns details to use on notifications
*
* @param {object} room
* @param {object} user
* @param {string} notificationMessage
* @return {object} Notification details
*/
getNotificationDetails(room, user, notificationMessage) {
const title = `[livechat] ${ user.name || user.username }`;
const text = notificationMessage;

return { title, text };
}
}

RocketChat.roomTypes.add(new LivechatRoomTypeServer());

0 comments on commit 5a691ca

Please sign in to comment.