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
This commit is contained in:
Alex Junior 2022-02-21 13:06:57 -03:00 committed by GitHub
parent a3a4b66810
commit 17c63c717b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 35 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -1,6 +1,6 @@
export interface IRocketChatRecord {
_id: string;
_updatedAt: Date;
_id?: string;
_updatedAt?: Date;
}
export type RocketChatRecordDeleted<T> = T &

View File

@ -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;

View File

@ -98,12 +98,12 @@ export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' |
_id: string;
id: string;
token: string;
createdAt: Date;
roles: string[];
type: string;
active: boolean;
name?: string;
createdAt?: Date;
roles?: string[];
type?: string;
active?: boolean;
username: string;
name?: string;
services?: IUserServices;
emails?: IUserEmail[];
status?: UserStatus;
@ -118,7 +118,6 @@ export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' |
oauth?: {
authorizedClients: string[];
};
_updatedAt: Date;
statusLivechat?: string;
e2e?: {
private_key: string;

View File

@ -1,18 +1,22 @@
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';
import log from '../../utils/log';
import { IUpload, IUser, TUploadModel } from '../../definitions';
import { IFileUpload } from '../../utils/fileUpload/interfaces';
const uploadQueue = {};
const uploadQueue: { [index: string]: StatefulPromise<FetchBlobResponse> } = {};
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<void> {
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<FetchBlobResponse | void> {
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);
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;
});

View File

@ -43,6 +43,7 @@ class FileUpload {
upload.formData.append(item.name, {
// @ts-ignore
uri: item.uri,
// @ts-ignore
type: item.type,
name: item.filename
});

View File

@ -1,7 +1,7 @@
export interface IFileUpload {
name: string;
uri?: string;
type: string;
filename: string;
data: any;
type?: string;
filename?: string;
data?: any;
}

View File

@ -230,6 +230,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
},
thread?.id,
server,
// @ts-ignore
{ id: user.id, token: user.token }
);
}