2022-02-21 13:59:44 +00:00
|
|
|
import { ILastMessage } from '../../definitions';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import database from '../database';
|
2022-04-07 16:53:07 +00:00
|
|
|
import sdk from '../services/sdk';
|
2022-02-21 13:59:44 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-03-04 00:49:20 +00:00
|
|
|
async function load({ rid: roomId, lastOpen }: { rid: string; lastOpen?: Date }) {
|
2022-02-21 13:59:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
export function loadMissedMessages(args: { rid: string; lastOpen?: Date }): Promise<void> {
|
2022-02-21 13:59:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|