Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Dont update a user's livechat status when its status is offline #29589

Merged
merged 13 commits into from
Jun 29, 2023
Merged
5 changes: 5 additions & 0 deletions .changeset/thin-hats-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Avoid updating a user's livechat status on login when its status is set to offline
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ export abstract class AbstractBusinessHourBehavior {
return this.UsersRepository.isAgentWithinBusinessHours(agentId);
}

// After logout, users are turned not-available by default
// This will turn them available unless they put themselves offline (manual status change)
async changeAgentActiveStatus(agentId: string, status: string): Promise<any> {
return this.UsersRepository.setLivechatStatusIf(
agentId,
status,
{ livechatStatusSystemModified: true },
// Why this works: statusDefault is the property set when a user manually changes their status
// So if it's set to offline, we can be sure the user will be offline after login and we can skip the update
{ livechatStatusSystemModified: true, statusDefault: { $ne: 'offline' } },
{ livechatStatusSystemModified: true },
);
}
Expand Down
10 changes: 5 additions & 5 deletions apps/meteor/app/livechat/server/business-hour/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { cronJobs } from '@rocket.chat/cron';
import type { IUser } from '@rocket.chat/core-typings';
import { Accounts } from 'meteor/accounts-base';

import { BusinessHourManager } from './BusinessHourManager';
import { SingleBusinessHourBehavior } from './Single';
Expand All @@ -16,8 +17,7 @@ Meteor.startup(async () => {
businessHourManager.registerBusinessHourBehavior(new BusinessHourBehaviorClass());
businessHourManager.registerBusinessHourType(new DefaultBusinessHour());

Accounts.onLogin(
async ({ user }: { user: any }) =>
user?.roles?.includes('livechat-agent') && !user?.roles?.includes('bot') && businessHourManager.onLogin(user._id),
);
Accounts.onLogin((user: IUser) => {
void (user?.roles?.includes('livechat-agent') && !user?.roles?.includes('bot') && businessHourManager.onLogin(user._id));
});
});
5 changes: 3 additions & 2 deletions apps/meteor/ee/server/startup/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ Meteor.startup(function () {
if (login.type !== 'resume') {
return;
}
void Presence.newConnection(login.user._id, login.connection.id, nodeId).then(() => {
void (async function () {
await Presence.newConnection(login.user._id, login.connection.id, nodeId);
updateConns();
});
})();
});

Accounts.onLogout(function (login: any): void {
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/models/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,7 @@ export class UsersRaw extends BaseRaw {
setLivechatStatusActiveBasedOnBusinessHours(userId) {
const query = {
_id: userId,
statusDefault: { $ne: 'offline' },
openBusinessHours: {
$exists: true,
$not: { $size: 0 },
Expand Down