Refactor contact sync processing

This commit is contained in:
Fedor Indutny 2022-08-24 22:04:42 -07:00 committed by GitHub
parent 76e73f63dc
commit 7ce4beb270
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 229 additions and 156 deletions

View file

@ -30,10 +30,12 @@ export type ModifiedGroupDetails = MessageWithAvatar<Proto.GroupDetails>;
export type ModifiedContactDetails = MessageWithAvatar<Proto.ContactDetails>;
class ParserBase<
abstract class ParserBase<
Message extends OptionalAvatar,
Decoder extends DecoderBase<Message>
> {
Decoder extends DecoderBase<Message>,
Result
> implements Iterable<Result>
{
protected readonly reader: protobuf.Reader;
constructor(bytes: Uint8Array, private readonly decoder: Decoder) {
@ -83,17 +85,28 @@ class ParserBase<
return undefined;
}
}
public abstract next(): Result | undefined;
*[Symbol.iterator](): Iterator<Result> {
let result = this.next();
while (result !== undefined) {
yield result;
result = this.next();
}
}
}
export class GroupBuffer extends ParserBase<
Proto.GroupDetails,
typeof Proto.GroupDetails
typeof Proto.GroupDetails,
ModifiedGroupDetails
> {
constructor(arrayBuffer: Uint8Array) {
super(arrayBuffer, Proto.GroupDetails);
}
public next(): ModifiedGroupDetails | undefined {
public override next(): ModifiedGroupDetails | undefined {
const proto = this.decodeDelimited();
if (!proto) {
return undefined;
@ -120,13 +133,14 @@ export class GroupBuffer extends ParserBase<
export class ContactBuffer extends ParserBase<
Proto.ContactDetails,
typeof Proto.ContactDetails
typeof Proto.ContactDetails,
ModifiedContactDetails
> {
constructor(arrayBuffer: Uint8Array) {
super(arrayBuffer, Proto.ContactDetails);
}
public next(): ModifiedContactDetails | undefined {
public override next(): ModifiedContactDetails | undefined {
const proto = this.decodeDelimited();
if (!proto) {
return undefined;

View file

@ -104,7 +104,6 @@ import {
StickerPackEvent,
ReadSyncEvent,
ViewSyncEvent,
ContactEvent,
ContactSyncEvent,
GroupEvent,
GroupSyncEvent,
@ -561,11 +560,6 @@ export default class MessageReceiver
handler: (ev: ViewSyncEvent) => void
): void;
public override addEventListener(
name: 'contact',
handler: (ev: ContactEvent) => void
): void;
public override addEventListener(
name: 'contactSync',
handler: (ev: ContactSyncEvent) => void
@ -2748,6 +2742,9 @@ export default class MessageReceiver
return this.handleSentMessage(envelope, sentMessage);
}
if (syncMessage.contacts) {
// Note: we do not return here because we don't want to block the next
// message on this attachment download and a lot of processing of that
// attachment.
this.handleContacts(envelope, syncMessage.contacts);
return;
}
@ -3068,26 +3065,14 @@ export default class MessageReceiver
this.removeFromCache(envelope);
// Note: we do not return here because we don't want to block the next message on
// this attachment download and a lot of processing of that attachment.
const attachmentPointer = await this.handleAttachment(blob);
const results = [];
const contactBuffer = new ContactBuffer(attachmentPointer.data);
let contactDetails = contactBuffer.next();
while (contactDetails !== undefined) {
const contactEvent = new ContactEvent(
contactDetails,
envelope.receivedAtCounter
);
results.push(this.dispatchAndWait(contactEvent));
contactDetails = contactBuffer.next();
}
await Promise.all(results);
const finalEvent = new ContactSyncEvent();
await this.dispatchAndWait(finalEvent);
const contactSync = new ContactSyncEvent(
Array.from(contactBuffer),
envelope.receivedAtCounter
);
await this.dispatchAndWait(contactSync);
log.info('handleContacts: finished');
}

View file

@ -73,17 +73,11 @@ export class ErrorEvent extends Event {
}
}
export class ContactEvent extends Event {
export class ContactSyncEvent extends Event {
constructor(
public readonly contactDetails: ModifiedContactDetails,
public readonly contacts: ReadonlyArray<ModifiedContactDetails>,
public readonly receivedAtCounter: number
) {
super('contact');
}
}
export class ContactSyncEvent extends Event {
constructor() {
super('contactSync');
}
}