From 1a4cf30d57b1b6c879f35aba0090d9d0199822f3 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 10 May 2021 10:09:27 +1000 Subject: [PATCH] speed up first load of room message by prefetching token --- ts/opengroup/opengroupV2/OpenGroupAPIV2.ts | 3 +++ .../opengroupV2/OpenGroupServerPoller.ts | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts index 87718dd63..abe7a5b48 100644 --- a/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupAPIV2.ts @@ -96,14 +96,17 @@ export async function sendApiV2Request( // this call will either return the token on the db, // or the promise currently fetching a new token for that same room // or fetch from the open group a new token for that room. + const token = await getAuthToken({ roomId: request.room, serverUrl: request.server, }); + if (!token) { window.log.error('Failed to get token for open group v2'); return null; } + headers.Authorization = token; const res = await sendViaOnion( destinationX25519Key, diff --git a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts index 03c558c90..9d9abb913 100644 --- a/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts +++ b/ts/opengroup/opengroupV2/OpenGroupServerPoller.ts @@ -14,13 +14,14 @@ import { import _ from 'lodash'; import { ConversationModel } from '../../models/conversation'; import { getMessageIdsFromServerIds, removeMessage } from '../../data/data'; -import { getV2OpenGroupRoom, saveV2OpenGroupRoom } from '../../data/opengroups'; +import { getV2OpenGroupRoom, OpenGroupV2Room, saveV2OpenGroupRoom } from '../../data/opengroups'; import { OpenGroupMessageV2 } from './OpenGroupMessageV2'; import { handleOpenGroupV2Message } from '../../receiver/receiver'; import { DAYS, MINUTES, SECONDS } from '../../session/utils/Number'; import autoBind from 'auto-bind'; import { sha256 } from '../../session/crypto'; import { fromBase64ToArrayBuffer } from '../../session/utils/String'; +import { getAuthToken } from './ApiAuth'; const pollForEverythingInterval = SECONDS * 4; const pollForRoomAvatarInterval = DAYS * 1; @@ -97,9 +98,7 @@ export class OpenGroupServerPoller { ); if (this.roomIdsToPoll.size) { - void this.compactPoll(); - void this.previewPerRoomPoll(); - void this.pollForAllMemberCount(); + void this.triggerPollAfterAdd(); } } @@ -118,9 +117,7 @@ export class OpenGroupServerPoller { this.roomIdsToPoll.add(room.roomId); // if we are not already polling right now, trigger a polling - void this.compactPoll(); - void this.previewPerRoomPoll(); - void this.pollForAllMemberCount(); + void this.triggerPollAfterAdd(); } public removeRoomFromPoll(room: OpenGroupRequestCommonType) { @@ -168,6 +165,21 @@ export class OpenGroupServerPoller { } } + private async triggerPollAfterAdd(room?: OpenGroupRequestCommonType) { + if (this.roomIdsToPoll.size) { + await Promise.all( + [...this.roomIdsToPoll].map(async r => { + // this call either get the token from db, or fetch a new one + await getAuthToken({ roomId: r, serverUrl: this.serverUrl }); + }) + ); + } + + await this.compactPoll(); + await this.previewPerRoomPoll(); + await this.pollForAllMemberCount(); + } + private shouldPoll() { if (this.wasStopped) { window.log.error('Serverpoller was stopped. CompactPoll should not happen');