Skip to content

Commit

Permalink
chore: remove openRoom meteor method
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva committed Oct 30, 2024
1 parent d1e8497 commit 80e6842
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 69 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Meteor } from 'meteor/meteor';
import { isTruthy } from '../../../../lib/isTruthy';
import { eraseRoom } from '../../../../server/lib/eraseRoom';
import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { openRoom } from '../../../../server/lib/openRoom';
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom';
import { canAccessRoomAsync } from '../../../authorization/server';
Expand Down Expand Up @@ -351,7 +352,7 @@ API.v1.addRoute(
return API.v1.failure(`The channel, ${findResult.name}, is already open to the sender`);
}

await Meteor.callAsync('openRoom', findResult._id);
await openRoom(this.userId, findResult._id);

return API.v1.success();
},
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Filter } from 'mongodb';

import { eraseRoom } from '../../../../server/lib/eraseRoom';
import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { openRoom } from '../../../../server/lib/openRoom';
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom';
import { canAccessRoomAsync, roomAccessAttributes } from '../../../authorization/server';
Expand Down Expand Up @@ -863,7 +864,7 @@ API.v1.addRoute(
return API.v1.failure(`The private group, ${findResult.name}, is already open for the sender`);
}

await Meteor.callAsync('openRoom', findResult.rid);
await openRoom(this.userId, findResult.rid);

return API.v1.success();
},
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Match, check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

import { eraseRoom } from '../../../../server/lib/eraseRoom';
import { openRoom } from '../../../../server/lib/openRoom';
import { createDirectMessage } from '../../../../server/methods/createDirectMessage';
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
import { canAccessRoomIdAsync } from '../../../authorization/server/functions/canAccessRoom';
Expand Down Expand Up @@ -553,7 +554,7 @@ API.v1.addRoute(
const { room, subscription } = await findDirectMessageRoom({ roomId }, this.userId);

if (!subscription?.open) {
await Meteor.callAsync('openRoom', room._id);
await openRoom(this.userId, room._id);
}

return API.v1.success();
Expand Down
20 changes: 20 additions & 0 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
isRoomsExportProps,
isRoomsIsMemberProps,
isRoomsCleanHistoryProps,
isRoomsOpenProps,
} from '@rocket.chat/rest-typings';
import { Meteor } from 'meteor/meteor';

import { isTruthy } from '../../../../lib/isTruthy';
import { omit } from '../../../../lib/utils/omit';
import * as dataExport from '../../../../server/lib/dataExport';
import { eraseRoom } from '../../../../server/lib/eraseRoom';
import { openRoom } from '../../../../server/lib/openRoom';
import { muteUserInRoom } from '../../../../server/methods/muteUserInRoom';
import { unmuteUserInRoom } from '../../../../server/methods/unmuteUserInRoom';
import { canAccessRoomAsync, canAccessRoomIdAsync } from '../../../authorization/server/functions/canAccessRoom';
Expand Down Expand Up @@ -877,3 +879,21 @@ API.v1.addRoute(
},
},
);

API.v1.addRoute(
'rooms.open',
{ authRequired: true, isRoomsOpenProps },
{
async post() {
const { roomId } = this.bodyParams;

if (!roomId) {
return API.v1.failure('Room not found');
}

await openRoom(this.userId, roomId);

return API.v1.success();
},
},
);
1 change: 0 additions & 1 deletion apps/meteor/client/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './hideRoom';
import './openRoom';
import './pinMessage';
import './starMessage';
import './toggleFavorite';
Expand Down
26 changes: 0 additions & 26 deletions apps/meteor/client/methods/openRoom.ts

This file was deleted.

5 changes: 3 additions & 2 deletions apps/meteor/client/views/room/hooks/useOpenRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { NotAuthorizedError } from '../../../lib/errors/NotAuthorizedError';
import { OldUrlRoomError } from '../../../lib/errors/OldUrlRoomError';
import { RoomNotFoundError } from '../../../lib/errors/RoomNotFoundError';
import { queryClient } from '../../../lib/queryClient';
import { useOpenRoomMutation } from './useOpenRoomMutation';

export function useOpenRoom({ type, reference }: { type: RoomType; reference: string }) {
const user = useUser();
const allowAnonymousRead = useSetting<boolean>('Accounts_AllowAnonymousRead') ?? true;
const getRoomByTypeAndName = useMethod('getRoomByTypeAndName');
const createDirectMessage = useMethod('createDirectMessage');
const openRoom = useMethod('openRoom');
const directRoute = useRoute('direct');
const openRoom = useOpenRoomMutation();

const unsubscribeFromRoomOpenedEvent = useRef<() => void>(() => undefined);

Expand Down Expand Up @@ -97,7 +98,7 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st
// update user's room subscription
const sub = ChatSubscription.findOne({ rid: room._id });
if (sub && !sub.open) {
await openRoom(room._id);
await openRoom.mutateAsync({ roomId: room._id });
}
return { rid: room._id };
},
Expand Down
23 changes: 23 additions & 0 deletions apps/meteor/client/views/room/hooks/useOpenRoomMutation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEndpoint, useUserId } from '@rocket.chat/ui-contexts';
import { useMutation } from '@tanstack/react-query';

import { ChatSubscription } from '../../../../app/models/client';

export const useOpenRoomMutation = () => {
const openRoom = useEndpoint('POST', '/v1/rooms.open');
const userId = useUserId();

return useMutation({
mutationFn: async ({ roomId }: { roomId: string }) => {
if (!userId) {
throw new Error('error-invalid-user');
}

await openRoom({ roomId });
return roomId;
},
onSuccess: (roomId: string) => {
ChatSubscription.update({ roomId, 'u._id': Meteor.userId() }, { $set: { open: true } });
},
});
};
24 changes: 24 additions & 0 deletions apps/meteor/server/lib/openRoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Subscriptions } from '@rocket.chat/models';

import { notifyOnSubscriptionChangedByRoomIdAndUserId } from '../../app/lib/server/lib/notifyListener';

export async function openRoom(userId: string, roomId: string) {
check(userId, String);
check(roomId, String);

if (!roomId) {
throw new Meteor.Error('error-invalid-room', 'Invalid room');
}

if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user');
}

const openByRoomResponse = await Subscriptions.openByRoomIdAndUserId(roomId, userId);

if (openByRoomResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(roomId, userId);
}

return openByRoomResponse.modifiedCount;
}
1 change: 0 additions & 1 deletion apps/meteor/server/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import './loadSurroundingMessages';
import './logoutCleanUp';
import './messageSearch';
import './muteUserInRoom';
import './openRoom';
import './readMessages';
import './readThreads';
import './registerUser';
Expand Down
36 changes: 0 additions & 36 deletions apps/meteor/server/methods/openRoom.ts

This file was deleted.

19 changes: 19 additions & 0 deletions packages/rest-typings/src/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,21 @@ const roomsCleanHistorySchema = {

export const isRoomsCleanHistoryProps = ajv.compile<RoomsCleanHistoryProps>(roomsCleanHistorySchema);

type RoomsOpenProps = {
roomId: string;
};

const roomsOpenSchema = {
type: 'object',
properties: {
roomId: {
type: 'string',
},
},
};

export const isRoomsOpenProps = ajv.compile<RoomsOpenProps>(roomsOpenSchema);

export type RoomsEndpoints = {
'/v1/rooms.autocomplete.channelAndPrivate': {
GET: (params: RoomsAutoCompleteChannelAndPrivateProps) => {
Expand Down Expand Up @@ -764,4 +779,8 @@ export type RoomsEndpoints = {
files: IUpload[];
}>;
};

'/v1/rooms.open': {
POST: (params: RoomsOpenProps) => void;
};
};

0 comments on commit 80e6842

Please sign in to comment.