From 17c63c717b624f54b296c170ba2aee353db9fdb3 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Mon, 21 Feb 2022 13:06:57 -0300 Subject: [PATCH] Chore: Migrate methods/sendFileMessage to typescript (#3683) * chore: start the migration * chore: update sendFileMessage to ts * chore: removing an `any` from uploadQueue * chore: minor tweak * chore: minor tweak * chore: minor tweaks after merge with developer * chore: minor tweak after merge develop into current --- app/containers/MessageBox/index.tsx | 3 +- app/definitions/ILoggedUser.ts | 6 +-- app/definitions/IRocketChatRecord.ts | 4 +- app/definitions/IUpload.ts | 11 ++--- app/definitions/IUser.ts | 11 +++-- ...{sendFileMessage.js => sendFileMessage.ts} | 42 ++++++++++++------- app/utils/fileUpload/index.ios.ts | 1 + app/utils/fileUpload/interfaces.ts | 6 +-- app/views/ShareView/index.tsx | 1 + 9 files changed, 50 insertions(+), 35 deletions(-) rename app/lib/methods/{sendFileMessage.js => sendFileMessage.ts} (71%) diff --git a/app/containers/MessageBox/index.tsx b/app/containers/MessageBox/index.tsx index 68fd0ed2b..3c924df6f 100644 --- a/app/containers/MessageBox/index.tsx +++ b/app/containers/MessageBox/index.tsx @@ -82,6 +82,7 @@ interface IMessageBoxProps { isFocused(): boolean; user: { id: string; + _id: string; username: string; token: string; }; @@ -1184,5 +1185,5 @@ const mapStateToProps = (state: any) => ({ const dispatchToProps = { typing: (rid: any, status: any) => userTypingAction(rid, status) }; -// @ts-ignore + export default connect(mapStateToProps, dispatchToProps, null, { forwardRef: true })(withActionSheet(MessageBox)) as any; diff --git a/app/definitions/ILoggedUser.ts b/app/definitions/ILoggedUser.ts index 380bcd86c..ecd9e2674 100644 --- a/app/definitions/ILoggedUser.ts +++ b/app/definitions/ILoggedUser.ts @@ -8,10 +8,10 @@ export interface ILoggedUser { language?: string; status: string; statusText?: string; - roles: string[]; + roles?: string[]; avatarETag?: string; - showMessageInMainThread: boolean; - isFromWebView: boolean; + showMessageInMainThread?: boolean; + isFromWebView?: boolean; enableMessageParserEarlyAdoption?: boolean; } diff --git a/app/definitions/IRocketChatRecord.ts b/app/definitions/IRocketChatRecord.ts index bed6d1147..8a933059a 100644 --- a/app/definitions/IRocketChatRecord.ts +++ b/app/definitions/IRocketChatRecord.ts @@ -1,6 +1,6 @@ export interface IRocketChatRecord { - _id: string; - _updatedAt: Date; + _id?: string; + _updatedAt?: Date; } export type RocketChatRecordDeleted = T & diff --git a/app/definitions/IUpload.ts b/app/definitions/IUpload.ts index 6ff03c519..059555130 100644 --- a/app/definitions/IUpload.ts +++ b/app/definitions/IUpload.ts @@ -1,16 +1,17 @@ import Model from '@nozbe/watermelondb/Model'; export interface IUpload { - id: string; - path?: string; + id?: string; + rid?: string; + path: string; name?: string; description?: string; size: number; type?: string; store?: string; - progress: number; - error: boolean; - subscription: { id: string }; + progress?: number; + error?: boolean; + subscription?: { id: string }; } export type TUploadModel = IUpload & Model; diff --git a/app/definitions/IUser.ts b/app/definitions/IUser.ts index c486a56ed..461b52fb4 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -98,12 +98,12 @@ export interface IUser extends IRocketChatRecord, Omit } = {}; -export function isUploadActive(path) { +export function isUploadActive(path: string): boolean { return !!uploadQueue[path]; } -export async function cancelUpload(item) { - if (uploadQueue[item.path]) { +export async function cancelUpload(item: TUploadModel): Promise { + if (!isEmpty(uploadQueue[item.path])) { try { await uploadQueue[item.path].cancel(); } catch { @@ -20,7 +24,7 @@ export async function cancelUpload(item) { } try { const db = database.active; - await db.action(async () => { + await db.write(async () => { await item.destroyPermanently(); }); } catch (e) { @@ -30,7 +34,13 @@ export async function cancelUpload(item) { } } -export function sendFileMessage(rid, fileInfo, tmid, server, user) { +export function sendFileMessage( + rid: string, + fileInfo: IUpload, + tmid: string, + server: string, + user: IUser +): Promise { return new Promise(async (resolve, reject) => { try { const { id, token } = user; @@ -41,16 +51,18 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { const db = database.active; const uploadsCollection = db.get('uploads'); - let uploadRecord; + let uploadRecord: TUploadModel; try { uploadRecord = await uploadsCollection.find(fileInfo.path); } catch (error) { try { - await db.action(async () => { + await db.write(async () => { uploadRecord = await uploadsCollection.create(u => { u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema); Object.assign(u, fileInfo); - u.subscription.id = rid; + if (u.subscription) { + u.subscription.id = rid; + } }); }); } catch (e) { @@ -58,7 +70,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { } } - const formData = []; + const formData: IFileUpload[] = []; formData.push({ name: 'file', type: fileInfo.type, @@ -89,9 +101,9 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { uploadQueue[fileInfo.path] = FileUpload.fetch('POST', uploadUrl, headers, formData); - uploadQueue[fileInfo.path].uploadProgress(async (loaded, total) => { + uploadQueue[fileInfo.path].uploadProgress(async (loaded: number, total: number) => { try { - await db.action(async () => { + await db.write(async () => { await uploadRecord.update(u => { u.progress = Math.floor((loaded / total) * 100); }); @@ -105,7 +117,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { if (response.respInfo.status >= 200 && response.respInfo.status < 400) { // If response is all good... try { - await db.action(async () => { + await db.write(async () => { await uploadRecord.destroyPermanently(); }); resolve(response); @@ -114,7 +126,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { } } else { try { - await db.action(async () => { + await db.write(async () => { await uploadRecord.update(u => { u.error = true; }); @@ -132,7 +144,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { uploadQueue[fileInfo.path].catch(async error => { try { - await db.action(async () => { + await db.write(async () => { await uploadRecord.update(u => { u.error = true; }); diff --git a/app/utils/fileUpload/index.ios.ts b/app/utils/fileUpload/index.ios.ts index ae5cfabc2..96c2ae355 100644 --- a/app/utils/fileUpload/index.ios.ts +++ b/app/utils/fileUpload/index.ios.ts @@ -43,6 +43,7 @@ class FileUpload { upload.formData.append(item.name, { // @ts-ignore uri: item.uri, + // @ts-ignore type: item.type, name: item.filename }); diff --git a/app/utils/fileUpload/interfaces.ts b/app/utils/fileUpload/interfaces.ts index a3002f727..91b0d7d46 100644 --- a/app/utils/fileUpload/interfaces.ts +++ b/app/utils/fileUpload/interfaces.ts @@ -1,7 +1,7 @@ export interface IFileUpload { name: string; uri?: string; - type: string; - filename: string; - data: any; + type?: string; + filename?: string; + data?: any; } diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index af8b75b27..66707c134 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -230,6 +230,7 @@ class ShareView extends Component { }, thread?.id, server, + // @ts-ignore { id: user.id, token: user.token } ); }