Chore: Migrate loadMissedMessages to typescript (#3704)
* chore: migrate loadMissedMessages to typescript * remove loaderItem * remove this from functions
This commit is contained in:
parent
5e39cc0ba5
commit
bdd1ce4abf
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
|
@ -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<void> {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue