Skip to content

Commit cfcff99

Browse files
authored
fix: Forwarding messages to channels with join code (RocketChat#39541)
1 parent e41b3af commit cfcff99

4 files changed

Lines changed: 575 additions & 3 deletions

File tree

.changeset/new-students-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
---
4+
5+
Fixes an issue when forwarding messages to a password-protected room.

apps/meteor/app/lib/server/functions/getRoomByNameOrIdWithOptionToJoin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Room } from '@rocket.chat/core-services';
22
import type { IRoom, IUser, RoomType } from '@rocket.chat/core-typings';
3-
import { Rooms, Users } from '@rocket.chat/models';
3+
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
44
import { Meteor } from 'meteor/meteor';
55

66
import { isObject } from '../../../../lib/utils/isObject';
@@ -88,7 +88,10 @@ export const getRoomByNameOrIdWithOptionToJoin = async ({
8888
// If the room type is channel and joinChannel has been passed, try to join them
8989
// if they can't join the room, this will error out!
9090
if (room.t === 'c' && joinChannel) {
91-
await Room.join({ room, user });
91+
const sub = await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id, { projection: { _id: 1 } });
92+
if (!sub) {
93+
await Room.join({ room, user });
94+
}
9295
}
9396

9497
return room;

apps/meteor/tests/end-to-end/api/chat.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,32 @@ const pinMessage = ({ msgId }: { msgId: IMessage['_id'] }) => {
2828
describe('[Chat]', () => {
2929
let testChannel: IRoom;
3030
let message: { _id: IMessage['_id'] };
31+
let protectedChannel: IRoom;
3132

3233
before((done) => getCredentials(done));
3334

3435
before(async () => {
3536
testChannel = (await createRoom({ type: 'c', name: `chat.api-test-${Date.now()}` })).body.channel;
37+
protectedChannel = (await createRoom({ type: 'c', name: `chat.api-protected-test-${Date.now()}` })).body.channel;
38+
39+
await request
40+
.post(api('rooms.saveRoomSettings'))
41+
.set(credentials)
42+
.send({
43+
rid: protectedChannel._id,
44+
joinCode: 'super-secret-password',
45+
})
46+
.expect('Content-Type', 'application/json')
47+
.expect(200)
48+
.expect((res) => {
49+
expect(res.body).to.have.property('success', true);
50+
});
3651
});
3752

38-
after(() => deleteRoom({ type: 'c', roomId: testChannel._id }));
53+
after(async () => {
54+
await deleteRoom({ type: 'c', roomId: testChannel._id });
55+
await deleteRoom({ type: 'c', roomId: protectedChannel._id });
56+
});
3957

4058
describe('/chat.postMessage', () => {
4159
it('should throw an error when at least one of required parameters(channel, roomId) is not sent', (done) => {
@@ -586,6 +604,34 @@ describe('[Chat]', () => {
586604
.end(done);
587605
});
588606

607+
it('should allow forwarding a message into the same password protected room', async () => {
608+
const postResponse = await request
609+
.post(api('chat.postMessage'))
610+
.set(credentials)
611+
.send({
612+
roomId: [protectedChannel._id],
613+
text: 'Message to be forwarded',
614+
})
615+
.expect('Content-Type', 'application/json')
616+
.expect(200);
617+
618+
expect(postResponse.body).to.have.property('success', true);
619+
const originalMessageId = postResponse.body.message._id as IMessage['_id'];
620+
621+
const forwardResponse = await request
622+
.post(api('chat.postMessage'))
623+
.set(credentials)
624+
.send({
625+
roomId: [protectedChannel._id],
626+
text: `[](http://localhost:3000/channel/${protectedChannel.name}?msg=${originalMessageId}`,
627+
})
628+
.expect('Content-Type', 'application/json')
629+
.expect(200);
630+
631+
expect(forwardResponse.body).to.have.property('success', true);
632+
expect(forwardResponse.body).to.have.nested.property('message.rid', protectedChannel._id);
633+
});
634+
589635
it('should return statusCode 200 when postMessage successfully', (done) => {
590636
void request
591637
.post(api('chat.postMessage'))

0 commit comments

Comments
 (0)