speed up first load of room message by prefetching token

This commit is contained in:
Audric Ackermann 2021-05-10 10:09:27 +10:00
parent b574fd731d
commit 1a4cf30d57
No known key found for this signature in database
GPG key ID: 999F434D76324AD4
2 changed files with 22 additions and 7 deletions

View file

@ -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,

View file

@ -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');