forked from RocketChat/Rocket.Chat.ReactNative
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added pagination on sync.messages (RocketChat#5992)
* chore: added pagination on sync.mesages * chore: improved function names * fix: lint and added new condition to paginate * fix: import * improvements from review --------- Co-authored-by: Diego Mello <[email protected]>
- Loading branch information
1 parent
1b2f310
commit 395a557
Showing
1 changed file
with
110 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,129 @@ | ||
import { ILastMessage } from '../../definitions'; | ||
import { compareServerVersion } from './helpers'; | ||
import updateMessages from './updateMessages'; | ||
import log from './helpers/log'; | ||
import database from '../database'; | ||
import sdk from '../services/sdk'; | ||
import updateMessages from './updateMessages'; | ||
import { store } from '../store/auxStore'; | ||
import { getSubscriptionByRoomId } from '../database/services/Subscription'; | ||
|
||
const getLastUpdate = async (rid: string) => { | ||
const count = 50; | ||
|
||
const syncMessages = async ({ roomId, next, type }: { roomId: string; next: number; type: 'UPDATED' | 'DELETED' }) => { | ||
// @ts-ignore // this method dont have type | ||
const { result } = await sdk.get('chat.syncMessages', { roomId, next, count, type }); | ||
return result; | ||
}; | ||
|
||
const getSyncMessagesFromCursor = async ( | ||
roomId: string, | ||
lastOpen?: number, | ||
updatedNext?: number | null, | ||
deletedNext?: number | null | ||
) => { | ||
try { | ||
const db = database.active; | ||
const subsCollection = db.get('subscriptions'); | ||
const sub = await subsCollection.find(rid); | ||
return sub.lastOpen?.toISOString(); | ||
} catch (e) { | ||
// Do nothing | ||
const promises = []; | ||
|
||
if (lastOpen && !updatedNext && !deletedNext) { | ||
promises.push(syncMessages({ roomId, next: lastOpen, type: 'UPDATED' })); | ||
promises.push(syncMessages({ roomId, next: lastOpen, type: 'DELETED' })); | ||
} | ||
if (updatedNext) { | ||
promises.push(syncMessages({ roomId, next: updatedNext, type: 'UPDATED' })); | ||
} | ||
if (deletedNext) { | ||
promises.push(syncMessages({ roomId, next: deletedNext, type: 'DELETED' })); | ||
} | ||
|
||
const [updatedMessages, deletedMessages] = await Promise.all(promises); | ||
return { | ||
deleted: deletedMessages?.deleted ?? [], | ||
deletedNext: deletedMessages?.cursor.next, | ||
updated: updatedMessages?.updated ?? [], | ||
updatedNext: updatedMessages?.cursor.next | ||
}; | ||
} catch (error) { | ||
log(error); | ||
} | ||
}; | ||
|
||
const getLastUpdate = async (rid: string) => { | ||
const sub = await getSubscriptionByRoomId(rid); | ||
if (!sub) { | ||
return null; | ||
} | ||
return null; | ||
return sub.lastOpen; | ||
}; | ||
|
||
async function load({ rid: roomId, lastOpen }: { rid: string; lastOpen?: Date }) { | ||
let lastUpdate; | ||
async function load({ | ||
rid: roomId, | ||
lastOpen, | ||
updatedNext, | ||
deletedNext | ||
}: { | ||
rid: string; | ||
lastOpen?: Date; | ||
updatedNext?: number | null; | ||
deletedNext?: number | null; | ||
}) { | ||
const { version: serverVersion } = store.getState().server; | ||
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '7.1.0')) { | ||
let lastOpenTimestamp; | ||
if (lastOpen) { | ||
lastOpenTimestamp = new Date(lastOpen).getTime(); | ||
} else { | ||
const lastUpdate = await getLastUpdate(roomId); | ||
lastOpenTimestamp = lastUpdate?.getTime(); | ||
} | ||
const result = await getSyncMessagesFromCursor(roomId, lastOpenTimestamp, updatedNext, deletedNext); | ||
return result; | ||
} | ||
|
||
let lastOpenISOString; | ||
if (lastOpen) { | ||
lastUpdate = new Date(lastOpen).toISOString(); | ||
lastOpenISOString = new Date(lastOpen).toISOString(); | ||
} else { | ||
lastUpdate = await getLastUpdate(roomId); | ||
const lastUpdate = await getLastUpdate(roomId); | ||
lastOpenISOString = lastUpdate?.toISOString(); | ||
} | ||
// RC 0.60.0 | ||
// @ts-ignore // this method dont have type | ||
const { result } = await sdk.get('chat.syncMessages', { roomId, lastUpdate }); | ||
const { result } = await sdk.get('chat.syncMessages', { roomId, lastUpdate: lastOpenISOString }); | ||
return result; | ||
} | ||
|
||
export function loadMissedMessages(args: { rid: string; lastOpen?: Date }): 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 }); | ||
export async function loadMissedMessages(args: { | ||
rid: string; | ||
lastOpen?: Date; | ||
updatedNext?: number | null; | ||
deletedNext?: number | null; | ||
}): Promise<void> { | ||
try { | ||
const data = await load({ | ||
rid: args.rid, | ||
lastOpen: args.lastOpen, | ||
updatedNext: args.updatedNext, | ||
deletedNext: args.deletedNext | ||
}); | ||
if (data) { | ||
const { | ||
updated, | ||
updatedNext, | ||
deleted, | ||
deletedNext | ||
}: { updated: ILastMessage[]; deleted: ILastMessage[]; updatedNext: number | null; deletedNext: number | null } = data; | ||
// @ts-ignore // TODO: remove loaderItem obligatoriness | ||
await updateMessages({ rid: args.rid, update: updated, remove: deleted }); | ||
|
||
if (deletedNext || updatedNext) { | ||
loadMissedMessages({ | ||
rid: args.rid, | ||
lastOpen: args.lastOpen, | ||
updatedNext, | ||
deletedNext | ||
}); | ||
} | ||
resolve(); | ||
} catch (e) { | ||
log(e); | ||
reject(e); | ||
} | ||
}); | ||
} catch (e) { | ||
log(e); | ||
} | ||
} |