-
Notifications
You must be signed in to change notification settings - Fork 13.5k
refactor: migrate AppUploadsConverter to TypeScript #40001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kaustubh2k5
wants to merge
5
commits into
RocketChat:develop
Choose a base branch
from
Kaustubh2k5:refactor/migrate_uplods_js_to_ts
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−98
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ae7655
refactor: migrate AppUploadsConverter to TypeScript
Kaustubh2k5 d49f210
Removed the use of any and added fail fast validation for missing rid…
Kaustubh2k5 cb26508
Merge branch 'develop' into refactor/migrate_uplods_js_to_ts
Kaustubh2k5 fa81770
fixed preserving path field in AppUploadsConverter round trip conversion
Kaustubh2k5 56315ac
proper conversion of value to maintain type safety and added undefine…
Kaustubh2k5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<IAppsUpload['room']| undefined> }; | ||
| get(name: 'users'): { convertById(id: string): Promise<IAppsUpload['user']| undefined> }; | ||
| get(name: 'visitors'): { convertByToken(token: string): Promise<IAppsUpload['visitor']| undefined> }; | ||
| } | ||
|
|
||
| interface IOrchestrator { | ||
| getConverters(): IConverters; | ||
| } | ||
|
|
||
| type AppUploadWithUnmapped = IAppsUpload & { _unmappedProperties_?: Partial<IUpload> }; | ||
| 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<IAppsUpload | undefined> { | ||
| const upload = await Uploads.findOneById(id); | ||
|
|
||
| return this.convertToApp(upload ?? undefined); | ||
| } | ||
|
|
||
| async convertToApp(upload: IUpload | undefined): Promise<IAppsUpload | undefined> { | ||
| 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<IUpload>).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<IUpload>).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<string, unknown> = { | ||
| _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; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.