diff --git a/app/lib/methods/loadMissedMessages.js b/app/lib/methods/loadMissedMessages.js deleted file mode 100644 index 7b2e8c978..000000000 --- a/app/lib/methods/loadMissedMessages.js +++ /dev/null @@ -1,44 +0,0 @@ -import database from '../database'; -import log from '../../utils/log'; -import updateMessages from './updateMessages'; - -const getLastUpdate = async rid => { - try { - const db = database.active; - const subsCollection = db.get('subscriptions'); - const sub = await subsCollection.find(rid); - return sub.lastOpen.toISOString(); - } catch (e) { - // Do nothing - } - return null; -}; - -async function load({ rid: roomId, lastOpen }) { - let lastUpdate; - if (lastOpen) { - lastUpdate = new Date(lastOpen).toISOString(); - } else { - lastUpdate = await getLastUpdate(roomId); - } - // RC 0.60.0 - const { result } = await this.sdk.get('chat.syncMessages', { roomId, lastUpdate }); - return result; -} - -export default function loadMissedMessages(args) { - return new Promise(async (resolve, reject) => { - try { - const data = await load.call(this, { rid: args.rid, lastOpen: args.lastOpen }); - - if (data) { - const { updated, deleted } = data; - await updateMessages({ rid: args.rid, update: updated, remove: deleted }); - } - resolve(); - } catch (e) { - log(e); - reject(e); - } - }); -} diff --git a/app/lib/methods/loadMissedMessages.ts b/app/lib/methods/loadMissedMessages.ts new file mode 100644 index 000000000..e4578a867 --- /dev/null +++ b/app/lib/methods/loadMissedMessages.ts @@ -0,0 +1,47 @@ +import { ILastMessage } from '../../definitions'; +import log from '../../utils/log'; +import database from '../database'; +import sdk from '../rocketchat/services/sdk'; +import updateMessages from './updateMessages'; + +const getLastUpdate = async (rid: string) => { + try { + const db = database.active; + const subsCollection = db.get('subscriptions'); + const sub = await subsCollection.find(rid); + return sub.lastOpen?.toISOString(); + } catch (e) { + // Do nothing + } + return null; +}; + +async function load({ rid: roomId, lastOpen }: { rid: string; lastOpen: string }) { + let lastUpdate; + if (lastOpen) { + lastUpdate = new Date(lastOpen).toISOString(); + } else { + lastUpdate = await getLastUpdate(roomId); + } + // RC 0.60.0 + // @ts-ignore // this method dont have type + const { result } = await sdk.get('chat.syncMessages', { roomId, lastUpdate }); + return result; +} + +export default function loadMissedMessages(args: { rid: string; lastOpen: string }): Promise { + return new Promise(async (resolve, reject) => { + try { + const data = await load({ rid: args.rid, lastOpen: args.lastOpen }); + if (data) { + const { updated, deleted }: { updated: ILastMessage[]; deleted: ILastMessage[] } = data; + // @ts-ignore // TODO: remove loaderItem obligatoriness + await updateMessages({ rid: args.rid, update: updated, remove: deleted }); + } + resolve(); + } catch (e) { + log(e); + reject(e); + } + }); +}