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:
parent
a3a4b66810
commit
17c63c717b
|
@ -82,6 +82,7 @@ interface IMessageBoxProps {
|
||||||
isFocused(): boolean;
|
isFocused(): boolean;
|
||||||
user: {
|
user: {
|
||||||
id: string;
|
id: string;
|
||||||
|
_id: string;
|
||||||
username: string;
|
username: string;
|
||||||
token: string;
|
token: string;
|
||||||
};
|
};
|
||||||
|
@ -1184,5 +1185,5 @@ const mapStateToProps = (state: any) => ({
|
||||||
const dispatchToProps = {
|
const dispatchToProps = {
|
||||||
typing: (rid: any, status: any) => userTypingAction(rid, status)
|
typing: (rid: any, status: any) => userTypingAction(rid, status)
|
||||||
};
|
};
|
||||||
// @ts-ignore
|
|
||||||
export default connect(mapStateToProps, dispatchToProps, null, { forwardRef: true })(withActionSheet(MessageBox)) as any;
|
export default connect(mapStateToProps, dispatchToProps, null, { forwardRef: true })(withActionSheet(MessageBox)) as any;
|
||||||
|
|
|
@ -8,10 +8,10 @@ export interface ILoggedUser {
|
||||||
language?: string;
|
language?: string;
|
||||||
status: string;
|
status: string;
|
||||||
statusText?: string;
|
statusText?: string;
|
||||||
roles: string[];
|
roles?: string[];
|
||||||
avatarETag?: string;
|
avatarETag?: string;
|
||||||
showMessageInMainThread: boolean;
|
showMessageInMainThread?: boolean;
|
||||||
isFromWebView: boolean;
|
isFromWebView?: boolean;
|
||||||
enableMessageParserEarlyAdoption?: boolean;
|
enableMessageParserEarlyAdoption?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export interface IRocketChatRecord {
|
export interface IRocketChatRecord {
|
||||||
_id: string;
|
_id?: string;
|
||||||
_updatedAt: Date;
|
_updatedAt?: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RocketChatRecordDeleted<T> = T &
|
export type RocketChatRecordDeleted<T> = T &
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
import Model from '@nozbe/watermelondb/Model';
|
import Model from '@nozbe/watermelondb/Model';
|
||||||
|
|
||||||
export interface IUpload {
|
export interface IUpload {
|
||||||
id: string;
|
id?: string;
|
||||||
path?: string;
|
rid?: string;
|
||||||
|
path: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
size: number;
|
size: number;
|
||||||
type?: string;
|
type?: string;
|
||||||
store?: string;
|
store?: string;
|
||||||
progress: number;
|
progress?: number;
|
||||||
error: boolean;
|
error?: boolean;
|
||||||
subscription: { id: string };
|
subscription?: { id: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TUploadModel = IUpload & Model;
|
export type TUploadModel = IUpload & Model;
|
||||||
|
|
|
@ -98,12 +98,12 @@ export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' |
|
||||||
_id: string;
|
_id: string;
|
||||||
id: string;
|
id: string;
|
||||||
token: string;
|
token: string;
|
||||||
createdAt: Date;
|
createdAt?: Date;
|
||||||
roles: string[];
|
roles?: string[];
|
||||||
type: string;
|
type?: string;
|
||||||
active: boolean;
|
active?: boolean;
|
||||||
name?: string;
|
|
||||||
username: string;
|
username: string;
|
||||||
|
name?: string;
|
||||||
services?: IUserServices;
|
services?: IUserServices;
|
||||||
emails?: IUserEmail[];
|
emails?: IUserEmail[];
|
||||||
status?: UserStatus;
|
status?: UserStatus;
|
||||||
|
@ -118,7 +118,6 @@ export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' |
|
||||||
oauth?: {
|
oauth?: {
|
||||||
authorizedClients: string[];
|
authorizedClients: string[];
|
||||||
};
|
};
|
||||||
_updatedAt: Date;
|
|
||||||
statusLivechat?: string;
|
statusLivechat?: string;
|
||||||
e2e?: {
|
e2e?: {
|
||||||
private_key: string;
|
private_key: string;
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
||||||
import { settings as RocketChatSettings } from '@rocket.chat/sdk';
|
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 FileUpload from '../../utils/fileUpload';
|
||||||
import database from '../database';
|
import database from '../database';
|
||||||
import log from '../../utils/log';
|
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];
|
return !!uploadQueue[path];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function cancelUpload(item) {
|
export async function cancelUpload(item: TUploadModel): Promise<void> {
|
||||||
if (uploadQueue[item.path]) {
|
if (!isEmpty(uploadQueue[item.path])) {
|
||||||
try {
|
try {
|
||||||
await uploadQueue[item.path].cancel();
|
await uploadQueue[item.path].cancel();
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -20,7 +24,7 @@ export async function cancelUpload(item) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await item.destroyPermanently();
|
await item.destroyPermanently();
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} 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) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const { id, token } = user;
|
const { id, token } = user;
|
||||||
|
@ -41,16 +51,18 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
||||||
|
|
||||||
const db = database.active;
|
const db = database.active;
|
||||||
const uploadsCollection = db.get('uploads');
|
const uploadsCollection = db.get('uploads');
|
||||||
let uploadRecord;
|
let uploadRecord: TUploadModel;
|
||||||
try {
|
try {
|
||||||
uploadRecord = await uploadsCollection.find(fileInfo.path);
|
uploadRecord = await uploadsCollection.find(fileInfo.path);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
uploadRecord = await uploadsCollection.create(u => {
|
uploadRecord = await uploadsCollection.create(u => {
|
||||||
u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema);
|
u._raw = sanitizedRaw({ id: fileInfo.path }, uploadsCollection.schema);
|
||||||
Object.assign(u, fileInfo);
|
Object.assign(u, fileInfo);
|
||||||
u.subscription.id = rid;
|
if (u.subscription) {
|
||||||
|
u.subscription.id = rid;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -58,7 +70,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const formData = [];
|
const formData: IFileUpload[] = [];
|
||||||
formData.push({
|
formData.push({
|
||||||
name: 'file',
|
name: 'file',
|
||||||
type: fileInfo.type,
|
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] = FileUpload.fetch('POST', uploadUrl, headers, formData);
|
||||||
|
|
||||||
uploadQueue[fileInfo.path].uploadProgress(async (loaded, total) => {
|
uploadQueue[fileInfo.path].uploadProgress(async (loaded: number, total: number) => {
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await uploadRecord.update(u => {
|
await uploadRecord.update(u => {
|
||||||
u.progress = Math.floor((loaded / total) * 100);
|
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.respInfo.status >= 200 && response.respInfo.status < 400) {
|
||||||
// If response is all good...
|
// If response is all good...
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await uploadRecord.destroyPermanently();
|
await uploadRecord.destroyPermanently();
|
||||||
});
|
});
|
||||||
resolve(response);
|
resolve(response);
|
||||||
|
@ -114,7 +126,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await uploadRecord.update(u => {
|
await uploadRecord.update(u => {
|
||||||
u.error = true;
|
u.error = true;
|
||||||
});
|
});
|
||||||
|
@ -132,7 +144,7 @@ export function sendFileMessage(rid, fileInfo, tmid, server, user) {
|
||||||
|
|
||||||
uploadQueue[fileInfo.path].catch(async error => {
|
uploadQueue[fileInfo.path].catch(async error => {
|
||||||
try {
|
try {
|
||||||
await db.action(async () => {
|
await db.write(async () => {
|
||||||
await uploadRecord.update(u => {
|
await uploadRecord.update(u => {
|
||||||
u.error = true;
|
u.error = true;
|
||||||
});
|
});
|
|
@ -43,6 +43,7 @@ class FileUpload {
|
||||||
upload.formData.append(item.name, {
|
upload.formData.append(item.name, {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
uri: item.uri,
|
uri: item.uri,
|
||||||
|
// @ts-ignore
|
||||||
type: item.type,
|
type: item.type,
|
||||||
name: item.filename
|
name: item.filename
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
export interface IFileUpload {
|
export interface IFileUpload {
|
||||||
name: string;
|
name: string;
|
||||||
uri?: string;
|
uri?: string;
|
||||||
type: string;
|
type?: string;
|
||||||
filename: string;
|
filename?: string;
|
||||||
data: any;
|
data?: any;
|
||||||
}
|
}
|
||||||
|
|
|
@ -230,6 +230,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
|
||||||
},
|
},
|
||||||
thread?.id,
|
thread?.id,
|
||||||
server,
|
server,
|
||||||
|
// @ts-ignore
|
||||||
{ id: user.id, token: user.token }
|
{ id: user.id, token: user.token }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue