2 CalendarNotificationSettings,
3 CreateOrUpdateCalendarEventData,
4 } from '@proton/shared/lib/interfaces/calendar';
5 import isTruthy from '@proton/utils/isTruthy';
7 import { uint8ArrayToBase64String } from '../helpers/encoding';
8 import type { SimpleMap } from '../interfaces';
9 import type { AttendeeClearPartResult } from '../interfaces/calendar/Attendee';
10 import type { EncryptPartResult, SignPartResult } from '../interfaces/calendar/PartResult';
11 import { CALENDAR_CARD_TYPE } from './constants';
13 const { ENCRYPTED_AND_SIGNED, SIGNED } = CALENDAR_CARD_TYPE;
16 * Format the data into what the API expects.
18 interface FormatDataArguments {
19 sharedSignedPart?: SignPartResult;
20 sharedEncryptedPart?: EncryptPartResult;
21 sharedSessionKey?: Uint8Array;
22 cancelledOccurrenceSignedPart?: SignPartResult;
23 calendarSignedPart?: SignPartResult;
24 calendarEncryptedPart?: EncryptPartResult;
25 calendarSessionKey?: Uint8Array;
26 attendeesEncryptedPart?: EncryptPartResult;
27 attendeesClearPart?: AttendeeClearPartResult[];
28 removedAttendeesEmails?: string[];
29 attendeesEncryptedSessionKeysMap?: SimpleMap<Uint8Array>;
30 notificationsPart?: CalendarNotificationSettings[];
33 export const formatData = ({
37 cancelledOccurrenceSignedPart,
39 calendarEncryptedPart,
43 attendeesEncryptedPart,
45 removedAttendeesEmails,
46 attendeesEncryptedSessionKeysMap,
47 }: FormatDataArguments) => {
48 const result: Omit<CreateOrUpdateCalendarEventData, 'Permissions'> = {
49 Notifications: notificationsPart || null,
50 Color: colorPart || null,
53 if (sharedSessionKey) {
54 result.SharedKeyPacket = uint8ArrayToBase64String(sharedSessionKey);
57 if (sharedSignedPart && sharedEncryptedPart) {
58 result.SharedEventContent = [
61 Data: sharedSignedPart.data,
62 Signature: sharedSignedPart.signature,
65 Type: ENCRYPTED_AND_SIGNED,
66 Data: uint8ArrayToBase64String(sharedEncryptedPart.dataPacket),
67 Signature: sharedEncryptedPart.signature,
72 if (cancelledOccurrenceSignedPart) {
73 result.CancelledOccurrenceContent = [
76 Data: cancelledOccurrenceSignedPart.data,
77 Signature: cancelledOccurrenceSignedPart.signature,
82 if (calendarEncryptedPart && calendarSessionKey) {
83 result.CalendarKeyPacket = uint8ArrayToBase64String(calendarSessionKey);
86 if (calendarSignedPart || calendarEncryptedPart) {
87 result.CalendarEventContent = [
88 calendarSignedPart && {
90 Data: calendarSignedPart.data,
91 Signature: calendarSignedPart.signature,
93 calendarEncryptedPart && {
94 Type: ENCRYPTED_AND_SIGNED,
95 Data: uint8ArrayToBase64String(calendarEncryptedPart.dataPacket),
96 Signature: calendarEncryptedPart.signature,
101 if (attendeesEncryptedPart) {
102 result.AttendeesEventContent = [
104 Type: ENCRYPTED_AND_SIGNED,
105 Data: uint8ArrayToBase64String(attendeesEncryptedPart.dataPacket),
106 Signature: attendeesEncryptedPart.signature,
111 if (attendeesClearPart) {
112 result.Attendees = attendeesClearPart.map(({ token, status }) => ({
118 if (removedAttendeesEmails?.length) {
119 result.RemovedAttendeeAddresses = removedAttendeesEmails;
122 if (attendeesEncryptedSessionKeysMap) {
123 result.AddedProtonAttendees = Object.keys(attendeesEncryptedSessionKeysMap)
125 const sharedEncryptedSessionKey = attendeesEncryptedSessionKeysMap[email];
126 if (!sharedEncryptedSessionKey) {
129 return { Email: email, AddressKeyPacket: uint8ArrayToBase64String(sharedEncryptedSessionKey) };