Regression: Remove model observe that was used to control the status of the Omnichannel agents (#17078)

pull/17090/head
Renato Becker 5 years ago committed by Diego Sampaio
parent a84edc77b0
commit 0758bb7ba3
No known key found for this signature in database
GPG Key ID: E060152B30502562
  1. 2
      app/livechat/server/index.js
  2. 105
      app/livechat/server/lib/stream/agentStatus.ts
  3. 4
      app/livechat/server/methods/takeInquiry.js
  4. 95
      app/livechat/server/unclosedLivechats.js
  5. 9
      app/models/server/models/Users.js
  6. 3
      app/settings/server/functions/settings.d.ts
  7. 1
      packages/rocketchat-i18n/i18n/en.i18n.json
  8. 1
      packages/rocketchat-i18n/i18n/pt-BR.i18n.json

@ -75,10 +75,10 @@ import './lib/RoutingManager';
import './lib/routing/External';
import './lib/routing/ManualSelection';
import './lib/routing/AutoSelection';
import './lib/stream/agentStatus';
import './lib/stream/departmentAgents';
import './lib/stream/queueManager';
import './sendMessageBySMS';
import './unclosedLivechats';
import './api';
import './api/rest';

@ -0,0 +1,105 @@
import { Meteor } from 'meteor/meteor';
import { Livechat } from '../Livechat';
import { settings } from '../../../../settings/server';
import { Users } from '../../../../models/server';
let monitorAgents = false;
let actionTimeout = 60000;
let action = 'none';
let comment = '';
settings.get('Livechat_agent_leave_action_timeout', function(_key: string, value: number) {
actionTimeout = value * 1000;
});
settings.get('Livechat_agent_leave_action', function(_key: string, value: boolean) {
monitorAgents = value;
});
settings.get('Livechat_agent_leave_action', function(_key: string, value: string) {
action = value;
});
settings.get('Livechat_agent_leave_comment', function(_key: string, value: string) {
comment = value;
});
const onlineAgents = {
users: new Set(),
queue: new Map(),
add(userId: string): void {
if (this.exists(userId)) {
return;
}
if (this.queue.has(userId)) {
clearTimeout(this.queue.get(userId));
this.queue.delete(userId);
}
this.users.add(userId);
},
remove(userId: string): void {
if (!this.exists(userId)) {
return;
}
if (this.queue.has(userId)) {
clearTimeout(this.queue.get(userId));
}
this.queue.set(userId, setTimeout(this.runAgentLeaveAction, actionTimeout, userId));
},
exists(userId: string): boolean {
return this.users.has(userId);
},
runAgentLeaveAction: Meteor.bindEnvironment((userId: string) => {
onlineAgents.users.delete(userId);
onlineAgents.queue.delete(userId);
if (action === 'close') {
return Livechat.closeOpenChats(userId, comment);
}
if (action === 'forward') {
return Livechat.forwardOpenChats(userId);
}
}),
};
Users.on('change', ({ clientAction, id, diff }) => {
if (!monitorAgents) {
return;
}
if (clientAction !== 'removed' && diff && !diff.status && !diff.statusLivechat) {
return;
}
switch (clientAction) {
case 'updated':
case 'inserted':
const agent = Users.findOneAgentById(id, {
fields: {
status: 1,
statusLivechat: 1,
},
});
const serviceOnline = agent && agent.status !== 'offline' && agent.statusLivechat === 'available';
if (serviceOnline) {
return onlineAgents.add(id);
}
onlineAgents.remove(id);
break;
case 'removed':
onlineAgents.remove(id);
break;
}
});

@ -17,6 +17,10 @@ Meteor.methods({
}
const user = Users.findOneById(Meteor.userId());
const { status, statusLivechat } = user;
if (status === 'offline' || statusLivechat !== 'available') {
throw new Meteor.Error('error-agent-offline', 'Agent offline', { method: 'livechat:takeInquiry' });
}
const agent = {
agentId: user._id,

@ -1,95 +0,0 @@
import { Meteor } from 'meteor/meteor';
import { UserPresenceMonitor } from 'meteor/konecty:user-presence';
import { Livechat } from './lib/Livechat';
import { settings } from '../../settings';
import { Users } from '../../models';
let agentsHandler;
let monitorAgents = false;
let actionTimeout = 60000;
const onlineAgents = {
users: {},
queue: {},
add(userId) {
if (this.queue[userId]) {
clearTimeout(this.queue[userId]);
delete this.queue[userId];
}
this.users[userId] = 1;
},
remove(userId, callback) {
if (this.queue[userId]) {
clearTimeout(this.queue[userId]);
}
this.queue[userId] = setTimeout(Meteor.bindEnvironment(() => {
callback();
delete this.users[userId];
delete this.queue[userId];
}), actionTimeout);
},
exists(userId) {
return !!this.users[userId];
},
};
function runAgentLeaveAction(userId) {
const action = settings.get('Livechat_agent_leave_action');
if (action === 'close') {
return Livechat.closeOpenChats(userId, settings.get('Livechat_agent_leave_comment'));
} if (action === 'forward') {
return Livechat.forwardOpenChats(userId);
}
}
settings.get('Livechat_agent_leave_action_timeout', function(key, value) {
actionTimeout = value * 1000;
});
settings.get('Livechat_agent_leave_action', function(key, value) {
monitorAgents = value;
if (value !== 'none') {
if (!agentsHandler) {
agentsHandler = Users.findOnlineAgents().observeChanges({
added(id) {
onlineAgents.add(id);
},
changed(id, fields) {
if (fields.statusLivechat && fields.statusLivechat === 'not-available') {
onlineAgents.remove(id, () => {
runAgentLeaveAction(id);
});
} else {
onlineAgents.add(id);
}
},
removed(id) {
onlineAgents.remove(id, () => {
runAgentLeaveAction(id);
});
},
});
}
} else if (agentsHandler) {
agentsHandler.stop();
agentsHandler = null;
}
});
UserPresenceMonitor.onSetUserStatus((user, status/* , statusConnection*/) => {
if (!monitorAgents) {
return;
}
if (onlineAgents.exists(user._id)) {
if (status === 'offline' || user.statusLivechat === 'not-available') {
onlineAgents.remove(user._id, () => {
runAgentLeaveAction(user._id);
});
}
}
});

@ -134,6 +134,15 @@ export class Users extends Base {
return this.findOne(query);
}
findOneAgentById(_id, options) {
const query = {
_id,
roles: 'livechat-agent',
};
return this.findOne(query, options);
}
findAgents() {
const query = {
roles: 'livechat-agent',

@ -0,0 +1,3 @@
export namespace settings {
export function get(name: string, callback: (key: string, value: any) => void): string;
}

@ -1279,6 +1279,7 @@
"Entertainment": "Entertainment",
"Error": "Error",
"error-action-not-allowed": "__action__ is not allowed",
"error-agent-offline": "Agent is offline",
"error-application-not-found": "Application not found",
"error-archived-duplicate-name": "There's an archived channel with name '__room_name__'",
"error-avatar-invalid-url": "Invalid avatar URL: __url__",

@ -1192,6 +1192,7 @@
"Entertainment": "Diversão",
"Error": "Erro",
"error-action-not-allowed": "__action__ não é permitido",
"error-agent-offline": "Agente está offline",
"error-application-not-found": "Aplicação não encontrada",
"error-archived-duplicate-name": "Já há um canal arquivado com o nome '__room_name__'",
"error-avatar-invalid-url": "URL inválida de avatar: __url__",

Loading…
Cancel
Save