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: inconsistent behavior when removing subscriptions and inquiries #30572

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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/long-cars-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixed intermittent errors caused by the removal of subscriptions and inquiries when lacking permissions.
9 changes: 7 additions & 2 deletions apps/meteor/app/livechat/client/lib/stream/queueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ const events = {
},
changed: async (inquiry: ILivechatInquiryRecord) => {
if (inquiry.status !== 'queued' || (inquiry.department && !departments.has(inquiry.department))) {
return LivechatInquiry.remove(inquiry._id);
return removeInquiry(inquiry);
}

LivechatInquiry.upsert({ _id: inquiry._id }, { ...inquiry, alert: true, _updatedAt: new Date(inquiry._updatedAt) });
await queryClient.invalidateQueries(['/v1/rooms.info', inquiry.rid]);
},
removed: (inquiry: ILivechatInquiryRecord) => LivechatInquiry.remove(inquiry._id),
removed: (inquiry: ILivechatInquiryRecord) => removeInquiry(inquiry),
};

const removeInquiry = async (inquiry: ILivechatInquiryRecord) => {
await queryClient.invalidateQueries(['rooms', { reference: inquiry.rid, type: 'l' }]);
return LivechatInquiry.remove(inquiry._id);
};

const getInquiriesFromAPI = async () => {
Expand Down
10 changes: 10 additions & 0 deletions apps/meteor/client/views/room/hooks/useOpenRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { omit } from '../../../../lib/utils/omit';
import { NotAuthorizedError } from '../../../lib/errors/NotAuthorizedError';
import { OldUrlRoomError } from '../../../lib/errors/OldUrlRoomError';
import { RoomNotFoundError } from '../../../lib/errors/RoomNotFoundError';
import { queryClient } from '../../../lib/queryClient';

export function useOpenRoom({ type, reference }: { type: RoomType; reference: string }) {
const user = useUser();
Expand Down Expand Up @@ -102,6 +103,15 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st
},
{
retry: 0,
onError: async (error) => {
if (['l', 'v'].includes(type) && error instanceof RoomNotFoundError) {
const { ChatRoom } = await import('../../../../app/models/client');

ChatRoom.remove(reference);
queryClient.removeQueries(['rooms', reference]);
queryClient.removeQueries(['/v1/rooms.info', reference]);
}
},
},
);
}
22 changes: 2 additions & 20 deletions apps/meteor/client/views/room/providers/RoomProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IRoom } from '@rocket.chat/core-typings';
import { usePermission, useStream, useUserId, useRouter } from '@rocket.chat/ui-contexts';
import { useStream, useRouter } 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 { ChatRoom, ChatSubscription } from '../../../../app/models/client';
import { 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 @@ -32,8 +32,6 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => {
const subscribeToRoom = useStream('room-data');

const queryClient = useQueryClient();
const userId = useUserId();
const isLivechatAdmin = usePermission('view-livechat-rooms');
const { t: roomType } = room ?? {};

// TODO: move this to omnichannel context only
Expand All @@ -55,22 +53,6 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => {
}
}, [isSuccess, room, router]);

const { _id: servedById } = room?.servedBy ?? {};

// TODO: Review the necessity of this effect when we move away from cached collections
useEffect(() => {
if (roomType !== 'l' || !servedById) {
return;
}

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

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

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