@openclaw/mattermost 2026.9.6 → 2026.9.8
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/assets/icon.png +0 -0
- package/dist/.setup/{channel-plugin-api-CCmaCFdK.mjs → channel-plugin-api-Bc8Qc1-t.mjs} +2 -2
- package/dist/.setup/{channel-plugin-api-CzBhdovL.mjs → channel-plugin-api-DXqHarCt.mjs} +2 -2
- package/dist/.setup/{channel-plugin-runtime-yAOg5GcM.mjs → channel-plugin-runtime-B0CAMiHe.mjs} +122 -160
- package/dist/.setup/channel.runtime-E2B5dNrh.mjs +383 -0
- package/dist/.setup/{client-BSSVve1x.mjs → client-CVeeTqCO.mjs} +18 -39
- package/dist/.setup/doctor-contract-BI6SB2cd.mjs +175 -0
- package/dist/.setup/{interactions-rBQWCbK8.mjs → interactions-DMSZYnEx.mjs} +170 -80
- package/dist/.setup/{channel.runtime-CihvzY84.mjs → monitor-BZyzPkkb.mjs} +172 -628
- package/dist/.setup/{monitor-auth-CCpN0z_f.mjs → monitor-auth-aZ0b1gqx.mjs} +9 -14
- package/dist/.setup/{normalize-DM54w7ud.mjs → normalize-BCjtmxtp.mjs} +8 -17
- package/dist/.setup/rolldown-runtime-BBjsoOtd.mjs +27 -0
- package/dist/.setup/send-ClgX-usq.mjs +438 -0
- package/dist/.setup/{slash-state-DfnmzXMr.mjs → slash-state-Dni1XNc1.mjs} +144 -781
- package/dist/api.js +2 -2
- package/dist/channel-plugin-api.js +2 -2
- package/dist/channel-plugin-runtime.js +1 -1
- package/dist/doctor-contract-api.js +1 -1
- package/dist/policy-api.js +1 -1
- package/dist/runtime-api.js +2 -23
- package/dist/setup-entry.js +1 -1
- package/dist/slash-route-api.js +1 -1
- package/openclaw.plugin.json +20 -0
- package/package.json +4 -4
- package/dist/.setup/doctor-contract-mvORF4f6.mjs +0 -18
|
@@ -152,26 +152,21 @@ async function authorizeMattermostCommandInvocation(params) {
|
|
|
152
152
|
kind,
|
|
153
153
|
dmPolicy: account.config.dmPolicy ?? "pairing"
|
|
154
154
|
});
|
|
155
|
-
if (denyReason) return {
|
|
156
|
-
ok: false,
|
|
157
|
-
denyReason,
|
|
158
|
-
commandAuthorized: false,
|
|
159
|
-
channelInfo,
|
|
160
|
-
kind,
|
|
161
|
-
chatType,
|
|
162
|
-
channelName,
|
|
163
|
-
channelDisplay,
|
|
164
|
-
roomLabel
|
|
165
|
-
};
|
|
166
155
|
return {
|
|
167
|
-
ok: true,
|
|
168
|
-
commandAuthorized: ingress.commandAccess.authorized,
|
|
169
156
|
channelInfo,
|
|
170
157
|
kind,
|
|
171
158
|
chatType,
|
|
172
159
|
channelName,
|
|
173
160
|
channelDisplay,
|
|
174
|
-
roomLabel
|
|
161
|
+
roomLabel,
|
|
162
|
+
...denyReason ? {
|
|
163
|
+
ok: false,
|
|
164
|
+
denyReason,
|
|
165
|
+
commandAuthorized: false
|
|
166
|
+
} : {
|
|
167
|
+
ok: true,
|
|
168
|
+
commandAuthorized: ingress.commandAccess.authorized
|
|
169
|
+
}
|
|
175
170
|
};
|
|
176
171
|
}
|
|
177
172
|
//#endregion
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { h as normalizeMattermostBaseUrl } from "./client-
|
|
1
|
+
import { h as normalizeMattermostBaseUrl } from "./client-CVeeTqCO.mjs";
|
|
2
2
|
import { normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
3
3
|
import { resolveChannelPreviewStreamMode, resolveChannelStreamingBlockCoalesce, resolveChannelStreamingBlockEnabled, resolveChannelStreamingChunkMode } from "openclaw/plugin-sdk/channel-outbound";
|
|
4
4
|
import { createAccountListHelpers, hasConfiguredAccountValue } from "openclaw/plugin-sdk/account-helpers";
|
|
5
|
+
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id";
|
|
5
6
|
import { normalizeMessagePresentation, renderMessagePresentationFallbackText, resolveMessagePresentationControlValue } from "openclaw/plugin-sdk/interactive-runtime";
|
|
6
7
|
import { resolveAskUserQuestionOptionIndex, resolveAskUserQuestionOptionIndices } from "openclaw/plugin-sdk/reply-payload";
|
|
7
|
-
import {
|
|
8
|
-
import { buildSecretInputSchema, hasConfiguredSecretInput, resolveSecretInputString } from "openclaw/plugin-sdk/secret-input";
|
|
8
|
+
import { resolveSecretInputString } from "openclaw/plugin-sdk/secret-input";
|
|
9
9
|
//#region extensions/mattermost/src/mattermost/accounts.ts
|
|
10
10
|
const { listAccountIds: listMattermostAccountIds, resolveDefaultAccountId: resolveDefaultMattermostAccountId, resolveAccountConfig: mergeMattermostAccountConfig } = createAccountListHelpers("mattermost", {
|
|
11
11
|
omitKeys: ["defaultAccount"],
|
|
@@ -157,27 +157,18 @@ function normalizeMattermostMessagingTarget(raw) {
|
|
|
157
157
|
const trimmed = raw.trim();
|
|
158
158
|
if (!trimmed) return;
|
|
159
159
|
const lower = normalizeLowercaseStringOrEmpty(trimmed);
|
|
160
|
-
if (lower.startsWith("channel:")) {
|
|
161
|
-
const id = trimmed.slice(
|
|
162
|
-
return id ? `channel:${id}` : void 0;
|
|
163
|
-
}
|
|
164
|
-
if (lower.startsWith("group:")) {
|
|
165
|
-
const id = trimmed.slice(6).trim();
|
|
160
|
+
if (lower.startsWith("channel:") || lower.startsWith("group:")) {
|
|
161
|
+
const id = trimmed.slice(trimmed.indexOf(":") + 1).trim();
|
|
166
162
|
return id ? `channel:${id}` : void 0;
|
|
167
163
|
}
|
|
168
|
-
if (lower.startsWith("user:")) {
|
|
169
|
-
const id = trimmed.slice(
|
|
170
|
-
return id ? `user:${id}` : void 0;
|
|
171
|
-
}
|
|
172
|
-
if (lower.startsWith("mattermost:")) {
|
|
173
|
-
const id = trimmed.slice(11).trim();
|
|
164
|
+
if (lower.startsWith("user:") || lower.startsWith("mattermost:")) {
|
|
165
|
+
const id = trimmed.slice(trimmed.indexOf(":") + 1).trim();
|
|
174
166
|
return id ? `user:${id}` : void 0;
|
|
175
167
|
}
|
|
176
168
|
if (trimmed.startsWith("@")) {
|
|
177
169
|
const id = trimmed.slice(1).trim();
|
|
178
170
|
return id ? `@${id}` : void 0;
|
|
179
171
|
}
|
|
180
|
-
if (trimmed.startsWith("#")) return;
|
|
181
172
|
}
|
|
182
173
|
/**
|
|
183
174
|
* True when media must be uploaded as a file: any non-empty, non-http(s) value
|
|
@@ -196,4 +187,4 @@ function looksLikeMattermostTargetId(raw, _normalized) {
|
|
|
196
187
|
return /^[a-z0-9]{26}$/i.test(trimmed) || /^[a-z0-9]{26}__[a-z0-9]{26}$/i.test(trimmed);
|
|
197
188
|
}
|
|
198
189
|
//#endregion
|
|
199
|
-
export { resolveMattermostPresentation as a, listMattermostAccountIds as c, resolveMattermostReplyToMode as d,
|
|
190
|
+
export { resolveMattermostPresentation as a, listMattermostAccountIds as c, resolveMattermostReplyToMode as d, requiresMattermostMediaUpload as i, resolveDefaultMattermostAccountId as l, normalizeMattermostMessagingTarget as n, inspectMattermostAccount as o, parseMattermostQuestionContext as r, isMattermostConfigured as s, looksLikeMattermostTargetId as t, resolveMattermostAccount as u };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __exportAll = (all, no_symbols) => {
|
|
7
|
+
let target = {};
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
17
|
+
key = keys[i];
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
19
|
+
get: ((k) => from[k]).bind(null, key),
|
|
20
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
26
|
+
//#endregion
|
|
27
|
+
export { __reExport as n, __exportAll as t };
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import { c as setInteractionSecret, n as buildButtonProps, o as resolveInteractionCallbackUrl } from "./interactions-DMSZYnEx.mjs";
|
|
2
|
+
import { t as getMattermostRuntime } from "./runtime-CNB4YGqJ.mjs";
|
|
3
|
+
import { a as createMattermostPost, b as uploadMattermostFile, c as fetchMattermostChannelByName, d as fetchMattermostUser, f as fetchMattermostUserByUsername, g as parseMattermostApiStatus, h as normalizeMattermostBaseUrl, i as createMattermostDirectChannelWithRetry, p as fetchMattermostUserTeams, r as createMattermostClient, s as fetchMattermostChannel, u as fetchMattermostMe } from "./client-CVeeTqCO.mjs";
|
|
4
|
+
import { o as resolveMattermostTrustedChatKind, v as loadOutboundMediaFromUrl } from "./monitor-auth-aZ0b1gqx.mjs";
|
|
5
|
+
import { u as resolveMattermostAccount } from "./normalize-BCjtmxtp.mjs";
|
|
6
|
+
import { normalizeLowercaseStringOrEmpty, normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
7
|
+
import { createChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound";
|
|
8
|
+
import { isPrivateNetworkOptInEnabled } from "openclaw/plugin-sdk/ssrf-runtime";
|
|
9
|
+
import { createMessageReceiptFromOutboundResults, listMessageReceiptPlatformIds } from "openclaw/plugin-sdk/channel-outbound";
|
|
10
|
+
import { resolveChannelMediaMaxBytes } from "openclaw/plugin-sdk/account-helpers";
|
|
11
|
+
import { FormatCapabilityProfile, convertMarkdownTables } from "openclaw/plugin-sdk/text-chunking";
|
|
12
|
+
import { pruneMapToMaxSize } from "openclaw/plugin-sdk/collection-runtime";
|
|
13
|
+
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/markdown-table-runtime";
|
|
14
|
+
import { extensionForMime } from "openclaw/plugin-sdk/media-mime";
|
|
15
|
+
import { requireRuntimeConfig } from "openclaw/plugin-sdk/plugin-config-runtime";
|
|
16
|
+
//#region extensions/mattermost/src/mattermost/target-resolution.ts
|
|
17
|
+
const MATTERMOST_OPAQUE_TARGET_CACHE_MAX_ENTRIES = 1024;
|
|
18
|
+
const MATTERMOST_OPAQUE_TARGET_CACHE_TTL_MS = 3e5;
|
|
19
|
+
const mattermostOpaqueTargetCache = /* @__PURE__ */ new Map();
|
|
20
|
+
function cacheMattermostOpaqueTarget(key, kind) {
|
|
21
|
+
mattermostOpaqueTargetCache.set(key, {
|
|
22
|
+
kind,
|
|
23
|
+
expiresAt: Date.now() + MATTERMOST_OPAQUE_TARGET_CACHE_TTL_MS
|
|
24
|
+
});
|
|
25
|
+
pruneMapToMaxSize(mattermostOpaqueTargetCache, MATTERMOST_OPAQUE_TARGET_CACHE_MAX_ENTRIES);
|
|
26
|
+
}
|
|
27
|
+
function getCachedMattermostOpaqueTargetKind(key) {
|
|
28
|
+
const cached = mattermostOpaqueTargetCache.get(key);
|
|
29
|
+
if (!cached) return;
|
|
30
|
+
if (cached.expiresAt <= Date.now()) {
|
|
31
|
+
mattermostOpaqueTargetCache.delete(key);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
return cached.kind;
|
|
35
|
+
}
|
|
36
|
+
function cacheKey$1(baseUrl, token, id) {
|
|
37
|
+
return `${baseUrl}::${token}::${id}`;
|
|
38
|
+
}
|
|
39
|
+
/** Mattermost IDs are 26-character lowercase alphanumeric strings. */
|
|
40
|
+
function isMattermostId(value) {
|
|
41
|
+
return /^[a-z0-9]{26}$/.test(value);
|
|
42
|
+
}
|
|
43
|
+
function parseMattermostTarget(raw) {
|
|
44
|
+
const trimmed = raw.trim();
|
|
45
|
+
if (!trimmed) throw new Error("Recipient is required for Mattermost sends");
|
|
46
|
+
const lower = normalizeLowercaseStringOrEmpty(trimmed);
|
|
47
|
+
if (lower.startsWith("channel:")) {
|
|
48
|
+
const id = trimmed.slice(8).trim();
|
|
49
|
+
if (!id) throw new Error("Channel id is required for Mattermost sends");
|
|
50
|
+
if (id.startsWith("#")) {
|
|
51
|
+
const name = id.slice(1).trim();
|
|
52
|
+
if (!name) throw new Error("Channel name is required for Mattermost sends");
|
|
53
|
+
return {
|
|
54
|
+
kind: "channel-name",
|
|
55
|
+
name
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (!isMattermostId(id)) return {
|
|
59
|
+
kind: "channel-name",
|
|
60
|
+
name: id
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
kind: "channel",
|
|
64
|
+
id
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (lower.startsWith("user:") || lower.startsWith("mattermost:")) {
|
|
68
|
+
const id = trimmed.slice(trimmed.indexOf(":") + 1).trim();
|
|
69
|
+
if (!id) throw new Error("User id is required for Mattermost sends");
|
|
70
|
+
return {
|
|
71
|
+
kind: "user",
|
|
72
|
+
id
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (trimmed.startsWith("@")) {
|
|
76
|
+
const username = trimmed.slice(1).trim();
|
|
77
|
+
if (!username) throw new Error("Username is required for Mattermost sends");
|
|
78
|
+
return {
|
|
79
|
+
kind: "user",
|
|
80
|
+
username
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (trimmed.startsWith("#")) {
|
|
84
|
+
const name = trimmed.slice(1).trim();
|
|
85
|
+
if (!name) throw new Error("Channel name is required for Mattermost sends");
|
|
86
|
+
return {
|
|
87
|
+
kind: "channel-name",
|
|
88
|
+
name
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (!isMattermostId(trimmed)) return {
|
|
92
|
+
kind: "channel-name",
|
|
93
|
+
name: trimmed
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
kind: "channel",
|
|
97
|
+
id: trimmed
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async function resolveMattermostOpaqueTarget(params) {
|
|
101
|
+
const input = params.input.trim();
|
|
102
|
+
if (!isMattermostId(input)) return null;
|
|
103
|
+
let client;
|
|
104
|
+
if ("client" in params) client = params.client;
|
|
105
|
+
else {
|
|
106
|
+
const account = resolveMattermostAccount({
|
|
107
|
+
cfg: params.cfg,
|
|
108
|
+
accountId: params.accountId
|
|
109
|
+
});
|
|
110
|
+
if (!account.enabled) throw new Error(`Mattermost account "${account.accountId}" is disabled`);
|
|
111
|
+
const token = normalizeOptionalString(account.botToken);
|
|
112
|
+
const baseUrl = normalizeMattermostBaseUrl(account.baseUrl);
|
|
113
|
+
if (!token || !baseUrl) return null;
|
|
114
|
+
client = createMattermostClient({
|
|
115
|
+
baseUrl,
|
|
116
|
+
botToken: token,
|
|
117
|
+
allowPrivateNetwork: isPrivateNetworkOptInEnabled(account.config)
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const key = cacheKey$1(client.baseUrl, client.token, input);
|
|
121
|
+
const cachedKind = getCachedMattermostOpaqueTargetKind(key);
|
|
122
|
+
if (cachedKind) return {
|
|
123
|
+
kind: cachedKind,
|
|
124
|
+
id: input,
|
|
125
|
+
to: cachedKind === "user" ? `user:${input}` : `channel:${input}`
|
|
126
|
+
};
|
|
127
|
+
try {
|
|
128
|
+
await fetchMattermostUser(client, input);
|
|
129
|
+
cacheMattermostOpaqueTarget(key, "user");
|
|
130
|
+
return {
|
|
131
|
+
kind: "user",
|
|
132
|
+
id: input,
|
|
133
|
+
to: `user:${input}`
|
|
134
|
+
};
|
|
135
|
+
} catch (err) {
|
|
136
|
+
if (parseMattermostApiStatus(err) !== 404) return {
|
|
137
|
+
kind: "channel",
|
|
138
|
+
id: input,
|
|
139
|
+
to: `channel:${input}`
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const channel = await fetchMattermostChannel(client, input);
|
|
144
|
+
const channelKind = resolveMattermostTrustedChatKind({ channelType: channel.type }) === "group" ? "group" : "channel";
|
|
145
|
+
cacheMattermostOpaqueTarget(key, channelKind);
|
|
146
|
+
return {
|
|
147
|
+
kind: channelKind,
|
|
148
|
+
id: input,
|
|
149
|
+
to: `channel:${input}`
|
|
150
|
+
};
|
|
151
|
+
} catch {
|
|
152
|
+
return {
|
|
153
|
+
kind: "channel",
|
|
154
|
+
id: input,
|
|
155
|
+
to: `channel:${input}`
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region extensions/mattermost/src/mattermost/send.ts
|
|
161
|
+
const MATTERMOST_BOT_USER_CACHE_MAX_ENTRIES = 64;
|
|
162
|
+
const MATTERMOST_TARGET_CACHE_MAX_ENTRIES = 1024;
|
|
163
|
+
const MATTERMOST_FORMAT_PROFILE = FormatCapabilityProfile.define({
|
|
164
|
+
mechanism: "markdown",
|
|
165
|
+
chunk: {
|
|
166
|
+
limit: 16383,
|
|
167
|
+
unit: "chars"
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
function renderMattermostMarkdown(markdown, tableMode) {
|
|
171
|
+
return tableMode === "off" && MATTERMOST_FORMAT_PROFILE.constructs.table === "native" ? markdown : convertMarkdownTables(markdown, tableMode);
|
|
172
|
+
}
|
|
173
|
+
const botUserCache = /* @__PURE__ */ new Map();
|
|
174
|
+
const userByNameCache = /* @__PURE__ */ new Map();
|
|
175
|
+
const channelByNameCache = /* @__PURE__ */ new Map();
|
|
176
|
+
const dmChannelCache = /* @__PURE__ */ new Map();
|
|
177
|
+
function cacheOutboundEntry(cache, key, value, maxEntries) {
|
|
178
|
+
cache.delete(key);
|
|
179
|
+
cache.set(key, value);
|
|
180
|
+
pruneMapToMaxSize(cache, maxEntries);
|
|
181
|
+
}
|
|
182
|
+
function createMattermostSendReceipt(params) {
|
|
183
|
+
return createMessageReceiptFromOutboundResults({
|
|
184
|
+
kind: params.kind,
|
|
185
|
+
...params.replyToId ? { replyToId: params.replyToId } : {},
|
|
186
|
+
results: [{
|
|
187
|
+
channel: "mattermost",
|
|
188
|
+
messageId: params.messageId,
|
|
189
|
+
channelId: params.channelId
|
|
190
|
+
}]
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
function resolveMattermostReceiptKind(params) {
|
|
194
|
+
if (params.fileIds?.length) return "media";
|
|
195
|
+
if (params.buttons?.length || params.props) return "card";
|
|
196
|
+
return "text";
|
|
197
|
+
}
|
|
198
|
+
function recordMattermostOutboundActivity(accountId) {
|
|
199
|
+
try {
|
|
200
|
+
getMattermostRuntime().channel.activity.record({
|
|
201
|
+
channel: "mattermost",
|
|
202
|
+
accountId,
|
|
203
|
+
direction: "outbound"
|
|
204
|
+
});
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (!(error instanceof Error) || error.message !== "Mattermost runtime not initialized") throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function cacheKey(baseUrl, token) {
|
|
210
|
+
return `${baseUrl}::${token}`;
|
|
211
|
+
}
|
|
212
|
+
function normalizeMessage(text, mediaUrl) {
|
|
213
|
+
return [normalizeOptionalString(text) ?? "", normalizeOptionalString(mediaUrl)].filter(Boolean).join("\n");
|
|
214
|
+
}
|
|
215
|
+
function isHttpUrl(value) {
|
|
216
|
+
return /^https?:\/\//i.test(value);
|
|
217
|
+
}
|
|
218
|
+
async function resolveBotUser(client) {
|
|
219
|
+
const key = cacheKey(client.baseUrl, client.token);
|
|
220
|
+
const cached = botUserCache.get(key);
|
|
221
|
+
if (cached) return cached;
|
|
222
|
+
const user = await fetchMattermostMe(client);
|
|
223
|
+
cacheOutboundEntry(botUserCache, key, user, MATTERMOST_BOT_USER_CACHE_MAX_ENTRIES);
|
|
224
|
+
return user;
|
|
225
|
+
}
|
|
226
|
+
async function resolveUserIdByUsername(params) {
|
|
227
|
+
const { client, username } = params;
|
|
228
|
+
const key = `${cacheKey(client.baseUrl, client.token)}::${normalizeLowercaseStringOrEmpty(username)}`;
|
|
229
|
+
const cached = userByNameCache.get(key);
|
|
230
|
+
if (cached?.id) return cached.id;
|
|
231
|
+
const user = await fetchMattermostUserByUsername(client, username);
|
|
232
|
+
cacheOutboundEntry(userByNameCache, key, user, MATTERMOST_TARGET_CACHE_MAX_ENTRIES);
|
|
233
|
+
return user.id;
|
|
234
|
+
}
|
|
235
|
+
async function resolveChannelIdByName(params) {
|
|
236
|
+
const { client, name } = params;
|
|
237
|
+
const key = `${cacheKey(client.baseUrl, client.token)}::channel::${normalizeLowercaseStringOrEmpty(name)}`;
|
|
238
|
+
const cached = channelByNameCache.get(key);
|
|
239
|
+
if (cached) return cached;
|
|
240
|
+
const me = await fetchMattermostMe(client);
|
|
241
|
+
const teams = await fetchMattermostUserTeams(client, me.id);
|
|
242
|
+
for (const team of teams) try {
|
|
243
|
+
const channel = await fetchMattermostChannelByName(client, team.id, name);
|
|
244
|
+
if (channel?.id) {
|
|
245
|
+
cacheOutboundEntry(channelByNameCache, key, channel.id, MATTERMOST_TARGET_CACHE_MAX_ENTRIES);
|
|
246
|
+
return channel.id;
|
|
247
|
+
}
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (parseMattermostApiStatus(error) !== 404) throw error;
|
|
250
|
+
}
|
|
251
|
+
throw new Error(`Mattermost channel "#${name}" not found in any team the bot belongs to`);
|
|
252
|
+
}
|
|
253
|
+
function mergeDmRetryOptions(base, override) {
|
|
254
|
+
const merged = {
|
|
255
|
+
maxRetries: override?.maxRetries ?? base?.maxRetries,
|
|
256
|
+
initialDelayMs: override?.initialDelayMs ?? base?.initialDelayMs,
|
|
257
|
+
maxDelayMs: override?.maxDelayMs ?? base?.maxDelayMs,
|
|
258
|
+
timeoutMs: override?.timeoutMs ?? base?.timeoutMs,
|
|
259
|
+
onRetry: override?.onRetry
|
|
260
|
+
};
|
|
261
|
+
return Object.values(merged).some((value) => value !== void 0) ? merged : void 0;
|
|
262
|
+
}
|
|
263
|
+
async function resolveTargetChannelId(params) {
|
|
264
|
+
if (params.target.kind === "channel") return params.target.id;
|
|
265
|
+
if (params.target.kind === "channel-name") return await resolveChannelIdByName({
|
|
266
|
+
client: params.client,
|
|
267
|
+
name: params.target.name
|
|
268
|
+
});
|
|
269
|
+
const userId = params.target.id ? params.target.id : await resolveUserIdByUsername({
|
|
270
|
+
client: params.client,
|
|
271
|
+
username: params.target.username ?? ""
|
|
272
|
+
});
|
|
273
|
+
const dmKey = `${cacheKey(params.client.baseUrl, params.client.token)}::dm::${userId}`;
|
|
274
|
+
const cachedDm = dmChannelCache.get(dmKey);
|
|
275
|
+
if (cachedDm) return cachedDm;
|
|
276
|
+
const botUser = await resolveBotUser(params.client);
|
|
277
|
+
const channel = await createMattermostDirectChannelWithRetry(params.client, [botUser.id, userId], {
|
|
278
|
+
...params.dmRetryOptions,
|
|
279
|
+
onRetry: (attempt, delayMs, error) => {
|
|
280
|
+
params.dmRetryOptions?.onRetry?.(attempt, delayMs, error);
|
|
281
|
+
params.logger?.warn?.(`DM channel creation retry ${attempt} after ${delayMs}ms: ${error.message}`);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
cacheOutboundEntry(dmChannelCache, dmKey, channel.id, MATTERMOST_TARGET_CACHE_MAX_ENTRIES);
|
|
285
|
+
return channel.id;
|
|
286
|
+
}
|
|
287
|
+
async function resolveMattermostSendContext(to, opts) {
|
|
288
|
+
const core = getMattermostRuntime();
|
|
289
|
+
const logger = core.logging.getChildLogger({ module: "mattermost" });
|
|
290
|
+
if (!opts?.cfg) throw new Error("Mattermost send requires a resolved runtime config. Load and resolve config at the command or gateway boundary, then pass cfg through the runtime path.");
|
|
291
|
+
const cfg = requireRuntimeConfig(opts.cfg, "Mattermost send");
|
|
292
|
+
const account = resolveMattermostAccount({
|
|
293
|
+
cfg,
|
|
294
|
+
accountId: opts.accountId
|
|
295
|
+
});
|
|
296
|
+
const token = normalizeOptionalString(opts.botToken) ?? normalizeOptionalString(account.botToken);
|
|
297
|
+
if (!token) throw new Error(`Mattermost bot token missing for account "${account.accountId}" (set channels.mattermost.accounts.${account.accountId}.botToken or MATTERMOST_BOT_TOKEN for default).`);
|
|
298
|
+
const baseUrl = normalizeMattermostBaseUrl(opts.baseUrl ?? account.baseUrl);
|
|
299
|
+
if (!baseUrl) throw new Error(`Mattermost baseUrl missing for account "${account.accountId}" (set channels.mattermost.accounts.${account.accountId}.baseUrl or MATTERMOST_URL for default).`);
|
|
300
|
+
const client = createMattermostClient({
|
|
301
|
+
baseUrl,
|
|
302
|
+
botToken: token,
|
|
303
|
+
allowPrivateNetwork: isPrivateNetworkOptInEnabled(account.config),
|
|
304
|
+
assertRequestCurrent: opts.assertDirectAdapterHandoff
|
|
305
|
+
});
|
|
306
|
+
const dmRetryOptions = mergeDmRetryOptions(account.config.dmChannelRetry, opts.dmRetryOptions);
|
|
307
|
+
let channelId;
|
|
308
|
+
try {
|
|
309
|
+
const trimmedTo = normalizeOptionalString(to) ?? "";
|
|
310
|
+
channelId = await resolveTargetChannelId({
|
|
311
|
+
target: parseMattermostTarget((await resolveMattermostOpaqueTarget({
|
|
312
|
+
input: trimmedTo,
|
|
313
|
+
client
|
|
314
|
+
}))?.to ?? trimmedTo),
|
|
315
|
+
client,
|
|
316
|
+
dmRetryOptions,
|
|
317
|
+
logger: core.logging.shouldLogVerbose() ? logger : void 0
|
|
318
|
+
});
|
|
319
|
+
} catch (error) {
|
|
320
|
+
client.assertRequestCurrent?.();
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
return {
|
|
324
|
+
cfg,
|
|
325
|
+
accountId: account.accountId,
|
|
326
|
+
client,
|
|
327
|
+
channelId,
|
|
328
|
+
mediaMaxBytes: resolveChannelMediaMaxBytes({
|
|
329
|
+
cfg,
|
|
330
|
+
accountId: account.accountId,
|
|
331
|
+
resolveChannelLimitMb: () => account.config.mediaMaxMb
|
|
332
|
+
})
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
async function sendMessageMattermost(to, text, opts) {
|
|
336
|
+
const core = getMattermostRuntime();
|
|
337
|
+
const logger = core.logging.getChildLogger({ module: "mattermost" });
|
|
338
|
+
const { cfg, accountId, client, channelId, mediaMaxBytes } = await resolveMattermostSendContext(to, opts);
|
|
339
|
+
client.assertRequestCurrent?.();
|
|
340
|
+
let props = opts.props;
|
|
341
|
+
if (!props && Array.isArray(opts.buttons) && opts.buttons.length > 0) {
|
|
342
|
+
setInteractionSecret(accountId, client.token);
|
|
343
|
+
props = buildButtonProps({
|
|
344
|
+
callbackUrl: resolveInteractionCallbackUrl(accountId, {
|
|
345
|
+
gateway: cfg.gateway,
|
|
346
|
+
interactions: resolveMattermostAccount({
|
|
347
|
+
cfg,
|
|
348
|
+
accountId
|
|
349
|
+
}).config?.interactions
|
|
350
|
+
}),
|
|
351
|
+
accountId,
|
|
352
|
+
channelId,
|
|
353
|
+
buttons: opts.buttons,
|
|
354
|
+
text: opts.attachmentText
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
let message = normalizeOptionalString(text) ?? "";
|
|
358
|
+
let fileIds;
|
|
359
|
+
let uploadError;
|
|
360
|
+
const mediaUrl = opts.mediaUrl?.trim();
|
|
361
|
+
if (mediaUrl) try {
|
|
362
|
+
const media = await loadOutboundMediaFromUrl(mediaUrl, {
|
|
363
|
+
maxBytes: mediaMaxBytes,
|
|
364
|
+
mediaLocalRoots: opts.mediaLocalRoots,
|
|
365
|
+
mediaReadFile: opts.mediaReadFile,
|
|
366
|
+
workspaceDir: opts.workspaceDir
|
|
367
|
+
});
|
|
368
|
+
fileIds = [(await uploadMattermostFile(client, {
|
|
369
|
+
channelId,
|
|
370
|
+
buffer: media.buffer,
|
|
371
|
+
fileName: media.fileName ?? `upload${extensionForMime(media.contentType) ?? ""}`,
|
|
372
|
+
contentType: media.contentType ?? void 0
|
|
373
|
+
})).id];
|
|
374
|
+
} catch (err) {
|
|
375
|
+
client.assertRequestCurrent?.();
|
|
376
|
+
uploadError = err instanceof Error ? err : new Error(String(err));
|
|
377
|
+
if (opts.requireMediaUpload || mediaMaxBytes !== void 0) throw new Error(`Mattermost media upload failed: ${uploadError.message}`, { cause: err });
|
|
378
|
+
if (core.logging.shouldLogVerbose()) logger.debug?.(`mattermost send: media upload failed, falling back to URL text: ${String(err)}`);
|
|
379
|
+
message = normalizeMessage(message, isHttpUrl(mediaUrl) ? mediaUrl : "");
|
|
380
|
+
}
|
|
381
|
+
if (message) {
|
|
382
|
+
const tableMode = resolveMarkdownTableMode({
|
|
383
|
+
cfg,
|
|
384
|
+
channel: "mattermost",
|
|
385
|
+
accountId
|
|
386
|
+
});
|
|
387
|
+
message = renderMattermostMarkdown(message, tableMode);
|
|
388
|
+
}
|
|
389
|
+
if (!message && (!fileIds || fileIds.length === 0)) {
|
|
390
|
+
if (uploadError) throw new Error(`Mattermost media upload failed: ${uploadError.message}`, { cause: uploadError });
|
|
391
|
+
throw new Error("Mattermost message is empty");
|
|
392
|
+
}
|
|
393
|
+
client.assertRequestCurrent?.();
|
|
394
|
+
try {
|
|
395
|
+
await opts.onPlatformSendDispatch?.();
|
|
396
|
+
} catch (error) {
|
|
397
|
+
client.assertRequestCurrent?.();
|
|
398
|
+
throw error;
|
|
399
|
+
}
|
|
400
|
+
const post = await createMattermostPost(client, {
|
|
401
|
+
channelId,
|
|
402
|
+
message,
|
|
403
|
+
rootId: opts.replyToId,
|
|
404
|
+
fileIds,
|
|
405
|
+
props
|
|
406
|
+
});
|
|
407
|
+
const messageId = post.id;
|
|
408
|
+
const receipt = createMattermostSendReceipt({
|
|
409
|
+
messageId,
|
|
410
|
+
channelId,
|
|
411
|
+
kind: resolveMattermostReceiptKind({
|
|
412
|
+
fileIds,
|
|
413
|
+
buttons: opts.buttons,
|
|
414
|
+
props
|
|
415
|
+
}),
|
|
416
|
+
replyToId: opts.replyToId
|
|
417
|
+
});
|
|
418
|
+
const result = {
|
|
419
|
+
messageId,
|
|
420
|
+
channelId,
|
|
421
|
+
receipt,
|
|
422
|
+
content: post.message ?? message
|
|
423
|
+
};
|
|
424
|
+
try {
|
|
425
|
+
await opts.onDeliveryResult?.(result);
|
|
426
|
+
recordMattermostOutboundActivity(accountId);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
throw createChannelPartialDeliveryError(error, {
|
|
429
|
+
messageIds: listMessageReceiptPlatformIds(receipt),
|
|
430
|
+
receipt,
|
|
431
|
+
visibleReplySent: true,
|
|
432
|
+
content: result.content
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return result;
|
|
436
|
+
}
|
|
437
|
+
//#endregion
|
|
438
|
+
export { resolveMattermostOpaqueTarget as n, sendMessageMattermost as t };
|