Skip to content

Commit

Permalink
fix: Removed agent access to already taken rooms (#28979)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Schoeler <martin.schoeler@rocket.chat>
  • Loading branch information
aleksandernsilva and MartinSchoeler committed Jun 6, 2023
1 parent fbe6905 commit 38c7d3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-knives-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

fix: Remove room from UI when another agent takes it
12 changes: 7 additions & 5 deletions apps/meteor/app/livechat/client/lib/stream/queueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ILivechatDepartment, ILivechatInquiryRecord, IOmnichannelAgent } f

import { LivechatInquiry } from '../../collections/LivechatInquiry';
import { callWithErrorHandling } from '../../../../../client/lib/utils/callWithErrorHandling';
import { queryClient } from '../../../../../client/lib/queryClient';
import { sdk } from '../../../../utils/client/lib/SDKClient';

const departments = new Set();
Expand All @@ -10,12 +11,13 @@ const events = {
added: (inquiry: ILivechatInquiryRecord) => {
departments.has(inquiry.department) && LivechatInquiry.insert({ ...inquiry, alert: true, _updatedAt: new Date(inquiry._updatedAt) });
},
changed: (inquiry: ILivechatInquiryRecord) => {
changed: async (inquiry: ILivechatInquiryRecord) => {
if (inquiry.status !== 'queued' || (inquiry.department && !departments.has(inquiry.department))) {
return LivechatInquiry.remove(inquiry._id);
}

LivechatInquiry.upsert({ _id: inquiry._id }, { ...inquiry, alert: true, _updatedAt: new Date(inquiry._updatedAt) });
await queryClient.invalidateQueries({ queryKey: ['rooms', inquiry.rid], exact: true });
},
removed: (inquiry: ILivechatInquiryRecord) => LivechatInquiry.remove(inquiry._id),
};
Expand All @@ -32,12 +34,12 @@ const removeListenerOfDepartment = (departmentId: ILivechatDepartment['_id']) =>

const appendListenerToDepartment = (departmentId: ILivechatDepartment['_id']) => {
departments.add(departmentId);
sdk.stream('livechat-inquiry-queue-observer', [`department/${departmentId}`], (args) => {
sdk.stream('livechat-inquiry-queue-observer', [`department/${departmentId}`], async (args) => {
if (!('type' in args)) {
return;
}
const { type, ...inquiry } = args;
events[args.type](inquiry);
await events[args.type](inquiry);
});
return () => removeListenerOfDepartment(departmentId);
};
Expand All @@ -57,12 +59,12 @@ const getAgentsDepartments = async (userId: IOmnichannelAgent['_id']) => {
const removeGlobalListener = () => sdk.stop('livechat-inquiry-queue-observer', 'public');

const addGlobalListener = () => {
sdk.stream('livechat-inquiry-queue-observer', ['public'], (args) => {
sdk.stream('livechat-inquiry-queue-observer', ['public'], async (args) => {
if (!('type' in args)) {
return;
}
const { type, ...inquiry } = args;
events[args.type](inquiry);
await events[args.type](inquiry);
});
return removeGlobalListener;
};
Expand Down
20 changes: 18 additions & 2 deletions apps/meteor/client/views/room/providers/RoomProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { IRoom } from '@rocket.chat/core-typings';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { useRoute, useStream } from '@rocket.chat/ui-contexts';
import { usePermission, useRoute, useStream, useUserId } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import type { ReactNode, ContextType, ReactElement } from 'react';
import React, { useMemo, memo, useEffect, useCallback } from 'react';

import { ChatSubscription } from '../../../../app/models/client';
import { ChatRoom, ChatSubscription } from '../../../../app/models/client';
import { RoomHistoryManager } from '../../../../app/ui-utils/client';
import { UserAction } from '../../../../app/ui/client/lib/UserAction';
import { useReactiveQuery } from '../../../hooks/useReactiveQuery';
Expand Down Expand Up @@ -34,6 +34,8 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => {
const subscribeToRoom = useStream('room-data');

const queryClient = useQueryClient();
const userId = useUserId();
const isLivechatAdmin = usePermission('view-livechat-rooms');

// TODO: move this to omnichannel context only
useEffect(() => {
Expand All @@ -54,6 +56,20 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => {
}
}, [isSuccess, room, homeRoute]);

// TODO: Review the necessity of this effect when we move away from cached collections
useEffect(() => {
if (!room || !isOmnichannelRoom(room) || !room.servedBy) {
return;
}

if (!isLivechatAdmin && room.servedBy._id !== userId) {
ChatRoom.remove(room._id);
queryClient.removeQueries(['rooms', room._id]);
queryClient.removeQueries(['rooms', { reference: room._id, type: 'l' }]);
queryClient.removeQueries(['/v1/rooms.info', room._id]);
}
}, [homeRoute, isLivechatAdmin, queryClient, userId, room]);

const subscriptionQuery = useReactiveQuery(['subscriptions', { rid }], () => ChatSubscription.findOne({ rid }) ?? null);

const pseudoRoom = useMemo(() => {
Expand Down

0 comments on commit 38c7d3e

Please sign in to comment.