1 import type { SPAM_ACTION } from '@proton/shared/lib/mail/mailSettings';
3 import type { SimpleMap } from '../interfaces';
4 import type { Message } from '../interfaces/mail/Message';
5 import type { PackageDirect } from '../interfaces/mail/crypto';
6 import type { CREATE_DRAFT_MESSAGE_ACTION, SEND_MESSAGE_DIRECT_ACTION } from '../interfaces/message';
7 import { getAppropriateSort } from './helpers/snoozeSort';
8 import type { MailboxItemsQueryParams } from './mailbox';
10 type BaseMessage = Pick<
12 'ToList' | 'CCList' | 'BCCList' | 'Subject' | 'Sender' | 'Body' | 'MIMEType' | 'Attachments' | 'Flags'
15 export interface MailSearchContext {
19 export const queryMessageMetadata = ({
43 }: MailboxItemsQueryParams) => ({
45 url: 'mail/v4/messages',
52 Sort: getAppropriateSort(LabelID, Sort),
73 export const queryMessageCount = (AddressID?: string) => ({
75 url: 'mail/v4/messages/count',
76 params: { AddressID },
79 export const getMessage = (messageID: string) => ({
81 url: `mail/v4/messages/${messageID}`,
84 interface SendMessageData {
85 AutoSaveContacts: number;
86 DelaySeconds?: number;
87 DeliveryTime?: number;
88 ExpirationTime?: number;
90 Packages: PackageDirect[];
93 export const sendMessage = (
95 { ExpirationTime, ExpiresIn, AutoSaveContacts, Packages, DelaySeconds = 0, DeliveryTime }: SendMessageData
98 url: `mail/v4/messages/${messageID}`,
99 data: { ExpirationTime, ExpiresIn, AutoSaveContacts, Packages, DelaySeconds, DeliveryTime },
102 export const sendMessageForm = (messageID: string, data: any, sendingFrom?: string) => ({
104 url: `mail/v4/messages/${messageID}`,
112 interface SendMessageDirectData {
113 Message: BaseMessage;
115 Action?: SEND_MESSAGE_DIRECT_ACTION;
116 AttachmentKeys: SimpleMap<string>;
117 ExpirationTime?: number;
119 AutoSaveContacts: number;
120 Packages: PackageDirect[];
123 export const sendMessageDirect = ({
132 }: SendMessageDirectData) => ({
134 url: `mail/v4/messages/send/direct`,
135 data: { Message, ParentID, Action, AttachmentKeys, ExpirationTime, ExpiresIn, AutoSaveContacts, Packages },
138 interface UpdateDraftData {
139 Message: BaseMessage;
140 AttachmentKeyPackets: SimpleMap<string>;
143 interface CreateDraftData extends UpdateDraftData {
145 Action?: CREATE_DRAFT_MESSAGE_ACTION;
148 export const createDraft = ({ Message, ParentID, Action, AttachmentKeyPackets }: CreateDraftData) => ({
150 url: 'mail/v4/messages',
151 data: { Message, ParentID, Action, AttachmentKeyPackets },
154 export const updateDraft = (messageID: string, { Message, AttachmentKeyPackets }: UpdateDraftData) => ({
156 url: `mail/v4/messages/${messageID}`,
157 data: { Message, AttachmentKeyPackets },
160 export const updateBody = (messageID: string, data: { Body: string }) => ({
162 url: `mail/v4/messages/${messageID}/body`,
166 export const markAsBroken = (messageID: string) => ({
168 url: `mail/v4/messages/${messageID}/mark/broken`,
171 export const readReceipt = (messageID: string) => ({
173 url: `mail/v4/messages/${messageID}/receipt`,
176 export const markAsHam = (messageID: string) => ({
178 url: `mail/v4/messages/${messageID}/mark/ham`,
181 export const oneClickUnsubscribe = (ID: string) => ({
183 url: `mail/v4/messages/${ID}/unsubscribe`,
186 export const markAsUnsubscribed = (IDs: string[]) => ({
188 url: `mail/v4/messages/mark/unsubscribed`,
192 export const markMessageAsRead = (IDs: string[]) => ({
194 url: 'mail/v4/messages/read',
198 export const markMessageAsUnread = (IDs: string[]) => ({
200 url: 'mail/v4/messages/unread',
204 export const markAllMessagesAsRead = ({ SearchContext }: { SearchContext: MailSearchContext }) => ({
206 url: 'mail/v4/messages/batch/read',
207 data: { SearchContext },
210 export const markAllMessagesAsUnread = ({ SearchContext }: { SearchContext: MailSearchContext }) => ({
212 url: 'mail/v4/messages/batch/unread',
213 data: { SearchContext },
216 export const deleteMessages = (IDs: string[], CurrentLabelID?: string) => ({
218 url: 'mail/v4/messages/delete',
219 data: { IDs, CurrentLabelID },
222 export const undeleteMessages = (IDs: string[]) => ({
224 url: 'mail/v4/messages/undelete',
228 export const labelMessages = ({
235 SpamAction?: SPAM_ACTION;
238 url: 'mail/v4/messages/label',
239 data: { LabelID, IDs, SpamAction },
242 export const unlabelMessages = ({ LabelID, IDs }: { LabelID: string; IDs: string[] }) => ({
244 url: 'mail/v4/messages/unlabel',
245 data: { LabelID, IDs },
248 export const emptyLabel = ({ LabelID, AddressID }: { LabelID: string; AddressID: string | undefined }) => ({
250 url: 'mail/v4/messages/empty',
251 params: { LabelID, AddressID },
254 export const labelAll = ({
259 SearchContext: MailSearchContext;
260 AddLabelIDs: string[];
261 RemoveLabelIDs: string[];
264 url: 'mail/v4/messages/batch/label',
265 data: { SearchContext, AddLabelIDs, RemoveLabelIDs },
268 export const moveAllBatch = ({
272 SearchContext: MailSearchContext;
273 DestinationLabelID: string;
276 url: 'mail/v4/messages/batch/move',
277 data: { SearchContext, DestinationLabelID },
280 export const cancelSend = (messageID: string) => ({
282 url: `mail/v4/messages/${messageID}/cancel_send`,
285 export const forceSend = (messageID: string) => ({
287 url: `mail/v4/messages/${messageID}/force_send`,
290 export const unsubscribeMessages = (IDs: string[]) => ({
292 url: 'mail/v4/messages/unsubscribe',
296 export const setExpiration = (IDs: string[], ExpirationTime: number | null) => ({
298 url: 'mail/v4/messages/expire',
299 data: { IDs, ExpirationTime },