From baaee3ff61c698adc25b7e8d2ceb3b7a4259693c Mon Sep 17 00:00:00 2001 From: Marcos Spessatto Defendi Date: Fri, 17 Jan 2025 15:32:34 -0300 Subject: [PATCH] refactor: remove getReadReceipts method calls (server) (#34891) --- apps/meteor/ee/server/api/chat.ts | 3 +- .../ee/server/methods/getReadReceipts.ts | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/meteor/ee/server/api/chat.ts b/apps/meteor/ee/server/api/chat.ts index 2c8f8c5ca60..e96e8ab9b1c 100644 --- a/apps/meteor/ee/server/api/chat.ts +++ b/apps/meteor/ee/server/api/chat.ts @@ -3,6 +3,7 @@ import { License } from '@rocket.chat/license'; import { Meteor } from 'meteor/meteor'; import { API } from '../../../app/api/server/api'; +import { getReadReceiptsFunction } from '../methods/getReadReceipts'; type GetMessageReadReceiptsProps = { messageId: IMessage['_id']; @@ -36,7 +37,7 @@ API.v1.addRoute( } return API.v1.success({ - receipts: await Meteor.callAsync('getReadReceipts', { messageId }), + receipts: await getReadReceiptsFunction(messageId, this.userId), }); }, }, diff --git a/apps/meteor/ee/server/methods/getReadReceipts.ts b/apps/meteor/ee/server/methods/getReadReceipts.ts index 04a6ed1dff3..d9ef843f15a 100644 --- a/apps/meteor/ee/server/methods/getReadReceipts.ts +++ b/apps/meteor/ee/server/methods/getReadReceipts.ts @@ -15,16 +15,28 @@ declare module '@rocket.chat/ddp-client' { } } -Meteor.methods({ - async getReadReceipts({ messageId }) { - if (!License.hasModule('message-read-receipt')) { - throw new Meteor.Error('error-action-not-allowed', 'This is an enterprise feature', { method: 'getReadReceipts' }); - } +export const getReadReceiptsFunction = async function (messageId: IMessage['_id'], userId: string): Promise { + if (!License.hasModule('message-read-receipt')) { + throw new Meteor.Error('error-action-not-allowed', 'This is an enterprise feature', { method: 'getReadReceipts' }); + } + check(messageId, String); - if (!messageId) { - throw new Meteor.Error('error-invalid-message', "The required 'messageId' param is missing.", { method: 'getReadReceipts' }); - } + const message = await Messages.findOneById(messageId); + if (!message) { + throw new Meteor.Error('error-invalid-message', 'Invalid message', { + method: 'getReadReceipts', + }); + } + + if (!(await canAccessRoomIdAsync(message.rid, userId))) { + throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'getReadReceipts' }); + } + return ReadReceipt.getReceipts(message); +}; + +Meteor.methods({ + async getReadReceipts({ messageId }) { check(messageId, String); const uid = Meteor.userId(); @@ -32,17 +44,6 @@ Meteor.methods({ throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'getReadReceipts' }); } - const message = await Messages.findOneById(messageId); - if (!message) { - throw new Meteor.Error('error-invalid-message', 'Invalid message', { - method: 'getReadReceipts', - }); - } - - if (!(await canAccessRoomIdAsync(message.rid, uid))) { - throw new Meteor.Error('error-invalid-room', 'Invalid room', { method: 'getReadReceipts' }); - } - - return ReadReceipt.getReceipts(message); + return getReadReceiptsFunction(messageId, uid); }, });