From eff4f490f3c4514928098df2065db65243d8f46b Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 24 May 2021 13:17:15 +1000 Subject: [PATCH] do not fetch all messages for room if user was away for > 2 weeks --- ts/data/opengroups.ts | 4 ++++ ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts | 11 +++++++++-- ts/opengroup/opengroupV2/OpenGroupServerPoller.ts | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ts/data/opengroups.ts b/ts/data/opengroups.ts index 6a85d856a..05f74541c 100644 --- a/ts/data/opengroups.ts +++ b/ts/data/opengroups.ts @@ -15,6 +15,10 @@ export type OpenGroupV2Room = { * This value represents the rowId of the last message deleted. Not the id of the last message ID */ lastMessageDeletedServerID?: number; + /** + * This value is set with the current timestamp whenever we get new messages. + */ + lastFetchTimestamp?: number; token?: string; // currently, the token is on a per room basis }; diff --git a/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts b/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts index d398d811c..fee6871fa 100644 --- a/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts +++ b/ts/opengroup/opengroupV2/OpenGroupAPIV2CompactPoll.ts @@ -188,6 +188,7 @@ const getCompactPollRequest = async ( try { const { lastMessageFetchedServerID, + lastFetchTimestamp, lastMessageDeletedServerID, token, roomId, @@ -197,7 +198,13 @@ const getCompactPollRequest = async ( auth_token: token || '', }; roomRequestContent.from_deletion_server_id = lastMessageDeletedServerID; - roomRequestContent.from_message_server_id = lastMessageFetchedServerID; + if (Date.now() - (lastFetchTimestamp || 0) <= DURATION.DAYS * 14) { + roomRequestContent.from_message_server_id = lastMessageFetchedServerID; + } else { + window?.log?.info( + "We've been away for a long time... Only fetching last messages of room" + ); + } return roomRequestContent; } catch (e) { @@ -276,7 +283,7 @@ async function sendOpenGroupV2RequestCompactPoll( roomDetails.token = undefined; // we might need to retry doing the request here, but how to make sure we don't retry indefinetely? await saveV2OpenGroupRoom(roomDetails); - // do not await for that. We have a only one at a time logic on a per room basis + // we should not await for that. We have a only one at a time logic on a per room basis await getAuthToken({ serverUrl, roomId }); }) ); diff --git a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts index 9d9abb913..aa80246f4 100644 --- a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts +++ b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts @@ -410,6 +410,7 @@ const handleNewMessages = async ( if (roomInfos && roomInfos.lastMessageFetchedServerID !== maxNewMessageId) { roomInfos.lastMessageFetchedServerID = maxNewMessageId; + roomInfos.lastFetchTimestamp = Date.now(); await saveV2OpenGroupRoom(roomInfos); } } catch (e) {