diff --git a/packages/rocketchat-livechat/imports/server/rest/departments.js b/packages/rocketchat-livechat/imports/server/rest/departments.js index 635f89b32de1..c4c8abce1d9f 100644 --- a/packages/rocketchat-livechat/imports/server/rest/departments.js +++ b/packages/rocketchat-livechat/imports/server/rest/departments.js @@ -1,12 +1,12 @@ import { check } from 'meteor/check'; -import { RocketChat } from 'meteor/rocketchat:lib'; import { API } from 'meteor/rocketchat:api'; +import { hasPermission } from 'meteor/rocketchat:authorization'; import { LivechatDepartment, LivechatDepartmentAgents } from '../../../server/models'; import { Livechat } from '../../../server/lib/Livechat'; API.v1.addRoute('livechat/department', { authRequired: true }, { get() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -15,7 +15,7 @@ API.v1.addRoute('livechat/department', { authRequired: true }, { }); }, post() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -43,7 +43,7 @@ API.v1.addRoute('livechat/department', { authRequired: true }, { API.v1.addRoute('livechat/department/:_id', { authRequired: true }, { get() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -61,7 +61,7 @@ API.v1.addRoute('livechat/department/:_id', { authRequired: true }, { } }, put() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -88,7 +88,7 @@ API.v1.addRoute('livechat/department/:_id', { authRequired: true }, { } }, delete() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } diff --git a/packages/rocketchat-livechat/imports/server/rest/facebook.js b/packages/rocketchat-livechat/imports/server/rest/facebook.js index d10c0664be23..a74b4cdf14f8 100644 --- a/packages/rocketchat-livechat/imports/server/rest/facebook.js +++ b/packages/rocketchat-livechat/imports/server/rest/facebook.js @@ -1,8 +1,8 @@ import crypto from 'crypto'; import { Random } from 'meteor/random'; -import { RocketChat } from 'meteor/rocketchat:lib'; import { API } from 'meteor/rocketchat:api'; - +import { Rooms, Users } from 'meteor/rocketchat:models'; +import { settings } from 'meteor/rocketchat:settings'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; import { Livechat } from '../../../server/lib/Livechat'; @@ -33,7 +33,7 @@ API.v1.addRoute('livechat/facebook', { }; } - if (!RocketChat.settings.get('Livechat_Facebook_Enabled')) { + if (!settings.get('Livechat_Facebook_Enabled')) { return { success: false, error: 'Integration disabled', @@ -41,7 +41,7 @@ API.v1.addRoute('livechat/facebook', { } // validate if request come from omni - const signature = crypto.createHmac('sha1', RocketChat.settings.get('Livechat_Facebook_API_Secret')).update(JSON.stringify(this.request.body)).digest('hex'); + const signature = crypto.createHmac('sha1', settings.get('Livechat_Facebook_API_Secret')).update(JSON.stringify(this.request.body)).digest('hex'); if (this.request.headers['x-hub-signature'] !== `sha1=${ signature }`) { return { success: false, @@ -61,7 +61,7 @@ API.v1.addRoute('livechat/facebook', { }; let visitor = LivechatVisitors.getVisitorByToken(this.bodyParams.token); if (visitor) { - const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(visitor.token).fetch(); + const rooms = Rooms.findOpenByVisitorToken(visitor.token).fetch(); if (rooms && rooms.length > 0) { sendMessage.message.rid = rooms[0]._id; } else { @@ -77,7 +77,7 @@ API.v1.addRoute('livechat/facebook', { name: `${ this.bodyParams.first_name } ${ this.bodyParams.last_name }`, }); - visitor = RocketChat.models.Users.findOneById(userId); + visitor = Users.findOneById(userId); } sendMessage.message.msg = this.bodyParams.text; diff --git a/packages/rocketchat-livechat/imports/server/rest/sms.js b/packages/rocketchat-livechat/imports/server/rest/sms.js index 8b7861fcd600..328267d00a01 100644 --- a/packages/rocketchat-livechat/imports/server/rest/sms.js +++ b/packages/rocketchat-livechat/imports/server/rest/sms.js @@ -1,6 +1,6 @@ import { Meteor } from 'meteor/meteor'; import { Random } from 'meteor/random'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Rooms } from 'meteor/rocketchat:models'; import { API } from 'meteor/rocketchat:api'; import { SMS } from 'meteor/rocketchat:sms'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; @@ -26,7 +26,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', { }; if (visitor) { - const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(visitor.token).fetch(); + const rooms = Rooms.findOpenByVisitorToken(visitor.token).fetch(); if (rooms && rooms.length > 0) { sendMessage.message.rid = rooms[0]._id; diff --git a/packages/rocketchat-livechat/imports/server/rest/upload.js b/packages/rocketchat-livechat/imports/server/rest/upload.js index 61dd1438bb5f..808eb1a232af 100644 --- a/packages/rocketchat-livechat/imports/server/rest/upload.js +++ b/packages/rocketchat-livechat/imports/server/rest/upload.js @@ -1,5 +1,7 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; +import { Settings, Rooms } from 'meteor/rocketchat:models'; +import { fileUploadIsValidContentType } from 'meteor/rocketchat:utils'; import { FileUpload } from 'meteor/rocketchat:file-upload'; import { API } from 'meteor/rocketchat:api'; import Busboy from 'busboy'; @@ -7,11 +9,11 @@ import filesize from 'filesize'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; let maxFileSize; -RocketChat.settings.get('FileUpload_MaxFileSize', function(key, value) { +settings.get('FileUpload_MaxFileSize', function(key, value) { try { maxFileSize = parseInt(value); } catch (e) { - maxFileSize = RocketChat.models.Settings.findOneById('FileUpload_MaxFileSize').packageValue; + maxFileSize = Settings.findOneById('FileUpload_MaxFileSize').packageValue; } }); @@ -28,7 +30,7 @@ API.v1.addRoute('livechat/upload/:rid', { return API.v1.unauthorized(); } - const room = RocketChat.models.Rooms.findOneOpenByRoomIdAndVisitorToken(this.urlParams.rid, visitorToken); + const room = Rooms.findOneOpenByRoomIdAndVisitorToken(this.urlParams.rid, visitorToken); if (!room) { return API.v1.unauthorized(); } @@ -68,7 +70,7 @@ API.v1.addRoute('livechat/upload/:rid', { const file = files[0]; - if (!RocketChat.fileUploadIsValidContentType(file.mimetype)) { + if (!fileUploadIsValidContentType(file.mimetype)) { return API.v1.failure({ reason: 'error-type-not-allowed', }); diff --git a/packages/rocketchat-livechat/imports/server/rest/users.js b/packages/rocketchat-livechat/imports/server/rest/users.js index e8df87995b43..494fd226be73 100644 --- a/packages/rocketchat-livechat/imports/server/rest/users.js +++ b/packages/rocketchat-livechat/imports/server/rest/users.js @@ -1,12 +1,13 @@ import { check } from 'meteor/check'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { hasPermission, getUsersInRole } from 'meteor/rocketchat:authorization'; import { API } from 'meteor/rocketchat:api'; +import { Users } from 'meteor/rocketchat:models'; import { Livechat } from '../../../server/lib/Livechat'; import _ from 'underscore'; API.v1.addRoute('livechat/users/:type', { authRequired: true }, { get() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -24,7 +25,7 @@ API.v1.addRoute('livechat/users/:type', { authRequired: true }, { throw 'Invalid type'; } - const users = RocketChat.authz.getUsersInRole(role); + const users = getUsersInRole(role); return API.v1.success({ users: users.fetch().map((user) => _.pick(user, '_id', 'username', 'name', 'status', 'statusLivechat')), @@ -34,7 +35,7 @@ API.v1.addRoute('livechat/users/:type', { authRequired: true }, { } }, post() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } try { @@ -69,7 +70,7 @@ API.v1.addRoute('livechat/users/:type', { authRequired: true }, { API.v1.addRoute('livechat/users/:type/:_id', { authRequired: true }, { get() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -79,7 +80,7 @@ API.v1.addRoute('livechat/users/:type/:_id', { authRequired: true }, { _id: String, }); - const user = RocketChat.models.Users.findOneById(this.urlParams._id); + const user = Users.findOneById(this.urlParams._id); if (!user) { return API.v1.failure('User not found'); @@ -109,7 +110,7 @@ API.v1.addRoute('livechat/users/:type/:_id', { authRequired: true }, { } }, delete() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -119,7 +120,7 @@ API.v1.addRoute('livechat/users/:type/:_id', { authRequired: true }, { _id: String, }); - const user = RocketChat.models.Users.findOneById(this.urlParams._id); + const user = Users.findOneById(this.urlParams._id); if (!user) { return API.v1.failure(); diff --git a/packages/rocketchat-livechat/lib/LivechatRoomType.js b/packages/rocketchat-livechat/lib/LivechatRoomType.js index 8a9d25171a36..a2a09784f581 100644 --- a/packages/rocketchat-livechat/lib/LivechatRoomType.js +++ b/packages/rocketchat-livechat/lib/LivechatRoomType.js @@ -1,6 +1,9 @@ import { Session } from 'meteor/session'; -import { ChatRoom } from 'meteor/rocketchat:ui'; -import { RocketChat, RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext, openRoom } from 'meteor/rocketchat:lib'; +import { ChatRoom } from 'meteor/rocketchat:models'; +import { settings } from 'meteor/rocketchat:settings'; +import { hasPermission } from 'meteor/rocketchat:authorization'; +import { openRoom } from 'meteor/rocketchat:ui-utils'; +import { RoomSettingsEnum, UiTextContext, RoomTypeRouteConfig, RoomTypeConfig } from 'meteor/rocketchat:utils'; import { LivechatInquiry } from './LivechatInquiry'; class LivechatRoomRoute extends RoomTypeRouteConfig { @@ -46,7 +49,7 @@ export default class LivechatRoomType extends RoomTypeConfig { } condition() { - return RocketChat.settings.get('Livechat_enabled') && RocketChat.authz.hasPermission('view-l-room'); + return settings.get('Livechat_enabled') && hasPermission('view-l-room'); } canSendMessage(roomId) { diff --git a/packages/rocketchat-livechat/lib/messageTypes.js b/packages/rocketchat-livechat/lib/messageTypes.js index ae0f7763045a..c1455b498164 100644 --- a/packages/rocketchat-livechat/lib/messageTypes.js +++ b/packages/rocketchat-livechat/lib/messageTypes.js @@ -1,9 +1,13 @@ import { Meteor } from 'meteor/meteor'; import { TAPi18n } from 'meteor/tap:i18n'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { MessageTypes } from 'meteor/rocketchat:ui-utils'; +import { actionLinks } from 'meteor/rocketchat:action-links'; +import { Notifications } from 'meteor/rocketchat:notifications'; +import { Messages, Rooms } from 'meteor/rocketchat:models'; +import { settings } from 'meteor/rocketchat:settings'; import { Livechat } from 'meteor/rocketchat:livechat'; -RocketChat.MessageTypes.registerType({ +MessageTypes.registerType({ id: 'livechat_navigation_history', system: true, message: 'New_visitor_navigation', @@ -17,34 +21,34 @@ RocketChat.MessageTypes.registerType({ }, }); -RocketChat.MessageTypes.registerType({ +MessageTypes.registerType({ id: 'livechat_video_call', system: true, message: 'New_videocall_request', }); -RocketChat.actionLinks.register('createLivechatCall', function(message, params, instance) { +actionLinks.register('createLivechatCall', function(message, params, instance) { if (Meteor.isClient) { instance.tabBar.open('video'); } }); -RocketChat.actionLinks.register('denyLivechatCall', function(message/* , params*/) { +actionLinks.register('denyLivechatCall', function(message/* , params*/) { if (Meteor.isServer) { const user = Meteor.user(); - RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser('command', message.rid, 'endCall', user); - RocketChat.Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id }); + Messages.createWithTypeRoomIdMessageAndUser('command', message.rid, 'endCall', user); + Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id }); - const language = user.language || RocketChat.settings.get('Language') || 'en'; + const language = user.language || settings.get('Language') || 'en'; Livechat.closeRoom({ user, - room: RocketChat.models.Rooms.findOneById(message.rid), + room: Rooms.findOneById(message.rid), comment: TAPi18n.__('Videocall_declined', { lng: language }), }); Meteor.defer(() => { - RocketChat.models.Messages.setHiddenById(message._id); + Messages.setHiddenById(message._id); }); } }); diff --git a/packages/rocketchat-livechat/package.js b/packages/rocketchat-livechat/package.js index b78f63442d48..fdd14ae623de 100644 --- a/packages/rocketchat-livechat/package.js +++ b/packages/rocketchat-livechat/package.js @@ -26,8 +26,10 @@ Package.onUse(function(api) { 'webapp', 'autoupdate', 'rocketchat:utils', + 'rocketchat:action-links', 'rocketchat:ui-utils', 'rocketchat:settings', + 'rocketchat:callbacks', 'rocketchat:models', 'rocketchat:lib', 'rocketchat:authorization', diff --git a/packages/rocketchat-livechat/server/agentStatus.js b/packages/rocketchat-livechat/server/agentStatus.js index 04628283dec1..6cf9a3bfde14 100644 --- a/packages/rocketchat-livechat/server/agentStatus.js +++ b/packages/rocketchat-livechat/server/agentStatus.js @@ -1,9 +1,9 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { hasRole } from 'meteor/rocketchat:authorization'; import { UserPresenceMonitor } from 'meteor/konecty:user-presence'; import { Livechat } from './lib/Livechat'; UserPresenceMonitor.onSetUserStatus((user, status) => { - if (RocketChat.authz.hasRole(user._id, 'livechat-manager') || RocketChat.authz.hasRole(user._id, 'livechat-agent')) { + if (hasRole(user._id, 'livechat-manager') || hasRole(user._id, 'livechat-agent')) { Livechat.notifyAgentStatusChanged(user._id, status); } }); diff --git a/packages/rocketchat-livechat/server/api/lib/livechat.js b/packages/rocketchat-livechat/server/api/lib/livechat.js index 03b55517b42c..a679c2b688d9 100644 --- a/packages/rocketchat-livechat/server/api/lib/livechat.js +++ b/packages/rocketchat-livechat/server/api/lib/livechat.js @@ -1,13 +1,13 @@ import { Meteor } from 'meteor/meteor'; import { Random } from 'meteor/random'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Users, Rooms } from 'meteor/rocketchat:models'; import { LivechatDepartment, LivechatTrigger } from '../../models'; import _ from 'underscore'; import LivechatVisitors from '../../models/LivechatVisitors'; import { Livechat } from '../../lib/Livechat'; export function online() { - return RocketChat.models.Users.findOnlineAgents().count() > 0; + return Users.findOnlineAgents().count() > 0; } export function findTriggers() { @@ -38,10 +38,10 @@ export function findRoom(token, rid) { }; if (!rid) { - return RocketChat.models.Rooms.findLivechatByVisitorToken(token, fields); + return Rooms.findLivechatByVisitorToken(token, fields); } - return RocketChat.models.Rooms.findLivechatByIdAndVisitorToken(rid, token, fields); + return Rooms.findLivechatByIdAndVisitorToken(rid, token, fields); } export function findOpenRoom(token, departmentId) { @@ -54,7 +54,7 @@ export function findOpenRoom(token, departmentId) { }; let room; - const rooms = departmentId ? RocketChat.models.Rooms.findOpenByVisitorTokenAndDepartmentId(token, departmentId, options).fetch() : RocketChat.models.Rooms.findOpenByVisitorToken(token, options).fetch(); + const rooms = departmentId ? Rooms.findOpenByVisitorTokenAndDepartmentId(token, departmentId, options).fetch() : Rooms.findOpenByVisitorToken(token, options).fetch(); if (rooms && rooms.length > 0) { room = rooms[0]; } @@ -77,7 +77,7 @@ export function getRoom(guest, rid, roomInfo) { } export function findAgent(agentId) { - return RocketChat.models.Users.getAgentInfo(agentId); + return Users.getAgentInfo(agentId); } export function settings() { diff --git a/packages/rocketchat-livechat/server/api/v1/config.js b/packages/rocketchat-livechat/server/api/v1/config.js index 03e86c76128d..99aaf7131904 100644 --- a/packages/rocketchat-livechat/server/api/v1/config.js +++ b/packages/rocketchat-livechat/server/api/v1/config.js @@ -1,4 +1,4 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Users } from 'meteor/rocketchat:models'; import { API } from 'meteor/rocketchat:api'; import { findGuest, settings, online, findOpenRoom } from '../lib/livechat'; import { Match, check } from 'meteor/check'; @@ -26,7 +26,7 @@ API.v1.addRoute('livechat/config', { if (token) { guest = findGuest(token); room = findOpenRoom(token); - agent = room && room.servedBy && RocketChat.models.Users.getAgentInfo(room.servedBy._id); + agent = room && room.servedBy && Users.getAgentInfo(room.servedBy._id); } Object.assign(config, { online: status, guest, room, agent }); diff --git a/packages/rocketchat-livechat/server/api/v1/message.js b/packages/rocketchat-livechat/server/api/v1/message.js index a988bbb9d4bd..c8432efd69b7 100644 --- a/packages/rocketchat-livechat/server/api/v1/message.js +++ b/packages/rocketchat-livechat/server/api/v1/message.js @@ -1,8 +1,10 @@ import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; import { Random } from 'meteor/random'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Messages, Rooms } from 'meteor/rocketchat:models'; +import { hasPermission } from 'meteor/rocketchat:authorization'; import { API } from 'meteor/rocketchat:api'; +import { loadMessageHistory } from 'meteor/rocketchat:lib'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; import { findGuest, findRoom } from '../lib/livechat'; import { Livechat } from '../../lib/Livechat'; @@ -85,7 +87,7 @@ API.v1.addRoute('livechat/message/:_id', { throw new Meteor.Error('invalid-room'); } - const msg = RocketChat.models.Messages.findOneById(_id); + const msg = Messages.findOneById(_id); if (!msg) { throw new Meteor.Error('invalid-message'); } @@ -94,7 +96,7 @@ API.v1.addRoute('livechat/message/:_id', { const result = Livechat.updateMessage({ guest, message }); if (result) { - const data = RocketChat.models.Messages.findOneById(_id); + const data = Messages.findOneById(_id); return API.v1.success({ message: { _id: data._id, rid: data.rid, msg: data.msg, u: data.u, ts: data.ts }, }); @@ -129,7 +131,7 @@ API.v1.addRoute('livechat/message/:_id', { throw new Meteor.Error('invalid-room'); } - const message = RocketChat.models.Messages.findOneById(_id); + const message = Messages.findOneById(_id); if (!message) { throw new Meteor.Error('invalid-message'); } @@ -190,7 +192,7 @@ API.v1.addRoute('livechat/messages.history/:rid', { limit = parseInt(this.queryParams.limit); } - const messages = RocketChat.loadMessageHistory({ userId: guest._id, rid, end, limit, ls }); + const messages = loadMessageHistory({ userId: guest._id, rid, end, limit, ls }); return API.v1.success(messages); } catch (e) { return API.v1.failure(e.error); @@ -200,7 +202,7 @@ API.v1.addRoute('livechat/messages.history/:rid', { API.v1.addRoute('livechat/messages', { authRequired: true }, { post() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } @@ -225,7 +227,7 @@ API.v1.addRoute('livechat/messages', { authRequired: true }, { let visitor = LivechatVisitors.getVisitorByToken(visitorToken); let rid; if (visitor) { - const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(visitorToken).fetch(); + const rooms = Rooms.findOpenByVisitorToken(visitorToken).fetch(); if (rooms && rooms.length > 0) { rid = rooms[0]._id; } else { diff --git a/packages/rocketchat-livechat/server/api/v1/videoCall.js b/packages/rocketchat-livechat/server/api/v1/videoCall.js index aaa7e0521d3f..1ecb196944a2 100644 --- a/packages/rocketchat-livechat/server/api/v1/videoCall.js +++ b/packages/rocketchat-livechat/server/api/v1/videoCall.js @@ -1,7 +1,8 @@ import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; import { Random } from 'meteor/random'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Messages } from 'meteor/rocketchat:models'; +import { settings as rcSettings } from 'meteor/rocketchat:settings'; import { API } from 'meteor/rocketchat:api'; import { findGuest, getRoom, settings } from '../lib/livechat'; @@ -30,15 +31,15 @@ API.v1.addRoute('livechat/video.call/:token', { throw new Meteor.Error('invalid-livechat-config'); } - RocketChat.models.Messages.createWithTypeRoomIdMessageAndUser('livechat_video_call', room._id, '', guest, { + Messages.createWithTypeRoomIdMessageAndUser('livechat_video_call', room._id, '', guest, { actionLinks: config.theme.actionLinks, }); const videoCall = { rid, - domain: RocketChat.settings.get('Jitsi_Domain'), + domain: rcSettings.get('Jitsi_Domain'), provider: 'jitsi', - room: RocketChat.settings.get('Jitsi_URL_Room_Prefix') + RocketChat.settings.get('uniqueID') + rid, + room: rcSettings.get('Jitsi_URL_Room_Prefix') + rcSettings.get('uniqueID') + rid, timeout: new Date(Date.now() + 3600 * 1000), }; diff --git a/packages/rocketchat-livechat/server/api/v1/visitor.js b/packages/rocketchat-livechat/server/api/v1/visitor.js index d0c3b08fc472..658879441061 100644 --- a/packages/rocketchat-livechat/server/api/v1/visitor.js +++ b/packages/rocketchat-livechat/server/api/v1/visitor.js @@ -1,6 +1,7 @@ import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Rooms } from 'meteor/rocketchat:models'; +import { hasPermission } from 'meteor/rocketchat:authorization'; import { API } from 'meteor/rocketchat:api'; import LivechatVisitors from '../../../server/models/LivechatVisitors'; import { findGuest } from '../lib/livechat'; @@ -39,7 +40,7 @@ API.v1.addRoute('livechat/visitor', { let visitor = LivechatVisitors.getVisitorByToken(token); // If it's updating an existing visitor, it must also update the roomInfo - const cursor = RocketChat.models.Rooms.findOpenByVisitorToken(token); + const cursor = Rooms.findOpenByVisitorToken(token); cursor.forEach((room) => { Livechat.saveRoomInfo(room, visitor); }); @@ -109,11 +110,11 @@ API.v1.addRoute('livechat/visitor/:token', { API.v1.addRoute('livechat/visitor/:token/room', { authRequired: true }, { get() { - if (!RocketChat.authz.hasPermission(this.userId, 'view-livechat-manager')) { + if (!hasPermission(this.userId, 'view-livechat-manager')) { return API.v1.unauthorized(); } - const rooms = RocketChat.models.Rooms.findOpenByVisitorToken(this.urlParams.token, { + const rooms = Rooms.findOpenByVisitorToken(this.urlParams.token, { fields: { name: 1, t: 1, diff --git a/packages/rocketchat-livechat/server/config.js b/packages/rocketchat-livechat/server/config.js index bb14dad38f38..8baec635c61b 100644 --- a/packages/rocketchat-livechat/server/config.js +++ b/packages/rocketchat-livechat/server/config.js @@ -1,13 +1,13 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; Meteor.startup(function() { - RocketChat.settings.addGroup('Livechat'); + settings.addGroup('Livechat'); - RocketChat.settings.add('Livechat_enabled', false, { type: 'boolean', group: 'Livechat', public: true }); + settings.add('Livechat_enabled', false, { type: 'boolean', group: 'Livechat', public: true }); - RocketChat.settings.add('Livechat_title', 'Rocket.Chat', { type: 'string', group: 'Livechat', public: true }); - RocketChat.settings.add('Livechat_title_color', '#C1272D', { + settings.add('Livechat_title', 'Rocket.Chat', { type: 'string', group: 'Livechat', public: true }); + settings.add('Livechat_title_color', '#C1272D', { type: 'color', editor: 'color', allowedTypes: ['color', 'expression'], @@ -15,7 +15,7 @@ Meteor.startup(function() { public: true, }); - RocketChat.settings.add('Livechat_display_offline_form', true, { + settings.add('Livechat_display_offline_form', true, { type: 'boolean', group: 'Livechat', public: true, @@ -23,7 +23,7 @@ Meteor.startup(function() { i18nLabel: 'Display_offline_form', }); - RocketChat.settings.add('Livechat_validate_offline_email', true, { + settings.add('Livechat_validate_offline_email', true, { type: 'boolean', group: 'Livechat', public: true, @@ -31,7 +31,7 @@ Meteor.startup(function() { i18nLabel: 'Validate_email_address', }); - RocketChat.settings.add('Livechat_offline_form_unavailable', '', { + settings.add('Livechat_offline_form_unavailable', '', { type: 'string', group: 'Livechat', public: true, @@ -39,14 +39,14 @@ Meteor.startup(function() { i18nLabel: 'Offline_form_unavailable_message', }); - RocketChat.settings.add('Livechat_offline_title', 'Leave a message', { + settings.add('Livechat_offline_title', 'Leave a message', { type: 'string', group: 'Livechat', public: true, section: 'Offline', i18nLabel: 'Title', }); - RocketChat.settings.add('Livechat_offline_title_color', '#666666', { + settings.add('Livechat_offline_title_color', '#666666', { type: 'color', editor: 'color', allowedTypes: ['color', 'expression'], @@ -55,7 +55,7 @@ Meteor.startup(function() { section: 'Offline', i18nLabel: 'Color', }); - RocketChat.settings.add('Livechat_offline_message', '', { + settings.add('Livechat_offline_message', '', { type: 'string', group: 'Livechat', public: true, @@ -63,13 +63,13 @@ Meteor.startup(function() { i18nLabel: 'Instructions', i18nDescription: 'Instructions_to_your_visitor_fill_the_form_to_send_a_message', }); - RocketChat.settings.add('Livechat_offline_email', '', { + settings.add('Livechat_offline_email', '', { type: 'string', group: 'Livechat', i18nLabel: 'Email_address_to_send_offline_messages', section: 'Offline', }); - RocketChat.settings.add('Livechat_offline_success_message', '', { + settings.add('Livechat_offline_success_message', '', { type: 'string', group: 'Livechat', public: true, @@ -77,46 +77,46 @@ Meteor.startup(function() { i18nLabel: 'Offline_success_message', }); - RocketChat.settings.add('Livechat_allow_switching_departments', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Allow_switching_departments' }); - RocketChat.settings.add('Livechat_show_agent_email', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Show_agent_email' }); + settings.add('Livechat_allow_switching_departments', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Allow_switching_departments' }); + settings.add('Livechat_show_agent_email', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Show_agent_email' }); - RocketChat.settings.add('Livechat_conversation_finished_message', '', { + settings.add('Livechat_conversation_finished_message', '', { type: 'string', group: 'Livechat', public: true, i18nLabel: 'Conversation_finished_message', }); - RocketChat.settings.add('Livechat_registration_form', true, { + settings.add('Livechat_registration_form', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Show_preregistration_form', }); - RocketChat.settings.add('Livechat_name_field_registration_form', true, { + settings.add('Livechat_name_field_registration_form', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Show_name_field', }); - RocketChat.settings.add('Livechat_email_field_registration_form', true, { + settings.add('Livechat_email_field_registration_form', true, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Show_email_field', }); - RocketChat.settings.add('Livechat_guest_count', 1, { type: 'int', group: 'Livechat' }); + settings.add('Livechat_guest_count', 1, { type: 'int', group: 'Livechat' }); - RocketChat.settings.add('Livechat_Room_Count', 1, { + settings.add('Livechat_Room_Count', 1, { type: 'int', group: 'Livechat', i18nLabel: 'Livechat_room_count', }); - RocketChat.settings.add('Livechat_agent_leave_action', 'none', { + settings.add('Livechat_agent_leave_action', 'none', { type: 'select', group: 'Livechat', values: [ @@ -127,7 +127,7 @@ Meteor.startup(function() { i18nLabel: 'How_to_handle_open_sessions_when_agent_goes_offline', }); - RocketChat.settings.add('Livechat_agent_leave_action_timeout', 60, { + settings.add('Livechat_agent_leave_action_timeout', 60, { type: 'int', group: 'Livechat', enableQuery: { _id: 'Livechat_agent_leave_action', value: { $ne: 'none' } }, @@ -135,56 +135,56 @@ Meteor.startup(function() { i18nDescription: 'Time_in_seconds', }); - RocketChat.settings.add('Livechat_agent_leave_comment', '', { + settings.add('Livechat_agent_leave_comment', '', { type: 'string', group: 'Livechat', enableQuery: { _id: 'Livechat_agent_leave_action', value: 'close' }, i18nLabel: 'Comment_to_leave_on_closing_session', }); - RocketChat.settings.add('Livechat_webhookUrl', false, { + settings.add('Livechat_webhookUrl', false, { type: 'string', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Webhook_URL', }); - RocketChat.settings.add('Livechat_secret_token', false, { + settings.add('Livechat_secret_token', false, { type: 'string', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Secret_token', }); - RocketChat.settings.add('Livechat_webhook_on_close', false, { + settings.add('Livechat_webhook_on_close', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Send_request_on_chat_close', }); - RocketChat.settings.add('Livechat_webhook_on_offline_msg', false, { + settings.add('Livechat_webhook_on_offline_msg', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Send_request_on_offline_messages', }); - RocketChat.settings.add('Livechat_webhook_on_visitor_message', false, { + settings.add('Livechat_webhook_on_visitor_message', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Send_request_on_visitor_message', }); - RocketChat.settings.add('Livechat_webhook_on_agent_message', false, { + settings.add('Livechat_webhook_on_agent_message', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Send_request_on_agent_message', }); - RocketChat.settings.add('Send_visitor_navigation_history_livechat_webhook_request', false, { + settings.add('Send_visitor_navigation_history_livechat_webhook_request', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', @@ -193,28 +193,28 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Visitor_navigation_as_a_message', value: true }, }); - RocketChat.settings.add('Livechat_webhook_on_capture', false, { + settings.add('Livechat_webhook_on_capture', false, { type: 'boolean', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Send_request_on_lead_capture', }); - RocketChat.settings.add('Livechat_lead_email_regex', '\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}\\b', { + settings.add('Livechat_lead_email_regex', '\\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}\\b', { type: 'string', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Lead_capture_email_regex', }); - RocketChat.settings.add('Livechat_lead_phone_regex', '((?:\\([0-9]{1,3}\\)|[0-9]{2})[ \\-]*?[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$)|[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$))', { + settings.add('Livechat_lead_phone_regex', '((?:\\([0-9]{1,3}\\)|[0-9]{2})[ \\-]*?[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$)|[0-9]{4,5}(?:[\\-\\s\\_]{1,2})?[0-9]{4}(?:(?=[^0-9])|$))', { type: 'string', group: 'Livechat', section: 'CRM_Integration', i18nLabel: 'Lead_capture_phone_regex', }); - RocketChat.settings.add('Livechat_Knowledge_Enabled', false, { + settings.add('Livechat_Knowledge_Enabled', false, { type: 'boolean', group: 'Livechat', section: 'Knowledge_Base', @@ -222,7 +222,7 @@ Meteor.startup(function() { i18nLabel: 'Enabled', }); - RocketChat.settings.add('Livechat_Knowledge_Apiai_Key', '', { + settings.add('Livechat_Knowledge_Apiai_Key', '', { type: 'string', group: 'Livechat', section: 'Knowledge_Base', @@ -230,7 +230,7 @@ Meteor.startup(function() { i18nLabel: 'Apiai_Key', }); - RocketChat.settings.add('Livechat_Knowledge_Apiai_Language', 'en', { + settings.add('Livechat_Knowledge_Apiai_Language', 'en', { type: 'string', group: 'Livechat', section: 'Knowledge_Base', @@ -238,7 +238,7 @@ Meteor.startup(function() { i18nLabel: 'Apiai_Language', }); - RocketChat.settings.add('Livechat_history_monitor_type', 'url', { + settings.add('Livechat_history_monitor_type', 'url', { type: 'select', group: 'Livechat', i18nLabel: 'Monitor_history_for_changes_on', @@ -248,28 +248,28 @@ Meteor.startup(function() { ], }); - RocketChat.settings.add('Livechat_Visitor_navigation_as_a_message', false, { + settings.add('Livechat_Visitor_navigation_as_a_message', false, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Send_Visitor_navigation_history_as_a_message', }); - RocketChat.settings.add('Livechat_enable_office_hours', false, { + settings.add('Livechat_enable_office_hours', false, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Office_hours_enabled', }); - RocketChat.settings.add('Livechat_continuous_sound_notification_new_livechat_room', false, { + settings.add('Livechat_continuous_sound_notification_new_livechat_room', false, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Continuous_sound_notifications_for_new_livechat_room', }); - RocketChat.settings.add('Livechat_videocall_enabled', false, { + settings.add('Livechat_videocall_enabled', false, { type: 'boolean', group: 'Livechat', public: true, @@ -278,7 +278,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Jitsi_Enabled', value: true }, }); - RocketChat.settings.add('Livechat_fileupload_enabled', true, { + settings.add('Livechat_fileupload_enabled', true, { type: 'boolean', group: 'Livechat', public: true, @@ -286,14 +286,14 @@ Meteor.startup(function() { enableQuery: { _id: 'FileUpload_Enabled', value: true }, }); - RocketChat.settings.add('Livechat_enable_transcript', false, { + settings.add('Livechat_enable_transcript', false, { type: 'boolean', group: 'Livechat', public: true, i18nLabel: 'Transcript_Enabled', }); - RocketChat.settings.add('Livechat_transcript_message', '', { + settings.add('Livechat_transcript_message', '', { type: 'string', group: 'Livechat', public: true, @@ -301,14 +301,14 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_enable_transcript', value: true }, }); - RocketChat.settings.add('Livechat_registration_form_message', '', { + settings.add('Livechat_registration_form_message', '', { type: 'string', group: 'Livechat', public: true, i18nLabel: 'Livechat_registration_form_message', }); - RocketChat.settings.add('Livechat_open_inquiery_show_connecting', false, { + settings.add('Livechat_open_inquiery_show_connecting', false, { type: 'boolean', group: 'Livechat', public: true, @@ -316,7 +316,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Routing_Method', value: 'Guest_Pool' }, }); - RocketChat.settings.add('Livechat_AllowedDomainsList', '', { + settings.add('Livechat_AllowedDomainsList', '', { type: 'string', group: 'Livechat', public: true, @@ -324,27 +324,27 @@ Meteor.startup(function() { i18nDescription: 'Domains_allowed_to_embed_the_livechat_widget', }); - RocketChat.settings.add('Livechat_Facebook_Enabled', false, { + settings.add('Livechat_Facebook_Enabled', false, { type: 'boolean', group: 'Livechat', section: 'Facebook', }); - RocketChat.settings.add('Livechat_Facebook_API_Key', '', { + settings.add('Livechat_Facebook_API_Key', '', { type: 'string', group: 'Livechat', section: 'Facebook', i18nDescription: 'If_you_dont_have_one_send_an_email_to_omni_rocketchat_to_get_yours', }); - RocketChat.settings.add('Livechat_Facebook_API_Secret', '', { + settings.add('Livechat_Facebook_API_Secret', '', { type: 'string', group: 'Livechat', section: 'Facebook', i18nDescription: 'If_you_dont_have_one_send_an_email_to_omni_rocketchat_to_get_yours', }); - RocketChat.settings.add('Livechat_RDStation_Token', '', { + settings.add('Livechat_RDStation_Token', '', { type: 'string', group: 'Livechat', public: false, @@ -352,7 +352,7 @@ Meteor.startup(function() { i18nLabel: 'RDStation_Token', }); - RocketChat.settings.add('Livechat_Routing_Method', 'Least_Amount', { + settings.add('Livechat_Routing_Method', 'Least_Amount', { type: 'select', group: 'Livechat', public: true, @@ -364,7 +364,7 @@ Meteor.startup(function() { ], }); - RocketChat.settings.add('Livechat_guest_pool_with_no_agents', false, { + settings.add('Livechat_guest_pool_with_no_agents', false, { type: 'boolean', group: 'Livechat', section: 'Routing', @@ -373,7 +373,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Routing_Method', value: 'Guest_Pool' }, }); - RocketChat.settings.add('Livechat_show_queue_list_link', false, { + settings.add('Livechat_show_queue_list_link', false, { type: 'boolean', group: 'Livechat', public: true, @@ -382,7 +382,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Routing_Method', value: { $ne: 'External' } }, }); - RocketChat.settings.add('Livechat_External_Queue_URL', '', { + settings.add('Livechat_External_Queue_URL', '', { type: 'string', group: 'Livechat', public: false, @@ -392,7 +392,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Routing_Method', value: 'External' }, }); - RocketChat.settings.add('Livechat_External_Queue_Token', '', { + settings.add('Livechat_External_Queue_Token', '', { type: 'string', group: 'Livechat', public: false, @@ -401,7 +401,7 @@ Meteor.startup(function() { enableQuery: { _id: 'Livechat_Routing_Method', value: 'External' }, }); - RocketChat.settings.add('Livechat_Allow_collect_and_store_HTTP_header_informations', false, { + settings.add('Livechat_Allow_collect_and_store_HTTP_header_informations', false, { type: 'boolean', group: 'Livechat', public: true, @@ -409,7 +409,7 @@ Meteor.startup(function() { i18nDescription: 'Allow_collect_and_store_HTTP_header_informations_description', }); - RocketChat.settings.add('Livechat_force_accept_data_processing_consent', false, { + settings.add('Livechat_force_accept_data_processing_consent', false, { type: 'boolean', group: 'Livechat', public: true, @@ -418,7 +418,7 @@ Meteor.startup(function() { i18nDescription: 'Force_visitor_to_accept_data_processing_consent_description', }); - RocketChat.settings.add('Livechat_data_processing_consent_text', '', { + settings.add('Livechat_data_processing_consent_text', '', { type: 'string', multiline: true, group: 'Livechat', diff --git a/packages/rocketchat-livechat/server/hooks/RDStation.js b/packages/rocketchat-livechat/server/hooks/RDStation.js index 5e04f5e5a8ba..34dbbe3d74a3 100644 --- a/packages/rocketchat-livechat/server/hooks/RDStation.js +++ b/packages/rocketchat-livechat/server/hooks/RDStation.js @@ -1,9 +1,10 @@ import { HTTP } from 'meteor/http'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; +import { callbacks } from 'meteor/rocketchat:callbacks'; import { Livechat } from '../lib/Livechat'; function sendToRDStation(room) { - if (!RocketChat.settings.get('Livechat_RDStation_Token')) { + if (!settings.get('Livechat_RDStation_Token')) { return room; } @@ -20,7 +21,7 @@ function sendToRDStation(room) { 'Content-Type': 'application/json', }, data: { - token_rdstation: RocketChat.settings.get('Livechat_RDStation_Token'), + token_rdstation: settings.get('Livechat_RDStation_Token'), identificador: 'rocketchat-livechat', client_id: livechatData.visitor._id, email, @@ -54,6 +55,6 @@ function sendToRDStation(room) { return room; } -RocketChat.callbacks.add('livechat.closeRoom', sendToRDStation, RocketChat.callbacks.priority.MEDIUM, 'livechat-rd-station-close-room'); +callbacks.add('livechat.closeRoom', sendToRDStation, callbacks.priority.MEDIUM, 'livechat-rd-station-close-room'); -RocketChat.callbacks.add('livechat.saveInfo', sendToRDStation, RocketChat.callbacks.priority.MEDIUM, 'livechat-rd-station-save-info'); +callbacks.add('livechat.saveInfo', sendToRDStation, callbacks.priority.MEDIUM, 'livechat-rd-station-save-info'); diff --git a/packages/rocketchat-livechat/server/hooks/externalMessage.js b/packages/rocketchat-livechat/server/hooks/externalMessage.js index 71bf0e9a1d8b..f94954b9c584 100644 --- a/packages/rocketchat-livechat/server/hooks/externalMessage.js +++ b/packages/rocketchat-livechat/server/hooks/externalMessage.js @@ -1,5 +1,6 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; +import { callbacks } from 'meteor/rocketchat:callbacks'; import { SystemLogger } from 'meteor/rocketchat:logger'; import { HTTP } from 'meteor/http'; import { LivechatExternalMessage } from '../../lib/LivechatExternalMessage'; @@ -8,17 +9,17 @@ import _ from 'underscore'; let knowledgeEnabled = false; let apiaiKey = ''; let apiaiLanguage = 'en'; -RocketChat.settings.get('Livechat_Knowledge_Enabled', function(key, value) { +settings.get('Livechat_Knowledge_Enabled', function(key, value) { knowledgeEnabled = value; }); -RocketChat.settings.get('Livechat_Knowledge_Apiai_Key', function(key, value) { +settings.get('Livechat_Knowledge_Apiai_Key', function(key, value) { apiaiKey = value; }); -RocketChat.settings.get('Livechat_Knowledge_Apiai_Language', function(key, value) { +settings.get('Livechat_Knowledge_Apiai_Language', function(key, value) { apiaiLanguage = value; }); -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { // skips this callback if the message was edited if (!message || message.editedAt) { return message; @@ -65,4 +66,4 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { }); return message; -}, RocketChat.callbacks.priority.LOW, 'externalWebHook'); +}, callbacks.priority.LOW, 'externalWebHook'); diff --git a/packages/rocketchat-livechat/server/hooks/leadCapture.js b/packages/rocketchat-livechat/server/hooks/leadCapture.js index 2c9087fe3831..d909dbcd0471 100644 --- a/packages/rocketchat-livechat/server/hooks/leadCapture.js +++ b/packages/rocketchat-livechat/server/hooks/leadCapture.js @@ -1,4 +1,5 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { settings } from 'meteor/rocketchat:settings'; import LivechatVisitors from '../../server/models/LivechatVisitors'; function validateMessage(message, room) { @@ -25,22 +26,22 @@ function validateMessage(message, room) { return true; } -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { if (!validateMessage(message, room)) { return message; } - const phoneRegexp = new RegExp(RocketChat.settings.get('Livechat_lead_phone_regex'), 'g'); + const phoneRegexp = new RegExp(settings.get('Livechat_lead_phone_regex'), 'g'); const msgPhones = message.msg.match(phoneRegexp); - const emailRegexp = new RegExp(RocketChat.settings.get('Livechat_lead_email_regex'), 'gi'); + const emailRegexp = new RegExp(settings.get('Livechat_lead_email_regex'), 'gi'); const msgEmails = message.msg.match(emailRegexp); if (msgEmails || msgPhones) { LivechatVisitors.saveGuestEmailPhoneById(room.v._id, msgEmails, msgPhones); - RocketChat.callbacks.run('livechat.leadCapture', room); + callbacks.run('livechat.leadCapture', room); } return message; -}, RocketChat.callbacks.priority.LOW, 'leadCapture'); +}, callbacks.priority.LOW, 'leadCapture'); diff --git a/packages/rocketchat-livechat/server/hooks/markRoomResponded.js b/packages/rocketchat-livechat/server/hooks/markRoomResponded.js index 3e6d8d933423..15249388c372 100644 --- a/packages/rocketchat-livechat/server/hooks/markRoomResponded.js +++ b/packages/rocketchat-livechat/server/hooks/markRoomResponded.js @@ -1,7 +1,8 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { Rooms } from 'meteor/rocketchat:models'; -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { // skips this callback if the message was edited if (!message || message.editedAt) { return message; @@ -18,7 +19,7 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { } Meteor.defer(() => { - RocketChat.models.Rooms.setResponseByRoomId(room._id, { + Rooms.setResponseByRoomId(room._id, { user: { _id: message.u._id, username: message.u.username, @@ -27,4 +28,4 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { }); return message; -}, RocketChat.callbacks.priority.LOW, 'markRoomResponded'); +}, callbacks.priority.LOW, 'markRoomResponded'); diff --git a/packages/rocketchat-livechat/server/hooks/offlineMessage.js b/packages/rocketchat-livechat/server/hooks/offlineMessage.js index 31a916e13aec..bf73c650ed45 100644 --- a/packages/rocketchat-livechat/server/hooks/offlineMessage.js +++ b/packages/rocketchat-livechat/server/hooks/offlineMessage.js @@ -1,8 +1,9 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { settings } from 'meteor/rocketchat:settings'; import { Livechat } from '../lib/Livechat'; -RocketChat.callbacks.add('livechat.offlineMessage', (data) => { - if (!RocketChat.settings.get('Livechat_webhook_on_offline_msg')) { +callbacks.add('livechat.offlineMessage', (data) => { + if (!settings.get('Livechat_webhook_on_offline_msg')) { return data; } @@ -17,4 +18,4 @@ RocketChat.callbacks.add('livechat.offlineMessage', (data) => { }; Livechat.sendRequest(postData); -}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-email-offline-message'); +}, callbacks.priority.MEDIUM, 'livechat-send-email-offline-message'); diff --git a/packages/rocketchat-livechat/server/hooks/saveAnalyticsData.js b/packages/rocketchat-livechat/server/hooks/saveAnalyticsData.js index 31b178fede11..4e516d5c421c 100644 --- a/packages/rocketchat-livechat/server/hooks/saveAnalyticsData.js +++ b/packages/rocketchat-livechat/server/hooks/saveAnalyticsData.js @@ -1,7 +1,8 @@ import { Meteor } from 'meteor/meteor'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { Rooms } from 'meteor/rocketchat:models'; -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { // skips this callback if the message was edited if (!message || message.editedAt) { return message; @@ -58,8 +59,8 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { } // ignore, its continuing response } - RocketChat.models.Rooms.saveAnalyticsDataByRoomId(room, message, analyticsData); + Rooms.saveAnalyticsDataByRoomId(room, message, analyticsData); }); return message; -}, RocketChat.callbacks.priority.LOW, 'saveAnalyticsData'); +}, callbacks.priority.LOW, 'saveAnalyticsData'); diff --git a/packages/rocketchat-livechat/server/hooks/sendToCRM.js b/packages/rocketchat-livechat/server/hooks/sendToCRM.js index 301b61988eaa..a22fd6f71bda 100644 --- a/packages/rocketchat-livechat/server/hooks/sendToCRM.js +++ b/packages/rocketchat-livechat/server/hooks/sendToCRM.js @@ -1,16 +1,18 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { Messages, Rooms } from 'meteor/rocketchat:models'; import { Livechat } from '../lib/Livechat'; const msgNavType = 'livechat_navigation_history'; const crmEnabled = () => { - const secretToken = RocketChat.settings.get('Livechat_secret_token'); - const webhookUrl = RocketChat.settings.get('Livechat_webhookUrl'); + const secretToken = settings.get('Livechat_secret_token'); + const webhookUrl = settings.get('Livechat_webhookUrl'); return secretToken !== '' && secretToken !== undefined && webhookUrl !== '' && webhookUrl !== undefined; }; const sendMessageType = (msgType) => { - const sendNavHistory = RocketChat.settings.get('Livechat_Visitor_navigation_as_a_message') && RocketChat.settings.get('Send_visitor_navigation_history_livechat_webhook_request'); + const sendNavHistory = settings.get('Livechat_Visitor_navigation_as_a_message') && settings.get('Send_visitor_navigation_history_livechat_webhook_request'); return sendNavHistory && msgType === msgNavType; }; @@ -28,7 +30,7 @@ function sendToCRM(type, room, includeMessages = true) { let messages; if (typeof includeMessages === 'boolean' && includeMessages) { - messages = RocketChat.models.Messages.findVisibleByRoomId(room._id, { sort: { ts: 1 } }); + messages = Messages.findVisibleByRoomId(room._id, { sort: { ts: 1 } }); } else if (includeMessages instanceof Array) { messages = includeMessages; } @@ -61,30 +63,30 @@ function sendToCRM(type, room, includeMessages = true) { const response = Livechat.sendRequest(postData); if (response && response.data && response.data.data) { - RocketChat.models.Rooms.saveCRMDataByRoomId(room._id, response.data.data); + Rooms.saveCRMDataByRoomId(room._id, response.data.data); } return room; } -RocketChat.callbacks.add('livechat.closeRoom', (room) => { - if (!RocketChat.settings.get('Livechat_webhook_on_close')) { +callbacks.add('livechat.closeRoom', (room) => { + if (!settings.get('Livechat_webhook_on_close')) { return room; } return sendToCRM('LivechatSession', room); -}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-close-room'); +}, callbacks.priority.MEDIUM, 'livechat-send-crm-close-room'); -RocketChat.callbacks.add('livechat.saveInfo', (room) => { +callbacks.add('livechat.saveInfo', (room) => { // Do not send to CRM if the chat is still open if (room.open) { return room; } return sendToCRM('LivechatEdit', room); -}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-save-info'); +}, callbacks.priority.MEDIUM, 'livechat-send-crm-save-info'); -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { // only call webhook if it is a livechat room if (room.t !== 'l' || room.v == null || room.v.token == null) { return message; @@ -93,10 +95,10 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { // if the message has a token, it was sent from the visitor // if not, it was sent from the agent if (message.token) { - if (!RocketChat.settings.get('Livechat_webhook_on_visitor_message')) { + if (!settings.get('Livechat_webhook_on_visitor_message')) { return message; } - } else if (!RocketChat.settings.get('Livechat_webhook_on_agent_message')) { + } else if (!settings.get('Livechat_webhook_on_agent_message')) { return message; } // if the message has a type means it is a special message (like the closing comment), so skips @@ -107,11 +109,11 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { sendToCRM('Message', room, [message]); return message; -}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-message'); +}, callbacks.priority.MEDIUM, 'livechat-send-crm-message'); -RocketChat.callbacks.add('livechat.leadCapture', (room) => { - if (!RocketChat.settings.get('Livechat_webhook_on_capture')) { +callbacks.add('livechat.leadCapture', (room) => { + if (!settings.get('Livechat_webhook_on_capture')) { return room; } return sendToCRM('LeadCapture', room, false); -}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-lead-capture'); +}, callbacks.priority.MEDIUM, 'livechat-send-crm-lead-capture'); diff --git a/packages/rocketchat-livechat/server/hooks/sendToFacebook.js b/packages/rocketchat-livechat/server/hooks/sendToFacebook.js index 0526f1e16ed8..2325c15acdac 100644 --- a/packages/rocketchat-livechat/server/hooks/sendToFacebook.js +++ b/packages/rocketchat-livechat/server/hooks/sendToFacebook.js @@ -1,13 +1,14 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { settings } from 'meteor/rocketchat:settings'; import OmniChannel from '../lib/OmniChannel'; -RocketChat.callbacks.add('afterSaveMessage', function(message, room) { +callbacks.add('afterSaveMessage', function(message, room) { // skips this callback if the message was edited if (message.editedAt) { return message; } - if (!RocketChat.settings.get('Livechat_Facebook_Enabled') || !RocketChat.settings.get('Livechat_Facebook_API_Key')) { + if (!settings.get('Livechat_Facebook_Enabled') || !settings.get('Livechat_Facebook_API_Key')) { return message; } @@ -34,4 +35,4 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) { return message; -}, RocketChat.callbacks.priority.LOW, 'sendMessageToFacebook'); +}, callbacks.priority.LOW, 'sendMessageToFacebook'); diff --git a/packages/rocketchat-livechat/server/lib/Analytics.js b/packages/rocketchat-livechat/server/lib/Analytics.js index b70301f6b424..75d0903cf900 100644 --- a/packages/rocketchat-livechat/server/lib/Analytics.js +++ b/packages/rocketchat-livechat/server/lib/Analytics.js @@ -1,4 +1,4 @@ -import { RocketChat } from 'meteor/rocketchat:lib'; +import { Rooms } from 'meteor/rocketchat:models'; import moment from 'moment'; /** @@ -119,14 +119,14 @@ export const Analytics = { * @returns {Integer} */ Total_conversations(date) { - return RocketChat.models.Rooms.getTotalConversationsBetweenDate('l', date); + return Rooms.getTotalConversationsBetweenDate('l', date); }, Avg_chat_duration(date) { let total = 0; let count = 0; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { if (metrics && metrics.chatDuration) { total += metrics.chatDuration; count++; @@ -140,7 +140,7 @@ export const Analytics = { Total_messages(date) { let total = 0; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ msgs }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ msgs }) => { if (msgs) { total += msgs; } @@ -158,7 +158,7 @@ export const Analytics = { Avg_first_response_time(date) { let frt = 0; let count = 0; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { if (metrics && metrics.response && metrics.response.ft) { frt += metrics.response.ft; count++; @@ -178,7 +178,7 @@ export const Analytics = { Best_first_response_time(date) { let maxFrt; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { if (metrics && metrics.response && metrics.response.ft) { maxFrt = (maxFrt) ? Math.min(maxFrt, metrics.response.ft) : metrics.response.ft; } @@ -198,7 +198,7 @@ export const Analytics = { Avg_response_time(date) { let art = 0; let count = 0; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { if (metrics && metrics.response && metrics.response.avg) { art += metrics.response.avg; count++; @@ -219,7 +219,7 @@ export const Analytics = { Avg_reaction_time(date) { let arnt = 0; let count = 0; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics }) => { if (metrics && metrics.reaction && metrics.reaction.ft) { arnt += metrics.reaction.ft; count++; @@ -284,7 +284,7 @@ export const Analytics = { lt: moment(m).add(1, 'days'), }; - const result = RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date); + const result = Rooms.getAnalyticsMetricsBetweenDate('l', date); totalConversations += result.count(); result.forEach(summarize(m)); @@ -302,7 +302,7 @@ export const Analytics = { lt: moment(h).add(1, 'hours'), }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ msgs, }) => { const dayHour = h.format('H'); // @int : 0, 1, ... 23 @@ -354,7 +354,7 @@ export const Analytics = { lt: to.add(1, 'days'), }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, }) => { if (metrics && metrics.response && metrics.reaction) { @@ -436,7 +436,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ servedBy, }) => { if (servedBy) { @@ -486,7 +486,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, servedBy, }) => { @@ -546,7 +546,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ servedBy, msgs, }) => { @@ -590,7 +590,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, servedBy, }) => { @@ -650,7 +650,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, servedBy, }) => { @@ -702,7 +702,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, servedBy, }) => { @@ -762,7 +762,7 @@ export const Analytics = { data: [], }; - RocketChat.models.Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ + Rooms.getAnalyticsMetricsBetweenDate('l', date).forEach(({ metrics, servedBy, }) => { diff --git a/packages/rocketchat-livechat/server/lib/Livechat.js b/packages/rocketchat-livechat/server/lib/Livechat.js index c8f3522de3e3..94697be0bfe9 100644 --- a/packages/rocketchat-livechat/server/lib/Livechat.js +++ b/packages/rocketchat-livechat/server/lib/Livechat.js @@ -3,8 +3,12 @@ import { Match, check } from 'meteor/check'; import { Random } from 'meteor/random'; import { TAPi18n } from 'meteor/tap:i18n'; import { HTTP } from 'meteor/http'; -import { RocketChat } from 'meteor/rocketchat:lib'; +import { settings } from 'meteor/rocketchat:settings'; +import { callbacks } from 'meteor/rocketchat:callbacks'; +import { Users, Rooms, Messages, Subscriptions, Settings } from 'meteor/rocketchat:models'; import { Logger } from 'meteor/rocketchat:logger'; +import { sendMessage, deleteMessage, updateMessage } from 'meteor/rocketchat:lib'; +import { addUserRoles, removeUserFromRoles } from 'meteor/rocketchat:authorization'; import _ from 'underscore'; import s from 'underscore.string'; import moment from 'moment'; @@ -13,7 +17,7 @@ import UAParser from 'ua-parser-js'; import * as Mailer from 'meteor/rocketchat:mailer'; import { LivechatDepartmentAgents, LivechatDepartment, LivechatCustomField } from '../models'; import { LivechatInquiry } from '../../lib/LivechatInquiry'; - +import { QueueMethods } from './QueueMethods'; import LivechatVisitors from '../models/LivechatVisitors'; import { Analytics } from './Analytics'; @@ -28,20 +32,20 @@ export const Livechat = { }), getNextAgent(department) { - if (RocketChat.settings.get('Livechat_Routing_Method') === 'External') { + if (settings.get('Livechat_Routing_Method') === 'External') { for (let i = 0; i < 10; i++) { try { const queryString = department ? `?departmentId=${ department }` : ''; - const result = HTTP.call('GET', `${ RocketChat.settings.get('Livechat_External_Queue_URL') }${ queryString }`, { + const result = HTTP.call('GET', `${ settings.get('Livechat_External_Queue_URL') }${ queryString }`, { headers: { 'User-Agent': 'RocketChat Server', Accept: 'application/json', - 'X-RocketChat-Secret-Token': RocketChat.settings.get('Livechat_External_Queue_Token'), + 'X-RocketChat-Secret-Token': settings.get('Livechat_External_Queue_Token'), }, }); if (result && result.data && result.data.username) { - const agent = RocketChat.models.Users.findOneOnlineAgentByUsername(result.data.username); + const agent = Users.findOneOnlineAgentByUsername(result.data.username); if (agent) { return { @@ -59,19 +63,19 @@ export const Livechat = { } else if (department) { return LivechatDepartmentAgents.getNextAgentForDepartment(department); } - return RocketChat.models.Users.getNextAgent(); + return Users.getNextAgent(); }, getAgents(department) { if (department) { return LivechatDepartmentAgents.findByDepartmentId(department); } - return RocketChat.models.Users.findAgents(); + return Users.findAgents(); }, getOnlineAgents(department) { if (department) { return LivechatDepartmentAgents.getOnlineForDepartment(department); } - return RocketChat.models.Users.findOnlineAgents(); + return Users.findOnlineAgents(); }, getRequiredDepartment(onlineRequired = true) { const departments = LivechatDepartment.findEnabledWithAgents(); @@ -88,7 +92,7 @@ export const Livechat = { }); }, getRoom(guest, message, roomInfo, agent) { - let room = RocketChat.models.Rooms.findOneById(message.rid); + let room = Rooms.findOneById(message.rid); let newRoom = false; if (room && !room.open) { @@ -107,8 +111,8 @@ export const Livechat = { } // delegate room creation to QueueMethods - const routingMethod = RocketChat.settings.get('Livechat_Routing_Method'); - room = RocketChat.QueueMethods[routingMethod](guest, message, roomInfo, agent); + const routingMethod = settings.get('Livechat_Routing_Method'); + room = QueueMethods[routingMethod](guest, message, roomInfo, agent); newRoom = true; } @@ -118,7 +122,7 @@ export const Livechat = { } if (newRoom) { - RocketChat.models.Messages.setRoomIdByToken(guest.token, room._id); + Messages.setRoomIdByToken(guest.token, room._id); } return { room, newRoom }; @@ -131,25 +135,25 @@ export const Livechat = { } // return messages; - return _.extend(RocketChat.sendMessage(guest, message, room), { newRoom, showConnecting: this.showConnecting() }); + return _.extend(sendMessage(guest, message, room), { newRoom, showConnecting: this.showConnecting() }); }, updateMessage({ guest, message }) { check(message, Match.ObjectIncluding({ _id: String })); - const originalMessage = RocketChat.models.Messages.findOneById(message._id); + const originalMessage = Messages.findOneById(message._id); if (!originalMessage || !originalMessage._id) { return; } - const editAllowed = RocketChat.settings.get('Message_AllowEditing'); + const editAllowed = settings.get('Message_AllowEditing'); const editOwn = originalMessage.u && originalMessage.u._id === guest._id; if (!editAllowed || !editOwn) { throw new Meteor.Error('error-action-not-allowed', 'Message editing not allowed', { method: 'livechatUpdateMessage' }); } - RocketChat.updateMessage(message, guest); + updateMessage(message, guest); return true; }, @@ -157,19 +161,19 @@ export const Livechat = { deleteMessage({ guest, message }) { check(message, Match.ObjectIncluding({ _id: String })); - const msg = RocketChat.models.Messages.findOneById(message._id); + const msg = Messages.findOneById(message._id); if (!msg || !msg._id) { return; } - const deleteAllowed = RocketChat.settings.get('Message_AllowDeleting'); + const deleteAllowed = settings.get('Message_AllowDeleting'); const editOwn = msg.u && msg.u._id === guest._id; if (!deleteAllowed || !editOwn) { throw new Meteor.Error('error-action-not-allowed', 'Message deleting not allowed', { method: 'livechatDeleteMessage' }); } - RocketChat.deleteMessage(message, guest); + deleteMessage(message, guest); return true; }, @@ -202,7 +206,7 @@ export const Livechat = { username, }; - const storeHttpHeaderData = RocketChat.settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations'); + const storeHttpHeaderData = settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations'); if (this.connection && storeHttpHeaderData) { userData.userAgent = this.connection.httpHeaders['user-agent']; @@ -271,7 +275,7 @@ export const Livechat = { const ret = LivechatVisitors.saveGuestById(_id, updateData); Meteor.defer(() => { - RocketChat.callbacks.run('livechat.saveGuest', updateData); + callbacks.run('livechat.saveGuest', updateData); }); return ret; @@ -303,7 +307,7 @@ export const Livechat = { }; } - RocketChat.models.Rooms.closeByRoomId(room._id, closeData); + Rooms.closeByRoomId(room._id, closeData); LivechatInquiry.closeByRoomId(room._id, closeData); const message = { @@ -312,15 +316,15 @@ export const Livechat = { groupable: false, }; - RocketChat.sendMessage(user, message, room); + sendMessage(user, message, room); if (room.servedBy) { - RocketChat.models.Subscriptions.hideByRoomIdAndUserId(room._id, room.servedBy._id); + Subscriptions.hideByRoomIdAndUserId(room._id, room.servedBy._id); } - RocketChat.models.Messages.createCommandWithRoomIdAndUser('promptTranscript', room._id, closeData.closedBy); + Messages.createCommandWithRoomIdAndUser('promptTranscript', room._id, closeData.closedBy); Meteor.defer(() => { - RocketChat.callbacks.run('livechat.closeRoom', room); + callbacks.run('livechat.closeRoom', room); }); return true; @@ -338,16 +342,16 @@ export const Livechat = { } if (customField.scope === 'room') { - return RocketChat.models.Rooms.updateLivechatDataByToken(token, key, value, overwrite); + return Rooms.updateLivechatDataByToken(token, key, value, overwrite); } else { return LivechatVisitors.updateLivechatDataByToken(token, key, value, overwrite); } }, getInitSettings() { - const settings = {}; + const rcSettings = {}; - RocketChat.models.Settings.findNotHiddenPublic([ + Settings.findNotHiddenPublic([ 'Livechat_title', 'Livechat_title_color', 'Livechat_enabled', @@ -373,41 +377,41 @@ export const Livechat = { 'Livechat_force_accept_data_processing_consent', 'Livechat_data_processing_consent_text', ]).forEach((setting) => { - settings[setting._id] = setting.value; + rcSettings[setting._id] = setting.value; }); - RocketChat.settings.get('Livechat_history_monitor_type', (key, value) => { - settings[key] = value; + settings.get('Livechat_history_monitor_type', (key, value) => { + rcSettings[key] = value; }); - settings.Livechat_Show_Connecting = this.showConnecting(); + rcSettings.Livechat_Show_Connecting = this.showConnecting(); - return settings; + return rcSettings; }, saveRoomInfo(roomData, guestData) { - if ((roomData.topic != null || roomData.tags != null) && !RocketChat.models.Rooms.setTopicAndTagsById(roomData._id, roomData.topic, roomData.tags)) { + if ((roomData.topic != null || roomData.tags != null) && !Rooms.setTopicAndTagsById(roomData._id, roomData.topic, roomData.tags)) { return false; } Meteor.defer(() => { - RocketChat.callbacks.run('livechat.saveRoom', roomData); + callbacks.run('livechat.saveRoom', roomData); }); if (!_.isEmpty(guestData.name)) { - return RocketChat.models.Rooms.setFnameById(roomData._id, guestData.name) && RocketChat.models.Subscriptions.updateDisplayNameByRoomId(roomData._id, guestData.name); + return Rooms.setFnameById(roomData._id, guestData.name) && Subscriptions.updateDisplayNameByRoomId(roomData._id, guestData.name); } }, closeOpenChats(userId, comment) { - const user = RocketChat.models.Users.findOneById(userId); - RocketChat.models.Rooms.findOpenByAgent(userId).forEach((room) => { + const user = Users.findOneById(userId); + Rooms.findOpenByAgent(userId).forEach((room) => { this.closeRoom({ user, room, comment }); }); }, forwardOpenChats(userId) { - RocketChat.models.Rooms.findOpenByAgent(userId).forEach((room) => { + Rooms.findOpenByAgent(userId).forEach((room) => { const guest = LivechatVisitors.findOneById(room.v._id); this.transfer(room, guest, { departmentId: guest.department }); }); @@ -416,7 +420,7 @@ export const Livechat = { savePageHistory(token, roomId, pageInfo) { if (pageInfo.change === Livechat.historyMonitorType) { - const user = RocketChat.models.Users.findOneById('rocket.cat'); + const user = Users.findOneById('rocket.cat'); const pageTitle = pageInfo.title; const pageUrl = pageInfo.location.href; @@ -433,11 +437,11 @@ export const Livechat = { extraData.expireAt = new Date().getTime() + keepHistoryMiliseconds; } - if (!RocketChat.settings.get('Livechat_Visitor_navigation_as_a_message')) { + if (!settings.get('Livechat_Visitor_navigation_as_a_message')) { extraData._hidden = true; } - return RocketChat.models.Messages.createNavigationHistoryWithRoomIdMessageAndUser(roomId, `${ pageTitle } - ${ pageUrl }`, user, extraData); + return Messages.createNavigationHistoryWithRoomIdMessageAndUser(roomId, `${ pageTitle } - ${ pageUrl }`, user, extraData); } return; @@ -447,14 +451,14 @@ export const Livechat = { let agent; if (transferData.userId) { - const user = RocketChat.models.Users.findOneOnlineAgentById(transferData.userId); + const user = Users.findOneOnlineAgentById(transferData.userId); if (!user) { return false; } const { _id: agentId, username } = user; agent = Object.assign({}, { agentId, username }); - } else if (RocketChat.settings.get('Livechat_Routing_Method') !== 'Guest_Pool') { + } else if (settings.get('Livechat_Routing_Method') !== 'Guest_Pool') { agent = Livechat.getNextAgent(transferData.departmentId); } else { return Livechat.returnRoomAsInquiry(room._id, transferData.departmentId); @@ -463,10 +467,10 @@ export const Livechat = { const { servedBy } = room; if (agent && agent.agentId !== servedBy._id) { - RocketChat.models.Rooms.changeAgentByRoomId(room._id, agent); + Rooms.changeAgentByRoomId(room._id, agent); if (transferData.departmentId) { - RocketChat.models.Rooms.changeDepartmentIdByRoomId(room._id, transferData.departmentId); + Rooms.changeDepartmentIdByRoomId(room._id, transferData.departmentId); } const subscriptionData = { @@ -486,13 +490,13 @@ export const Livechat = { mobilePushNotifications: 'all', emailNotifications: 'all', }; - RocketChat.models.Subscriptions.removeByRoomIdAndUserId(room._id, servedBy._id); + Subscriptions.removeByRoomIdAndUserId(room._id, servedBy._id); - RocketChat.models.Subscriptions.insert(subscriptionData); - RocketChat.models.Rooms.incUsersCountById(room._id); + Subscriptions.insert(subscriptionData); + Rooms.incUsersCountById(room._id); - RocketChat.models.Messages.createUserLeaveWithRoomIdAndUser(room._id, { _id: servedBy._id, username: servedBy.username }); - RocketChat.models.Messages.createUserJoinWithRoomIdAndUser(room._id, { _id: agent.agentId, username: agent.username }); + Messages.createUserLeaveWithRoomIdAndUser(room._id, { _id: servedBy._id, username: servedBy.username }); + Messages.createUserJoinWithRoomIdAndUser(room._id, { _id: agent.agentId, username: agent.username }); const guestData = { token: guest.token, @@ -500,7 +504,7 @@ export const Livechat = { }; this.setDepartmentForGuest(guestData); - const data = RocketChat.models.Users.getAgentInfo(agent.agentId); + const data = Users.getAgentInfo(agent.agentId); Livechat.stream.emit(room._id, { type: 'agentData', @@ -514,7 +518,7 @@ export const Livechat = { }, returnRoomAsInquiry(rid, departmentId) { - const room = RocketChat.models.Rooms.findOneById(rid); + const room = Rooms.findOneById(rid); if (!room) { throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'livechat:returnRoomAsInquiry' }); } @@ -523,7 +527,7 @@ export const Livechat = { return false; } - const user = RocketChat.models.Users.findOne(room.servedBy._id); + const user = Users.findOne(room.servedBy._id); if (!user || !user._id) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'livechat:returnRoomAsInquiry' }); } @@ -533,7 +537,7 @@ export const Livechat = { if (departmentId) { let agents = Livechat.getOnlineAgents(departmentId); - if (agents.count() === 0 && RocketChat.settings.get('Livechat_guest_pool_with_no_agents')) { + if (agents.count() === 0 && settings.get('Livechat_guest_pool_with_no_agents')) { agents = Livechat.getAgents(departmentId); } @@ -545,14 +549,14 @@ export const Livechat = { agentIds.push(agent.agentId); }); - RocketChat.models.Rooms.changeDepartmentIdByRoomId(room._id, departmentId); + Rooms.changeDepartmentIdByRoomId(room._id, departmentId); } // delete agent and room subscription - RocketChat.models.Subscriptions.removeByRoomId(rid); + Subscriptions.removeByRoomId(rid); // remove agent from room - RocketChat.models.Rooms.removeAgentByRoomId(rid); + Rooms.removeAgentByRoomId(rid); // find inquiry corresponding to room const inquiry = LivechatInquiry.findOne({ rid }); @@ -569,7 +573,7 @@ export const Livechat = { } if (openInq) { - RocketChat.models.Messages.createUserLeaveWithRoomIdAndUser(rid, { _id: room.servedBy._id, username: room.servedBy.username }); + Messages.createUserLeaveWithRoomIdAndUser(rid, { _id: room.servedBy._id, username: room.servedBy.username }); Livechat.stream.emit(rid, { type: 'agentData', @@ -584,11 +588,11 @@ export const Livechat = { try { const options = { headers: { - 'X-RocketChat-Livechat-Token': RocketChat.settings.get('Livechat_secret_token'), + 'X-RocketChat-Livechat-Token': settings.get('Livechat_secret_token'), }, data: postData, }; - return HTTP.post(RocketChat.settings.get('Livechat_webhookUrl'), options); + return HTTP.post(settings.get('Livechat_webhookUrl'), options); } catch (e) { Livechat.logger.webhook.error(`Response error on ${ trying } try ->`, e); // try 10 times after 10 seconds each @@ -604,7 +608,7 @@ export const Livechat = { getLivechatRoomGuestInfo(room) { const visitor = LivechatVisitors.findOneById(room.v._id); - const agent = RocketChat.models.Users.findOneById(room.servedBy && room.servedBy._id); + const agent = Users.findOneById(room.servedBy && room.servedBy._id); const ua = new UAParser(); ua.setUA(visitor.userAgent); @@ -662,15 +666,15 @@ export const Livechat = { addAgent(username) { check(username, String); - const user = RocketChat.models.Users.findOneByUsername(username, { fields: { _id: 1, username: 1 } }); + const user = Users.findOneByUsername(username, { fields: { _id: 1, username: 1 } }); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'livechat:addAgent' }); } - if (RocketChat.authz.addUserRoles(user._id, 'livechat-agent')) { - RocketChat.models.Users.setOperator(user._id, true); - RocketChat.models.Users.setLivechatStatus(user._id, 'available'); + if (addUserRoles(user._id, 'livechat-agent')) { + Users.setOperator(user._id, true); + Users.setLivechatStatus(user._id, 'available'); return user; } @@ -680,13 +684,13 @@ export const Livechat = { addManager(username) { check(username, String); - const user = RocketChat.models.Users.findOneByUsername(username, { fields: { _id: 1, username: 1 } }); + const user = Users.findOneByUsername(username, { fields: { _id: 1, username: 1 } }); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'livechat:addManager' }); } - if (RocketChat.authz.addUserRoles(user._id, 'livechat-manager')) { + if (addUserRoles(user._id, 'livechat-manager')) { return user; } @@ -696,15 +700,15 @@ export const Livechat = { removeAgent(username) { check(username, String); - const user = RocketChat.models.Users.findOneByUsername(username, { fields: { _id: 1 } }); + const user = Users.findOneByUsername(username, { fields: { _id: 1 } }); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'livechat:removeAgent' }); } - if (RocketChat.authz.removeUserFromRoles(user._id, 'livechat-agent')) { - RocketChat.models.Users.setOperator(user._id, false); - RocketChat.models.Users.setLivechatStatus(user._id, 'not-available'); + if (removeUserFromRoles(user._id, 'livechat-agent')) { + Users.setOperator(user._id, false); + Users.setLivechatStatus(user._id, 'not-available'); return true; } @@ -714,13 +718,13 @@ export const Livechat = { removeManager(username) { check(username, String); - const user = RocketChat.models.Users.findOneByUsername(username, { fields: { _id: 1 } }); + const user = Users.findOneByUsername(username, { fields: { _id: 1 } }); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'livechat:removeManager' }); } - return RocketChat.authz.removeUserFromRoles(user._id, 'livechat-manager'); + return removeUserFromRoles(user._id, 'livechat-manager'); }, removeGuest(_id) { @@ -743,13 +747,13 @@ export const Livechat = { const { token } = guest; - RocketChat.models.Rooms.findByVisitorToken(token).forEach((room) => { - RocketChat.models.Messages.removeFilesByRoomId(room._id); - RocketChat.models.Messages.removeByRoomId(room._id); + Rooms.findByVisitorToken(token).forEach((room) => { + Messages.removeFilesByRoomId(room._id); + Messages.removeByRoomId(room._id); }); - RocketChat.models.Subscriptions.removeByVisitorToken(token); - RocketChat.models.Rooms.removeByVisitorToken(token); + Subscriptions.removeByVisitorToken(token); + Rooms.removeByVisitorToken(token); }, saveDepartment(_id, departmentData, departmentAgents) { @@ -792,8 +796,8 @@ export const Livechat = { }, showConnecting() { - if (RocketChat.settings.get('Livechat_Routing_Method') === 'Guest_Pool') { - return RocketChat.settings.get('Livechat_open_inquiery_show_connecting'); + if (settings.get('Livechat_Routing_Method') === 'Guest_Pool') { + return settings.get('Livechat_open_inquiery_show_connecting'); } else { return false; } @@ -813,17 +817,17 @@ export const Livechat = { check(rid, String); check(email, String); - const room = RocketChat.models.Rooms.findOneById(rid); + const room = Rooms.findOneById(rid); const visitor = LivechatVisitors.getVisitorByToken(token); - const userLanguage = (visitor && visitor.language) || RocketChat.settings.get('Language') || 'en'; + const userLanguage = (visitor && visitor.language) || settings.get('Language') || 'en'; // allow to only user to send transcripts from their own chats if (!room || room.t !== 'l' || !room.v || room.v.token !== token) { throw new Meteor.Error('error-invalid-room', 'Invalid room'); } - const messages = RocketChat.models.Messages.findVisibleByRoomIdNotContainingTypes(rid, ['livechat_navigation_history'], { sort: { ts: 1 } }); + const messages = Messages.findVisibleByRoomIdNotContainingTypes(rid, ['livechat_navigation_history'], { sort: { ts: 1 } }); let html = '
Visitor email: ${ data.email }
Message:
${ message }