Skip to content

Commit

Permalink
fix: Dont update a user's livechat status when its status is offline (R…
Browse files Browse the repository at this point in the history
…ocketChat#29589)

Co-authored-by: Murtaza Patrawala <34130764+murtaza98@users.noreply.github.com>
  • Loading branch information
KevLehman and murtaza98 authored Jun 29, 2023
1 parent 094f3ba commit 674f95c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
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

0 comments on commit 674f95c

Please sign in to comment.