diff --git a/.changeset/friendly-terms-tell.md b/.changeset/friendly-terms-tell.md new file mode 100644 index 000000000000..80e0edafadc3 --- /dev/null +++ b/.changeset/friendly-terms-tell.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fix: self dm is not found with `im.messages` diff --git a/apps/meteor/app/lib/server/functions/getRoomByNameOrIdWithOptionToJoin.ts b/apps/meteor/app/lib/server/functions/getRoomByNameOrIdWithOptionToJoin.ts index e085ac4ff47f..d64d2da28ab8 100644 --- a/apps/meteor/app/lib/server/functions/getRoomByNameOrIdWithOptionToJoin.ts +++ b/apps/meteor/app/lib/server/functions/getRoomByNameOrIdWithOptionToJoin.ts @@ -40,8 +40,9 @@ export const getRoomByNameOrIdWithOptionToJoin = async ({ }); } - const rid = isObject(roomUser) ? [user._id, roomUser._id].sort().join('') : nameOrId; - room = await Rooms.findOneById(rid); + room = isObject(roomUser) + ? await Rooms.findOneDirectRoomContainingAllUserIDs([...new Set([user._id, roomUser._id])]) + : await Rooms.findOneById(nameOrId); // If the room hasn't been found yet, let's try some more if (!isObject(room)) { @@ -55,7 +56,7 @@ export const getRoomByNameOrIdWithOptionToJoin = async ({ } } - await createDirectMessage([roomUser.username], user._id); + const { rid } = await createDirectMessage([roomUser.username], user._id); return Rooms.findOneById(rid); } diff --git a/apps/meteor/tests/end-to-end/api/04-direct-message.js b/apps/meteor/tests/end-to-end/api/04-direct-message.js index 160db851ad87..03343a76e5b0 100644 --- a/apps/meteor/tests/end-to-end/api/04-direct-message.js +++ b/apps/meteor/tests/end-to-end/api/04-direct-message.js @@ -335,6 +335,24 @@ describe('[Direct Messages]', function () { await testFileUploads('im.files', directMessage, 'invalid-channel'); }); + describe('/im.messages', () => { + it('should return all DM messages that were sent to yourself using your username', (done) => { + request + .get(api('im.messages')) + .set(credentials) + .query({ + username: adminUsername, + }) + .expect('Content-Type', 'application/json') + .expect(200) + .expect((res) => { + expect(res.body).to.have.property('success', true); + expect(res.body).to.have.property('messages').and.to.be.an('array'); + }) + .end(done); + }); + }); + describe('/im.messages.others', () => { it('should fail when the endpoint is disabled', (done) => { updateSetting('API_Enable_Direct_Message_History_EndPoint', false).then(() => {