Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-coats-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes the Chat Limits locking mechanism to allow bot agents to skip the lock as they aren't limited
5 changes: 3 additions & 2 deletions apps/meteor/app/livechat/server/lib/conditionalLockAgent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Users } from '@rocket.chat/models';

import { hasRoleAsync } from '../../../authorization/server/functions/hasRole';
import { settings } from '../../../settings/server';

type LockResult = {
Expand All @@ -9,10 +10,10 @@ type LockResult = {
};

export async function conditionalLockAgent(agentId: string): Promise<LockResult> {
// Lock and chats limits enforcement are only required when waiting_queue is enabled
// Lock and chats limits enforcement are only required when waiting_queue is enabled and the agent is not a bot
const shouldLock = settings.get<boolean>('Livechat_waiting_queue');

if (!shouldLock) {
if (!shouldLock || (await hasRoleAsync(agentId, 'bot'))) {
return {
acquired: false,
required: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const mockSettings = {
get: sinon.stub(),
};

const mockHasRoleAsync = sinon.stub();

const { conditionalLockAgent } = proxyquire.noCallThru().load('../../../../../../app/livechat/server/lib/conditionalLockAgent', {
'@rocket.chat/models': { Users: mockUsers },
'../../../authorization/server/functions/hasRole': { hasRoleAsync: mockHasRoleAsync },
'../../../settings/server': { settings: mockSettings },
});

Expand All @@ -21,11 +24,13 @@ describe('conditionalLockAgent', () => {
mockUsers.acquireAgentLock.reset();
mockUsers.releaseAgentLock.reset();
mockSettings.get.reset();
mockHasRoleAsync.reset();
});

describe('when waiting_queue is enabled', () => {
beforeEach(() => {
mockSettings.get.withArgs('Livechat_waiting_queue').returns(true);
mockHasRoleAsync.resolves(false);
});

it('should return acquired: true when lock is successfully acquired', async () => {
Expand Down Expand Up @@ -59,6 +64,16 @@ describe('conditionalLockAgent', () => {
expect(mockUsers.releaseAgentLock.firstCall.args[0]).to.equal('agent1');
expect(mockUsers.releaseAgentLock.firstCall.args[1]).to.be.instanceOf(Date);
});

it('should skip lock acquisition for bot agents', async () => {
mockHasRoleAsync.resolves(true);

const result = await conditionalLockAgent('agent1');

expect(result.acquired).to.equal(false);
expect(result.required).to.equal(false);
expect(mockUsers.acquireAgentLock.called).to.equal(false);
});
});

describe('when waiting_queue is disabled', () => {
Expand All @@ -72,6 +87,7 @@ describe('conditionalLockAgent', () => {
expect(result.acquired).to.equal(false);
expect(result.required).to.equal(false);
expect(mockUsers.acquireAgentLock.called).to.equal(false);
expect(mockHasRoleAsync.called).to.equal(false);
});

it('should have a no-op unlock function', async () => {
Expand Down
Loading