signal-desktop/ts/textsecure/processSyncMessage.ts

66 lines
1.6 KiB
TypeScript
Raw Normal View History

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