From e58bdb8fe7d2a07e16763e45b82a2e2777d0d6df Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Tue, 22 Feb 2022 08:04:01 -0400 Subject: [PATCH] Chore: Migrate sendMessage to TS (#3712) Co-authored-by: Diego Mello --- app/definitions/ISubscription.ts | 2 +- app/definitions/IThread.ts | 4 +- .../{sendMessage.js => sendMessage.ts} | 72 ++++++++++--------- app/lib/methods/updateMessages.ts | 4 +- app/utils/room.ts | 4 +- app/views/ShareView/index.tsx | 3 +- 6 files changed, 49 insertions(+), 40 deletions(-) rename app/lib/methods/{sendMessage.js => sendMessage.ts} (76%) diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index b432c4836..9f88cf572 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -63,7 +63,7 @@ export interface ISubscription { ignored?: string[]; broadcast?: boolean; prid?: string; - draftMessage?: string; + draftMessage?: string | null; lastThreadSync?: Date; jitsiTimeout?: number; autoTranslate?: boolean; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index 670e10d52..be5dac1ba 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -39,7 +39,7 @@ export interface IThread { t?: MessageType; rid: string; _updatedAt?: Date; - ts?: Date; + ts?: string | Date; u?: IUserMessage; alias?: string; parseUrls?: boolean; @@ -67,7 +67,7 @@ export interface IThread { autoTranslate?: boolean; translations?: any; e2e?: string; - subscription: { id: string }; + subscription?: { id: string }; } export type TThreadModel = IThread & Model; diff --git a/app/lib/methods/sendMessage.js b/app/lib/methods/sendMessage.ts similarity index 76% rename from app/lib/methods/sendMessage.js rename to app/lib/methods/sendMessage.ts index 1a1ff9934..b4bd9bf9e 100644 --- a/app/lib/methods/sendMessage.js +++ b/app/lib/methods/sendMessage.ts @@ -1,4 +1,5 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import { Model } from '@nozbe/watermelondb'; import messagesStatus from '../../constants/messagesStatus'; import database from '../database'; @@ -6,12 +7,14 @@ import log from '../../utils/log'; import random from '../../utils/random'; import { Encryption } from '../encryption'; import { E2E_MESSAGE_TYPE, E2E_STATUS } from '../encryption/constants'; +import { IMessage, IUser, TMessageModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; -const changeMessageStatus = async (id, tmid, status, message) => { +const changeMessageStatus = async (id: string, status: number, tmid?: string, message?: IMessage) => { const db = database.active; const msgCollection = db.get('messages'); const threadMessagesCollection = db.get('thread_messages'); - const successBatch = []; + const successBatch: Model[] = []; const messageRecord = await msgCollection.find(id); successBatch.push( messageRecord.prepareUpdate(m => { @@ -37,7 +40,7 @@ const changeMessageStatus = async (id, tmid, status, message) => { } try { - await db.action(async () => { + await db.write(async () => { await db.batch(...successBatch); }); } catch (error) { @@ -45,48 +48,44 @@ const changeMessageStatus = async (id, tmid, status, message) => { } }; -export async function sendMessageCall(message) { +export async function sendMessageCall(message: any) { const { _id, tmid } = message; try { - const sdk = this.shareSDK || this.sdk; // RC 0.60.0 + // @ts-ignore const result = await sdk.post('chat.sendMessage', { message }); if (result.success) { - return changeMessageStatus(_id, tmid, messagesStatus.SENT, result.message); + // @ts-ignore + return changeMessageStatus(_id, messagesStatus.SENT, tmid, result.message); } } catch { // do nothing } - return changeMessageStatus(_id, tmid, messagesStatus.ERROR); + return changeMessageStatus(_id, messagesStatus.ERROR, tmid); } -export async function resendMessage(message, tmid) { +export async function resendMessage(message: TMessageModel, tmid?: string) { const db = database.active; try { - await db.action(async () => { + await db.write(async () => { await message.update(m => { m.status = messagesStatus.TEMP; }); }); - let m = { + const m = await Encryption.encryptMessage({ _id: message.id, - rid: message.subscription.id, - msg: message.msg - }; - if (tmid) { - m = { - ...m, - tmid - }; - } - m = await Encryption.encryptMessage(m); - await sendMessageCall.call(this, m); + rid: message.subscription ? message.subscription.id : '', + msg: message.msg, + ...(tmid && { tmid }) + } as IMessage); + + await sendMessageCall(m); } catch (e) { log(e); } } -export default async function (rid, msg, tmid, user, tshow) { +export default async function (rid: string, msg: string, tmid: string, user: IUser, tshow?: boolean) { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -94,19 +93,18 @@ export default async function (rid, msg, tmid, user, tshow) { const threadCollection = db.get('threads'); const threadMessagesCollection = db.get('thread_messages'); const messageId = random(17); - const batch = []; + const batch: Model[] = []; - let message = { + const message = await Encryption.encryptMessage({ _id: messageId, rid, msg, tmid, tshow - }; - message = await Encryption.encryptMessage(message); + } as IMessage); const messageDate = new Date(); - let tMessageRecord; + let tMessageRecord: TMessageModel; // If it's replying to a thread if (tmid) { @@ -116,7 +114,9 @@ export default async function (rid, msg, tmid, user, tshow) { batch.push( tMessageRecord.prepareUpdate(m => { m.tlm = messageDate; - m.tcount += 1; + if (m.tcount) { + m.tcount += 1; + } }) ); @@ -128,7 +128,9 @@ export default async function (rid, msg, tmid, user, tshow) { batch.push( threadCollection.prepareCreate(tm => { tm._raw = sanitizedRaw({ id: tmid }, threadCollection.schema); - tm.subscription.id = rid; + if (tm.subscription) { + tm.subscription.id = rid; + } tm.tmid = tmid; tm.msg = tMessageRecord.msg; tm.ts = tMessageRecord.ts; @@ -147,7 +149,9 @@ export default async function (rid, msg, tmid, user, tshow) { batch.push( threadMessagesCollection.prepareCreate(tm => { tm._raw = sanitizedRaw({ id: messageId }, threadMessagesCollection.schema); - tm.subscription.id = rid; + if (tm.subscription) { + tm.subscription.id = rid; + } tm.rid = tmid; tm.msg = msg; tm.ts = messageDate; @@ -173,7 +177,9 @@ export default async function (rid, msg, tmid, user, tshow) { batch.push( msgCollection.prepareCreate(m => { m._raw = sanitizedRaw({ id: messageId }, msgCollection.schema); - m.subscription.id = rid; + if (m.subscription) { + m.subscription.id = rid; + } m.msg = msg; m.ts = messageDate; m._updatedAt = messageDate; @@ -210,7 +216,7 @@ export default async function (rid, msg, tmid, user, tshow) { } try { - await db.action(async () => { + await db.write(async () => { await db.batch(...batch); }); } catch (e) { @@ -218,7 +224,7 @@ export default async function (rid, msg, tmid, user, tshow) { return; } - await sendMessageCall.call(this, message); + await sendMessageCall(message); } catch (e) { log(e); } diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index dd1f535cb..75294bc83 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -105,7 +105,9 @@ export default async function updateMessages({ threadCollection.prepareCreate( protectedFunction((t: TThreadModel) => { t._raw = sanitizedRaw({ id: thread._id }, threadCollection.schema); - t.subscription.id = sub.id; + if (t.subscription) { + t.subscription.id = sub.id; + } Object.assign(t, thread); }) ) diff --git a/app/utils/room.ts b/app/utils/room.ts index d39e40490..4bb8a29b9 100644 --- a/app/utils/room.ts +++ b/app/utils/room.ts @@ -22,7 +22,7 @@ export const capitalize = (s: string): string => { return s.charAt(0).toUpperCase() + s.slice(1); }; -export const formatDate = (date: Date): string => +export const formatDate = (date: string | Date): string => moment(date).calendar(null, { lastDay: `[${I18n.t('Yesterday')}]`, sameDay: 'LT', @@ -30,7 +30,7 @@ export const formatDate = (date: Date): string => sameElse: 'L' }); -export const formatDateThreads = (date: Date): string => +export const formatDateThreads = (date: string | Date): string => moment(date).calendar(null, { sameDay: 'LT', lastDay: `[${I18n.t('Yesterday')}] LT`, diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index 66707c134..15b8053c3 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -29,6 +29,7 @@ import Header from './Header'; import styles from './styles'; import { IAttachment } from './interfaces'; import { ISubscription } from '../../definitions/ISubscription'; +import { IUser } from '../../definitions'; interface IShareViewState { selected: IAttachment; @@ -240,7 +241,7 @@ class ShareView extends Component { // Send text message } else if (text.length) { - await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token }); + await RocketChat.sendMessage(room.rid, text, thread?.id, { id: user.id, token: user.token } as IUser); } } catch { // Do nothing