signal-desktop/ts/textsecure/processSyncMessage.ts

76 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-07-09 12:36:10 -07:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2023-06-29 21:17:27 +02:00
import type { SignalService as Proto } from '../protobuf';
import type { ServiceIdString } from '../types/ServiceId';
import { fromServiceIdBinaryOrString } from '../util/ServiceId';
2023-06-29 21:17:27 +02:00
import type { ProcessedSent, ProcessedSyncMessage } from './Types.d';
2021-07-09 12:36:10 -07:00
type ProtoServiceId = Readonly<{
destinationServiceId?: string | null;
destinationServiceIdBinary?: Uint8Array | null;
2023-06-29 21:17:27 +02:00
}>;
2021-07-09 12:36:10 -07:00
function processProtoWithDestinationServiceId<Input extends ProtoServiceId>(
2023-06-29 21:17:27 +02:00
input: Input
): Omit<Input, keyof ProtoServiceId> & {
destinationServiceId?: ServiceIdString;
2023-06-29 21:17:27 +02:00
} {
const {
destinationServiceId: rawDestinationServiceId,
destinationServiceIdBinary,
...remaining
} = input;
2021-07-09 12:36:10 -07:00
return {
2023-06-29 21:17:27 +02:00
...remaining,
2021-07-09 12:36:10 -07:00
destinationServiceId: fromServiceIdBinaryOrString(
destinationServiceIdBinary,
rawDestinationServiceId,
'processSyncMessage'
),
2021-07-09 12:36:10 -07:00
};
}
function processSent(
sent?: Proto.SyncMessage.ISent | null
): ProcessedSent | undefined {
if (!sent) {
return undefined;
}
2023-06-29 21:17:27 +02:00
const {
destinationServiceId: rawDestinationServiceId,
destinationServiceIdBinary,
2023-06-29 21:17:27 +02:00
unidentifiedStatus,
storyMessageRecipients,
...remaining
} = sent;
2021-07-09 12:36:10 -07:00
return {
2023-06-29 21:17:27 +02:00
...remaining,
2021-07-09 12:36:10 -07:00
destinationServiceId: fromServiceIdBinaryOrString(
destinationServiceIdBinary,
rawDestinationServiceId,
'processSent'
),
2021-07-09 12:36:10 -07:00
unidentifiedStatus: unidentifiedStatus
? unidentifiedStatus.map(processProtoWithDestinationServiceId)
2023-06-29 21:17:27 +02:00
: undefined,
storyMessageRecipients: storyMessageRecipients
? storyMessageRecipients.map(processProtoWithDestinationServiceId)
2021-07-09 12:36:10 -07:00
: undefined,
};
}
export function processSyncMessage(
syncMessage: Proto.ISyncMessage
): ProcessedSyncMessage {
return {
...syncMessage,
sent: processSent(syncMessage.sent),
};
}