@softeria/ms-365-mcp-server 0.193.1 → 0.193.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -23
- package/dist/__tests__/graph-tools.test.js +1 -1
- package/dist/apps/mail-draft.html +13 -11
- package/dist/apps/mail-draft.js +327 -148
- package/dist/cli.js +1 -1
- package/dist/endpoints.json +42 -8
- package/dist/graph-tools.js +47 -3
- package/dist/lib/attachment-upload.js +4 -1
- package/dist/mcp-apps.js +18 -4
- package/package.json +1 -1
- package/src/apps/mail-draft.html +13 -11
- package/src/apps/mail-draft.js +327 -148
- package/src/endpoints.json +42 -8
package/src/apps/mail-draft.js
CHANGED
|
@@ -3,43 +3,39 @@
|
|
|
3
3
|
(() => {
|
|
4
4
|
'use strict';
|
|
5
5
|
|
|
6
|
-
// Long enough for an approval prompt in the host;
|
|
6
|
+
// Long enough for an approval prompt in the host; progress restarts the timer.
|
|
7
7
|
const CALL_TIMEOUT_MS = 10 * 60 * 1000;
|
|
8
|
-
// Bidi controls and marks can make text read in another order than it is sent: one
|
|
9
|
-
// address like another, or the digit groups of an account number swapped
|
|
10
8
|
const BIDI = /[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g;
|
|
9
|
+
const RECIPIENTS = {
|
|
10
|
+
toRecipients: 'To',
|
|
11
|
+
ccRecipients: 'Cc',
|
|
12
|
+
bccRecipients: 'Bcc',
|
|
13
|
+
replyTo: 'Reply to',
|
|
14
|
+
};
|
|
11
15
|
|
|
12
16
|
let config = {};
|
|
13
17
|
try {
|
|
14
18
|
config = JSON.parse(document.getElementById('mcp-app-config').textContent) || {};
|
|
15
19
|
} catch {
|
|
16
|
-
// Unfilled placeholder: the view can still show the
|
|
20
|
+
// Unfilled placeholder: the view can still show the creation result.
|
|
17
21
|
}
|
|
18
|
-
// Role -> tool name, and only for tools this server has registered
|
|
19
22
|
const tools = isObject(config.tools) ? config.tools : {};
|
|
23
|
+
const inventory = config.attachmentInventory;
|
|
24
|
+
const shared = (str(config.toolName) || '').startsWith('create-shared-mailbox-');
|
|
20
25
|
|
|
21
26
|
const els = {
|
|
22
27
|
subject: document.getElementById('subject'),
|
|
23
28
|
fields: document.getElementById('fields'),
|
|
24
29
|
body: document.getElementById('body'),
|
|
30
|
+
bodyLabel: document.getElementById('body-label'),
|
|
31
|
+
attachments: document.getElementById('attachments'),
|
|
32
|
+
manual: document.getElementById('manual'),
|
|
25
33
|
send: document.getElementById('send'),
|
|
26
34
|
open: document.getElementById('open'),
|
|
27
35
|
status: document.getElementById('status'),
|
|
28
36
|
};
|
|
29
37
|
|
|
30
|
-
const state = {
|
|
31
|
-
// Arguments of the tool call that created the draft
|
|
32
|
-
input: undefined,
|
|
33
|
-
draft: undefined,
|
|
34
|
-
// Whether the draft shown is one the view read itself
|
|
35
|
-
read: false,
|
|
36
|
-
reading: false,
|
|
37
|
-
sending: false,
|
|
38
|
-
sent: false,
|
|
39
|
-
error: undefined,
|
|
40
|
-
notice: undefined,
|
|
41
|
-
};
|
|
42
|
-
|
|
38
|
+
const state = {};
|
|
43
39
|
const bridge = createMcpAppBridge({
|
|
44
40
|
name: 'ms-365-mail-draft',
|
|
45
41
|
onToolInput(args) {
|
|
@@ -51,6 +47,7 @@
|
|
|
51
47
|
if (!isObject(draft) || !str(draft.id)) {
|
|
52
48
|
throw new Error('The result does not include the draft, so it is not shown here.');
|
|
53
49
|
}
|
|
50
|
+
if (draft.isDraft === false) throw new Error('This message is no longer a draft.');
|
|
54
51
|
state.draft = draft;
|
|
55
52
|
} catch (error) {
|
|
56
53
|
state.error = error.message;
|
|
@@ -75,89 +72,208 @@
|
|
|
75
72
|
els.send.addEventListener('click', send);
|
|
76
73
|
els.open.addEventListener('click', openInOutlook);
|
|
77
74
|
|
|
78
|
-
//
|
|
79
|
-
|
|
75
|
+
// For replies/forwards, the input's messageId is the original mail, not the saved draft.
|
|
76
|
+
function mailboxArgs() {
|
|
77
|
+
const input = state.input || {};
|
|
78
|
+
const userId = str(input.userId) || str(input['user-id']);
|
|
79
|
+
const account = str(input.account);
|
|
80
|
+
return { messageId: state.draft.id, ...(userId && { userId }), ...(account && { account }) };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function canRead() {
|
|
84
|
+
return Boolean(
|
|
85
|
+
state.input &&
|
|
86
|
+
(!shared || mailboxArgs().userId) &&
|
|
87
|
+
bridge.capabilities.serverTools &&
|
|
88
|
+
str(tools.get)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
80
92
|
async function readOnOpen() {
|
|
81
|
-
if (!state.draft || state.
|
|
82
|
-
state.
|
|
93
|
+
if (!state.draft || state.readStarted || !canRead()) return;
|
|
94
|
+
state.readStarted = true;
|
|
95
|
+
state.busy = 'Reading the draft...';
|
|
96
|
+
render();
|
|
97
|
+
await refresh();
|
|
98
|
+
state.busy = undefined;
|
|
83
99
|
render();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function call(role, args) {
|
|
103
|
+
return bridge.callTool(tools[role], args, { timeout: CALL_TIMEOUT_MS });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// A changed or failed read invalidates the old review before we evaluate it again.
|
|
107
|
+
async function refresh() {
|
|
108
|
+
const args = mailboxArgs();
|
|
109
|
+
state.review = undefined;
|
|
110
|
+
state.attachments = undefined;
|
|
111
|
+
state.attachmentIssue = undefined;
|
|
112
|
+
state.read = false;
|
|
113
|
+
state.error = undefined;
|
|
84
114
|
try {
|
|
85
|
-
const
|
|
86
|
-
|
|
115
|
+
const result = await call('get', args);
|
|
116
|
+
const current = bridge.readToolResult(result, 'Reading the draft failed.');
|
|
117
|
+
if (!isObject(current) || current.id !== args.messageId) {
|
|
118
|
+
throw new Error('This result is not the draft.');
|
|
119
|
+
}
|
|
120
|
+
if (current.isDraft === false) {
|
|
121
|
+
state.draft = undefined;
|
|
122
|
+
throw new Error('This message is no longer a draft.');
|
|
123
|
+
}
|
|
124
|
+
if (incomplete(result, current)) throw new Error('The saved draft read is incomplete.');
|
|
87
125
|
state.draft = current;
|
|
88
126
|
state.read = true;
|
|
89
127
|
} catch (error) {
|
|
90
|
-
state.error =
|
|
128
|
+
state.error = error.message;
|
|
129
|
+
return;
|
|
91
130
|
}
|
|
92
|
-
|
|
131
|
+
|
|
132
|
+
if (!str(tools.attachments) || !Array.isArray(inventory?.select)) return;
|
|
133
|
+
state.busy = 'Reading attachments...';
|
|
93
134
|
render();
|
|
135
|
+
try {
|
|
136
|
+
const result = await call('attachments', {
|
|
137
|
+
...args,
|
|
138
|
+
select: inventory.select,
|
|
139
|
+
...(inventory.fetchAllPages && { fetchAllPages: true }),
|
|
140
|
+
});
|
|
141
|
+
const data = bridge.readToolResult(result, 'Reading attachments failed.');
|
|
142
|
+
if (!isObject(data) || !Array.isArray(data.value)) throw new Error('inventory unavailable');
|
|
143
|
+
// The body tells a whole list from a partial one. Not every host passes _meta on to a
|
|
144
|
+
// view, so its count is only checked when it is there.
|
|
145
|
+
const count = result._meta?.result_count;
|
|
146
|
+
if (
|
|
147
|
+
incomplete(result, data) ||
|
|
148
|
+
Object.hasOwn(data, '@odata.nextLink') ||
|
|
149
|
+
(count !== undefined && count !== data.value.length) ||
|
|
150
|
+
(Object.hasOwn(data, '@odata.count') && data['@odata.count'] !== data.value.length)
|
|
151
|
+
) {
|
|
152
|
+
state.attachmentIssue = 'Attachment inventory incomplete.';
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const items = data.value.map(attachmentMetadata);
|
|
156
|
+
if (
|
|
157
|
+
new Set(items.map((item) => item.id)).size !== items.length ||
|
|
158
|
+
(!items.some((item) => !item.isInline) && state.draft.hasAttachments === true)
|
|
159
|
+
) {
|
|
160
|
+
state.attachmentIssue = 'Attachment inventory incomplete.';
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
state.attachments = items;
|
|
164
|
+
state.review = reviewKey(state.draft, items);
|
|
165
|
+
} catch {
|
|
166
|
+
state.attachmentIssue = 'Attachment inventory unavailable.';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function incomplete(result, data) {
|
|
171
|
+
const meta = result._meta;
|
|
172
|
+
return Boolean(
|
|
173
|
+
data.truncated ||
|
|
174
|
+
data.incomplete ||
|
|
175
|
+
meta?.truncated ||
|
|
176
|
+
meta?.incomplete ||
|
|
177
|
+
(meta?.http_status !== undefined && meta.http_status !== 200) ||
|
|
178
|
+
(meta?.result_has_more !== undefined && meta.result_has_more !== false)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function attachmentMetadata(item) {
|
|
183
|
+
if (
|
|
184
|
+
!isObject(item) ||
|
|
185
|
+
!str(item.id) ||
|
|
186
|
+
!clean(item.name) ||
|
|
187
|
+
typeof item.isInline !== 'boolean'
|
|
188
|
+
) {
|
|
189
|
+
throw new Error('Attachment metadata is missing.');
|
|
190
|
+
}
|
|
191
|
+
const { id, name, isInline, size, contentType, lastModifiedDateTime } = item;
|
|
192
|
+
const type = item['@odata.type'];
|
|
193
|
+
if (
|
|
194
|
+
(size != null && (!Number.isSafeInteger(size) || size < 0)) ||
|
|
195
|
+
[contentType, lastModifiedDateTime, type].some(
|
|
196
|
+
(value) => value != null && typeof value !== 'string'
|
|
197
|
+
)
|
|
198
|
+
)
|
|
199
|
+
throw new Error('Attachment metadata is invalid.');
|
|
200
|
+
return { id, name, isInline, size, contentType, lastModifiedDateTime, type };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function reviewKey(draft, items) {
|
|
204
|
+
if (
|
|
205
|
+
draft.isDraft !== true ||
|
|
206
|
+
!str(draft.changeKey) ||
|
|
207
|
+
typeof draft.subject !== 'string' ||
|
|
208
|
+
!wholeBody(draft) ||
|
|
209
|
+
Object.keys(RECIPIENTS).some((key) => !recipientList(draft[key])) ||
|
|
210
|
+
(draft.from != null && !clean(draft.from?.emailAddress?.address))
|
|
211
|
+
)
|
|
212
|
+
return undefined;
|
|
213
|
+
const from = draft.from?.emailAddress;
|
|
214
|
+
const recipients = Object.keys(RECIPIENTS).map((key) =>
|
|
215
|
+
draft[key].map(({ emailAddress }) => [emailAddress.address, emailAddress.name])
|
|
216
|
+
);
|
|
217
|
+
// Graph may return the same attachments in a different order or split across pages.
|
|
218
|
+
const sorted = [...items].sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
219
|
+
return JSON.stringify([
|
|
220
|
+
draft.id,
|
|
221
|
+
draft.changeKey,
|
|
222
|
+
draft.subject,
|
|
223
|
+
draft.body.content,
|
|
224
|
+
from?.address,
|
|
225
|
+
from?.name,
|
|
226
|
+
recipients,
|
|
227
|
+
sorted,
|
|
228
|
+
]);
|
|
94
229
|
}
|
|
95
230
|
|
|
96
231
|
async function send() {
|
|
97
232
|
if (!canSend()) return;
|
|
98
|
-
|
|
99
|
-
state.error = undefined;
|
|
233
|
+
const reviewed = state.review;
|
|
100
234
|
state.notice = undefined;
|
|
235
|
+
state.error = undefined;
|
|
236
|
+
state.openFailed = false;
|
|
237
|
+
state.busy = 'Rechecking the draft...';
|
|
101
238
|
render();
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
239
|
+
await refresh();
|
|
240
|
+
// A draft that reads fine was not sent. One that cannot be read after a failed Send
|
|
241
|
+
// most likely was: it leaves Drafts, and its id with it.
|
|
242
|
+
if (state.read) state.sendFailed = false;
|
|
243
|
+
else if (state.sendFailed && state.error) {
|
|
244
|
+
state.error = sentence(state.error) + ' If the earlier Send went out, it is in Sent Items.';
|
|
245
|
+
}
|
|
246
|
+
if (state.review && state.review !== reviewed) {
|
|
247
|
+
// Nothing goes out on the click that first shows a review: after an edit, or when the
|
|
248
|
+
// read before it failed
|
|
249
|
+
state.notice = reviewed
|
|
250
|
+
? 'The draft or its attachments changed. Review them and press Send again.'
|
|
251
|
+
: 'Here is the saved draft. Review it and press Send again.';
|
|
252
|
+
} else if (state.review && hasRecipients(state.draft)) {
|
|
253
|
+
// The click is the confirmation. The freshness check and Send are separate calls.
|
|
254
|
+
state.busy = 'Sending...';
|
|
255
|
+
state.sending = true;
|
|
256
|
+
render();
|
|
257
|
+
try {
|
|
258
|
+
const result = await call('send', { ...mailboxArgs(), confirm: true });
|
|
259
|
+
if (!Array.isArray(result?.content)) throw new Error('The host returned no Send result.');
|
|
123
260
|
const error = bridge.toolError(result, 'Sending failed.');
|
|
124
261
|
if (error) throw new Error(error);
|
|
125
262
|
state.sent = true;
|
|
126
263
|
tellModel();
|
|
264
|
+
} catch (error) {
|
|
265
|
+
// The view cannot tell a refusal from a Send that went out without an answer coming
|
|
266
|
+
// back, so every failure says to look first. The next click reads the draft again
|
|
267
|
+
// before it sends.
|
|
268
|
+
state.sendFailed = true;
|
|
269
|
+
state.error = sentence(error.message) + ' Check Sent Items in Outlook before trying again.';
|
|
127
270
|
}
|
|
128
|
-
|
|
129
|
-
state.error =
|
|
130
|
-
error && error.name === 'TimeoutError'
|
|
131
|
-
? 'No answer from the host. Check Sent Items before you try again.'
|
|
132
|
-
: (error && error.message) || String(error);
|
|
271
|
+
state.sending = false;
|
|
133
272
|
}
|
|
134
|
-
state.
|
|
273
|
+
state.busy = undefined;
|
|
135
274
|
render();
|
|
136
275
|
}
|
|
137
276
|
|
|
138
|
-
async function readDraft() {
|
|
139
|
-
const result = await bridge.callTool(tools.get, mailboxArgs({}), {
|
|
140
|
-
timeout: CALL_TIMEOUT_MS,
|
|
141
|
-
});
|
|
142
|
-
const draft = bridge.readToolResult(result, 'Reading the draft failed.');
|
|
143
|
-
if (!isObject(draft) || draft.id !== state.draft.id) {
|
|
144
|
-
throw new Error('This result is not the draft.');
|
|
145
|
-
}
|
|
146
|
-
return draft;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// The draft's own id, in the mailbox it was created in: for a reply or forward draft, the
|
|
150
|
-
// input's messageId is the mail it answers
|
|
151
|
-
function mailboxArgs(extra) {
|
|
152
|
-
const input = state.input || {};
|
|
153
|
-
const args = Object.assign({ messageId: state.draft.id }, extra);
|
|
154
|
-
const userId = str(input.userId) || str(input['user-id']);
|
|
155
|
-
if (userId) args.userId = userId;
|
|
156
|
-
const account = str(input.account);
|
|
157
|
-
if (account) args.account = account;
|
|
158
|
-
return args;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
277
|
// So the model does not offer to send it again. No subject: a reply's comes from outside.
|
|
162
278
|
function tellModel() {
|
|
163
279
|
if (!bridge.capabilities.updateModelContext) return;
|
|
@@ -170,64 +286,162 @@
|
|
|
170
286
|
.catch(() => {});
|
|
171
287
|
}
|
|
172
288
|
|
|
289
|
+
// Hidden until the view's own read of the draft is in, and while a Send is out. After a
|
|
290
|
+
// read that failed, the link from the creation result still opens the draft.
|
|
291
|
+
function canOpenDraft() {
|
|
292
|
+
const draft = state.draft;
|
|
293
|
+
return Boolean(
|
|
294
|
+
draft &&
|
|
295
|
+
!state.sent &&
|
|
296
|
+
!state.sending &&
|
|
297
|
+
(state.read || !state.busy) &&
|
|
298
|
+
bridge.capabilities.openLinks &&
|
|
299
|
+
httpsUrl(draft.webLink)
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
173
303
|
async function openInOutlook() {
|
|
174
|
-
|
|
175
|
-
|
|
304
|
+
if (!canOpenDraft()) return;
|
|
305
|
+
state.openFailed = false;
|
|
176
306
|
try {
|
|
177
|
-
const result = await bridge.openLink(
|
|
307
|
+
const result = await bridge.openLink(httpsUrl(state.draft.webLink));
|
|
178
308
|
if (result && result.isError) throw new Error('The host did not open the link.');
|
|
179
|
-
} catch
|
|
180
|
-
state.
|
|
181
|
-
render();
|
|
309
|
+
} catch {
|
|
310
|
+
state.openFailed = true;
|
|
182
311
|
}
|
|
312
|
+
render();
|
|
183
313
|
}
|
|
184
314
|
|
|
185
315
|
function render() {
|
|
186
316
|
const draft = state.draft;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
317
|
+
els.subject.hidden = !draft;
|
|
318
|
+
els.subject.textContent = draft ? availableSubject(draft) : '';
|
|
319
|
+
els.fields.replaceChildren(...(draft ? fields(draft) : []));
|
|
320
|
+
const body = draft ? bodyText(draft) : '';
|
|
321
|
+
els.body.textContent = body;
|
|
322
|
+
els.body.hidden = !body;
|
|
323
|
+
els.bodyLabel.hidden = !draft || (wholeBody(draft) && Boolean(body));
|
|
324
|
+
els.bodyLabel.textContent = !draft
|
|
325
|
+
? ''
|
|
326
|
+
: wholeBody(draft)
|
|
327
|
+
? 'Body: (empty)'
|
|
328
|
+
: typeof draft.bodyPreview === 'string'
|
|
329
|
+
? 'Body excerpt (full body unavailable here)'
|
|
330
|
+
: 'Body unavailable';
|
|
331
|
+
renderAttachments(draft && !state.sent);
|
|
332
|
+
|
|
333
|
+
els.send.hidden = !draft || state.sent || !str(tools.send) || !bridge.capabilities.serverTools;
|
|
196
334
|
els.send.disabled = !canSend();
|
|
335
|
+
const openable = canOpenDraft();
|
|
197
336
|
els.open.hidden = !openable;
|
|
337
|
+
els.manual.hidden = !draft || state.sent || (openable && !state.openFailed);
|
|
338
|
+
if (draft) {
|
|
339
|
+
const { userId, account } = mailboxArgs();
|
|
340
|
+
const selectedAccount = clean(account);
|
|
341
|
+
const mailbox = clean(userId) || selectedAccount;
|
|
342
|
+
els.manual.textContent = mailbox
|
|
343
|
+
? 'In Outlook, open Drafts in ' +
|
|
344
|
+
mailbox +
|
|
345
|
+
(clean(userId) && selectedAccount ? ' using ' + selectedAccount : '') +
|
|
346
|
+
'.'
|
|
347
|
+
: 'In Outlook, open Drafts in the mailbox where it was created.';
|
|
348
|
+
}
|
|
349
|
+
|
|
198
350
|
if (state.error) setStatus(state.error, true);
|
|
199
351
|
else if (!draft) setStatus('Loading...', false);
|
|
200
352
|
else if (state.sent) setStatus('Sent.', false);
|
|
201
|
-
else if (state.
|
|
202
|
-
else if (state.
|
|
203
|
-
else if (!
|
|
353
|
+
else if (state.busy) setStatus(state.busy, false);
|
|
354
|
+
else if (state.openFailed) setStatus('Could not open the draft in Outlook.', true);
|
|
355
|
+
else if (!state.read)
|
|
356
|
+
setStatus('Saved draft unavailable. Showing the creation result only.', false);
|
|
357
|
+
else if (!state.review)
|
|
204
358
|
setStatus(
|
|
205
|
-
|
|
206
|
-
? '
|
|
207
|
-
: '
|
|
359
|
+
canSend()
|
|
360
|
+
? 'Complete review unavailable. Press Send to check again, or review and send in Outlook.'
|
|
361
|
+
: 'Complete review unavailable. Review and send in Outlook.',
|
|
208
362
|
false
|
|
209
363
|
);
|
|
210
|
-
|
|
364
|
+
else if (!hasRecipients(draft)) setStatus('This draft has no recipients yet.', false);
|
|
211
365
|
else if (state.notice) setStatus(state.notice, false);
|
|
366
|
+
else if (!str(tools.send)) setStatus('In-app Send unavailable. Send in Outlook.', false);
|
|
212
367
|
else setStatus('', false);
|
|
213
368
|
}
|
|
214
369
|
|
|
370
|
+
function renderAttachments(show) {
|
|
371
|
+
els.attachments.hidden = !show;
|
|
372
|
+
const items = state.attachments;
|
|
373
|
+
const summary = !items
|
|
374
|
+
? state.attachmentIssue ||
|
|
375
|
+
(state.busy ? 'Attachment inventory pending.' : 'Attachment inventory unavailable.')
|
|
376
|
+
: items.length
|
|
377
|
+
? 'Attachments (' + items.length + ')'
|
|
378
|
+
: 'Attachments: none';
|
|
379
|
+
els.attachments.replaceChildren(span('label', summary));
|
|
380
|
+
if (!items?.length) return;
|
|
381
|
+
const list = document.createElement('ul');
|
|
382
|
+
list.replaceChildren(
|
|
383
|
+
...items.map(({ name, isInline, size, contentType }) => {
|
|
384
|
+
const li = document.createElement('li');
|
|
385
|
+
const details = [
|
|
386
|
+
isInline ? 'inline' : 'not inline',
|
|
387
|
+
clean(contentType),
|
|
388
|
+
size != null ? size + ' bytes' : undefined,
|
|
389
|
+
];
|
|
390
|
+
li.textContent = clean(name) + ' (' + details.filter(Boolean).join(', ') + ')';
|
|
391
|
+
return li;
|
|
392
|
+
})
|
|
393
|
+
);
|
|
394
|
+
els.attachments.replaceChildren(span('label', summary), list);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function availableSubject(draft) {
|
|
398
|
+
return typeof draft.subject === 'string'
|
|
399
|
+
? clean(draft.subject) || '(no subject)'
|
|
400
|
+
: 'Subject unavailable';
|
|
401
|
+
}
|
|
402
|
+
|
|
215
403
|
function fields(draft) {
|
|
216
404
|
const rows = [];
|
|
217
405
|
const from = recipientText(draft.from);
|
|
218
406
|
if (from) rows.push(...field('From', from));
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
if (draft.hasAttachments === true) rows.push(...field('Attachments', 'Yes'));
|
|
407
|
+
for (const [key, label] of Object.entries(RECIPIENTS)) {
|
|
408
|
+
const list = draft[key];
|
|
409
|
+
if (!recipientList(list)) rows.push(...field(label, 'Unavailable'));
|
|
410
|
+
else if (list.length || key === 'toRecipients')
|
|
411
|
+
rows.push(...field(label, recipientsText(list) || 'Nobody yet'));
|
|
412
|
+
}
|
|
413
|
+
if (draft.isDraft !== true || !str(draft.changeKey))
|
|
414
|
+
rows.push(...field('Saved draft revision', 'Unavailable'));
|
|
228
415
|
return rows;
|
|
229
416
|
}
|
|
230
417
|
|
|
418
|
+
function recipientList(list) {
|
|
419
|
+
return Array.isArray(list) && list.every((item) => clean(item?.emailAddress?.address));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function hasRecipients(draft) {
|
|
423
|
+
return ['toRecipients', 'ccRecipients', 'bccRecipients'].some((key) => draft[key].length > 0);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// A click reads the draft and its attachments again before anything is sent, so it also
|
|
427
|
+
// retries a read that failed and picks up a later edit: the model may add recipients after
|
|
428
|
+
// the view opened. Once a read shows an HTML body, no click can complete the review.
|
|
429
|
+
function canSend() {
|
|
430
|
+
return Boolean(
|
|
431
|
+
state.draft &&
|
|
432
|
+
!state.sent &&
|
|
433
|
+
!state.busy &&
|
|
434
|
+
canReview() &&
|
|
435
|
+
(!state.read || wholeBody(state.draft))
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function canReview() {
|
|
440
|
+
return Boolean(
|
|
441
|
+
canRead() && str(tools.attachments) && Array.isArray(inventory?.select) && str(tools.send)
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
231
445
|
function field(label, value) {
|
|
232
446
|
return [span('label', label), span('value', value)];
|
|
233
447
|
}
|
|
@@ -248,16 +462,6 @@
|
|
|
248
462
|
return Array.isArray(list) ? list.map(recipientText).filter(Boolean).join(', ') : '';
|
|
249
463
|
}
|
|
250
464
|
|
|
251
|
-
function hasRecipients(draft) {
|
|
252
|
-
return ['toRecipients', 'ccRecipients', 'bccRecipients'].some(
|
|
253
|
-
(key) =>
|
|
254
|
-
Array.isArray(draft[key]) &&
|
|
255
|
-
draft[key].some(
|
|
256
|
-
(r) => isObject(r) && isObject(r.emailAddress) && clean(r.emailAddress.address)
|
|
257
|
-
)
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
465
|
// Bidi controls can make one address read like another, so they never reach the view
|
|
262
466
|
function clean(value) {
|
|
263
467
|
const text = str(value) && value.replace(BIDI, '').trim();
|
|
@@ -279,35 +483,6 @@
|
|
|
279
483
|
return text.replace(BIDI, '').trim();
|
|
280
484
|
}
|
|
281
485
|
|
|
282
|
-
function webLink(draft) {
|
|
283
|
-
return draft ? httpsUrl(draft.webLink) : undefined;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
// Send needs both tools, and once the view has read the draft, all of its body as text
|
|
287
|
-
function sendable() {
|
|
288
|
-
return Boolean(
|
|
289
|
-
state.draft && !state.sent && canCall() && (!state.read || wholeBody(state.draft))
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
// Without recipients a click still reads the draft again: the model may add them later
|
|
294
|
-
function canSend() {
|
|
295
|
-
return sendable() && !state.sending && !state.reading;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
function canRead() {
|
|
299
|
-
return Boolean(bridge.capabilities.serverTools && str(tools.get));
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// Sending needs both: the draft is read again right before it is sent
|
|
303
|
-
function canCall() {
|
|
304
|
-
return canRead() && Boolean(str(tools.send));
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function canOpenLinks() {
|
|
308
|
-
return Boolean(bridge.capabilities.openLinks);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
486
|
function span(className, text) {
|
|
312
487
|
const el = document.createElement('span');
|
|
313
488
|
el.className = className;
|
|
@@ -329,6 +504,10 @@
|
|
|
329
504
|
return typeof value === 'string' && value ? value : undefined;
|
|
330
505
|
}
|
|
331
506
|
|
|
507
|
+
function sentence(text) {
|
|
508
|
+
return /[.!?]$/.test(text) ? text : text + '.';
|
|
509
|
+
}
|
|
510
|
+
|
|
332
511
|
function httpsUrl(value) {
|
|
333
512
|
if (typeof value !== 'string') return undefined;
|
|
334
513
|
try {
|