2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
2022-03-24 17:05:48 +00:00
|
|
|
import protobuf from '../protobuf/wrap';
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
|
|
|
import { normalizeUuid } from '../util/normalizeUuid';
|
2022-11-16 20:18:02 +00:00
|
|
|
import { DurationInSeconds } from '../util/durations';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import Avatar = Proto.ContactDetails.IAvatar;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2022-03-24 17:05:48 +00:00
|
|
|
const { Reader } = protobuf;
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
type OptionalFields = { avatar?: Avatar | null; expireTimer?: number | null };
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
type DecoderBase<Message extends OptionalFields> = {
|
2022-03-24 17:05:48 +00:00
|
|
|
decodeDelimited(reader: protobuf.Reader): Message | undefined;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
type HydratedAvatar = Avatar & { data: Uint8Array };
|
|
|
|
|
|
|
|
type MessageWithAvatar<Message extends OptionalFields> = Omit<
|
2021-07-09 19:36:10 +00:00
|
|
|
Message,
|
|
|
|
'avatar'
|
|
|
|
> & {
|
2022-11-16 20:18:02 +00:00
|
|
|
avatar?: HydratedAvatar;
|
|
|
|
expireTimer?: DurationInSeconds;
|
2020-04-13 17:37:29 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
export type ModifiedGroupDetails = MessageWithAvatar<Proto.GroupDetails>;
|
|
|
|
|
|
|
|
export type ModifiedContactDetails = MessageWithAvatar<Proto.ContactDetails>;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2022-09-13 21:48:09 +00:00
|
|
|
/* eslint-disable @typescript-eslint/brace-style -- Prettier conflicts with ESLint */
|
2022-08-25 05:04:42 +00:00
|
|
|
abstract class ParserBase<
|
2022-11-16 20:18:02 +00:00
|
|
|
Message extends OptionalFields,
|
2022-08-25 05:04:42 +00:00
|
|
|
Decoder extends DecoderBase<Message>,
|
|
|
|
Result
|
|
|
|
> implements Iterable<Result>
|
|
|
|
{
|
2022-09-13 21:48:09 +00:00
|
|
|
/* eslint-enable @typescript-eslint/brace-style */
|
|
|
|
|
2022-03-24 17:05:48 +00:00
|
|
|
protected readonly reader: protobuf.Reader;
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
constructor(bytes: Uint8Array, private readonly decoder: Decoder) {
|
|
|
|
this.reader = new Reader(bytes);
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
protected decodeDelimited(): MessageWithAvatar<Message> | undefined {
|
|
|
|
if (this.reader.pos === this.reader.len) {
|
|
|
|
return undefined; // eof
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
try {
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = this.decoder.decodeDelimited(this.reader);
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
if (!proto) {
|
|
|
|
return undefined;
|
2018-05-02 01:54:43 +00:00
|
|
|
}
|
2017-09-11 16:50:35 +00:00
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
let avatar: HydratedAvatar | undefined;
|
|
|
|
if (proto.avatar) {
|
|
|
|
const attachmentLen = proto.avatar.length ?? 0;
|
|
|
|
const avatarData = this.reader.buf.slice(
|
|
|
|
this.reader.pos,
|
|
|
|
this.reader.pos + attachmentLen
|
|
|
|
);
|
|
|
|
this.reader.skip(attachmentLen);
|
|
|
|
|
|
|
|
avatar = {
|
|
|
|
...proto.avatar,
|
|
|
|
|
|
|
|
data: avatarData,
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
2020-03-05 21:14:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
let expireTimer: DurationInSeconds | undefined;
|
|
|
|
|
|
|
|
if (proto.expireTimer != null) {
|
|
|
|
expireTimer = DurationInSeconds.fromSeconds(proto.expireTimer);
|
|
|
|
}
|
2020-03-05 21:14:58 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
return {
|
|
|
|
...proto,
|
|
|
|
|
2022-11-16 20:18:02 +00:00
|
|
|
avatar,
|
|
|
|
expireTimer,
|
2021-07-09 19:36:10 +00:00
|
|
|
};
|
2018-07-21 19:00:08 +00:00
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('ProtoParser.next error:', Errors.toLogFormat(error));
|
2021-07-09 19:36:10 +00:00
|
|
|
return undefined;
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2022-08-25 05:04:42 +00:00
|
|
|
|
|
|
|
public abstract next(): Result | undefined;
|
|
|
|
|
|
|
|
*[Symbol.iterator](): Iterator<Result> {
|
|
|
|
let result = this.next();
|
|
|
|
while (result !== undefined) {
|
|
|
|
yield result;
|
|
|
|
result = this.next();
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
export class GroupBuffer extends ParserBase<
|
|
|
|
Proto.GroupDetails,
|
2022-08-25 05:04:42 +00:00
|
|
|
typeof Proto.GroupDetails,
|
|
|
|
ModifiedGroupDetails
|
2021-07-09 19:36:10 +00:00
|
|
|
> {
|
2021-09-24 00:49:05 +00:00
|
|
|
constructor(arrayBuffer: Uint8Array) {
|
2021-07-09 19:36:10 +00:00
|
|
|
super(arrayBuffer, Proto.GroupDetails);
|
|
|
|
}
|
|
|
|
|
2022-08-25 05:04:42 +00:00
|
|
|
public override next(): ModifiedGroupDetails | undefined {
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = this.decodeDelimited();
|
|
|
|
if (!proto) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proto.members) {
|
|
|
|
return proto;
|
|
|
|
}
|
2022-11-16 20:18:02 +00:00
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
return {
|
|
|
|
...proto,
|
|
|
|
members: proto.members.map((member, i) => {
|
|
|
|
if (!member.uuid) {
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...member,
|
|
|
|
uuid: normalizeUuid(member.uuid, `GroupBuffer.member[${i}].uuid`),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
export class ContactBuffer extends ParserBase<
|
|
|
|
Proto.ContactDetails,
|
2022-08-25 05:04:42 +00:00
|
|
|
typeof Proto.ContactDetails,
|
|
|
|
ModifiedContactDetails
|
2021-07-09 19:36:10 +00:00
|
|
|
> {
|
2021-09-24 00:49:05 +00:00
|
|
|
constructor(arrayBuffer: Uint8Array) {
|
2021-07-09 19:36:10 +00:00
|
|
|
super(arrayBuffer, Proto.ContactDetails);
|
|
|
|
}
|
|
|
|
|
2022-08-25 05:04:42 +00:00
|
|
|
public override next(): ModifiedContactDetails | undefined {
|
2021-07-09 19:36:10 +00:00
|
|
|
const proto = this.decodeDelimited();
|
|
|
|
if (!proto) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proto.uuid) {
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { verified } = proto;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...proto,
|
|
|
|
|
|
|
|
verified:
|
|
|
|
verified && verified.destinationUuid
|
|
|
|
? {
|
|
|
|
...verified,
|
|
|
|
|
|
|
|
destinationUuid: normalizeUuid(
|
|
|
|
verified.destinationUuid,
|
|
|
|
'ContactBuffer.verified.destinationUuid'
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: verified,
|
|
|
|
|
|
|
|
uuid: normalizeUuid(proto.uuid, 'ContactBuffer.uuid'),
|
|
|
|
};
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
}
|