diff --git a/apps/meteor/app/apps/server/converters/uploads.js b/apps/meteor/app/apps/server/converters/uploads.js deleted file mode 100644 index 60f85a8aa72f1..0000000000000 --- a/apps/meteor/app/apps/server/converters/uploads.js +++ /dev/null @@ -1,98 +0,0 @@ -import { Uploads } from '@rocket.chat/models'; - -import { transformMappedData } from './transformMappedData'; - -export class AppUploadsConverter { - constructor(orch) { - this.orch = orch; - } - - async convertById(id) { - const upload = await Uploads.findOneById(id); - - return this.convertToApp(upload); - } - - async convertToApp(upload) { - if (!upload) { - return undefined; - } - - const map = { - id: '_id', - name: 'name', - size: 'size', - type: 'type', - store: 'store', - description: 'description', - complete: 'complete', - uploading: 'uploading', - extension: 'extension', - progress: 'progress', - etag: 'etag', - path: 'path', - token: 'token', - url: 'url', - updatedAt: '_updatedAt', - uploadedAt: 'uploadedAt', - room: async (upload) => { - const result = await this.orch.getConverters().get('rooms').convertById(upload.rid); - delete upload.rid; - return result; - }, - user: async (upload) => { - if (!upload.userId) { - return undefined; - } - - const result = await this.orch.getConverters().get('users').convertById(upload.userId); - delete upload.userId; - return result; - }, - visitor: async (upload) => { - if (!upload.visitorToken) { - return undefined; - } - - const result = await this.orch.getConverters().get('visitors').convertByToken(upload.visitorToken); - delete upload.visitorToken; - return result; - }, - }; - - return transformMappedData(upload, map); - } - - convertToRocketChat(upload) { - if (!upload) { - return undefined; - } - - const { id: userId } = upload.user || {}; - const { token: visitorToken } = upload.visitor || {}; - const { id: rid } = upload.room; - - const newUpload = { - _id: upload.id, - name: upload.name, - size: upload.size, - type: upload.type, - extension: upload.extension, - description: upload.description, - store: upload.store, - etag: upload.etag, - complete: upload.complete, - uploading: upload.uploading, - progress: upload.progress, - token: upload.token, - url: upload.url, - _updatedAt: upload.updatedAt, - uploadedAt: upload.uploadedAt, - rid, - userId, - visitorToken, - }; - - return Object.assign(newUpload, upload._unmappedProperties_); - } -} diff --git a/apps/meteor/app/apps/server/converters/uploads.ts b/apps/meteor/app/apps/server/converters/uploads.ts new file mode 100644 index 0000000000000..abc25f8e1c2e5 --- /dev/null +++ b/apps/meteor/app/apps/server/converters/uploads.ts @@ -0,0 +1,125 @@ +import type { IUpload } from '@rocket.chat/core-typings'; +import type { IUpload as IAppsUpload } from '@rocket.chat/apps-engine/definition/uploads'; +import { Uploads } from '@rocket.chat/models'; + +import { transformMappedData } from './transformMappedData'; + +interface IConverters { + get(name: 'rooms'): { convertById(id: string): Promise }; + get(name: 'users'): { convertById(id: string): Promise }; + get(name: 'visitors'): { convertByToken(token: string): Promise }; +} + +interface IOrchestrator { + getConverters(): IConverters; +} + +type AppUploadWithUnmapped = IAppsUpload & { _unmappedProperties_?: Partial }; +type IUploadWithVisitorToken = IUpload & { visitorToken?: string }; +type IAppsUploadWithDescription = IAppsUpload & { description?: string }; + +export class AppUploadsConverter { + private orch: IOrchestrator; + + constructor(orch: IOrchestrator) { + this.orch = orch; + } + + async convertById(id: string): Promise { + const upload = await Uploads.findOneById(id); + + return this.convertToApp(upload ?? undefined); + } + + async convertToApp(upload: IUpload | undefined): Promise { + if (!upload) { + return undefined; + } + + const map = { + id: '_id', + name: 'name', + size: 'size', + type: 'type', + store: 'store', + description: 'description', + complete: 'complete', + uploading: 'uploading', + extension: 'extension', + progress: 'progress', + etag: 'etag', + path: 'path', + token: 'token', + url: 'url', + updatedAt: '_updatedAt', + uploadedAt: 'uploadedAt', + room: async (upload: IUpload) => { + if (!upload.rid) { + throw new Error('Invalid upload payload: missing rid'); + } + const result = await this.orch.getConverters().get('rooms').convertById(upload.rid); + delete (upload as Partial).rid; + return result; + }, + user: async (upload: IUpload) => { + if (!upload.userId) { + return undefined; + } + + const result = await this.orch.getConverters().get('users').convertById(upload.userId); + delete (upload as Partial).userId; + return result; + }, + visitor: async (upload: IUpload) => { + const uploadWithToken = upload as IUploadWithVisitorToken; + if (!uploadWithToken.visitorToken) { + return undefined; + } + const result = await this.orch.getConverters().get('visitors').convertByToken(uploadWithToken.visitorToken); + delete (upload as IUploadWithVisitorToken).visitorToken; + return result; + }, + }; + + return transformMappedData(upload, map) as unknown as IAppsUpload; + } + + convertToRocketChat(upload: IAppsUpload | undefined): IUpload | undefined { + if (!upload) { + return undefined; + } + + const rid = upload.room?.id; + if (!rid) { + throw new Error('Invalid app upload payload: missing room.id'); + } + + const { id: userId } = upload.user ?? {}; + const { token: visitorToken } = upload.visitor ?? {}; + const uploadWithExtras = upload as IAppsUploadWithDescription & { path?: IUpload['path'] }; + + const newUpload: Record = { + _id: upload.id, + name: upload.name, + size: upload.size !== undefined ? Number(upload.size) : undefined, + type: upload.type, + extension: upload.extension, + description: uploadWithExtras.description, + path: uploadWithExtras.path, + store: upload.store, + etag: upload.etag, + complete: upload.complete, + uploading: upload.uploading, + progress: upload.progress, + token: upload.token, + url: upload.url, + _updatedAt: upload.updatedAt, + uploadedAt: upload.uploadedAt, + rid, + userId, + visitorToken, + }; + + return Object.assign(newUpload, (upload as AppUploadWithUnmapped)._unmappedProperties_) as IUpload; + } +} \ No newline at end of file