|
1 | 1 | import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings'; |
2 | 2 | import { isEditedMessage } from '@rocket.chat/core-typings'; |
3 | | -import { Messages, Subscriptions, NotificationQueue } from '@rocket.chat/models'; |
| 3 | +import { Messages, Rooms, Subscriptions, NotificationQueue } from '@rocket.chat/models'; |
| 4 | +import { Meteor } from 'meteor/meteor'; |
4 | 5 |
|
5 | 6 | import { callbacks } from '../../../server/lib/callbacks'; |
| 7 | +import { canAccessRoomAsync } from '../../authorization/server'; |
6 | 8 | import { |
7 | 9 | notifyOnSubscriptionChangedByRoomIdAndUserIds, |
8 | 10 | notifyOnSubscriptionChangedByRoomIdAndUserId, |
9 | 11 | } from '../../lib/server/lib/notifyListener'; |
10 | 12 | import { getMentions, getUserIdsFromHighlights } from '../../lib/server/lib/notifyUsersOnMessage'; |
| 13 | +import { settings } from '../../settings/server'; |
11 | 14 |
|
12 | 15 | export async function reply({ tmid }: { tmid?: string }, message: IMessage, parentMessage: IMessage, followers: string[]) { |
13 | 16 | if (!tmid || isEditedMessage(message)) { |
@@ -98,3 +101,32 @@ export const readThread = async ({ user, room, tmid }: { user: IUser; room: IRoo |
98 | 101 |
|
99 | 102 | callbacks.runAsync('afterReadMessages', room, { uid: user._id, tmid }); |
100 | 103 | }; |
| 104 | + |
| 105 | +/** |
| 106 | + * Marks a thread as read for a user, replicating the full behavior of the |
| 107 | + * legacy `readThreads` DDP method: it validates that threads are enabled, that |
| 108 | + * the thread and its room exist, that the user can access the room, and runs |
| 109 | + * the `beforeReadMessages` callback before clearing the thread unread state. |
| 110 | + */ |
| 111 | +export const readThreadMethod = async ({ user, tmid }: { user: IUser; tmid: IMessage['_id'] }) => { |
| 112 | + if (!settings.get('Threads_enabled')) { |
| 113 | + throw new Meteor.Error('error-not-allowed', 'Threads Disabled', { method: 'readThreads' }); |
| 114 | + } |
| 115 | + |
| 116 | + const thread = await Messages.findOneById(tmid); |
| 117 | + if (!thread) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const room = await Rooms.findOneById(thread.rid); |
| 122 | + if (!room) { |
| 123 | + throw new Meteor.Error('error-room-does-not-exist', 'This room does not exist', { method: 'readThreads' }); |
| 124 | + } |
| 125 | + |
| 126 | + if (!(await canAccessRoomAsync(room, user))) { |
| 127 | + throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'readThreads' }); |
| 128 | + } |
| 129 | + |
| 130 | + await callbacks.run('beforeReadMessages', thread.rid, user._id); |
| 131 | + await readThread({ user, room, tmid }); |
| 132 | +}; |
0 commit comments