Use new server params for group log fetch

This commit is contained in:
Fedor Indutny 2022-02-04 13:42:20 -08:00 committed by GitHub
parent 0d19f9131b
commit 6de2710841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 17 deletions

View File

@ -2541,7 +2541,16 @@ export async function respondToGroupV2Migration({
logId: `getGroupLog/${logId}`,
publicParams,
secretParams,
request: (sender, options) => sender.getGroupLog(0, options),
request: (sender, options) =>
sender.getGroupLog(
{
startVersion: 0,
includeFirstState: true,
includeLastState: false,
maxSupportedChangeEpoch: SUPPORTED_CHANGE_EPOCH,
},
options
),
});
// Attempt to start with the first group state, only later processing future updates
@ -3276,6 +3285,7 @@ async function getGroupDelta({
});
const currentRevision = group.revision;
const isFirstFetch = !isNumber(currentRevision);
let revisionToFetch = isNumber(currentRevision)
? currentRevision + 1
: undefined;
@ -3284,7 +3294,15 @@ async function getGroupDelta({
const changes: Array<Proto.IGroupChanges> = [];
do {
// eslint-disable-next-line no-await-in-loop
response = await sender.getGroupLog(revisionToFetch, options);
response = await sender.getGroupLog(
{
startVersion: revisionToFetch,
includeFirstState: isFirstFetch,
includeLastState: false,
maxSupportedChangeEpoch: SUPPORTED_CHANGE_EPOCH,
},
options
);
changes.push(response.changes);
if (response.end) {
revisionToFetch = response.end + 1;

View File

@ -28,6 +28,7 @@ import type * as Attachment from '../types/Attachment';
import type { UUID, UUIDStringType } from '../types/UUID';
import type {
ChallengeType,
GetGroupLogOptionsType,
GroupCredentialsType,
GroupLogResponseType,
MultiRecipient200ResponseType,
@ -2134,10 +2135,10 @@ export default class MessageSender {
}
async getGroupLog(
startVersion: number | undefined,
options: Readonly<GroupCredentialsType>
options: GetGroupLogOptionsType,
credentials: GroupCredentialsType
): Promise<GroupLogResponseType> {
return this.server.getGroupLog(startVersion, options);
return this.server.getGroupLog(options, credentials);
}
async getGroupAvatar(key: string): Promise<Uint8Array> {

View File

@ -681,6 +681,12 @@ export type GroupCredentialsType = {
groupPublicParamsHex: string;
authCredentialPresentationHex: string;
};
export type GetGroupLogOptionsType = Readonly<{
startVersion: number | undefined;
includeFirstState: boolean;
includeLastState: boolean;
maxSupportedChangeEpoch: number;
}>;
export type GroupLogResponseType = {
currentRevision?: number;
start?: number;
@ -804,8 +810,8 @@ export type WebAPIType = {
options: GroupCredentialsType
) => Promise<Proto.GroupExternalCredential>;
getGroupLog: (
startVersion: number | undefined,
options: GroupCredentialsType
options: GetGroupLogOptionsType,
credentials: GroupCredentialsType
) => Promise<GroupLogResponseType>;
getIceServers: () => Promise<GetIceServersResultType>;
getKeysForIdentifier: (
@ -1071,7 +1077,7 @@ export function initialize({
let password = initialPassword;
const PARSE_RANGE_HEADER = /\/(\d+)$/;
const PARSE_GROUP_LOG_RANGE_HEADER =
/$versions (\d{1,10})-(\d{1,10})\/(d{1,10})/;
/^versions\s+(\d{1,10})-(\d{1,10})\/(\d{1,10})/;
let activeRegistration: ExplodePromiseResultType<void> | undefined;
@ -2603,14 +2609,21 @@ export function initialize({
}
async function getGroupLog(
startVersion: number | undefined,
options: GroupCredentialsType
options: GetGroupLogOptionsType,
credentials: GroupCredentialsType
): Promise<GroupLogResponseType> {
const basicAuth = generateGroupAuth(
options.groupPublicParamsHex,
options.authCredentialPresentationHex
credentials.groupPublicParamsHex,
credentials.authCredentialPresentationHex
);
const {
startVersion,
includeFirstState,
includeLastState,
maxSupportedChangeEpoch,
} = options;
// If we don't know starting revision - fetch it from the server
if (startVersion === undefined) {
const { data: joinedData } = await _ajax({
@ -2624,7 +2637,13 @@ export function initialize({
const { joinedAtVersion } = Proto.Member.decode(joinedData);
return getGroupLog(joinedAtVersion, options);
return getGroupLog(
{
...options,
startVersion: joinedAtVersion,
},
credentials
);
}
const withDetails = await _ajax({
@ -2634,7 +2653,11 @@ export function initialize({
host: storageUrl,
httpType: 'GET',
responseType: 'byteswithdetails',
urlParameters: `/${startVersion}`,
urlParameters:
`/${startVersion}?` +
`includeFirstState=${Boolean(includeFirstState)}&` +
`includeLastState=${Boolean(includeLastState)}&` +
`maxSupportedChangeEpoch=${Number(maxSupportedChangeEpoch)}`,
});
const { data, response } = withDetails;
const changes = Proto.GroupChanges.decode(data);
@ -2643,9 +2666,9 @@ export function initialize({
const range = response.headers.get('Content-Range');
const match = PARSE_GROUP_LOG_RANGE_HEADER.exec(range || '');
const start = match ? parseInt(match[0], 10) : undefined;
const end = match ? parseInt(match[1], 10) : undefined;
const currentRevision = match ? parseInt(match[2], 10) : undefined;
const start = match ? parseInt(match[1], 10) : undefined;
const end = match ? parseInt(match[2], 10) : undefined;
const currentRevision = match ? parseInt(match[3], 10) : undefined;
if (
match &&