[FIX] Livechat not sending desktop notifications (#11266)

pull/11351/head
Renato Becker 7 years ago committed by Diego Sampaio
parent 830607da64
commit 22d1caeb24
  1. 39
      packages/rocketchat-lib/lib/RoomTypeConfig.js
  2. 8
      packages/rocketchat-lib/lib/RoomTypesCommon.js
  3. 19
      packages/rocketchat-lib/lib/roomTypes/direct.js
  4. 14
      packages/rocketchat-lib/server/functions/notifications/desktop.js
  5. 2
      packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
  6. 3
      packages/rocketchat-livechat/client/roomType.js
  7. 6
      packages/rocketchat-livechat/imports/LivechatRoomType.js
  8. 5
      packages/rocketchat-livechat/package.js
  9. 25
      packages/rocketchat-livechat/server/roomType.js

@ -150,6 +150,16 @@ export class RoomTypeConfig {
return true;
}
/**
* Return a room's name
*
* @abstract
* @return {string} Room's name according to it's type
*/
roomName(/* room */) {
return '';
}
canBeCreated() {
return Meteor.isServer ?
RocketChat.authz.hasAtLeastOnePermission(Meteor.userId(), [`create-${ this._identifier }`]) :
@ -203,4 +213,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.roomName(room) }`;
const text = `${ RocketChat.settings.get('UI_Use_Real_Name') ? user.name : user.username }: ${ notificationMessage }`;
return { title, text };
}
}

@ -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];
}
}

@ -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 };
}
}

@ -16,19 +16,7 @@ export function notifyDesktopUser({
duration,
notificationMessage
}) {
const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;
let title = '';
let text = '';
if (room.t === 'd') {
title = UI_Use_Real_Name ? user.name : `@${ user.username }`;
text = notificationMessage;
} else if (room.name) {
title = `#${ room.name }`;
text = `${ UI_Use_Real_Name ? user.name : user.username }: ${ notificationMessage }`;
} else {
return;
}
const { title, text } = RocketChat.roomTypes.getConfig(room.t).getNotificationDetails(room, user, notificationMessage);
RocketChat.metrics.notificationsSent.inc({ notification_type: 'desktop' });
RocketChat.Notifications.notifyUser(userId, 'notification', {

@ -157,7 +157,7 @@ function sendAllNotifications(message, room) {
return message;
}
const sender = (room.t !== 'l') ? RocketChat.models.Users.findOneById(message.u._id) : room.v;
const sender = RocketChat.roomTypes.getConfig(room.t).getMsgSender(message.u._id);
if (!sender) {
return message;
}

@ -0,0 +1,3 @@
import LivechatRoomType from '../imports/LivechatRoomType';
RocketChat.roomTypes.add(new LivechatRoomType());

@ -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() {
@ -20,7 +20,7 @@ class LivechatRoomRoute extends RoomTypeRouteConfig {
}
}
class LivechatRoomType extends RoomTypeConfig {
export default class LivechatRoomType extends RoomTypeConfig {
constructor() {
super({
identifier: 'l',
@ -82,5 +82,3 @@ class LivechatRoomType extends RoomTypeConfig {
}
}
}
RocketChat.roomTypes.add(new LivechatRoomType());

@ -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');
@ -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');

@ -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] ${ this.roomName(room) }`;
const text = notificationMessage;
return { title, text };
}
}
RocketChat.roomTypes.add(new LivechatRoomTypeServer());
Loading…
Cancel
Save