@chrischall/microsoft-teams-mcp 0.14.0 → 0.15.0
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/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +241 -33
- package/dist/attachments.js +238 -0
- package/dist/auth-fetchproxy.js +7 -6
- package/dist/browser-open.js +138 -0
- package/dist/bundle.js +6840 -3949
- package/dist/capture-feedback.js +283 -0
- package/dist/chatsvc.js +132 -7
- package/dist/compose.js +9 -1
- package/dist/file-upload.js +335 -0
- package/dist/index.js +11 -1
- package/dist/model.js +1 -0
- package/dist/presence.js +120 -0
- package/dist/profile.js +182 -0
- package/dist/recap-auth.js +6 -4
- package/dist/recap.js +3 -1
- package/dist/sender-email.js +147 -0
- package/dist/sharepoint-auth.js +42 -21
- package/dist/sharepoint.js +150 -3
- package/dist/substrate-auth.js +8 -7
- package/dist/substrate.js +98 -4
- package/dist/tools/attach.js +167 -0
- package/dist/tools/attachment.js +3 -1
- package/dist/tools/conversation.js +14 -5
- package/dist/tools/edit.js +42 -14
- package/dist/tools/errors.js +10 -11
- package/dist/tools/export.js +30 -3
- package/dist/tools/format.js +29 -6
- package/dist/tools/group-chat.js +349 -33
- package/dist/tools/healthcheck.js +23 -9
- package/dist/tools/mention-check.js +225 -0
- package/dist/tools/people.js +149 -39
- package/dist/tools/profile.js +67 -0
- package/dist/tools/send.js +134 -37
- package/dist/tools/sender-email-arg.js +37 -0
- package/dist/tools/token-status.js +97 -0
- package/dist/tools/unread.js +3 -2
- package/dist/tools/upload-slots.js +51 -0
- package/dist/version.js +1 -1
- package/mint.yaml +19 -0
- package/package.json +4 -1
- package/server.json +30 -2
- package/skills/teams-fpx/references/teams-env.sh +1 -1
package/dist/tools/edit.js
CHANGED
|
@@ -5,9 +5,13 @@ import { htmlToText, isDeleted, SELF_CHAT } from '../model.js';
|
|
|
5
5
|
import { mentionsOf, sanitiseHtml } from '../rich.js';
|
|
6
6
|
import { ownMessage, writeAndReadBack } from './own-message.js';
|
|
7
7
|
import { wrapApiError } from './errors.js';
|
|
8
|
-
import { formatParam, mentionArg, resolveMentions, resolveTarget } from './send.js';
|
|
8
|
+
import { formatParam, mentionArg, once, resolveMentions, resolveTarget, unmatchedNote } from './send.js';
|
|
9
|
+
import { checkMentions, mentionScope } from './mention-check.js';
|
|
10
|
+
import { recentSenders } from './people.js';
|
|
9
11
|
import { untrustedField, untrustedResult, UNTRUSTED_DESCRIPTION_SUFFIX } from './untrusted.js';
|
|
10
12
|
import { workMeta } from './hints.js';
|
|
13
|
+
import { attachmentsParam } from '../attachments.js';
|
|
14
|
+
import { attachmentResult, deliverAttachments, mergedFilesProperty, planAttachments } from './attach.js';
|
|
11
15
|
const TOOL = 'teams_edit_message';
|
|
12
16
|
/** A quote-reply's quote block, as Teams (and `compose.quoteHtml`) put it at the start of the body. */
|
|
13
17
|
const QUOTE_BLOCK = /^\s*(<blockquote\b[^>]*schema\.skype\.com\/Reply[^>]*>[\s\S]*?<\/blockquote\s*>)/i;
|
|
@@ -29,19 +33,22 @@ export function sameBody(a, b) {
|
|
|
29
33
|
const norm = (h) => sanitiseHtml(h).replace(BLOCK_TAG, '$1').trim();
|
|
30
34
|
return norm(a) === norm(b);
|
|
31
35
|
}
|
|
32
|
-
export function registerEditTools(server, { chatsvc }) {
|
|
36
|
+
export function registerEditTools(server, { chatsvc, ...attach }) {
|
|
33
37
|
server.registerTool(TOOL, {
|
|
34
38
|
title: 'Edit a Teams message',
|
|
35
39
|
description: 'Replace the text of one of your own messages in any chat or channel. conversation is an id or name as ' +
|
|
36
40
|
'for teams_list_messages; message_id comes from teams_list_messages and must be a message you sent ' +
|
|
37
41
|
"(someone else's, a deleted one or a system event is refused). text replaces the whole message: plain " +
|
|
38
42
|
'text by default, or Markdown with format "markdown" (as teams_send_message reads it). mentions ' +
|
|
39
|
-
'@-mention people as in teams_send_message
|
|
43
|
+
'@-mention people (or the channel or its team) as in teams_send_message, and the preview checks them the same ' +
|
|
44
|
+
'way: an "@name" no mention covers is listed as unmatched_mentions above the text (not refused), with ' +
|
|
45
|
+
'mentions_summary and, per mention, its type, name and whether the person is in the conversation. A quote-reply keeps its quote. Text that renders exactly as ' +
|
|
40
46
|
'the message already does, mentioning the same people, changes nothing (status "unchanged"). Everyone in the conversation sees the ' +
|
|
41
47
|
'message marked edited. The result says whether reading the message back shows the edit (verified); ' +
|
|
42
48
|
'status "unknown" means Teams did not confirm it and the read-back did not show it — check the message ' +
|
|
43
49
|
`before retrying. conversation "${SELF_CHAT}" (or "notes" / "self") is the user's own notes self-chat — ` +
|
|
44
|
-
'use it to try this tool. ' +
|
|
50
|
+
'use it to try this tool. attachments adds files to the message as file cards (kept beside any it already has), ' +
|
|
51
|
+
'uploaded and shared exactly as teams_send_message does; the edit is never made without them. ' +
|
|
45
52
|
CONFIRM_FLOW_SENTENCE +
|
|
46
53
|
' The preview shows the conversation, the current text, the new text and exactly what will replace it. ' +
|
|
47
54
|
CONFIRM_INJECTION_RULE +
|
|
@@ -61,9 +68,10 @@ export function registerEditTools(server, { chatsvc }) {
|
|
|
61
68
|
.describe('The whole new message: plain text (newlines kept, :key: shortcodes become emoji), or Markdown with format "markdown".'),
|
|
62
69
|
format: formatParam,
|
|
63
70
|
mentions: z.array(mentionArg).max(20).optional().describe('People to @-mention in text.'),
|
|
71
|
+
attachments: attachmentsParam,
|
|
64
72
|
confirmToken: confirmTokenParam,
|
|
65
73
|
},
|
|
66
|
-
}, async ({ conversation, message_id, text, format = 'text', mentions, confirmToken }, ctx) => {
|
|
74
|
+
}, async ({ conversation, message_id, text, format = 'text', mentions, attachments, confirmToken }, ctx) => {
|
|
67
75
|
try {
|
|
68
76
|
const me = await chatsvc.meMri();
|
|
69
77
|
const target = await resolveTarget(chatsvc, conversation, me, 'edit a message in');
|
|
@@ -74,15 +82,27 @@ export function registerEditTools(server, { chatsvc }) {
|
|
|
74
82
|
});
|
|
75
83
|
}
|
|
76
84
|
const before = original.content ?? '';
|
|
77
|
-
|
|
85
|
+
// Every file is read, vetted and hashed on BOTH calls; nothing is uploaded here.
|
|
86
|
+
const plan = attachments !== undefined ? await planAttachments(chatsvc, attach, target, attachments, me) : undefined;
|
|
87
|
+
const senders = once(() => recentSenders(chatsvc, target.id));
|
|
88
|
+
const scope = await mentionScope(chatsvc, me, target, mentions ?? []);
|
|
89
|
+
const people = await resolveMentions(chatsvc, target.id, mentions ?? [], { scope, senders });
|
|
78
90
|
const rendered = renderMessageHtml(text, people.map((p) => p.target), format);
|
|
79
91
|
const content = quotePrefix(before) + rendered.html;
|
|
80
92
|
// A mention span names only who it shows, so the people are compared too.
|
|
81
93
|
const mentionedNow = rendered.mentions.map((m) => m.mri.toLowerCase());
|
|
82
94
|
const mentionedBefore = mentionsOf(original.properties?.mentions).map((m) => m.mri.toLowerCase());
|
|
83
|
-
if (sameBody(content, before) && mentionedNow.join('\n') === mentionedBefore.join('\n')) {
|
|
95
|
+
if (plan === undefined && sameBody(content, before) && mentionedNow.join('\n') === mentionedBefore.join('\n')) {
|
|
84
96
|
return untrustedResult({ status: 'unchanged', conversation: target, message_id });
|
|
85
97
|
}
|
|
98
|
+
const checked = await checkMentions(chatsvc, {
|
|
99
|
+
target,
|
|
100
|
+
me,
|
|
101
|
+
text,
|
|
102
|
+
mentions: people.map((p, i) => ({ ...p, name_in_text: mentions[i].name_in_text })),
|
|
103
|
+
scope,
|
|
104
|
+
senders,
|
|
105
|
+
});
|
|
86
106
|
const qtdMsgs = original.properties?.qtdMsgs;
|
|
87
107
|
const body = {
|
|
88
108
|
content,
|
|
@@ -92,6 +112,7 @@ export function registerEditTools(server, { chatsvc }) {
|
|
|
92
112
|
properties: {
|
|
93
113
|
mentions: rendered.mentions.length > 0 ? mentionsProperty(rendered.mentions) : '[]',
|
|
94
114
|
...(typeof qtdMsgs === 'string' && quotePrefix(before) !== '' ? { qtdMsgs } : {}),
|
|
115
|
+
...(plan !== undefined ? { files: mergedFilesProperty(original.properties?.files, []) } : {}),
|
|
95
116
|
},
|
|
96
117
|
};
|
|
97
118
|
const path = `/users/ME/conversations/${encodeURIComponent(target.id)}/messages/${encodeURIComponent(message_id)}`;
|
|
@@ -100,7 +121,9 @@ export function registerEditTools(server, { chatsvc }) {
|
|
|
100
121
|
action: 'chat.edit_message',
|
|
101
122
|
message: 'Review and confirm this edit before it replaces your Teams message:',
|
|
102
123
|
// The title is someone else's text: JSON-quoted so it cannot break out of its quotes.
|
|
103
|
-
summary: `Edit your message ${message_id} in ${target.kind} ${JSON.stringify(target.title)}
|
|
124
|
+
summary: `Edit your message ${message_id} in ${target.kind} ${JSON.stringify(target.title)}` +
|
|
125
|
+
(plan ? `, adding ${plan.files.length} attached file${plan.files.length === 1 ? '' : 's'}` : '') +
|
|
126
|
+
unmatchedNote(checked.unmatched),
|
|
104
127
|
account: me,
|
|
105
128
|
target: `${target.id}/${message_id}`,
|
|
106
129
|
request: { method: 'PUT', path, body },
|
|
@@ -109,20 +132,25 @@ export function registerEditTools(server, { chatsvc }) {
|
|
|
109
132
|
message_id,
|
|
110
133
|
// The user's own text, but a quote-reply's quote is someone else's.
|
|
111
134
|
current_text: untrustedField(htmlToText(before)),
|
|
135
|
+
// Mention warnings sit ABOVE the new text, where they are read first.
|
|
136
|
+
...checked.above,
|
|
112
137
|
text,
|
|
113
138
|
format,
|
|
114
|
-
...(people.length > 0
|
|
115
|
-
|
|
116
|
-
mentions: untrustedField(people.map((p, i) => ({ name_in_text: mentions[i].name_in_text, person: p.person, mri: p.target.mri }))),
|
|
117
|
-
}
|
|
118
|
-
: {}),
|
|
139
|
+
...(people.length > 0 ? { mentions: untrustedField(checked.mentions) } : {}),
|
|
140
|
+
...(plan ? { ...plan.preview, note: 'properties.files gets the new file cards once the files are uploaded.' } : {}),
|
|
119
141
|
},
|
|
120
142
|
confirmToken,
|
|
121
143
|
});
|
|
122
144
|
if (gate)
|
|
123
145
|
return gate;
|
|
146
|
+
// Confirmed: upload and share every file first; any failure throws, so
|
|
147
|
+
// the edit is never made without its files.
|
|
148
|
+
const uploaded = plan ? await deliverAttachments(attach, plan) : [];
|
|
149
|
+
if (uploaded.length > 0)
|
|
150
|
+
body.properties.files = mergedFilesProperty(original.properties?.files, uploaded);
|
|
124
151
|
const { status, ...settled } = await writeAndReadBack(() => chatsvc.editMessage(target.id, message_id, body), () => chatsvc.getMessage(target.id, message_id), (m) => readsAs(m, content), 'edit', 'the new text');
|
|
125
|
-
|
|
152
|
+
const files = plan ? await attachmentResult(plan, uploaded, status === 'done') : {};
|
|
153
|
+
return untrustedResult({ status: status === 'done' ? 'edited' : 'unknown', conversation: target, message_id, ...files, ...settled });
|
|
126
154
|
}
|
|
127
155
|
catch (err) {
|
|
128
156
|
throw wrapApiError(err, 'edit the message');
|
package/dist/tools/errors.js
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import { McpToolError } from '@chrischall/mcp-utils';
|
|
2
|
+
import { captureFailure, TokenCaptureError } from '../capture-feedback.js';
|
|
2
3
|
/**
|
|
3
4
|
* Classify a chat-service failure. The client already raises actionable
|
|
4
5
|
* `McpToolError`s (401 after re-capture, 429, 5xx), which pass through as-is;
|
|
5
|
-
* what is left is the token capture, which fails as a
|
|
6
|
-
* `
|
|
6
|
+
* what is left is the token capture, which fails as a `TokenCaptureError` (or,
|
|
7
|
+
* from an older path or a test, a plain `Error` reading "could not capture …").
|
|
8
|
+
* That becomes `captureFailure`'s error: the step that failed, how long it
|
|
9
|
+
* waited, a stable `token_<step>` code, and the one next thing to do — acting
|
|
10
|
+
* WHILE the retry waits, since a capture only sees requests made during it.
|
|
7
11
|
*/
|
|
8
12
|
export function wrapApiError(err, action) {
|
|
9
13
|
if (err instanceof McpToolError)
|
|
10
14
|
return err;
|
|
11
15
|
const message = err instanceof Error ? err.message : String(err);
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
if (err instanceof TokenCaptureError || /could not capture a .*token/i.test(message)) {
|
|
17
|
+
return captureFailure(err, action);
|
|
18
|
+
}
|
|
19
|
+
// A pairing prompt from anywhere else on the bridge path: approving it is the fix.
|
|
14
20
|
if (/pair code|not granted|not in declared/i.test(message)) {
|
|
15
21
|
return new McpToolError(`The Teams browser bridge is not yet approved: ${message}`, {
|
|
16
22
|
hint: 'Approve the pairing request in the ContextMint Bridge extension popup, then retry.',
|
|
17
23
|
});
|
|
18
24
|
}
|
|
19
|
-
if (/could not capture a Teams token/i.test(message)) {
|
|
20
|
-
return new McpToolError(`Could not get a Teams access token to ${action}.`, {
|
|
21
|
-
hint: 'Open a signed-in teams.cloud.microsoft tab and click into any chat (that is what makes ' +
|
|
22
|
-
'Teams call its chat service, which is where the token is read from), then retry.',
|
|
23
|
-
cause: err,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
25
|
return new McpToolError(`Failed to ${action}: ${message}`);
|
|
27
26
|
}
|
package/dist/tools/export.js
CHANGED
|
@@ -53,6 +53,8 @@ import { conversationName, enrichTitles } from './titles.js';
|
|
|
53
53
|
import { resolveTimeZone, tzArg } from './timezone.js';
|
|
54
54
|
import { untrustedResult, UNTRUSTED_DESCRIPTION_SUFFIX } from './untrusted.js';
|
|
55
55
|
import { workMeta } from './hints.js';
|
|
56
|
+
import { assertSenderEmail, SENDER_EMAIL_DESCRIPTION, senderEmailArg, senderEmailReport } from './sender-email-arg.js';
|
|
57
|
+
import { withSenderEmails } from '../sender-email.js';
|
|
56
58
|
export const EXPORT_SERVER_NAME = 'microsoft-teams-mcp';
|
|
57
59
|
export const EXPORT_OUTPUT_ENV = 'TEAMS_OUTPUT_DIR';
|
|
58
60
|
export const DEFAULT_MAX_ITEMS = 5000;
|
|
@@ -103,6 +105,7 @@ const storedArgs = z
|
|
|
103
105
|
until: isoTimeArg('until'),
|
|
104
106
|
format: z.enum(EXPORT_FORMATS),
|
|
105
107
|
attachments: z.boolean().optional(),
|
|
108
|
+
sender_email: z.boolean().optional(),
|
|
106
109
|
tz: tzArg,
|
|
107
110
|
})
|
|
108
111
|
.strict();
|
|
@@ -206,8 +209,8 @@ function chatParticipants(titled, messages) {
|
|
|
206
209
|
return out;
|
|
207
210
|
}
|
|
208
211
|
/** Arguments that start an export, which a cursor already carries. */
|
|
209
|
-
const STARTING_ARGS = ['conversations', 'kinds', 'since', 'until', 'format', 'attachments', 'dest', 'tz'];
|
|
210
|
-
async function runExport({ chatsvc, sharepoint }, input, opts) {
|
|
212
|
+
const STARTING_ARGS = ['conversations', 'kinds', 'since', 'until', 'format', 'attachments', 'sender_email', 'dest', 'tz'];
|
|
213
|
+
async function runExport({ chatsvc, sharepoint, substrate }, input, opts) {
|
|
211
214
|
const clock = opts.now ?? Date.now;
|
|
212
215
|
const sleep = opts.sleep ?? realSleep;
|
|
213
216
|
const started = clock();
|
|
@@ -228,6 +231,7 @@ async function runExport({ chatsvc, sharepoint }, input, opts) {
|
|
|
228
231
|
...(input.until !== undefined ? { until: input.until } : {}),
|
|
229
232
|
format: input.format ?? 'jsonl',
|
|
230
233
|
...(input.attachments === true ? { attachments: true } : {}),
|
|
234
|
+
...(input.sender_email === true ? { sender_email: true } : {}),
|
|
231
235
|
...(input.tz !== undefined ? { tz: input.tz } : {}),
|
|
232
236
|
};
|
|
233
237
|
const since = args.since !== undefined ? parseIsoTime(args.since) : undefined;
|
|
@@ -236,6 +240,16 @@ async function runExport({ chatsvc, sharepoint }, input, opts) {
|
|
|
236
240
|
throw new McpToolError('since is after until.', { hint: 'Pass a since at or before until.' });
|
|
237
241
|
}
|
|
238
242
|
const timeZone = resolveTimeZone(args.tz);
|
|
243
|
+
// Markdown renders no sender email, so the lookups (which can hold the call
|
|
244
|
+
// for a token capture) would buy nothing: refuse the pair up front.
|
|
245
|
+
if (args.sender_email === true && args.format === 'markdown') {
|
|
246
|
+
throw new McpToolError('sender_email needs format "jsonl" (the markdown export shows no sender email).', {
|
|
247
|
+
hint: 'Drop sender_email, or export as jsonl.',
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
assertSenderEmail(args.sender_email, 'full', substrate);
|
|
251
|
+
/** What looking up sender emails could not do, across the conversations exported. */
|
|
252
|
+
const senderEmails = {};
|
|
239
253
|
const maxItems = input.max_items ?? DEFAULT_MAX_ITEMS;
|
|
240
254
|
const budgetMs = (input.budget_seconds ?? DEFAULT_BUDGET_SECONDS) * 1000;
|
|
241
255
|
let raw;
|
|
@@ -371,6 +385,14 @@ async function runExport({ chatsvc, sharepoint }, input, opts) {
|
|
|
371
385
|
}
|
|
372
386
|
if (page.length > 0) {
|
|
373
387
|
const normalised = page.map((m) => normaliseMessage(m, me, { timeZone }));
|
|
388
|
+
// After one directory failure the rest of this call goes without, rather than failing again per conversation.
|
|
389
|
+
if (args.sender_email === true && substrate !== undefined && senderEmails.error === undefined) {
|
|
390
|
+
const r = await withSenderEmails(substrate, normalised);
|
|
391
|
+
if (r.error !== undefined)
|
|
392
|
+
senderEmails.error = r.error;
|
|
393
|
+
if (r.unresolved !== undefined)
|
|
394
|
+
senderEmails.unresolved = (senderEmails.unresolved ?? 0) + r.unresolved;
|
|
395
|
+
}
|
|
374
396
|
let messages = normalised;
|
|
375
397
|
if (args.attachments === true) {
|
|
376
398
|
const got = await exportAttachments({ chatsvc, sharepoint }, sink, shortId(id), id, normalised);
|
|
@@ -478,6 +500,7 @@ async function runExport({ chatsvc, sharepoint }, input, opts) {
|
|
|
478
500
|
...(cursor !== undefined ? { cursor } : {}),
|
|
479
501
|
...(errors.length > 0 ? { errors: errors.slice(0, RESULT_ERRORS_CAP) } : {}),
|
|
480
502
|
...(errors.length > RESULT_ERRORS_CAP ? { errors_truncated: errors.length - RESULT_ERRORS_CAP } : {}),
|
|
503
|
+
...senderEmailReport(senderEmails),
|
|
481
504
|
});
|
|
482
505
|
}
|
|
483
506
|
export function registerExportTools(server, deps, opts = {}) {
|
|
@@ -506,7 +529,10 @@ export function registerExportTools(server, deps, opts = {}) {
|
|
|
506
529
|
'With attachments true, inline images and shared files (up to 25 MB) are saved under ' +
|
|
507
530
|
'attachments/<conversation>/<message id>-<name> and each message\'s record lists them by that path ' +
|
|
508
531
|
'relative to the export folder; one that cannot be fetched (a SharePoint file the account cannot ' +
|
|
509
|
-
'open, no token yet, too large) is listed as skipped with the reason, never failing the export.' +
|
|
532
|
+
'open, no token yet, too large) is listed as skipped with the reason, never failing the export. ' +
|
|
533
|
+
'Every JSONL record carries senderMri (the sender\'s MRI).' +
|
|
534
|
+
SENDER_EMAIL_DESCRIPTION +
|
|
535
|
+
' (jsonl only: refused with format "markdown", which shows no email).' +
|
|
510
536
|
UNTRUSTED_DESCRIPTION_SUFFIX,
|
|
511
537
|
annotations: toolAnnotations({ readOnly: true, openWorld: true }),
|
|
512
538
|
_meta: workMeta('other', { aliases: ['export', 'save the chat', 'chat history'] }),
|
|
@@ -531,6 +557,7 @@ export function registerExportTools(server, deps, opts = {}) {
|
|
|
531
557
|
.describe('Also download each message\'s inline images and shared files (up to 25 MB each) into ' +
|
|
532
558
|
'attachments/<conversation>/<message id>-<name>; each record lists them with that relative path, ' +
|
|
533
559
|
'or skipped and why. Default false.'),
|
|
560
|
+
sender_email: senderEmailArg,
|
|
534
561
|
dest: z
|
|
535
562
|
.string()
|
|
536
563
|
.min(1)
|
package/dist/tools/format.js
CHANGED
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
* summary), or — when it could not be fetched — a `{ id, missing: true }`
|
|
18
18
|
* placeholder (`root_missing`).
|
|
19
19
|
*
|
|
20
|
+
* Every format reports `message_count` (the messages read, never an
|
|
21
|
+
* outside-window root). In full and compact a channel ALSO lists its messages
|
|
22
|
+
* flat, as `messages` (each with `thread_root_id`), beside `threads` — so
|
|
23
|
+
* reading `messages` works the same for a chat and a channel.
|
|
24
|
+
*
|
|
20
25
|
* Every format keeps the conversation's id, and everything else a read reports
|
|
21
26
|
* (unread_count, next_cursor, truncated, …) is the same in all three: the
|
|
22
27
|
* format only changes how the messages are shown, never which were read.
|
|
@@ -31,7 +36,9 @@ export const formatArg = z
|
|
|
31
36
|
'summary: no messages, just per conversation its counts, newest message time and, for a channel, ' +
|
|
32
37
|
'each thread\'s root id, subject, reply count and newest time.');
|
|
33
38
|
/** Shared description text for the `format` argument. */
|
|
34
|
-
export const FORMAT_DESCRIPTION = '
|
|
39
|
+
export const FORMAT_DESCRIPTION = 'In full and compact a channel lists its messages flat (messages, each with thread_root_id) as well as threads; ' +
|
|
40
|
+
'every format has message_count. ' +
|
|
41
|
+
'format "compact" trims each message to id, time, sender and text, and the conversation to id, kind ' +
|
|
35
42
|
'and title; format "summary" returns no messages, only message_count, newest_time and, for a channel, ' +
|
|
36
43
|
'threads of { root_id, subject, reply_count, newest_time } — every format keeps the conversation id. ';
|
|
37
44
|
const NO_ROOTS = new Map();
|
|
@@ -117,10 +124,18 @@ export function formatConversation(summary, format) {
|
|
|
117
124
|
const { id, kind, title, team } = summary;
|
|
118
125
|
return { id, kind, title, ...(team !== undefined ? { team } : {}) };
|
|
119
126
|
}
|
|
127
|
+
/** Which thread a channel message belongs to: its root's id (a root's own id). */
|
|
128
|
+
const withRoot = (m, shown) => ({
|
|
129
|
+
...shown,
|
|
130
|
+
thread_root_id: m.replyTo ?? m.id,
|
|
131
|
+
});
|
|
120
132
|
/**
|
|
121
|
-
* A conversation's oldest-first messages as a result shows them
|
|
122
|
-
*
|
|
123
|
-
*
|
|
133
|
+
* A conversation's oldest-first messages as a result shows them, in full or
|
|
134
|
+
* compact: `message_count` and `messages` for every kind — for a channel the
|
|
135
|
+
* flat list (roots and replies in time order, each with `thread_root_id`) AND
|
|
136
|
+
* its `threads` (with `roots` fetched from outside the window) — or, in
|
|
137
|
+
* summary, just the counts. A root from outside the window is context: it
|
|
138
|
+
* shows in `threads` only, never in `messages` or `message_count`.
|
|
124
139
|
*/
|
|
125
140
|
export function formatBody(kind, messages, format, roots = NO_ROOTS) {
|
|
126
141
|
const channel = kind === 'channel';
|
|
@@ -133,13 +148,21 @@ export function formatBody(kind, messages, format, roots = NO_ROOTS) {
|
|
|
133
148
|
}
|
|
134
149
|
if (format === 'compact') {
|
|
135
150
|
if (!channel)
|
|
136
|
-
return { messages: messages.map(compactMessage) };
|
|
151
|
+
return { message_count: messages.length, messages: messages.map(compactMessage) };
|
|
137
152
|
return {
|
|
153
|
+
message_count: messages.length,
|
|
154
|
+
messages: messages.map((m) => withRoot(m, compactMessage(m))),
|
|
138
155
|
threads: groupThreads(messages, roots).map(({ root, replies }) => ({
|
|
139
156
|
root: compactRoot(root),
|
|
140
157
|
replies: replies.map(compactMessage),
|
|
141
158
|
})),
|
|
142
159
|
};
|
|
143
160
|
}
|
|
144
|
-
|
|
161
|
+
if (!channel)
|
|
162
|
+
return { message_count: messages.length, messages: [...messages] };
|
|
163
|
+
return {
|
|
164
|
+
message_count: messages.length,
|
|
165
|
+
messages: messages.map((m) => withRoot(m, m)),
|
|
166
|
+
threads: groupThreads(messages, roots),
|
|
167
|
+
};
|
|
145
168
|
}
|