|
1 | 1 | import type { IMessage } from '@rocket.chat/core-typings'; |
2 | | -import type { ServerMethods } from '@rocket.chat/ddp-client'; |
3 | | -import { Meteor } from 'meteor/meteor'; |
4 | 2 |
|
5 | 3 | import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator'; |
6 | 4 | import { getUser, getUserId } from '../../../../client/lib/user'; |
7 | 5 | import { Rooms, Subscriptions, Messages } from '../../../../client/stores'; |
8 | 6 | import { emoji } from '../../../emoji/client'; |
9 | 7 |
|
10 | | -Meteor.methods<ServerMethods>({ |
11 | | - async setReaction(reaction, messageId) { |
12 | | - if (!getUserId()) { |
13 | | - throw new Meteor.Error(203, 'User_logged_out'); |
14 | | - } |
15 | | - |
16 | | - const user = getUser(); |
| 8 | +export const runOptimisticSetReaction = (reaction: string, messageId: IMessage['_id']): void => { |
| 9 | + if (!getUserId()) { |
| 10 | + return; |
| 11 | + } |
17 | 12 |
|
18 | | - if (!user?.username) { |
19 | | - return false; |
20 | | - } |
| 13 | + const user = getUser(); |
| 14 | + if (!user?.username) { |
| 15 | + return; |
| 16 | + } |
21 | 17 |
|
22 | | - const message: IMessage | undefined = Messages.state.get(messageId); |
23 | | - if (!message) { |
24 | | - return false; |
25 | | - } |
| 18 | + const message: IMessage | undefined = Messages.state.get(messageId); |
| 19 | + if (!message) { |
| 20 | + return; |
| 21 | + } |
26 | 22 |
|
27 | | - const room = Rooms.state.get(message.rid); |
28 | | - if (!room) { |
29 | | - return false; |
30 | | - } |
31 | | - |
32 | | - if (message.private) { |
33 | | - return false; |
34 | | - } |
| 23 | + const room = Rooms.state.get(message.rid); |
| 24 | + if (!room) { |
| 25 | + return; |
| 26 | + } |
35 | 27 |
|
36 | | - if (!emoji.list[reaction]) { |
37 | | - return false; |
38 | | - } |
| 28 | + if (message.private) { |
| 29 | + return; |
| 30 | + } |
39 | 31 |
|
40 | | - if (roomCoordinator.readOnly(room, user)) { |
41 | | - return false; |
42 | | - } |
| 32 | + if (!emoji.list[reaction]) { |
| 33 | + return; |
| 34 | + } |
43 | 35 |
|
44 | | - if (!Subscriptions.state.find(({ rid }) => rid === message.rid)) { |
45 | | - return false; |
46 | | - } |
| 36 | + if (roomCoordinator.readOnly(room, user)) { |
| 37 | + return; |
| 38 | + } |
47 | 39 |
|
48 | | - if (message.reactions?.[reaction] && message.reactions[reaction].usernames.indexOf(user.username) !== -1) { |
49 | | - message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(user.username), 1); |
| 40 | + if (!Subscriptions.state.find(({ rid }) => rid === message.rid)) { |
| 41 | + return; |
| 42 | + } |
50 | 43 |
|
51 | | - if (message.reactions[reaction].usernames.length === 0) { |
52 | | - delete message.reactions[reaction]; |
53 | | - } |
| 44 | + if (message.reactions?.[reaction] && message.reactions[reaction].usernames.indexOf(user.username) !== -1) { |
| 45 | + message.reactions[reaction].usernames.splice(message.reactions[reaction].usernames.indexOf(user.username), 1); |
54 | 46 |
|
55 | | - if (!message.reactions || typeof message.reactions !== 'object' || Object.keys(message.reactions).length === 0) { |
56 | | - delete message.reactions; |
57 | | - Messages.state.update( |
58 | | - (record) => record._id === messageId, |
59 | | - ({ reactions: _, ...record }) => record, |
60 | | - ); |
61 | | - } else { |
62 | | - Messages.state.update( |
63 | | - (record) => record._id === messageId, |
64 | | - (record) => ({ ...record, reactions: message.reactions }), |
65 | | - ); |
66 | | - } |
67 | | - } else { |
68 | | - if (!message.reactions) { |
69 | | - message.reactions = {}; |
70 | | - } |
71 | | - if (!message.reactions[reaction]) { |
72 | | - message.reactions[reaction] = { |
73 | | - usernames: [], |
74 | | - }; |
75 | | - } |
76 | | - message.reactions[reaction].usernames.push(user.username); |
| 47 | + if (message.reactions[reaction].usernames.length === 0) { |
| 48 | + delete message.reactions[reaction]; |
| 49 | + } |
77 | 50 |
|
| 51 | + if (!message.reactions || typeof message.reactions !== 'object' || Object.keys(message.reactions).length === 0) { |
| 52 | + delete message.reactions; |
78 | 53 | Messages.state.update( |
79 | 54 | (record) => record._id === messageId, |
80 | | - (record) => ({ ...record, reactions: message.reactions }), |
| 55 | + ({ reactions: _, ...record }) => record, |
81 | 56 | ); |
| 57 | + return; |
82 | 58 | } |
83 | | - }, |
84 | | -}); |
| 59 | + |
| 60 | + Messages.state.update( |
| 61 | + (record) => record._id === messageId, |
| 62 | + (record) => ({ ...record, reactions: message.reactions }), |
| 63 | + ); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (!message.reactions) { |
| 68 | + message.reactions = {}; |
| 69 | + } |
| 70 | + if (!message.reactions[reaction]) { |
| 71 | + message.reactions[reaction] = { |
| 72 | + usernames: [], |
| 73 | + }; |
| 74 | + } |
| 75 | + message.reactions[reaction].usernames.push(user.username); |
| 76 | + |
| 77 | + Messages.state.update( |
| 78 | + (record) => record._id === messageId, |
| 79 | + (record) => ({ ...record, reactions: message.reactions }), |
| 80 | + ); |
| 81 | +}; |
0 commit comments