|
| 1 | +import { isEditedMessage, type IMessage } from '@rocket.chat/core-typings'; |
| 2 | +import { Messages } from '@rocket.chat/models'; |
| 3 | +import type { IMessage as AppsEngineMessage } from '@rocket.chat/apps-engine/definition/messages'; |
| 4 | +import type { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; |
| 5 | + |
| 6 | +import { transformMappedData } from '../../../../ee/lib/misc/transformMappedData'; |
| 7 | + |
| 8 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 9 | +interface Orchestrator { |
| 10 | + rooms: () => { |
| 11 | + convertById(id: string): Promise<unknown>; |
| 12 | + }; |
| 13 | + users: () => { |
| 14 | + convertById(id: string): Promise<unknown>; |
| 15 | + convertToApp(user: unknown): Promise<unknown>; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +const cachedFunction = <F extends (...args: any[]) => any>(fn: F) => { |
| 20 | + const cache = new Map<string, unknown>(); |
| 21 | + |
| 22 | + return ((...args) => { |
| 23 | + const cacheKey = JSON.stringify(args); |
| 24 | + |
| 25 | + if (cache.has(cacheKey)) { |
| 26 | + return cache.get(cacheKey) as ReturnType<F>; |
| 27 | + } |
| 28 | + |
| 29 | + const result = fn(...args); |
| 30 | + |
| 31 | + cache.set(cacheKey, result); |
| 32 | + |
| 33 | + return result; |
| 34 | + }) as F; |
| 35 | +}; |
| 36 | + |
| 37 | +export class AppThreadsConverter { |
| 38 | + constructor( |
| 39 | + private readonly orch: { |
| 40 | + getConverters: () => { |
| 41 | + get: <O extends keyof Orchestrator>(key: O) => ReturnType<Orchestrator[O]>; |
| 42 | + }; |
| 43 | + }, |
| 44 | + ) { |
| 45 | + this.orch = orch; |
| 46 | + } |
| 47 | + |
| 48 | + async convertById(threadId: string) { |
| 49 | + const query = { |
| 50 | + $or: [ |
| 51 | + { |
| 52 | + _id: threadId, |
| 53 | + }, |
| 54 | + { |
| 55 | + tmid: threadId, |
| 56 | + }, |
| 57 | + ], |
| 58 | + }; |
| 59 | + |
| 60 | + const mainMessage = await Messages.findOneById(threadId); |
| 61 | + |
| 62 | + if (!mainMessage) { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + const replies = await Messages.find(query).toArray(); |
| 67 | + |
| 68 | + const room = (await this.orch.getConverters().get('rooms').convertById(mainMessage.rid)) as IRoom | undefined; |
| 69 | + |
| 70 | + if (!room) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + const convertToApp = cachedFunction(this.orch.getConverters().get('users').convertToApp.bind(this.orch.getConverters().get('users'))); |
| 75 | + |
| 76 | + const convertUserById = cachedFunction(this.orch.getConverters().get('users').convertById.bind(this.orch.getConverters().get('users'))); |
| 77 | + |
| 78 | + return Promise.all([mainMessage, ...replies].map((msg) => this.convertMessage(msg, room, convertUserById, convertToApp))); |
| 79 | + } |
| 80 | + |
| 81 | + async convertMessage( |
| 82 | + msgObj: IMessage, |
| 83 | + room: IRoom, |
| 84 | + convertUserById: ReturnType<Orchestrator['users']>['convertById'], |
| 85 | + convertToApp: ReturnType<Orchestrator['users']>['convertToApp'], |
| 86 | + ): Promise<AppsEngineMessage> { |
| 87 | + const map = { |
| 88 | + id: '_id', |
| 89 | + threadId: 'tmid', |
| 90 | + reactions: 'reactions', |
| 91 | + parseUrls: 'parseUrls', |
| 92 | + text: 'msg', |
| 93 | + createdAt: 'ts', |
| 94 | + updatedAt: '_updatedAt', |
| 95 | + editedAt: 'editedAt', |
| 96 | + emoji: 'emoji', |
| 97 | + avatarUrl: 'avatar', |
| 98 | + alias: 'alias', |
| 99 | + file: 'file', |
| 100 | + customFields: 'customFields', |
| 101 | + groupable: 'groupable', |
| 102 | + token: 'token', |
| 103 | + blocks: 'blocks', |
| 104 | + room, |
| 105 | + editor: async (message: IMessage) => { |
| 106 | + if (!isEditedMessage(message)) { |
| 107 | + return undefined; |
| 108 | + } |
| 109 | + |
| 110 | + const { editedBy } = message; |
| 111 | + |
| 112 | + return convertUserById(editedBy._id); |
| 113 | + }, |
| 114 | + attachments: async (message: IMessage) => { |
| 115 | + if (!message.attachments) { |
| 116 | + return undefined; |
| 117 | + } |
| 118 | + const result = await this._convertAttachmentsToApp(message.attachments); |
| 119 | + delete message.attachments; |
| 120 | + return result; |
| 121 | + }, |
| 122 | + sender: async (message: IMessage) => { |
| 123 | + if (!message.u?._id) { |
| 124 | + return undefined; |
| 125 | + } |
| 126 | + |
| 127 | + let user = await convertUserById(message.u._id); |
| 128 | + |
| 129 | + // When the sender of the message is a Guest (livechat) and not a user |
| 130 | + if (!user) { |
| 131 | + user = await convertToApp(message.u); |
| 132 | + } |
| 133 | + |
| 134 | + return user; |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + return (await transformMappedData(msgObj, map)) as unknown as AppsEngineMessage; |
| 139 | + } |
| 140 | + |
| 141 | + async _convertAttachmentsToApp(attachments: NonNullable<IMessage['attachments']>) { |
| 142 | + const map = { |
| 143 | + collapsed: 'collapsed', |
| 144 | + color: 'color', |
| 145 | + text: 'text', |
| 146 | + timestampLink: 'message_link', |
| 147 | + thumbnailUrl: 'thumb_url', |
| 148 | + imageDimensions: 'image_dimensions', |
| 149 | + imagePreview: 'image_preview', |
| 150 | + imageUrl: 'image_url', |
| 151 | + imageType: 'image_type', |
| 152 | + imageSize: 'image_size', |
| 153 | + audioUrl: 'audio_url', |
| 154 | + audioType: 'audio_type', |
| 155 | + audioSize: 'audio_size', |
| 156 | + videoUrl: 'video_url', |
| 157 | + videoType: 'video_type', |
| 158 | + videoSize: 'video_size', |
| 159 | + fields: 'fields', |
| 160 | + actionButtonsAlignment: 'button_alignment', |
| 161 | + actions: 'actions', |
| 162 | + type: 'type', |
| 163 | + description: 'description', |
| 164 | + author: (attachment: NonNullable<IMessage['attachments']>[number]) => { |
| 165 | + if (!('author_name' in attachment)) { |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + const { author_name: name, author_link: link, author_icon: icon } = attachment; |
| 170 | + |
| 171 | + delete attachment.author_name; |
| 172 | + delete attachment.author_link; |
| 173 | + delete attachment.author_icon; |
| 174 | + |
| 175 | + return { name, link, icon }; |
| 176 | + }, |
| 177 | + title: (attachment: NonNullable<IMessage['attachments']>[number]) => { |
| 178 | + const { title: value, title_link: link, title_link_download: displayDownloadLink } = attachment; |
| 179 | + |
| 180 | + delete attachment.title; |
| 181 | + delete attachment.title_link; |
| 182 | + delete attachment.title_link_download; |
| 183 | + |
| 184 | + return { value, link, displayDownloadLink }; |
| 185 | + }, |
| 186 | + timestamp: (attachment: NonNullable<IMessage['attachments']>[number]) => { |
| 187 | + const result = attachment.ts ? new Date(attachment.ts) : undefined; |
| 188 | + delete attachment.ts; |
| 189 | + return result; |
| 190 | + }, |
| 191 | + }; |
| 192 | + |
| 193 | + return Promise.all(attachments.map(async (attachment) => transformMappedData(attachment, map))); |
| 194 | + } |
| 195 | +} |
0 commit comments