From 5f92a509d1251546f0bf2ff5863be659cbd0d612 Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Wed, 2 Mar 2022 11:41:15 -0400 Subject: [PATCH] Chore: Migrate loadThreadMessages to TS (#3718) * Migrate loadThreadMessages to TypeScript * Update interfaces --- app/lib/methods/loadThreadMessages.js | 76 ----------------------- app/lib/methods/loadThreadMessages.ts | 87 +++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 76 deletions(-) delete mode 100644 app/lib/methods/loadThreadMessages.js create mode 100644 app/lib/methods/loadThreadMessages.ts diff --git a/app/lib/methods/loadThreadMessages.js b/app/lib/methods/loadThreadMessages.js deleted file mode 100644 index 1a17f564de..0000000000 --- a/app/lib/methods/loadThreadMessages.js +++ /dev/null @@ -1,76 +0,0 @@ -import { Q } from '@nozbe/watermelondb'; -import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; -import EJSON from 'ejson'; - -import database from '../database'; -import log from '../../utils/log'; -import { Encryption } from '../encryption'; -import protectedFunction from './helpers/protectedFunction'; -import buildMessage from './helpers/buildMessage'; - -async function load({ tmid }) { - try { - // RC 1.0 - const result = await this.methodCallWrapper('getThreadMessages', { tmid }); - if (!result) { - return []; - } - return EJSON.fromJSONValue(result); - } catch (error) { - console.log(error); - return []; - } -} - -export default function loadThreadMessages({ tmid, rid }) { - return new Promise(async (resolve, reject) => { - try { - let data = await load.call(this, { tmid }); - if (data && data.length) { - try { - data = data.filter(m => m.tmid).map(m => buildMessage(m)); - data = await Encryption.decryptMessages(data); - const db = database.active; - const threadMessagesCollection = db.get('thread_messages'); - const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch(); - let threadMessagesToCreate = data.filter(i1 => !allThreadMessagesRecords.find(i2 => i1._id === i2.id)); - let threadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => data.find(i2 => i1.id === i2._id)); - - threadMessagesToCreate = threadMessagesToCreate.map(threadMessage => - threadMessagesCollection.prepareCreate( - protectedFunction(tm => { - tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); - Object.assign(tm, threadMessage); - tm.subscription.id = rid; - tm.rid = threadMessage.tmid; - delete threadMessage.tmid; - }) - ) - ); - - threadMessagesToUpdate = threadMessagesToUpdate.map(threadMessage => { - const newThreadMessage = data.find(t => t._id === threadMessage.id); - return threadMessage.prepareUpdate( - protectedFunction(tm => { - Object.assign(tm, newThreadMessage); - tm.rid = threadMessage.tmid; - delete threadMessage.tmid; - }) - ); - }); - - await db.action(async () => { - await db.batch(...threadMessagesToCreate, ...threadMessagesToUpdate); - }); - } catch (e) { - log(e); - } - return resolve(data); - } else { - return resolve([]); - } - } catch (e) { - reject(e); - } - }); -} diff --git a/app/lib/methods/loadThreadMessages.ts b/app/lib/methods/loadThreadMessages.ts new file mode 100644 index 0000000000..e9390668eb --- /dev/null +++ b/app/lib/methods/loadThreadMessages.ts @@ -0,0 +1,87 @@ +import { Q } from '@nozbe/watermelondb'; +import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; +import EJSON from 'ejson'; + +import database from '../database'; +import log from '../../utils/log'; +import { Encryption } from '../encryption'; +import protectedFunction from './helpers/protectedFunction'; +import buildMessage from './helpers/buildMessage'; +import { TThreadMessageModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; + +async function load({ tmid }: { tmid: string }) { + try { + // RC 1.0 + const result = await sdk.methodCallWrapper('getThreadMessages', { tmid }); + if (!result) { + return []; + } + return EJSON.fromJSONValue(result); + } catch (error) { + console.log(error); + return []; + } +} + +export default function loadThreadMessages({ tmid, rid }: { tmid: string; rid: string }) { + return new Promise(async (resolve, reject) => { + try { + let data = await load({ tmid }); + if (data && data.length) { + try { + data = data.filter((m: TThreadMessageModel) => m.tmid).map((m: TThreadMessageModel) => buildMessage(m)); + data = await Encryption.decryptMessages(data); + const db = database.active; + const threadMessagesCollection = db.get('thread_messages'); + const allThreadMessagesRecords = await threadMessagesCollection.query(Q.where('rid', tmid)).fetch(); + const filterThreadMessagesToCreate = data.filter( + (i1: TThreadMessageModel) => !allThreadMessagesRecords.find(i2 => i1._id === i2.id) + ); + const filterThreadMessagesToUpdate = allThreadMessagesRecords.filter(i1 => + data.find((i2: TThreadMessageModel) => i1.id === i2._id) + ); + + const threadMessagesToCreate = filterThreadMessagesToCreate.map((threadMessage: TThreadMessageModel) => + threadMessagesCollection.prepareCreate( + protectedFunction((tm: TThreadMessageModel) => { + tm._raw = sanitizedRaw({ id: threadMessage._id }, threadMessagesCollection.schema); + Object.assign(tm, threadMessage); + if (tm.subscription) { + tm.subscription.id = rid; + } + if (threadMessage.tmid) { + tm.rid = threadMessage.tmid; + } + delete threadMessage.tmid; + }) + ) + ); + + const threadMessagesToUpdate = filterThreadMessagesToUpdate.map(threadMessage => { + const newThreadMessage = data.find((t: TThreadMessageModel) => t._id === threadMessage.id); + return threadMessage.prepareUpdate( + protectedFunction((tm: TThreadMessageModel) => { + Object.assign(tm, newThreadMessage); + if (threadMessage.tmid) { + tm.rid = threadMessage.tmid; + } + delete threadMessage.tmid; + }) + ); + }); + + await db.write(async () => { + await db.batch(...threadMessagesToCreate, ...threadMessagesToUpdate); + }); + } catch (e) { + log(e); + } + return resolve(data); + } + return resolve(); + } catch (e) { + reject(e); + } + }); +}