Skip to content
Merged
3 changes: 2 additions & 1 deletion app/containers/MessageBox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ interface IMessageBoxProps {
isFocused(): boolean;
user: {
id: string;
_id: string;
username: string;
token: string;
};
Expand Down Expand Up @@ -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;
6 changes: 3 additions & 3 deletions app/definitions/ILoggedUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions app/definitions/IRocketChatRecord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IRocketChatRecord {
_id: string;
_updatedAt: Date;
_id?: string;
_updatedAt?: Date;
}

export type RocketChatRecordDeleted<T> = T &
Expand Down
11 changes: 6 additions & 5 deletions app/definitions/IUpload.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 5 additions & 6 deletions app/definitions/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -118,7 +118,6 @@ export interface IUser extends IRocketChatRecord, Omit<ILoggedUser, 'username' |
oauth?: {
authorizedClients: string[];
};
_updatedAt: Date;
statusLivechat?: string;
e2e?: {
private_key: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
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 {
// Do nothing
}
try {
const db = database.active;
await db.action(async () => {
await db.write(async () => {
await item.destroyPermanently();
});
} catch (e) {
Expand All @@ -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;
Expand All @@ -41,24 +51,26 @@ 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) {
return log(e);
}
}

const formData = [];
const formData: IFileUpload[] = [];
formData.push({
name: 'file',
type: fileInfo.type,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
Expand All @@ -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;
});
Expand All @@ -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;
});
Expand Down
1 change: 1 addition & 0 deletions app/utils/fileUpload/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FileUpload {
upload.formData.append(item.name, {
// @ts-ignore
uri: item.uri,
// @ts-ignore
type: item.type,
name: item.filename
});
Expand Down
6 changes: 3 additions & 3 deletions app/utils/fileUpload/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface IFileUpload {
name: string;
uri?: string;
type: string;
filename: string;
data: any;
type?: string;
filename?: string;
data?: any;
}
1 change: 1 addition & 0 deletions app/views/ShareView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class ShareView extends Component<IShareViewProps, IShareViewState> {
},
thread?.id,
server,
// @ts-ignore
{ id: user.id, token: user.token }
);
}
Expand Down