Skip to content

Commit

Permalink
chore: Remove openRoom client meteor method (#33833)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva authored Dec 15, 2024
1 parent 85980d7 commit 7f22793
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 40 deletions.
16 changes: 16 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 @@ -893,3 +895,17 @@ API.v1.addRoute(
},
},
);

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

await openRoom(this.userId, roomId);

return API.v1.success();
},
},
);
2 changes: 1 addition & 1 deletion apps/meteor/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import('./polyfills')
.then(() => import('./meteorOverrides'))
.then(() => import('./ecdh'))
.then(() => import('./importPackages'))
.then(() => Promise.all([import('./methods'), import('./startup')]))
.then(() => import('./startup'))
.then(() => import('./omnichannel'))
.then(() => Promise.all([import('./views/admin'), import('./views/marketplace'), import('./views/account')]));
1 change: 0 additions & 1 deletion apps/meteor/client/methods/index.ts

This file was deleted.

26 changes: 0 additions & 26 deletions apps/meteor/client/methods/openRoom.ts

This file was deleted.

7 changes: 4 additions & 3 deletions apps/meteor/client/views/room/hooks/useOpenRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMethod, useRoute, useSetting, useUser } from '@rocket.chat/ui-contex
import { useQuery } from '@tanstack/react-query';
import { useRef } from 'react';

import { useOpenRoomMutation } from './useOpenRoomMutation';
import { roomFields } from '../../../../lib/publishFields';
import { omit } from '../../../../lib/utils/omit';
import { NotAuthorizedError } from '../../../lib/errors/NotAuthorizedError';
Expand All @@ -15,8 +16,8 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st
const allowAnonymousRead = useSetting('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 @@ -96,8 +97,8 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st

// update user's room subscription
const sub = Subscriptions.findOne({ rid: room._id });
if (sub && !sub.open) {
await openRoom(room._id);
if (!!user?._id && sub && !sub.open) {
await openRoom.mutateAsync({ roomId: room._id, userId: user._id });
}
return { rid: room._id };
},
Expand Down
32 changes: 32 additions & 0 deletions apps/meteor/client/views/room/hooks/useOpenRoomMutation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useMutation } from '@tanstack/react-query';

import { updateSubscription } from '../../../lib/mutationEffects/updateSubscription';

type OpenRoomParams = {
roomId: string;
userId: string;
};

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

return useMutation({
mutationFn: async ({ roomId, userId }: OpenRoomParams) => {
await openRoom({ roomId });

return { userId, roomId };
},
onMutate: async ({ roomId, userId }) => {
return updateSubscription(roomId, userId, { open: true });
},
onError: async (_, { roomId, userId }, rollbackDocument) => {
if (!rollbackDocument) {
return;
}

const { open } = rollbackDocument;
await updateSubscription(roomId, userId, { open });
},
});
};
13 changes: 13 additions & 0 deletions apps/meteor/server/lib/openRoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Subscriptions } from '@rocket.chat/models';

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

export async function openRoom(userId: string, roomId: string) {
const openByRoomResponse = await Subscriptions.openByRoomIdAndUserId(roomId, userId);

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

return openByRoomResponse.modifiedCount;
}
11 changes: 2 additions & 9 deletions apps/meteor/server/methods/openRoom.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { IRoom } from '@rocket.chat/core-typings';
import type { ServerMethods } from '@rocket.chat/ddp-client';
import { Subscriptions } from '@rocket.chat/models';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';

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

declare module '@rocket.chat/ddp-client' {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -25,12 +24,6 @@ Meteor.methods<ServerMethods>({
});
}

const openByRoomResponse = await Subscriptions.openByRoomIdAndUserId(rid, uid);

if (openByRoomResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(rid, uid);
}

return openByRoomResponse.modifiedCount;
return openRoom(uid, rid);
},
});
48 changes: 48 additions & 0 deletions apps/meteor/tests/end-to-end/api/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3317,6 +3317,7 @@ describe('[Rooms]', () => {
});
});
});

describe('/rooms.isMember', () => {
let testChannel: IRoom;
let testGroup: IRoom;
Expand Down Expand Up @@ -3599,4 +3600,51 @@ describe('[Rooms]', () => {
});
});
});

describe('/rooms.open', () => {
let room: IRoom;

before(async () => {
room = (await createRoom({ type: 'c', name: `rooms.open.test.${Date.now()}` })).body.channel;
});

after(async () => {
await deleteRoom({ type: 'c', roomId: room._id });
});

it('should open the room', (done) => {
void request
.post(api('rooms.open'))
.set(credentials)
.send({ roomId: room._id })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
});

void request
.get(api('subscriptions.getOne'))
.set(credentials)
.query({ roomId: room._id })
.send()
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body.subscription).to.have.property('open', true);
})
.end(done);
});

it('should fail if roomId is not provided', async () => {
await request
.post(api('rooms.open'))
.set(credentials)
.send()
.expect('Content-Type', 'application/json')
.expect(400)
.expect((res: Response) => {
expect(res.body).to.have.property('success', false);
});
});
});
});
22 changes: 22 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,24 @@ const roomsCleanHistorySchema = {

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

type RoomsOpenProps = {
roomId: string;
};

const roomsOpenSchema = {
type: 'object',
properties: {
roomId: {
type: 'string',
minLength: 1,
},
},
required: ['roomId'],
additionalProperties: false,
};

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

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

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

0 comments on commit 7f22793

Please sign in to comment.