From e0b55bdd751e61e9b005cf220fba2d8dafe398c5 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Wed, 2 Feb 2022 17:55:25 -0300 Subject: [PATCH 1/7] chore: start the migration --- .../methods/{sendFileMessage.js => sendFileMessage.ts} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename app/lib/methods/{sendFileMessage.js => sendFileMessage.ts} (93%) diff --git a/app/lib/methods/sendFileMessage.js b/app/lib/methods/sendFileMessage.ts similarity index 93% rename from app/lib/methods/sendFileMessage.js rename to app/lib/methods/sendFileMessage.ts index 35d2815ca26..379dc001df2 100644 --- a/app/lib/methods/sendFileMessage.js +++ b/app/lib/methods/sendFileMessage.ts @@ -5,13 +5,13 @@ import FileUpload from '../../utils/fileUpload'; import database from '../database'; import log from '../../utils/log'; -const uploadQueue = {}; +const uploadQueue: { [index: string]: any } = {}; -export function isUploadActive(path) { +export function isUploadActive(path: string): boolean { return !!uploadQueue[path]; } -export async function cancelUpload(item) { +export async function cancelUpload(item: { path: string }): Promise { if (uploadQueue[item.path]) { try { await uploadQueue[item.path].cancel(); @@ -20,7 +20,7 @@ export async function cancelUpload(item) { } try { const db = database.active; - await db.action(async () => { + await db.write(async () => { await item.destroyPermanently(); }); } catch (e) { From bb0f4b20e6e6af9cb9a254de49c0f13c23a1c308 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Thu, 3 Feb 2022 01:53:10 -0300 Subject: [PATCH 2/7] chore: update sendFileMessage to ts --- app/definitions/IUpload.ts | 11 ++++++----- app/definitions/IUser.ts | 5 +++-- app/lib/methods/sendFileMessage.ts | 30 +++++++++++++++++------------- app/utils/fileUpload/index.ios.ts | 1 + app/utils/fileUpload/interfaces.ts | 6 +++--- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/definitions/IUpload.ts b/app/definitions/IUpload.ts index 6ff03c519f7..05955513058 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 012ef808714..9072effaae7 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -1,9 +1,10 @@ import Model from '@nozbe/watermelondb/Model'; export interface IUser { - _id: string; + id: string; + token: string; name?: string; - username: string; + username?: string; avatarETag?: string; } diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index 379dc001df2..bb187398a3a 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -4,6 +4,8 @@ import { settings as RocketChatSettings } from '@rocket.chat/sdk'; import FileUpload from '../../utils/fileUpload'; import database from '../database'; import log from '../../utils/log'; +import { IUpload, IUser, TUploadModel } from '../../definitions'; +import { IFileUpload } from '../../utils/fileUpload/interfaces'; const uploadQueue: { [index: string]: any } = {}; @@ -11,7 +13,7 @@ export function isUploadActive(path: string): boolean { return !!uploadQueue[path]; } -export async function cancelUpload(item: { path: string }): Promise { +export async function cancelUpload(item: TUploadModel): Promise { if (uploadQueue[item.path]) { try { await uploadQueue[item.path].cancel(); @@ -30,7 +32,7 @@ export async function cancelUpload(item: { path: string }): Promise { } } -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 +43,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 +62,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { } } - const formData = []; + const formData: IFileUpload[] = []; formData.push({ name: 'file', type: fileInfo.type, @@ -89,9 +93,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); }); @@ -101,11 +105,11 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { } }); - uploadQueue[fileInfo.path].then(async response => { + uploadQueue[fileInfo.path].then(async (response: { respInfo: { status: number } } & Promise) => { 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 +118,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; }); @@ -130,9 +134,9 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) { } }); - uploadQueue[fileInfo.path].catch(async error => { + uploadQueue[fileInfo.path].catch(async (error: unknown) => { 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 ae5cfabc228..96c2ae35510 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 a3002f72703..91b0d7d46f5 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; } From 46053c27e524cf2375728f2760526d608dc85219 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Fri, 4 Feb 2022 01:54:13 -0300 Subject: [PATCH 3/7] chore: removing an `any` from uploadQueue --- app/lib/methods/sendFileMessage.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index bb187398a3a..933c9257ca7 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -1,5 +1,6 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { settings as RocketChatSettings } from '@rocket.chat/sdk'; +import { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob'; import FileUpload from '../../utils/fileUpload'; import database from '../database'; @@ -7,14 +8,14 @@ import log from '../../utils/log'; import { IUpload, IUser, TUploadModel } from '../../definitions'; import { IFileUpload } from '../../utils/fileUpload/interfaces'; -const uploadQueue: { [index: string]: any } = {}; +const uploadQueue: { [index: string]: StatefulPromise } = {}; export function isUploadActive(path: string): boolean { return !!uploadQueue[path]; } export async function cancelUpload(item: TUploadModel): Promise { - if (uploadQueue[item.path]) { + if (await uploadQueue[item.path]) { try { await uploadQueue[item.path].cancel(); } catch { @@ -32,7 +33,13 @@ export async function cancelUpload(item: TUploadModel): Promise { } } -export function sendFileMessage(rid: string, fileInfo: IUpload, tmid: string, server: string, user: IUser): Promise { +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; @@ -105,7 +112,7 @@ export function sendFileMessage(rid: string, fileInfo: IUpload, tmid: string, se } }); - uploadQueue[fileInfo.path].then(async (response: { respInfo: { status: number } } & Promise) => { + uploadQueue[fileInfo.path].then(async response => { if (response.respInfo.status >= 200 && response.respInfo.status < 400) { // If response is all good... try { From b7e098753a72ea26ec4106b274ff797e32209523 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Sat, 5 Feb 2022 01:58:02 -0300 Subject: [PATCH 4/7] chore: minor tweak --- app/lib/methods/sendFileMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index 933c9257ca7..560782b8b63 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -39,7 +39,7 @@ export function sendFileMessage( tmid: string, server: string, user: IUser -): Promise { +): Promise { return new Promise(async (resolve, reject) => { try { const { id, token } = user; From 74e5388fb6517ce7f574ae6d2e413ff0b63fcb67 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Sat, 5 Feb 2022 20:56:51 -0300 Subject: [PATCH 5/7] chore: minor tweak --- app/lib/methods/sendFileMessage.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index 560782b8b63..99e8c17755a 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -1,6 +1,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { settings as RocketChatSettings } from '@rocket.chat/sdk'; import { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob'; +import isEmpty from 'lodash/isEmpty'; import FileUpload from '../../utils/fileUpload'; import database from '../database'; @@ -15,7 +16,7 @@ export function isUploadActive(path: string): boolean { } export async function cancelUpload(item: TUploadModel): Promise { - if (await uploadQueue[item.path]) { + if (!isEmpty(uploadQueue[item.path])) { try { await uploadQueue[item.path].cancel(); } catch { From 08f84d3fba52b9aa5ccdd3c530021c3cf2db8ddd Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Mon, 14 Feb 2022 20:29:18 -0300 Subject: [PATCH 6/7] chore: minor tweaks after merge with developer --- app/definitions/IRocketChatRecord.ts | 4 ++-- app/definitions/IUser.ts | 12 ++++++------ app/lib/methods/sendFileMessage.ts | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/definitions/IRocketChatRecord.ts b/app/definitions/IRocketChatRecord.ts index bed6d114761..8a933059a19 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/IUser.ts b/app/definitions/IUser.ts index 1b5d244c818..b111b7ba187 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -94,11 +94,12 @@ export interface IUserSettings { } export interface IUser extends IRocketChatRecord { - _id: string; - createdAt: Date; - roles: string[]; - type: string; - active: boolean; + id: string; + token: string; + createdAt?: Date; + roles?: string[]; + type?: string; + active?: boolean; username?: string; name?: string; services?: IUserServices; @@ -115,7 +116,6 @@ export interface IUser extends IRocketChatRecord { oauth?: { authorizedClients: string[]; }; - _updatedAt: Date; statusLivechat?: string; e2e?: { private_key: string; diff --git a/app/lib/methods/sendFileMessage.ts b/app/lib/methods/sendFileMessage.ts index 99e8c17755a..cc11606b699 100644 --- a/app/lib/methods/sendFileMessage.ts +++ b/app/lib/methods/sendFileMessage.ts @@ -142,7 +142,7 @@ export function sendFileMessage( } }); - uploadQueue[fileInfo.path].catch(async (error: unknown) => { + uploadQueue[fileInfo.path].catch(async error => { try { await db.write(async () => { await uploadRecord.update(u => { From e5c5cefbc5c81a029b81cd780d7f3dfa1d8a187b Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Mon, 21 Feb 2022 12:23:44 -0300 Subject: [PATCH 7/7] chore: minor tweak after merge develop into current --- app/containers/MessageBox/index.tsx | 3 ++- app/definitions/ILoggedUser.ts | 6 +++--- app/definitions/IUser.ts | 2 +- app/views/ShareView/index.tsx | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/containers/MessageBox/index.tsx b/app/containers/MessageBox/index.tsx index 68fd0ed2b0f..3c924df6f00 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 380bcd86c0b..ecd9e2674df 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/IUser.ts b/app/definitions/IUser.ts index 09a63e5b999..461b52fb48c 100644 --- a/app/definitions/IUser.ts +++ b/app/definitions/IUser.ts @@ -102,7 +102,7 @@ export interface IUser extends IRocketChatRecord, Omit { }, thread?.id, server, + // @ts-ignore { id: user.id, token: user.token } ); }