-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathMessagesController.ts
More file actions
253 lines (195 loc) · 11.5 KB
/
MessagesController.ts
File metadata and controls
253 lines (195 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import MessageSummary from './MessageSummary';
import Message from './Message';
import FileStreamResult from './FileStreamResult';
import MessageRelayOptions from './MessageRelayOptions';
import axios from "axios";
import PagedResult from './PagedResult';
export default class MessagesController {
constructor() {
}
private apiBaseUrl = `api/Messages`;
public getNewSummaries_url(lastSeenMessageId: string | null, mailboxName: string, pageSize: number = 50): string {
return `${this.apiBaseUrl}/new?mailboxName=${mailboxName}&lastSeenMessageId=${encodeURIComponent(lastSeenMessageId ?? "")}&pageSize=${pageSize}`;
}
public async getNewSummaries(lastSeenMessageId: string|null, mailboxName: string, pageSize: number = 50): Promise<MessageSummary[]> {
return (await axios.get(this.getNewSummaries_url(lastSeenMessageId, mailboxName, pageSize), null || undefined)).data as MessageSummary[];
}
public getSummaries_url(mailboxName: string, searchTerms: string, sortColumn: string, sortIsDescending: boolean, page: number = 1, pageSize: number = 25, folderName: string | null = null): string {
let url = `${this.apiBaseUrl}?mailboxName=${encodeURIComponent(mailboxName)}&searchTerms=${encodeURIComponent(searchTerms)}&sortColumn=${encodeURIComponent(sortColumn)}&sortIsDescending=${sortIsDescending}&page=${page}&pageSize=${pageSize}`;
if (folderName) {
url += `&folderName=${encodeURIComponent(folderName)}`;
}
return url;
}
public async getSummaries(mailboxName: string, searchTerms: string, sortColumn: string, sortIsDescending: boolean, page: number = 1, pageSize: number = 25, folderName: string | null = null): Promise<PagedResult<MessageSummary>> {
return (await axios.get(this.getSummaries_url(mailboxName, searchTerms, sortColumn, sortIsDescending, page, pageSize, folderName), null || undefined)).data as PagedResult<MessageSummary>;
}
// get: api/Messages/${encodeURIComponent(id)}
public getMessage_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}`;
}
public async getMessage(id: string): Promise<Message> {
return (await axios.get(this.getMessage_url(id), null || undefined)).data as Message;
}
// post: api/Messages/${encodeURIComponent(id)}
public markMessageRead_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/markRead`;
}
public async markMessageRead(id: string): Promise<void> {
return (await axios.post(this.markMessageRead_url(id), null || undefined)).data as void;
}
public async markAllMessageRead(mailboxName: string): Promise<void> {
return await axios.post(`${this.apiBaseUrl}/markAllRead?mailboxName=${encodeURIComponent(mailboxName)}`);
}
// get: api/Messages/${encodeURIComponent(id)}/download
public downloadMessage_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/download`;
}
public async downloadMessage(id: string): Promise<FileStreamResult> {
return (await axios.get(this.downloadMessage_url(id), null || undefined)).data as FileStreamResult;
}
// post: api/Messages/${encodeURIComponent(id)}/relay
public relayMessage_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/relay`;
}
public async relayMessage(id: string, options: MessageRelayOptions): Promise<void> {
return (await axios.post(this.relayMessage_url(id), options || undefined)).data as void;
}
// post: api/Messages/${encodeURIComponent(id)}/reply
public reply_url(id: string, from: string, to: string, cc: string, bcc: string, deliverToAll: boolean, subject: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/reply?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}&cc=${encodeURIComponent(cc)}&bcc=${encodeURIComponent(bcc)}&deliverToAll=${encodeURIComponent(deliverToAll)}&subject=${encodeURIComponent(subject)}`;
}
public async reply(id: string, from: string, to: string, cc: string, bcc: string, deliverToAll: boolean, subject: string, bodyHtml: string, attachments?: File[]): Promise<void> {
// If attachments provided, use multipart/form-data, otherwise use text/html
if (attachments && attachments.length > 0) {
const formData = new FormData();
formData.append('bodyHtml', bodyHtml);
attachments.forEach(file => {
formData.append('attachments', file);
});
return (await axios.post(this.reply_url(id, from, to, cc, bcc, deliverToAll, subject), formData, {
headers: { "Content-Type": "multipart/form-data" }
})).data as void;
} else {
return (await axios.post(this.reply_url(id, from, to, cc, bcc, deliverToAll, subject), bodyHtml || undefined, {
headers: { "Content-Type": "text/html" }
})).data as void;
}
}
// post: api/Messages/send
public send_url(from: string, to: string, cc: string, bcc: string, deliverToAll: boolean, subject: string): string {
return `${this.apiBaseUrl}/send?from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}&cc=${encodeURIComponent(cc)}&bcc=${encodeURIComponent(bcc)}&deliverToAll=${encodeURIComponent(deliverToAll)}&subject=${encodeURIComponent(subject)}`;
}
public async send(from: string, to: string, cc: string, bcc: string, deliverToAll: boolean, subject: string, bodyHtml: string, attachments?: File[], customHeaders?: Record<string, string>): Promise<void> {
// If attachments or custom headers provided, use multipart/form-data, otherwise use text/html
if ((attachments && attachments.length > 0) || customHeaders) {
const formData = new FormData();
formData.append('bodyHtml', bodyHtml);
if (customHeaders && Object.keys(customHeaders).length > 0) {
formData.append('headers', JSON.stringify(customHeaders));
}
attachments?.forEach(file => {
formData.append('attachments', file);
});
return (await axios.post(this.send_url(from, to, cc, bcc, deliverToAll, subject), formData, {
headers: { "Content-Type": "multipart/form-data" }
})).data as void;
} else {
return (await axios.post(this.send_url(from, to, cc, bcc, deliverToAll, subject), bodyHtml || undefined, {
headers: { "Content-Type": "text/html" }
})).data as void;
}
}
public async sendRaw(emlContent: string | ArrayBuffer, from?: string, to?: string, deliverToAll?: boolean): Promise<void> {
let url = `${this.apiBaseUrl}/send`;
const params: string[] = [];
if (from) params.push(`from=${encodeURIComponent(from)}`);
if (to) params.push(`to=${encodeURIComponent(to)}`);
if (deliverToAll !== undefined) params.push(`deliverToAll=${encodeURIComponent(deliverToAll)}`);
if (params.length > 0) url += '?' + params.join('&');
const body = new Blob([emlContent], { type: 'message/rfc822' });
return (await axios.post(url, body, {
headers: { "Content-Type": "message/rfc822" }
})).data as void;
}
// get: api/Messages/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/content
public getPartContent_url(id: string, partid: string, download: boolean): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/content?download=${download}`;
}
public async getPartContent(id: string, partid: string, download: boolean): Promise<FileStreamResult> {
return (await axios.get(this.getPartContent_url(id, partid, download), null || undefined)).data as FileStreamResult;
}
// get: api/Messages/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/source
public getPartSource_url(id: string, partid: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/source`;
}
public async getPartSource(id: string, partid: string): Promise<string> {
return (await axios.get(this.getPartSource_url(id, partid), null || undefined)).data as string;
}
// get: api/Messages/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/raw
public getPartSourceRaw_url(id: string, partid: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/part/${encodeURIComponent(partid)}/raw`;
}
public async getPartSourceRaw(id: string, partid: string): Promise<string> {
return (await axios.get(this.getPartSourceRaw_url(id, partid), null || undefined)).data as string;
}
// get: api/Messages/${encodeURIComponent(id)}/raw
public getMessageSourceRaw_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/raw`;
}
public async getMessageSourceRaw(id: string): Promise<string> {
return (await axios.get(this.getMessageSourceRaw_url(id), null || undefined)).data as string;
}
// get: api/Messages/${encodeURIComponent(id)}/source
public getMessageSource_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/source`;
}
public async getMessageSource(id: string): Promise<string> {
return (await axios.get(this.getMessageSource_url(id), null || undefined)).data as string;
}
// get: api/Messages/${encodeURIComponent(id)}/html
public getMessageHtml_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/html`;
}
public async getMessageHtml(id: string): Promise<string> {
return (await axios.get(this.getMessageHtml_url(id), null || undefined)).data as string;
}
// get: api/Messages/${encodeURIComponent(id)}/plaintext
public getMessagePlainText_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}/plaintext`;
}
public async getMessagePlainText(id: string): Promise<string> {
return (await axios.get(this.getMessagePlainText_url(id), null || undefined)).data as string;
}
// delete: api/Messages/${encodeURIComponent(id)}
public delete_url(id: string): string {
return `${this.apiBaseUrl}/${encodeURIComponent(id)}`;
}
public async delete(id: string): Promise<void> {
return (await axios.delete(this.delete_url(id), null || undefined)).data as void;
}
// delete: api/Messages/*
public deleteAll_url(mailboxName: string): string {
return `${this.apiBaseUrl}/*?mailboxName=${encodeURIComponent(mailboxName)}`;
}
public async deleteAll(mailboxName: string): Promise<void> {
return (await axios.delete(this.deleteAll_url(mailboxName), null || undefined)).data as void;
}
// put: api/Messages
public import_url(mailboxName: string): string {
return `${this.apiBaseUrl}?mailboxName=${encodeURIComponent(mailboxName)}`;
}
public async import(emlContent: string, mailboxName: string): Promise<string> {
return (await axios.put(this.import_url(mailboxName), emlContent, {
headers: {
'Content-Type': 'message/rfc822'
}
})).data;
}
public getFolders_url(mailboxName: string): string {
return `${this.apiBaseUrl}/folders?mailboxName=${encodeURIComponent(mailboxName)}`;
}
public async getFolders(mailboxName: string): Promise<string[]> {
return (await axios.get(this.getFolders_url(mailboxName), null || undefined)).data as string[];
}
}