@desplega.ai/slack-mock 0.4.0 → 0.4.1
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 +3 -1
- package/dist/cli.js +1 -1
- package/dist/{index-y82vvfa1.js → index-86bw4z6k.js} +2054 -2043
- package/dist/index.js +1 -1
- package/package.json +1 -1
- package/src/web-api.ts +17 -2
|
@@ -4,1448 +4,606 @@ import {
|
|
|
4
4
|
screenshot2
|
|
5
5
|
} from "./index-xs37gskt.js";
|
|
6
6
|
|
|
7
|
-
// src/
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
// src/frames.ts
|
|
8
|
+
import { copyFileSync, mkdirSync as mkdirSync2, mkdtempSync, readFileSync as readFileSync3, rmSync, writeFileSync } from "fs";
|
|
9
|
+
import { tmpdir } from "os";
|
|
10
|
+
import { join, resolve } from "path";
|
|
10
11
|
|
|
11
|
-
// src/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
let out = prefix;
|
|
15
|
-
for (let i = 0;i < length; i++)
|
|
16
|
-
out += ALPHABET[Math.floor(Math.random() * ALPHABET.length)];
|
|
17
|
-
return out;
|
|
12
|
+
// src/render/mrkdwn.ts
|
|
13
|
+
function escapeHtml(s) {
|
|
14
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
18
15
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let micros = Date.now() * 1000;
|
|
22
|
-
if (micros <= lastMicros)
|
|
23
|
-
micros = lastMicros + 1;
|
|
24
|
-
lastMicros = micros;
|
|
25
|
-
const seconds = Math.floor(micros / 1e6);
|
|
26
|
-
const fraction = (micros % 1e6).toString().padStart(6, "0");
|
|
27
|
-
return `${seconds}.${fraction}`;
|
|
16
|
+
function unescapeSlack(s) {
|
|
17
|
+
return s.replace(/&(amp|lt|gt|quot);/g, (_m, name) => name === "amp" ? "&" : name === "lt" ? "<" : name === "gt" ? ">" : '"');
|
|
28
18
|
}
|
|
29
|
-
function
|
|
30
|
-
return
|
|
19
|
+
function reEscape(s) {
|
|
20
|
+
return escapeHtml(unescapeSlack(s));
|
|
31
21
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
var EMOJI = {
|
|
23
|
+
eyes: "\uD83D\uDC40",
|
|
24
|
+
white_check_mark: "\u2705",
|
|
25
|
+
x: "\u274C",
|
|
26
|
+
heavy_plus_sign: "\u2795",
|
|
27
|
+
heavy_minus_sign: "\u2796",
|
|
28
|
+
heavy_multiplication_x: "\u2716\uFE0F",
|
|
29
|
+
zap: "\u26A1",
|
|
30
|
+
speech_balloon: "\uD83D\uDCAC",
|
|
31
|
+
robot_face: "\uD83E\uDD16",
|
|
32
|
+
gear: "\u2699\uFE0F",
|
|
33
|
+
rocket: "\uD83D\uDE80",
|
|
34
|
+
star: "\u2B50",
|
|
35
|
+
crystal_ball: "\uD83D\uDD2E",
|
|
36
|
+
bulb: "\uD83D\uDCA1",
|
|
37
|
+
wrench: "\uD83D\uDD27",
|
|
38
|
+
hammer: "\uD83D\uDD28",
|
|
39
|
+
crown: "\uD83D\uDC51",
|
|
40
|
+
tada: "\uD83C\uDF89",
|
|
41
|
+
warning: "\u26A0\uFE0F",
|
|
42
|
+
hourglass: "\u231B",
|
|
43
|
+
hourglass_flowing_sand: "\u23F3",
|
|
44
|
+
thumbsup: "\uD83D\uDC4D",
|
|
45
|
+
thumbsdown: "\uD83D\uDC4E",
|
|
46
|
+
"+1": "\uD83D\uDC4D",
|
|
47
|
+
"-1": "\uD83D\uDC4E",
|
|
48
|
+
heavy_check_mark: "\u2714\uFE0F",
|
|
49
|
+
question: "\u2753",
|
|
50
|
+
grey_question: "\u2754",
|
|
51
|
+
exclamation: "\u2757",
|
|
52
|
+
white_circle: "\u26AA",
|
|
53
|
+
large_blue_circle: "\uD83D\uDD35",
|
|
54
|
+
black_circle: "\u26AB",
|
|
55
|
+
red_circle: "\uD83D\uDD34",
|
|
56
|
+
large_green_circle: "\uD83D\uDFE2",
|
|
57
|
+
large_yellow_circle: "\uD83D\uDFE1",
|
|
58
|
+
fire: "\uD83D\uDD25",
|
|
59
|
+
sparkles: "\u2728",
|
|
60
|
+
memo: "\uD83D\uDCDD",
|
|
61
|
+
clipboard: "\uD83D\uDCCB",
|
|
62
|
+
mag: "\uD83D\uDD0D",
|
|
63
|
+
mag_right: "\uD83D\uDD0E",
|
|
64
|
+
link: "\uD83D\uDD17",
|
|
65
|
+
lock: "\uD83D\uDD12",
|
|
66
|
+
unlock: "\uD83D\uDD13",
|
|
67
|
+
bell: "\uD83D\uDD14",
|
|
68
|
+
package: "\uD83D\uDCE6",
|
|
69
|
+
construction: "\uD83D\uDEA7",
|
|
70
|
+
bug: "\uD83D\uDC1B",
|
|
71
|
+
boom: "\uD83D\uDCA5",
|
|
72
|
+
pencil2: "\u270F\uFE0F",
|
|
73
|
+
scroll: "\uD83D\uDCDC",
|
|
74
|
+
page_facing_up: "\uD83D\uDCC4",
|
|
75
|
+
file_folder: "\uD83D\uDCC1",
|
|
76
|
+
card_index_dividers: "\uD83D\uDDC2\uFE0F",
|
|
77
|
+
paperclip: "\uD83D\uDCCE",
|
|
78
|
+
bar_chart: "\uD83D\uDCCA",
|
|
79
|
+
chart_with_upwards_trend: "\uD83D\uDCC8",
|
|
80
|
+
calendar: "\uD83D\uDCC5",
|
|
81
|
+
clock1: "\uD83D\uDD50",
|
|
82
|
+
arrow_right: "\u27A1\uFE0F",
|
|
83
|
+
arrow_left: "\u2B05\uFE0F",
|
|
84
|
+
arrow_up: "\u2B06\uFE0F",
|
|
85
|
+
arrow_down: "\u2B07\uFE0F",
|
|
86
|
+
arrow_right_hook: "\u21AA\uFE0F",
|
|
87
|
+
repeat: "\uD83D\uDD01",
|
|
88
|
+
arrows_counterclockwise: "\uD83D\uDD04",
|
|
89
|
+
recycle: "\u267B\uFE0F",
|
|
90
|
+
no_entry: "\u26D4",
|
|
91
|
+
no_entry_sign: "\uD83D\uDEAB",
|
|
92
|
+
stop_sign: "\uD83D\uDED1",
|
|
93
|
+
pause_button: "\u23F8\uFE0F",
|
|
94
|
+
information_source: "\u2139\uFE0F",
|
|
95
|
+
wave: "\uD83D\uDC4B",
|
|
96
|
+
pray: "\uD83D\uDE4F",
|
|
97
|
+
muscle: "\uD83D\uDCAA",
|
|
98
|
+
brain: "\uD83E\uDDE0",
|
|
99
|
+
dart: "\uD83C\uDFAF",
|
|
100
|
+
trophy: "\uD83C\uDFC6",
|
|
101
|
+
medal: "\uD83C\uDFC5",
|
|
102
|
+
checkered_flag: "\uD83C\uDFC1",
|
|
103
|
+
ok_hand: "\uD83D\uDC4C",
|
|
104
|
+
point_right: "\uD83D\uDC49",
|
|
105
|
+
clap: "\uD83D\uDC4F",
|
|
106
|
+
raised_hands: "\uD83D\uDE4C",
|
|
107
|
+
thinking_face: "\uD83E\uDD14",
|
|
108
|
+
"100": "\uD83D\uDCAF",
|
|
109
|
+
green_heart: "\uD83D\uDC9A",
|
|
110
|
+
thread: "\uD83E\uDDF5",
|
|
111
|
+
satellite_antenna: "\uD83D\uDCE1",
|
|
112
|
+
inbox_tray: "\uD83D\uDCE5",
|
|
113
|
+
outbox_tray: "\uD83D\uDCE4",
|
|
114
|
+
incoming_envelope: "\uD83D\uDCE8",
|
|
115
|
+
mailbox_with_no_mail: "\uD83D\uDCED",
|
|
116
|
+
envelope: "\u2709\uFE0F",
|
|
117
|
+
test_tube: "\uD83E\uDDEA",
|
|
118
|
+
ship: "\uD83D\uDEA2",
|
|
119
|
+
sos: "\uD83C\uDD98",
|
|
120
|
+
small_blue_diamond: "\uD83D\uDD39",
|
|
121
|
+
small_orange_diamond: "\uD83D\uDD38",
|
|
122
|
+
black_small_square: "\u25AA\uFE0F",
|
|
123
|
+
white_small_square: "\u25AB\uFE0F"
|
|
124
|
+
};
|
|
125
|
+
function emojiChar(name) {
|
|
126
|
+
return EMOJI[name.replace(/^:|:$/g, "").replace(/::skin-tone-\d$/, "")];
|
|
42
127
|
}
|
|
43
|
-
function
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
entries.push({ line, change: JSON.parse(raw) });
|
|
53
|
-
} catch (e) {
|
|
54
|
-
console.warn(`[slack-mock] skipping corrupt journal line ${line} in ${source}: ${e instanceof Error ? e.message : e}`);
|
|
55
|
-
}
|
|
128
|
+
function emojiFromUnicode(code) {
|
|
129
|
+
const points = code.split("-").map((p) => Number.parseInt(p, 16));
|
|
130
|
+
if (points.some((p) => !Number.isFinite(p)))
|
|
131
|
+
return;
|
|
132
|
+
try {
|
|
133
|
+
return String.fromCodePoint(...points);
|
|
134
|
+
} catch {
|
|
135
|
+
return;
|
|
56
136
|
}
|
|
57
|
-
return entries;
|
|
58
137
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.bot = {
|
|
83
|
-
userId: opts.botUserId ?? "U0BOT00000",
|
|
84
|
-
botId: opts.botId ?? "B0BOT00000",
|
|
85
|
-
token: opts.botToken ?? "xoxb-mock-bot-token",
|
|
86
|
-
appToken: opts.appToken ?? "xapp-mock-app-token",
|
|
87
|
-
userToken: opts.userToken
|
|
88
|
-
};
|
|
89
|
-
this.dataFile = opts.dataFile;
|
|
90
|
-
if (this.dataFile && existsSync(this.dataFile))
|
|
91
|
-
this.replay(parseJournal(readFileSync(this.dataFile, "utf8"), this.dataFile).map((e) => e.change));
|
|
92
|
-
if (opts.replay)
|
|
93
|
-
this.replay(opts.replay);
|
|
94
|
-
if (!this.users.has(this.bot.userId)) {
|
|
95
|
-
this.addUser({
|
|
96
|
-
id: this.bot.userId,
|
|
97
|
-
name: this.app.name,
|
|
98
|
-
real_name: this.app.name,
|
|
99
|
-
is_bot: true
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
onChange(listener) {
|
|
104
|
-
this.listeners.add(listener);
|
|
105
|
-
return () => this.listeners.delete(listener);
|
|
138
|
+
function renderEmoji(html) {
|
|
139
|
+
return html.replace(/:([a-z][a-z0-9_+'-]*|\+1|-1|100):/g, (m, name) => {
|
|
140
|
+
const char = emojiChar(name);
|
|
141
|
+
return char ?? `<span class="sm-emoji-name">${m}</span>`;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function displayName(user) {
|
|
145
|
+
return user.profile.display_name || user.real_name || user.name;
|
|
146
|
+
}
|
|
147
|
+
function pill(text) {
|
|
148
|
+
return `<span class="sm-pill">${escapeHtml(text)}</span>`;
|
|
149
|
+
}
|
|
150
|
+
function isSafeUrl(url) {
|
|
151
|
+
return /^(https?:\/\/|mailto:|tel:)/i.test(url);
|
|
152
|
+
}
|
|
153
|
+
function renderControl(inner, ctx) {
|
|
154
|
+
const bar = inner.indexOf("|");
|
|
155
|
+
const head = bar >= 0 ? inner.slice(0, bar) : inner;
|
|
156
|
+
const label = bar >= 0 ? inner.slice(bar + 1) : "";
|
|
157
|
+
if (head.startsWith("@")) {
|
|
158
|
+
const id = head.slice(1);
|
|
159
|
+
const user = ctx.users.get(id);
|
|
160
|
+
return pill(`@${user ? displayName(user) : label || id}`);
|
|
106
161
|
}
|
|
107
|
-
|
|
108
|
-
|
|
162
|
+
if (head.startsWith("#")) {
|
|
163
|
+
const id = head.slice(1);
|
|
164
|
+
const channel = ctx.channels.get(id);
|
|
165
|
+
return pill(`#${channel ? channel.name : label.replace(/^#/, "") || id}`);
|
|
109
166
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
`);
|
|
167
|
+
if (head.startsWith("!")) {
|
|
168
|
+
const cmd = head.slice(1);
|
|
169
|
+
if (cmd === "here" || cmd === "channel" || cmd === "everyone")
|
|
170
|
+
return pill(`@${cmd}`);
|
|
171
|
+
if (cmd.startsWith("subteam^")) {
|
|
172
|
+
const handle = label || cmd.slice("subteam^".length);
|
|
173
|
+
return pill(handle.startsWith("@") ? handle : `@${handle}`);
|
|
114
174
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
try {
|
|
121
|
-
for (const change of changes)
|
|
122
|
-
this.applyReplayed(change);
|
|
123
|
-
} finally {
|
|
124
|
-
this.replaying = false;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
applyReplayed(change) {
|
|
128
|
-
switch (change.kind) {
|
|
129
|
-
case "user.add":
|
|
130
|
-
this.users.set(change.user.id, change.user);
|
|
131
|
-
break;
|
|
132
|
-
case "channel.add":
|
|
133
|
-
case "channel.update":
|
|
134
|
-
this.channels.set(change.channel.id, change.channel);
|
|
135
|
-
break;
|
|
136
|
-
case "member.join": {
|
|
137
|
-
const c = this.channels.get(change.channel.id);
|
|
138
|
-
if (c && !c.members.includes(change.user))
|
|
139
|
-
c.members.push(change.user);
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
case "message.add":
|
|
143
|
-
this.insertMessage(change.message);
|
|
144
|
-
break;
|
|
145
|
-
case "message.update":
|
|
146
|
-
case "reaction.add":
|
|
147
|
-
case "reaction.remove": {
|
|
148
|
-
const list = this.messages.get(change.message.channel);
|
|
149
|
-
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
150
|
-
if (list && i >= 0)
|
|
151
|
-
list[i] = change.message;
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
case "message.delete": {
|
|
155
|
-
const list = this.messages.get(change.message.channel);
|
|
156
|
-
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
157
|
-
if (list && i >= 0)
|
|
158
|
-
list.splice(i, 1);
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
case "file.add":
|
|
162
|
-
this.files.set(change.file.id, change.file);
|
|
163
|
-
break;
|
|
164
|
-
default:
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
addUser(input) {
|
|
169
|
-
const id = input.id ?? slackId("U");
|
|
170
|
-
const realName = input.real_name ?? input.name;
|
|
171
|
-
const user = {
|
|
172
|
-
id,
|
|
173
|
-
team_id: this.team.id,
|
|
174
|
-
name: input.name,
|
|
175
|
-
real_name: realName,
|
|
176
|
-
deleted: false,
|
|
177
|
-
is_bot: input.is_bot ?? false,
|
|
178
|
-
is_admin: input.is_admin ?? false,
|
|
179
|
-
is_app_user: false,
|
|
180
|
-
tz: input.tz ?? "UTC",
|
|
181
|
-
profile: {
|
|
182
|
-
real_name: realName,
|
|
183
|
-
display_name: input.name,
|
|
184
|
-
real_name_normalized: realName,
|
|
185
|
-
display_name_normalized: input.name,
|
|
186
|
-
email: input.email,
|
|
187
|
-
title: input.title,
|
|
188
|
-
status_text: input.status,
|
|
189
|
-
team: this.team.id
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
this.users.set(id, user);
|
|
193
|
-
this.emit({ kind: "user.add", user });
|
|
194
|
-
return user;
|
|
195
|
-
}
|
|
196
|
-
user(id) {
|
|
197
|
-
const u = this.users.get(id);
|
|
198
|
-
if (!u)
|
|
199
|
-
throw new SlackApiError2("user_not_found");
|
|
200
|
-
return u;
|
|
201
|
-
}
|
|
202
|
-
userByEmail(email) {
|
|
203
|
-
for (const u of this.users.values())
|
|
204
|
-
if (u.profile.email === email)
|
|
205
|
-
return u;
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
isBotUser(userId) {
|
|
209
|
-
return userId === this.bot.userId;
|
|
210
|
-
}
|
|
211
|
-
addChannel(input) {
|
|
212
|
-
const name = input.name.replace(/^#/, "");
|
|
213
|
-
for (const c of this.channels.values()) {
|
|
214
|
-
if (c.name === name && !c.is_im)
|
|
215
|
-
throw new SlackApiError2("name_taken");
|
|
175
|
+
if (cmd.startsWith("date^")) {
|
|
176
|
+
if (label)
|
|
177
|
+
return renderEmoji(reEscape(label));
|
|
178
|
+
const secs = Number(cmd.split("^")[1]);
|
|
179
|
+
return Number.isFinite(secs) ? escapeHtml(new Date(secs * 1000).toISOString()) : "";
|
|
216
180
|
}
|
|
217
|
-
|
|
218
|
-
const creator = input.creator ?? this.bot.userId;
|
|
219
|
-
const members = [...new Set([creator, ...input.members ?? []])];
|
|
220
|
-
const channel = {
|
|
221
|
-
id,
|
|
222
|
-
name,
|
|
223
|
-
is_channel: !input.is_private,
|
|
224
|
-
is_group: !!input.is_private,
|
|
225
|
-
is_im: false,
|
|
226
|
-
is_mpim: false,
|
|
227
|
-
is_private: !!input.is_private,
|
|
228
|
-
is_archived: false,
|
|
229
|
-
is_general: name === "general",
|
|
230
|
-
created: nowUnix(),
|
|
231
|
-
creator,
|
|
232
|
-
topic: { value: input.topic ?? "", creator, last_set: 0 },
|
|
233
|
-
purpose: { value: input.purpose ?? "", creator, last_set: 0 },
|
|
234
|
-
members
|
|
235
|
-
};
|
|
236
|
-
this.channels.set(id, channel);
|
|
237
|
-
this.messages.set(id, []);
|
|
238
|
-
this.emit({ kind: "channel.add", channel });
|
|
239
|
-
return channel;
|
|
240
|
-
}
|
|
241
|
-
openDm(userId) {
|
|
242
|
-
this.user(userId);
|
|
243
|
-
for (const c of this.channels.values())
|
|
244
|
-
if (c.is_im && c.user === userId)
|
|
245
|
-
return c;
|
|
246
|
-
const id = slackId("D");
|
|
247
|
-
const channel = {
|
|
248
|
-
id,
|
|
249
|
-
name: id,
|
|
250
|
-
is_channel: false,
|
|
251
|
-
is_group: false,
|
|
252
|
-
is_im: true,
|
|
253
|
-
is_mpim: false,
|
|
254
|
-
is_private: true,
|
|
255
|
-
is_archived: false,
|
|
256
|
-
is_general: false,
|
|
257
|
-
created: nowUnix(),
|
|
258
|
-
creator: userId,
|
|
259
|
-
user: userId,
|
|
260
|
-
topic: { value: "", creator: "", last_set: 0 },
|
|
261
|
-
purpose: { value: "", creator: "", last_set: 0 },
|
|
262
|
-
members: [userId, this.bot.userId]
|
|
263
|
-
};
|
|
264
|
-
this.channels.set(id, channel);
|
|
265
|
-
this.messages.set(id, []);
|
|
266
|
-
this.emit({ kind: "channel.add", channel });
|
|
267
|
-
return channel;
|
|
268
|
-
}
|
|
269
|
-
channel(id) {
|
|
270
|
-
const c = this.channels.get(id);
|
|
271
|
-
if (!c)
|
|
272
|
-
throw new SlackApiError2("channel_not_found");
|
|
273
|
-
return c;
|
|
274
|
-
}
|
|
275
|
-
channelByName(name) {
|
|
276
|
-
const n = name.replace(/^#/, "");
|
|
277
|
-
for (const c of this.channels.values())
|
|
278
|
-
if (c.name === n)
|
|
279
|
-
return c;
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
channelType(c) {
|
|
283
|
-
if (c.is_im)
|
|
284
|
-
return "im";
|
|
285
|
-
if (c.is_mpim)
|
|
286
|
-
return "mpim";
|
|
287
|
-
if (c.is_private)
|
|
288
|
-
return "group";
|
|
289
|
-
return "channel";
|
|
181
|
+
return pill(`@${cmd}`);
|
|
290
182
|
}
|
|
291
|
-
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (channel.members.includes(userId))
|
|
296
|
-
return { alreadyMember: true };
|
|
297
|
-
channel.members.push(userId);
|
|
298
|
-
this.emit({ kind: "member.join", channel, user: userId, inviter });
|
|
299
|
-
return { alreadyMember: false };
|
|
183
|
+
if (isSafeUrl(head)) {
|
|
184
|
+
const shown = label || head.replace(/^mailto:/i, "");
|
|
185
|
+
const attrs = /^mailto:/i.test(head) ? "" : ' target="_blank" rel="noopener"';
|
|
186
|
+
return `<a class="sm-link" href="${escapeHtml(head)}"${attrs}>${renderEmoji(reEscape(shown))}</a>`;
|
|
300
187
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
188
|
+
return escapeHtml(`<${unescapeSlack(inner)}>`);
|
|
189
|
+
}
|
|
190
|
+
function preHtml(body) {
|
|
191
|
+
let code = body.replace(/^\n/, "").replace(/\n$/, "");
|
|
192
|
+
if (code.includes(`
|
|
193
|
+
`))
|
|
194
|
+
code = code.replace(/^[a-z][a-z0-9+#-]{0,11}\n/, "");
|
|
195
|
+
return `<pre class="sm-pre">${reEscape(code)}</pre>`;
|
|
196
|
+
}
|
|
197
|
+
var BOUND_L = `(^|[\\s(\\[{'"\\uE000\\uE001])`;
|
|
198
|
+
var BOUND_R = `(?=$|[\\s.,;:!?)\\]}'"\\uE000\\uE001])`;
|
|
199
|
+
function styleRule(marker) {
|
|
200
|
+
const m = marker.replace(/[*]/g, "\\*");
|
|
201
|
+
return new RegExp(`${BOUND_L}${m}(?![\\s${m}])([^${m}\\n]*[^\\s${m}])${m}${BOUND_R}`, "g");
|
|
202
|
+
}
|
|
203
|
+
var RULES = [
|
|
204
|
+
[styleRule("*"), "strong"],
|
|
205
|
+
[styleRule("~"), "s"],
|
|
206
|
+
[styleRule("_"), "em"]
|
|
207
|
+
];
|
|
208
|
+
function inlineStyles(s) {
|
|
209
|
+
let out = s;
|
|
210
|
+
for (const [re, tag] of RULES)
|
|
211
|
+
out = out.replace(re, `$1<${tag}>$2</${tag}>`);
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
var QUOTE = /^\s*(?:>|>)\s?/;
|
|
215
|
+
var BULLET = /^(\s*)(?:\u2022|-|\*)\s+(.*)$/;
|
|
216
|
+
var NUMBER = /^(\s*)\d+[.)]\s+(.*)$/;
|
|
217
|
+
function blockify(text) {
|
|
218
|
+
const groups = [];
|
|
219
|
+
const push = (kind, html, indent = 0) => {
|
|
220
|
+
const last = groups[groups.length - 1];
|
|
221
|
+
if (last?.kind === kind)
|
|
222
|
+
last.items.push({ html, indent });
|
|
223
|
+
else
|
|
224
|
+
groups.push({ kind, items: [{ html, indent }] });
|
|
225
|
+
};
|
|
226
|
+
for (const line of text.split(`
|
|
227
|
+
`)) {
|
|
228
|
+
const bullet = BULLET.exec(line);
|
|
229
|
+
const numbered = bullet ? null : NUMBER.exec(line);
|
|
230
|
+
if (QUOTE.test(line))
|
|
231
|
+
push("quote", line.replace(QUOTE, ""));
|
|
232
|
+
else if (bullet)
|
|
233
|
+
push("ul", bullet[2] ?? "", Math.min(4, Math.floor((bullet[1]?.length ?? 0) / 2)));
|
|
234
|
+
else if (numbered)
|
|
235
|
+
push("ol", numbered[2] ?? "", Math.min(4, Math.floor((numbered[1]?.length ?? 0) / 2)));
|
|
236
|
+
else
|
|
237
|
+
push("p", line);
|
|
309
238
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
i
|
|
316
|
-
list.
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
239
|
+
return groups.map((g) => {
|
|
240
|
+
if (g.kind === "p")
|
|
241
|
+
return g.items.map((i) => i.html).join("<br>");
|
|
242
|
+
const inner = g.items.map((i) => `<li${i.indent ? ` style="margin-left:${i.indent * 18}px"` : ""}>${i.html}</li>`).join("");
|
|
243
|
+
if (g.kind === "quote")
|
|
244
|
+
return `<blockquote class="sm-quote">${g.items.map((i) => i.html).join("<br>")}</blockquote>`;
|
|
245
|
+
return `<${g.kind} class="sm-list">${inner}</${g.kind}>`;
|
|
246
|
+
}).join("");
|
|
247
|
+
}
|
|
248
|
+
var BLOCK_TAGS = "pre|blockquote|ul|ol";
|
|
249
|
+
function tidy(html) {
|
|
250
|
+
return html.replace(new RegExp(`<br>\\s*(<(?:${BLOCK_TAGS})\\b)`, "g"), "$1").replace(new RegExp(`(</(?:${BLOCK_TAGS})>)\\s*<br>`, "g"), "$1");
|
|
251
|
+
}
|
|
252
|
+
function mrkdwnToHtml(text, ctx) {
|
|
253
|
+
if (!text)
|
|
254
|
+
return "";
|
|
255
|
+
const tokens = [];
|
|
256
|
+
const hold = (html) => {
|
|
257
|
+
tokens.push(html);
|
|
258
|
+
return `\uE000${tokens.length - 1}\uE001`;
|
|
259
|
+
};
|
|
260
|
+
let s = text.replace(/\r\n/g, `
|
|
261
|
+
`);
|
|
262
|
+
s = s.replace(/```([\s\S]*?)```/g, (_m, body) => hold(preHtml(body)));
|
|
263
|
+
s = s.replace(/`([^`\n]+)`/g, (_m, body) => hold(`<code class="sm-code">${reEscape(body)}</code>`));
|
|
264
|
+
s = s.replace(/<([^<>\n]*)>/g, (_m, inner) => hold(renderControl(inner, ctx)));
|
|
265
|
+
s = renderEmoji(reEscape(s));
|
|
266
|
+
s = inlineStyles(s);
|
|
267
|
+
s = blockify(s);
|
|
268
|
+
const expand = (html) => html.replace(/\uE000(\d+)\uE001/g, (_m, i) => expand(tokens[Number(i)] ?? ""));
|
|
269
|
+
return tidy(expand(s));
|
|
270
|
+
}
|
|
271
|
+
function plainTextToHtml(text) {
|
|
272
|
+
if (!text)
|
|
273
|
+
return "";
|
|
274
|
+
return renderEmoji(reEscape(text.replace(/\r\n/g, `
|
|
275
|
+
`))).replace(/\n/g, "<br>");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// src/render/blocks.ts
|
|
279
|
+
function rec(v) {
|
|
280
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : undefined;
|
|
281
|
+
}
|
|
282
|
+
function str(v) {
|
|
283
|
+
return typeof v === "string" ? v : "";
|
|
284
|
+
}
|
|
285
|
+
function arr(v) {
|
|
286
|
+
return Array.isArray(v) ? v : [];
|
|
287
|
+
}
|
|
288
|
+
function safeUrl(url) {
|
|
289
|
+
return /^(https?:\/\/|\/)/i.test(url) ? escapeHtml(url) : undefined;
|
|
290
|
+
}
|
|
291
|
+
function unsupported(what) {
|
|
292
|
+
return `<div class="sm-unsupported">unsupported: ${escapeHtml(what || "unknown")}</div>`;
|
|
293
|
+
}
|
|
294
|
+
function textObject(v, ctx) {
|
|
295
|
+
if (typeof v === "string")
|
|
296
|
+
return plainTextToHtml(v);
|
|
297
|
+
const o = rec(v);
|
|
298
|
+
if (!o)
|
|
299
|
+
return "";
|
|
300
|
+
const text = str(o.text);
|
|
301
|
+
return o.type === "mrkdwn" ? mrkdwnToHtml(text, ctx) : plainTextToHtml(text);
|
|
302
|
+
}
|
|
303
|
+
function plainOf(v) {
|
|
304
|
+
const o = rec(v);
|
|
305
|
+
return plainTextToHtml(o ? str(o.text) : str(v));
|
|
306
|
+
}
|
|
307
|
+
function image(url, alt, cls) {
|
|
308
|
+
const src = safeUrl(url);
|
|
309
|
+
if (!src)
|
|
310
|
+
return unsupported("image url");
|
|
311
|
+
return `<img class="${cls}" src="${src}" alt="${escapeHtml(alt)}">`;
|
|
312
|
+
}
|
|
313
|
+
function imageSrcOf(block) {
|
|
314
|
+
const direct = str(block.image_url);
|
|
315
|
+
if (direct)
|
|
316
|
+
return direct;
|
|
317
|
+
const file = rec(block.slack_file);
|
|
318
|
+
return file ? str(file.url) || str(file.url_private) : "";
|
|
319
|
+
}
|
|
320
|
+
function button(el, ctx) {
|
|
321
|
+
const style = str(el.style);
|
|
322
|
+
const cls = `sm-btn${style === "primary" ? " sm-btn-primary" : style === "danger" ? " sm-btn-danger" : ""}`;
|
|
323
|
+
const label = textObject(el.text, ctx) || "Button";
|
|
324
|
+
const url = safeUrl(str(el.url));
|
|
325
|
+
const confirm = rec(el.confirm) ? '<span class="sm-btn-confirm" title="confirmation dialog">\u26A0</span>' : "";
|
|
326
|
+
return url ? `<a class="${cls}" href="${url}" target="_blank" rel="noopener">${label}${confirm}</a>` : `<span class="${cls}">${label}${confirm}</span>`;
|
|
327
|
+
}
|
|
328
|
+
function selectPill(el, ctx, glyph = "\u25BE") {
|
|
329
|
+
const initial = rec(el.initial_option);
|
|
330
|
+
const label = initial && textObject(initial.text, ctx) || textObject(el.placeholder, ctx) || plainTextToHtml(str(el.type).replace(/_/g, " "));
|
|
331
|
+
return `<span class="sm-select">${label}<span class="sm-select-caret">${glyph}</span></span>`;
|
|
332
|
+
}
|
|
333
|
+
function inputBox(el, ctx) {
|
|
334
|
+
const multiline = el.multiline === true;
|
|
335
|
+
const value = str(el.initial_value);
|
|
336
|
+
const inner = value ? plainTextToHtml(value) : `<span class="sm-input-ph">${textObject(el.placeholder, ctx)}</span>`;
|
|
337
|
+
return `<div class="sm-input${multiline ? " sm-input-multi" : ""}">${inner}</div>`;
|
|
338
|
+
}
|
|
339
|
+
function renderElement(v, ctx) {
|
|
340
|
+
const el = rec(v);
|
|
341
|
+
if (!el)
|
|
342
|
+
return "";
|
|
343
|
+
const type = str(el.type);
|
|
344
|
+
switch (type) {
|
|
345
|
+
case "button":
|
|
346
|
+
return button(el, ctx);
|
|
347
|
+
case "image":
|
|
348
|
+
return image(imageSrcOf(el), str(el.alt_text), "sm-acc-img");
|
|
349
|
+
case "overflow":
|
|
350
|
+
return `<span class="sm-select sm-overflow">\u22EF</span>`;
|
|
351
|
+
case "datepicker":
|
|
352
|
+
return selectPill({ ...el, placeholder: el.placeholder ?? "Select a date" }, ctx, "\uD83D\uDCC5");
|
|
353
|
+
case "timepicker":
|
|
354
|
+
return selectPill({ ...el, placeholder: el.placeholder ?? "Select a time" }, ctx, "\uD83D\uDD50");
|
|
355
|
+
case "plain_text_input":
|
|
356
|
+
return inputBox(el, ctx);
|
|
357
|
+
case "checkboxes":
|
|
358
|
+
case "radio_buttons":
|
|
359
|
+
return `<div class="sm-options">${arr(el.options).map((o) => {
|
|
360
|
+
const opt = rec(o);
|
|
361
|
+
const mark = type === "checkboxes" ? "\u2610" : "\u25EF";
|
|
362
|
+
return `<div class="sm-option">${mark} ${opt ? textObject(opt.text, ctx) : ""}</div>`;
|
|
363
|
+
}).join("")}</div>`;
|
|
364
|
+
default:
|
|
365
|
+
if (type.endsWith("_select") || type.startsWith("multi_"))
|
|
366
|
+
return selectPill(el, ctx);
|
|
367
|
+
return unsupported(type);
|
|
356
368
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
369
|
+
}
|
|
370
|
+
function richStyle(html, style) {
|
|
371
|
+
if (!html || !style)
|
|
372
|
+
return html;
|
|
373
|
+
let out = html;
|
|
374
|
+
if (style.code === true)
|
|
375
|
+
out = `<code class="sm-code">${out}</code>`;
|
|
376
|
+
if (style.bold === true)
|
|
377
|
+
out = `<strong>${out}</strong>`;
|
|
378
|
+
if (style.italic === true)
|
|
379
|
+
out = `<em>${out}</em>`;
|
|
380
|
+
if (style.strike === true)
|
|
381
|
+
out = `<s>${out}</s>`;
|
|
382
|
+
return out;
|
|
383
|
+
}
|
|
384
|
+
function richElement(v, ctx) {
|
|
385
|
+
const el = rec(v);
|
|
386
|
+
if (!el)
|
|
387
|
+
return "";
|
|
388
|
+
const style = rec(el.style);
|
|
389
|
+
switch (str(el.type)) {
|
|
390
|
+
case "text":
|
|
391
|
+
return richStyle(plainTextToHtml(str(el.text)).replace(/\n/g, "<br>"), style);
|
|
392
|
+
case "link": {
|
|
393
|
+
const url = safeUrl(str(el.url));
|
|
394
|
+
const label = plainTextToHtml(str(el.text) || str(el.url));
|
|
395
|
+
if (!url)
|
|
396
|
+
return label;
|
|
397
|
+
return richStyle(`<a class="sm-link" href="${url}" target="_blank" rel="noopener">${label}</a>`, style);
|
|
363
398
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
ts: input.ts ?? nextTs(),
|
|
368
|
-
...input
|
|
369
|
-
};
|
|
370
|
-
if (message.ephemeral_user)
|
|
371
|
-
this.ephemerals.push(message);
|
|
372
|
-
else
|
|
373
|
-
this.insertMessage(message);
|
|
374
|
-
this.emit({ kind: "message.add", message });
|
|
375
|
-
return message;
|
|
376
|
-
}
|
|
377
|
-
updateMessage(channelId, ts, patch, editor, opts = {}) {
|
|
378
|
-
const message = this.message(channelId, ts);
|
|
379
|
-
const previous = structuredClone(message);
|
|
380
|
-
Object.assign(message, patch, opts.markEdited === false ? {} : { edited: { user: editor, ts: nextTs() } });
|
|
381
|
-
this.emit({ kind: "message.update", message, previous });
|
|
382
|
-
return message;
|
|
383
|
-
}
|
|
384
|
-
deleteMessage(channelId, ts) {
|
|
385
|
-
const list = this.messages.get(channelId);
|
|
386
|
-
const i = list?.findIndex((m) => m.ts === ts) ?? -1;
|
|
387
|
-
if (!list || i < 0)
|
|
388
|
-
throw new SlackApiError2("message_not_found");
|
|
389
|
-
const [message] = list.splice(i, 1);
|
|
390
|
-
if (message.thread_ts && message.thread_ts !== ts) {
|
|
391
|
-
const parent = this.findMessage(channelId, message.thread_ts);
|
|
392
|
-
if (parent?.reply_count)
|
|
393
|
-
parent.reply_count -= 1;
|
|
399
|
+
case "user": {
|
|
400
|
+
const user = ctx.users.get(str(el.user_id));
|
|
401
|
+
return `<span class="sm-pill">@${escapeHtml(user ? displayName(user) : str(el.user_id))}</span>`;
|
|
394
402
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
message.reactions ??= [];
|
|
401
|
-
let reaction = message.reactions.find((r) => r.name === name);
|
|
402
|
-
if (reaction?.users.includes(userId))
|
|
403
|
-
throw new SlackApiError2("already_reacted");
|
|
404
|
-
if (!reaction) {
|
|
405
|
-
reaction = { name, users: [], count: 0 };
|
|
406
|
-
message.reactions.push(reaction);
|
|
403
|
+
case "usergroup":
|
|
404
|
+
return `<span class="sm-pill">@${escapeHtml(str(el.usergroup_id))}</span>`;
|
|
405
|
+
case "channel": {
|
|
406
|
+
const channel = ctx.channels.get(str(el.channel_id));
|
|
407
|
+
return `<span class="sm-pill">#${escapeHtml(channel ? channel.name : str(el.channel_id))}</span>`;
|
|
407
408
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
409
|
+
case "broadcast":
|
|
410
|
+
return `<span class="sm-pill">@${escapeHtml(str(el.range) || "channel")}</span>`;
|
|
411
|
+
case "emoji": {
|
|
412
|
+
const char = emojiChar(str(el.name)) ?? (el.unicode ? emojiFromUnicode(str(el.unicode)) : undefined);
|
|
413
|
+
return char ?? `<span class="sm-emoji-name">:${escapeHtml(str(el.name))}:</span>`;
|
|
414
|
+
}
|
|
415
|
+
case "date":
|
|
416
|
+
return plainTextToHtml(str(el.fallback) || str(el.timestamp));
|
|
417
|
+
case "color":
|
|
418
|
+
return plainTextToHtml(str(el.value));
|
|
419
|
+
default:
|
|
420
|
+
return "";
|
|
412
421
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
const parent = this.message(channelId, threadTs);
|
|
433
|
-
const all = [
|
|
434
|
-
parent,
|
|
435
|
-
...(this.messages.get(channelId) ?? []).filter((m) => m.thread_ts === threadTs && m.ts !== threadTs)
|
|
436
|
-
];
|
|
437
|
-
return paginate(all, q);
|
|
438
|
-
}
|
|
439
|
-
addFile(input) {
|
|
440
|
-
const id = input.id ?? slackId("F");
|
|
441
|
-
const ext = input.name.includes(".") ? input.name.split(".").pop().toLowerCase() : "";
|
|
442
|
-
const mimetype = input.mimetype ?? guessMime(ext);
|
|
443
|
-
const now = nowUnix();
|
|
444
|
-
const file = {
|
|
445
|
-
id,
|
|
446
|
-
created: now,
|
|
447
|
-
timestamp: now,
|
|
448
|
-
name: input.name,
|
|
449
|
-
title: input.title ?? input.name,
|
|
450
|
-
mimetype,
|
|
451
|
-
filetype: ext || "binary",
|
|
452
|
-
pretty_type: ext ? ext.toUpperCase() : "Binary",
|
|
453
|
-
user: input.user,
|
|
454
|
-
size: input.bytes.byteLength,
|
|
455
|
-
mode: "hosted",
|
|
456
|
-
is_external: false,
|
|
457
|
-
is_public: false,
|
|
458
|
-
url_private: `${input.baseUrl}/files-pri/${this.team.id}-${id}/${encodeURIComponent(input.name)}`,
|
|
459
|
-
url_private_download: `${input.baseUrl}/files-pri/${this.team.id}-${id}/download/${encodeURIComponent(input.name)}`,
|
|
460
|
-
permalink: `${input.baseUrl}/files/${input.user}/${id}/${encodeURIComponent(input.name)}`,
|
|
461
|
-
channels: [],
|
|
462
|
-
bytes: input.bytes
|
|
463
|
-
};
|
|
464
|
-
if (mimetype.startsWith("image/")) {
|
|
465
|
-
const thumbs = file;
|
|
466
|
-
for (const size of [64, 80, 160, 360, 480])
|
|
467
|
-
thumbs[`thumb_${size}`] = file.url_private;
|
|
422
|
+
}
|
|
423
|
+
function richSection(v, ctx) {
|
|
424
|
+
const block = rec(v);
|
|
425
|
+
if (!block)
|
|
426
|
+
return "";
|
|
427
|
+
const inner = arr(block.elements).map((e) => richElement(e, ctx)).join("");
|
|
428
|
+
switch (str(block.type)) {
|
|
429
|
+
case "rich_text_section":
|
|
430
|
+
return `<div class="sm-rt-section">${inner}</div>`;
|
|
431
|
+
case "rich_text_quote":
|
|
432
|
+
return `<blockquote class="sm-quote">${inner}</blockquote>`;
|
|
433
|
+
case "rich_text_preformatted":
|
|
434
|
+
return `<pre class="sm-pre">${inner}</pre>`;
|
|
435
|
+
case "rich_text_list": {
|
|
436
|
+
const tag = str(block.style) === "ordered" ? "ol" : "ul";
|
|
437
|
+
const indent = Number(block.indent) || 0;
|
|
438
|
+
const items = arr(block.elements).map((item) => `<li>${richSection(item, ctx)}</li>`).join("");
|
|
439
|
+
const margin = indent ? ` style="margin-left:${indent * 18}px"` : "";
|
|
440
|
+
return `<${tag} class="sm-list"${margin}>${items}</${tag}>`;
|
|
468
441
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
return file;
|
|
472
|
-
}
|
|
473
|
-
file(id) {
|
|
474
|
-
const f = this.files.get(id);
|
|
475
|
-
if (!f)
|
|
476
|
-
throw new SlackApiError2("file_not_found");
|
|
477
|
-
return f;
|
|
478
|
-
}
|
|
479
|
-
openView(view) {
|
|
480
|
-
const id = view.id ?? slackId("V");
|
|
481
|
-
const full = {
|
|
482
|
-
...view,
|
|
483
|
-
id,
|
|
484
|
-
team_id: this.team.id,
|
|
485
|
-
app_id: this.app.id,
|
|
486
|
-
bot_id: this.bot.botId,
|
|
487
|
-
hash: `${nowUnix()}.${slackId("", 6).toLowerCase()}`,
|
|
488
|
-
root_view_id: id
|
|
489
|
-
};
|
|
490
|
-
this.views.set(id, full);
|
|
491
|
-
this.emit({ kind: "view.open", view: full });
|
|
492
|
-
return full;
|
|
493
|
-
}
|
|
494
|
-
recordApiCall(call) {
|
|
495
|
-
this.apiCalls.push(call);
|
|
496
|
-
this.emit({ kind: "api.call", call });
|
|
442
|
+
default:
|
|
443
|
+
return inner;
|
|
497
444
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
445
|
+
}
|
|
446
|
+
function sectionBlock(block, ctx) {
|
|
447
|
+
const parts = [];
|
|
448
|
+
if (block.text)
|
|
449
|
+
parts.push(`<div class="sm-section-text">${textObject(block.text, ctx)}</div>`);
|
|
450
|
+
const fields = arr(block.fields);
|
|
451
|
+
if (fields.length) {
|
|
452
|
+
parts.push(`<div class="sm-fields">${fields.map((f) => `<div class="sm-field">${textObject(f, ctx)}</div>`).join("")}</div>`);
|
|
506
453
|
}
|
|
454
|
+
const accessory = block.accessory ? renderElement(block.accessory, ctx) : "";
|
|
455
|
+
const body = parts.join("");
|
|
456
|
+
if (!accessory)
|
|
457
|
+
return `<div class="sm-block sm-section">${body}</div>`;
|
|
458
|
+
return `<div class="sm-block sm-section sm-section-split"><div class="sm-section-main">${body}</div><div class="sm-section-acc">${accessory}</div></div>`;
|
|
507
459
|
}
|
|
508
|
-
function
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
return {
|
|
519
|
-
items: page,
|
|
520
|
-
has_more,
|
|
521
|
-
next_cursor: has_more ? Buffer.from(`offset:${start + limit}`).toString("base64") : ""
|
|
522
|
-
};
|
|
460
|
+
function contextBlock(block, ctx) {
|
|
461
|
+
const inner = arr(block.elements).map((e) => {
|
|
462
|
+
const el = rec(e);
|
|
463
|
+
if (!el)
|
|
464
|
+
return "";
|
|
465
|
+
if (str(el.type) === "image")
|
|
466
|
+
return image(imageSrcOf(el), str(el.alt_text), "sm-context-img");
|
|
467
|
+
return `<span class="sm-context-text">${textObject(el, ctx)}</span>`;
|
|
468
|
+
}).join("");
|
|
469
|
+
return `<div class="sm-block sm-context">${inner}</div>`;
|
|
523
470
|
}
|
|
524
|
-
function
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
md: "text/markdown",
|
|
534
|
-
log: "text/plain",
|
|
535
|
-
json: "application/json",
|
|
536
|
-
csv: "text/csv",
|
|
537
|
-
pdf: "application/pdf",
|
|
538
|
-
html: "text/html",
|
|
539
|
-
zip: "application/zip"
|
|
540
|
-
};
|
|
541
|
-
return map[ext] ?? "application/octet-stream";
|
|
471
|
+
function imageBlock(block, ctx) {
|
|
472
|
+
const title = block.title ? `<div class="sm-img-title">${textObject(block.title, ctx)}</div>` : "";
|
|
473
|
+
const src = imageSrcOf(block);
|
|
474
|
+
const alt = str(block.alt_text);
|
|
475
|
+
if (!src) {
|
|
476
|
+
const file = rec(block.slack_file);
|
|
477
|
+
return `<div class="sm-block">${title}${unsupported(`image (file ${str(file?.id) || "?"})`)}</div>`;
|
|
478
|
+
}
|
|
479
|
+
return `<div class="sm-block sm-image">${title}${image(src, alt, "sm-img")}</div>`;
|
|
542
480
|
}
|
|
543
|
-
function
|
|
544
|
-
const
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
return
|
|
481
|
+
function inputBlock(block, ctx) {
|
|
482
|
+
const label = block.label ? `<div class="sm-label">${plainOf(block.label)}</div>` : "";
|
|
483
|
+
const element = block.element ? renderElement(block.element, ctx) : "";
|
|
484
|
+
const hint = block.hint ? `<div class="sm-hint">${plainOf(block.hint)}</div>` : "";
|
|
485
|
+
return `<div class="sm-block sm-input-block">${label}${element}${hint}</div>`;
|
|
548
486
|
}
|
|
549
|
-
function
|
|
550
|
-
const
|
|
551
|
-
|
|
487
|
+
function videoBlock(block, ctx) {
|
|
488
|
+
const thumb = safeUrl(str(block.thumbnail_url));
|
|
489
|
+
const url = safeUrl(str(block.title_url) || str(block.video_url));
|
|
490
|
+
const title = textObject(block.title, ctx) || plainTextToHtml(str(block.alt_text));
|
|
491
|
+
const inner = `${thumb ? `<img class="sm-video-thumb" src="${thumb}" alt="${escapeHtml(str(block.alt_text))}">` : ""}<div class="sm-video-title">\u25B6 ${title}</div>`;
|
|
492
|
+
return `<div class="sm-block sm-video">${url ? `<a class="sm-video-link" href="${url}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`;
|
|
552
493
|
}
|
|
553
|
-
function
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
494
|
+
function renderBlock(v, ctx) {
|
|
495
|
+
const block = rec(v);
|
|
496
|
+
if (!block)
|
|
497
|
+
return unsupported("block");
|
|
498
|
+
switch (str(block.type)) {
|
|
499
|
+
case "header":
|
|
500
|
+
return `<div class="sm-block sm-header-block">${textObject(block.text, ctx)}</div>`;
|
|
501
|
+
case "section":
|
|
502
|
+
return sectionBlock(block, ctx);
|
|
503
|
+
case "divider":
|
|
504
|
+
return `<div class="sm-block"><hr class="sm-hr"></div>`;
|
|
505
|
+
case "context":
|
|
506
|
+
return contextBlock(block, ctx);
|
|
507
|
+
case "actions":
|
|
508
|
+
return `<div class="sm-block sm-actions">${arr(block.elements).map((e) => renderElement(e, ctx)).join("")}</div>`;
|
|
509
|
+
case "image":
|
|
510
|
+
return imageBlock(block, ctx);
|
|
511
|
+
case "rich_text":
|
|
512
|
+
return `<div class="sm-block sm-rich-text">${arr(block.elements).map((e) => richSection(e, ctx)).join("")}</div>`;
|
|
513
|
+
case "input":
|
|
514
|
+
return inputBlock(block, ctx);
|
|
515
|
+
case "file":
|
|
516
|
+
return `<div class="sm-block sm-file-block">\uD83D\uDCCE ${escapeHtml(str(block.external_id) || str(block.file_id) || "file")}</div>`;
|
|
517
|
+
case "video":
|
|
518
|
+
return videoBlock(block, ctx);
|
|
519
|
+
default:
|
|
520
|
+
return `<div class="sm-block">${unsupported(str(block.type))}</div>`;
|
|
561
521
|
}
|
|
562
|
-
return change;
|
|
563
|
-
}
|
|
564
|
-
function ensureDir(file) {
|
|
565
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
566
522
|
}
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
var JSON_FIELDS = new Set([
|
|
573
|
-
"blocks",
|
|
574
|
-
"attachments",
|
|
575
|
-
"metadata",
|
|
576
|
-
"files",
|
|
577
|
-
"view",
|
|
578
|
-
"prompts",
|
|
579
|
-
"icons",
|
|
580
|
-
"user_ids",
|
|
581
|
-
"types",
|
|
582
|
-
"chunks",
|
|
583
|
-
"loading_messages"
|
|
584
|
-
]);
|
|
585
|
-
function coerce(key, value) {
|
|
586
|
-
if (JSON_FIELDS.has(key)) {
|
|
587
|
-
try {
|
|
588
|
-
return JSON.parse(value);
|
|
589
|
-
} catch {
|
|
590
|
-
return value;
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
if (value === "true")
|
|
594
|
-
return true;
|
|
595
|
-
if (value === "false")
|
|
596
|
-
return false;
|
|
597
|
-
return value;
|
|
523
|
+
function renderBlocks(blocks, ctx) {
|
|
524
|
+
const list = arr(blocks);
|
|
525
|
+
if (!list.length)
|
|
526
|
+
return "";
|
|
527
|
+
return `<div class="sm-blocks">${list.map((b) => renderBlock(b, ctx)).join("")}</div>`;
|
|
598
528
|
}
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
}
|
|
613
|
-
if (contentType.includes("multipart/form-data")) {
|
|
614
|
-
const form = await req.formData();
|
|
615
|
-
form.forEach((v, k) => {
|
|
616
|
-
args[k] = typeof v === "string" ? coerce(k, v) : v;
|
|
617
|
-
});
|
|
618
|
-
return args;
|
|
619
|
-
}
|
|
620
|
-
const text = await req.text();
|
|
621
|
-
if (!text)
|
|
622
|
-
return args;
|
|
623
|
-
for (const [k, v] of new URLSearchParams(text))
|
|
624
|
-
args[k] = coerce(k, v);
|
|
625
|
-
return args;
|
|
626
|
-
}
|
|
627
|
-
function bearerToken(req, args) {
|
|
628
|
-
const header = req.headers.get("authorization");
|
|
629
|
-
if (header?.toLowerCase().startsWith("bearer "))
|
|
630
|
-
return header.slice(7).trim();
|
|
631
|
-
if (typeof args.token === "string")
|
|
632
|
-
return args.token;
|
|
633
|
-
return;
|
|
634
|
-
}
|
|
635
|
-
function str(args, key) {
|
|
636
|
-
const v = args[key];
|
|
637
|
-
return typeof v === "string" ? v : v == null ? undefined : String(v);
|
|
638
|
-
}
|
|
639
|
-
function num(args, key, fallback) {
|
|
640
|
-
const v = args[key];
|
|
641
|
-
const n = typeof v === "number" ? v : typeof v === "string" ? Number.parseInt(v, 10) : Number.NaN;
|
|
642
|
-
return Number.isFinite(n) ? n : fallback;
|
|
643
|
-
}
|
|
644
|
-
function bool(args, key) {
|
|
645
|
-
const v = args[key];
|
|
646
|
-
if (typeof v === "boolean")
|
|
647
|
-
return v;
|
|
648
|
-
if (v === "true" || v === "1" || v === 1)
|
|
649
|
-
return true;
|
|
650
|
-
if (v === "false" || v === "0" || v === 0)
|
|
651
|
-
return false;
|
|
652
|
-
return;
|
|
529
|
+
var NAMED_COLORS = {
|
|
530
|
+
good: "#2eb67d",
|
|
531
|
+
warning: "#ecb22e",
|
|
532
|
+
danger: "#e01e5a"
|
|
533
|
+
};
|
|
534
|
+
function barColor(v) {
|
|
535
|
+
const raw = str(v).trim();
|
|
536
|
+
if (!raw)
|
|
537
|
+
return "#dddddd";
|
|
538
|
+
const named = NAMED_COLORS[raw.toLowerCase()];
|
|
539
|
+
if (named)
|
|
540
|
+
return named;
|
|
541
|
+
const hex = raw.startsWith("#") ? raw : `#${raw}`;
|
|
542
|
+
return /^#[0-9a-fA-F]{3,8}$/.test(hex) ? hex : "#dddddd";
|
|
653
543
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
event,
|
|
665
|
-
type: "event_callback",
|
|
666
|
-
event_id: slackId("Ev", 9),
|
|
667
|
-
event_time: nowUnix(),
|
|
668
|
-
authorizations: [
|
|
669
|
-
{
|
|
670
|
-
enterprise_id: null,
|
|
671
|
-
team_id: store.team.id,
|
|
672
|
-
user_id: store.bot.userId,
|
|
673
|
-
is_bot: true,
|
|
674
|
-
is_enterprise_install: false
|
|
675
|
-
}
|
|
676
|
-
],
|
|
677
|
-
is_ext_shared_channel: false,
|
|
678
|
-
event_context: Buffer.from(`${store.team.id}:${store.app.id}:${event.channel ?? ""}`).toString("base64")
|
|
679
|
-
};
|
|
544
|
+
function attachmentFields(fields, ctx) {
|
|
545
|
+
if (!fields.length)
|
|
546
|
+
return "";
|
|
547
|
+
return `<div class="sm-fields">${fields.map((f) => {
|
|
548
|
+
const field = rec(f);
|
|
549
|
+
if (!field)
|
|
550
|
+
return "";
|
|
551
|
+
const wide = field.short === true ? "" : " sm-field-wide";
|
|
552
|
+
return `<div class="sm-field${wide}"><div class="sm-field-title">${plainTextToHtml(str(field.title))}</div><div>${mrkdwnToHtml(str(field.value), ctx)}</div></div>`;
|
|
553
|
+
}).join("")}</div>`;
|
|
680
554
|
}
|
|
681
|
-
function
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
channel: m.channel,
|
|
691
|
-
event_ts: m.ts,
|
|
692
|
-
channel_type: store.channelType(channel),
|
|
693
|
-
team
|
|
694
|
-
};
|
|
695
|
-
if (m.files?.length && !m.subtype)
|
|
696
|
-
event.subtype = "file_share";
|
|
697
|
-
if (m.files?.length) {
|
|
698
|
-
event.upload = false;
|
|
699
|
-
event.display_as_bot = false;
|
|
555
|
+
function attachment(v, ctx) {
|
|
556
|
+
const att = rec(v);
|
|
557
|
+
if (!att)
|
|
558
|
+
return "";
|
|
559
|
+
const parts = [];
|
|
560
|
+
const author = str(att.author_name);
|
|
561
|
+
if (author) {
|
|
562
|
+
const icon = safeUrl(str(att.author_icon));
|
|
563
|
+
parts.push(`<div class="sm-att-author">${icon ? `<img class="sm-att-author-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(author)}</div>`);
|
|
700
564
|
}
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
const ts = nextTs();
|
|
730
|
-
return {
|
|
731
|
-
type: "message",
|
|
732
|
-
subtype: "message_deleted",
|
|
733
|
-
channel: m.channel,
|
|
734
|
-
channel_type: store.channelType(channel),
|
|
735
|
-
hidden: true,
|
|
736
|
-
deleted_ts: m.ts,
|
|
737
|
-
ts,
|
|
738
|
-
event_ts: ts,
|
|
739
|
-
previous_message: { ...wireMessage(m), channel: undefined }
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
function reactionEvent(kind, m, name, user) {
|
|
743
|
-
return {
|
|
744
|
-
type: kind,
|
|
745
|
-
user,
|
|
746
|
-
reaction: name,
|
|
747
|
-
item_user: m.user,
|
|
748
|
-
item: { type: "message", channel: m.channel, ts: m.ts },
|
|
749
|
-
event_ts: nextTs()
|
|
750
|
-
};
|
|
751
|
-
}
|
|
752
|
-
function memberJoinedEvent(store, channel, user, inviter) {
|
|
753
|
-
return {
|
|
754
|
-
type: "member_joined_channel",
|
|
755
|
-
user,
|
|
756
|
-
channel: channel.id,
|
|
757
|
-
channel_type: store.channelType(channel) === "channel" ? "C" : "G",
|
|
758
|
-
team: store.team.id,
|
|
759
|
-
inviter,
|
|
760
|
-
event_ts: nextTs()
|
|
761
|
-
};
|
|
762
|
-
}
|
|
763
|
-
function assistantThreadStartedEvent(store, dm, user, threadTs, context = {}) {
|
|
764
|
-
return {
|
|
765
|
-
type: "assistant_thread_started",
|
|
766
|
-
assistant_thread: {
|
|
767
|
-
user_id: user,
|
|
768
|
-
context: { team_id: store.team.id, enterprise_id: null, ...context },
|
|
769
|
-
channel_id: dm.id,
|
|
770
|
-
thread_ts: threadTs
|
|
771
|
-
},
|
|
772
|
-
event_ts: nextTs()
|
|
773
|
-
};
|
|
565
|
+
const title = str(att.title);
|
|
566
|
+
if (title) {
|
|
567
|
+
const link = safeUrl(str(att.title_link));
|
|
568
|
+
const inner = plainTextToHtml(title);
|
|
569
|
+
parts.push(`<div class="sm-att-title">${link ? `<a class="sm-link" href="${link}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`);
|
|
570
|
+
}
|
|
571
|
+
const text = str(att.text) || (parts.length ? "" : str(att.fallback));
|
|
572
|
+
if (text)
|
|
573
|
+
parts.push(`<div class="sm-att-text">${mrkdwnToHtml(text, ctx)}</div>`);
|
|
574
|
+
parts.push(attachmentFields(arr(att.fields), ctx));
|
|
575
|
+
const img = safeUrl(str(att.image_url));
|
|
576
|
+
if (img)
|
|
577
|
+
parts.push(`<img class="sm-att-img" src="${img}" alt="">`);
|
|
578
|
+
const thumb = !img ? safeUrl(str(att.thumb_url)) : undefined;
|
|
579
|
+
if (thumb)
|
|
580
|
+
parts.push(`<img class="sm-att-thumb" src="${thumb}" alt="">`);
|
|
581
|
+
const blocks = arr(att.blocks);
|
|
582
|
+
if (blocks.length)
|
|
583
|
+
parts.push(renderBlocks(blocks, ctx));
|
|
584
|
+
const footer = str(att.footer);
|
|
585
|
+
const ts = att.ts ? formatAttachmentTs(att.ts) : "";
|
|
586
|
+
if (footer || ts) {
|
|
587
|
+
const icon = safeUrl(str(att.footer_icon));
|
|
588
|
+
parts.push(`<div class="sm-att-footer">${icon ? `<img class="sm-att-footer-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(footer)}${footer && ts ? " | " : ""}${escapeHtml(ts)}</div>`);
|
|
589
|
+
}
|
|
590
|
+
const pretext = str(att.pretext);
|
|
591
|
+
const box = `<div class="sm-att" style="border-left-color:${barColor(att.color)}">${parts.join("")}</div>`;
|
|
592
|
+
return pretext ? `<div class="sm-att-pretext">${mrkdwnToHtml(pretext, ctx)}</div>${box}` : box;
|
|
774
593
|
}
|
|
775
|
-
function
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
thread_ts: threadTs
|
|
783
|
-
},
|
|
784
|
-
event_ts: nextTs()
|
|
785
|
-
};
|
|
594
|
+
function formatAttachmentTs(v) {
|
|
595
|
+
const secs = Number(v);
|
|
596
|
+
if (!Number.isFinite(secs) || secs <= 0)
|
|
597
|
+
return str(v);
|
|
598
|
+
const d = new Date(secs * 1000);
|
|
599
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
600
|
+
return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
786
601
|
}
|
|
787
|
-
function
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
channel_id: input.channel.id,
|
|
793
|
-
channel_name: input.channel.is_im ? "directmessage" : input.channel.name,
|
|
794
|
-
user_id: input.user.id,
|
|
795
|
-
user_name: input.user.name,
|
|
796
|
-
command: input.command,
|
|
797
|
-
text: input.text,
|
|
798
|
-
api_app_id: store.app.id,
|
|
799
|
-
is_enterprise_install: "false",
|
|
800
|
-
response_url: input.responseUrl,
|
|
801
|
-
trigger_id: input.triggerId
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
function blockActionsPayload(store, input) {
|
|
805
|
-
return {
|
|
806
|
-
type: "block_actions",
|
|
807
|
-
user: {
|
|
808
|
-
id: input.user.id,
|
|
809
|
-
username: input.user.name,
|
|
810
|
-
name: input.user.name,
|
|
811
|
-
team_id: store.team.id
|
|
812
|
-
},
|
|
813
|
-
api_app_id: store.app.id,
|
|
814
|
-
token: VERIFICATION_TOKEN,
|
|
815
|
-
container: {
|
|
816
|
-
type: "message",
|
|
817
|
-
message_ts: input.message.ts,
|
|
818
|
-
channel_id: input.channel.id,
|
|
819
|
-
is_ephemeral: !!input.message.ephemeral_user,
|
|
820
|
-
...input.message.thread_ts ? { thread_ts: input.message.thread_ts } : {}
|
|
821
|
-
},
|
|
822
|
-
trigger_id: input.triggerId,
|
|
823
|
-
team: { id: store.team.id, domain: store.team.domain },
|
|
824
|
-
enterprise: null,
|
|
825
|
-
is_enterprise_install: false,
|
|
826
|
-
channel: { id: input.channel.id, name: input.channel.name },
|
|
827
|
-
message: wireMessage(input.message),
|
|
828
|
-
state: { values: {} },
|
|
829
|
-
response_url: input.responseUrl,
|
|
830
|
-
actions: input.actions.map((a) => ({ action_ts: nextTs(), ...a }))
|
|
831
|
-
};
|
|
832
|
-
}
|
|
833
|
-
function viewSubmissionPayload(store, input) {
|
|
834
|
-
const { opened_by: _o, trigger_id: _t, ...view } = input.view;
|
|
835
|
-
return {
|
|
836
|
-
type: "view_submission",
|
|
837
|
-
team: { id: store.team.id, domain: store.team.domain },
|
|
838
|
-
user: {
|
|
839
|
-
id: input.user.id,
|
|
840
|
-
username: input.user.name,
|
|
841
|
-
name: input.user.name,
|
|
842
|
-
team_id: store.team.id
|
|
843
|
-
},
|
|
844
|
-
api_app_id: store.app.id,
|
|
845
|
-
token: VERIFICATION_TOKEN,
|
|
846
|
-
trigger_id: input.triggerId,
|
|
847
|
-
view: { ...view, state: { values: input.values } },
|
|
848
|
-
response_urls: [],
|
|
849
|
-
is_enterprise_install: false,
|
|
850
|
-
enterprise: null
|
|
851
|
-
};
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// src/render/mrkdwn.ts
|
|
855
|
-
function escapeHtml(s) {
|
|
856
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
857
|
-
}
|
|
858
|
-
function unescapeSlack(s) {
|
|
859
|
-
return s.replace(/&(amp|lt|gt|quot);/g, (_m, name) => name === "amp" ? "&" : name === "lt" ? "<" : name === "gt" ? ">" : '"');
|
|
860
|
-
}
|
|
861
|
-
function reEscape(s) {
|
|
862
|
-
return escapeHtml(unescapeSlack(s));
|
|
863
|
-
}
|
|
864
|
-
var EMOJI = {
|
|
865
|
-
eyes: "\uD83D\uDC40",
|
|
866
|
-
white_check_mark: "\u2705",
|
|
867
|
-
x: "\u274C",
|
|
868
|
-
heavy_plus_sign: "\u2795",
|
|
869
|
-
heavy_minus_sign: "\u2796",
|
|
870
|
-
heavy_multiplication_x: "\u2716\uFE0F",
|
|
871
|
-
zap: "\u26A1",
|
|
872
|
-
speech_balloon: "\uD83D\uDCAC",
|
|
873
|
-
robot_face: "\uD83E\uDD16",
|
|
874
|
-
gear: "\u2699\uFE0F",
|
|
875
|
-
rocket: "\uD83D\uDE80",
|
|
876
|
-
star: "\u2B50",
|
|
877
|
-
crystal_ball: "\uD83D\uDD2E",
|
|
878
|
-
bulb: "\uD83D\uDCA1",
|
|
879
|
-
wrench: "\uD83D\uDD27",
|
|
880
|
-
hammer: "\uD83D\uDD28",
|
|
881
|
-
crown: "\uD83D\uDC51",
|
|
882
|
-
tada: "\uD83C\uDF89",
|
|
883
|
-
warning: "\u26A0\uFE0F",
|
|
884
|
-
hourglass: "\u231B",
|
|
885
|
-
hourglass_flowing_sand: "\u23F3",
|
|
886
|
-
thumbsup: "\uD83D\uDC4D",
|
|
887
|
-
thumbsdown: "\uD83D\uDC4E",
|
|
888
|
-
"+1": "\uD83D\uDC4D",
|
|
889
|
-
"-1": "\uD83D\uDC4E",
|
|
890
|
-
heavy_check_mark: "\u2714\uFE0F",
|
|
891
|
-
question: "\u2753",
|
|
892
|
-
grey_question: "\u2754",
|
|
893
|
-
exclamation: "\u2757",
|
|
894
|
-
white_circle: "\u26AA",
|
|
895
|
-
large_blue_circle: "\uD83D\uDD35",
|
|
896
|
-
black_circle: "\u26AB",
|
|
897
|
-
red_circle: "\uD83D\uDD34",
|
|
898
|
-
large_green_circle: "\uD83D\uDFE2",
|
|
899
|
-
large_yellow_circle: "\uD83D\uDFE1",
|
|
900
|
-
fire: "\uD83D\uDD25",
|
|
901
|
-
sparkles: "\u2728",
|
|
902
|
-
memo: "\uD83D\uDCDD",
|
|
903
|
-
clipboard: "\uD83D\uDCCB",
|
|
904
|
-
mag: "\uD83D\uDD0D",
|
|
905
|
-
mag_right: "\uD83D\uDD0E",
|
|
906
|
-
link: "\uD83D\uDD17",
|
|
907
|
-
lock: "\uD83D\uDD12",
|
|
908
|
-
unlock: "\uD83D\uDD13",
|
|
909
|
-
bell: "\uD83D\uDD14",
|
|
910
|
-
package: "\uD83D\uDCE6",
|
|
911
|
-
construction: "\uD83D\uDEA7",
|
|
912
|
-
bug: "\uD83D\uDC1B",
|
|
913
|
-
boom: "\uD83D\uDCA5",
|
|
914
|
-
pencil2: "\u270F\uFE0F",
|
|
915
|
-
scroll: "\uD83D\uDCDC",
|
|
916
|
-
page_facing_up: "\uD83D\uDCC4",
|
|
917
|
-
file_folder: "\uD83D\uDCC1",
|
|
918
|
-
card_index_dividers: "\uD83D\uDDC2\uFE0F",
|
|
919
|
-
paperclip: "\uD83D\uDCCE",
|
|
920
|
-
bar_chart: "\uD83D\uDCCA",
|
|
921
|
-
chart_with_upwards_trend: "\uD83D\uDCC8",
|
|
922
|
-
calendar: "\uD83D\uDCC5",
|
|
923
|
-
clock1: "\uD83D\uDD50",
|
|
924
|
-
arrow_right: "\u27A1\uFE0F",
|
|
925
|
-
arrow_left: "\u2B05\uFE0F",
|
|
926
|
-
arrow_up: "\u2B06\uFE0F",
|
|
927
|
-
arrow_down: "\u2B07\uFE0F",
|
|
928
|
-
arrow_right_hook: "\u21AA\uFE0F",
|
|
929
|
-
repeat: "\uD83D\uDD01",
|
|
930
|
-
arrows_counterclockwise: "\uD83D\uDD04",
|
|
931
|
-
recycle: "\u267B\uFE0F",
|
|
932
|
-
no_entry: "\u26D4",
|
|
933
|
-
no_entry_sign: "\uD83D\uDEAB",
|
|
934
|
-
stop_sign: "\uD83D\uDED1",
|
|
935
|
-
pause_button: "\u23F8\uFE0F",
|
|
936
|
-
information_source: "\u2139\uFE0F",
|
|
937
|
-
wave: "\uD83D\uDC4B",
|
|
938
|
-
pray: "\uD83D\uDE4F",
|
|
939
|
-
muscle: "\uD83D\uDCAA",
|
|
940
|
-
brain: "\uD83E\uDDE0",
|
|
941
|
-
dart: "\uD83C\uDFAF",
|
|
942
|
-
trophy: "\uD83C\uDFC6",
|
|
943
|
-
medal: "\uD83C\uDFC5",
|
|
944
|
-
checkered_flag: "\uD83C\uDFC1",
|
|
945
|
-
ok_hand: "\uD83D\uDC4C",
|
|
946
|
-
point_right: "\uD83D\uDC49",
|
|
947
|
-
clap: "\uD83D\uDC4F",
|
|
948
|
-
raised_hands: "\uD83D\uDE4C",
|
|
949
|
-
thinking_face: "\uD83E\uDD14",
|
|
950
|
-
"100": "\uD83D\uDCAF",
|
|
951
|
-
green_heart: "\uD83D\uDC9A",
|
|
952
|
-
thread: "\uD83E\uDDF5",
|
|
953
|
-
satellite_antenna: "\uD83D\uDCE1",
|
|
954
|
-
inbox_tray: "\uD83D\uDCE5",
|
|
955
|
-
outbox_tray: "\uD83D\uDCE4",
|
|
956
|
-
incoming_envelope: "\uD83D\uDCE8",
|
|
957
|
-
mailbox_with_no_mail: "\uD83D\uDCED",
|
|
958
|
-
envelope: "\u2709\uFE0F",
|
|
959
|
-
test_tube: "\uD83E\uDDEA",
|
|
960
|
-
ship: "\uD83D\uDEA2",
|
|
961
|
-
sos: "\uD83C\uDD98",
|
|
962
|
-
small_blue_diamond: "\uD83D\uDD39",
|
|
963
|
-
small_orange_diamond: "\uD83D\uDD38",
|
|
964
|
-
black_small_square: "\u25AA\uFE0F",
|
|
965
|
-
white_small_square: "\u25AB\uFE0F"
|
|
966
|
-
};
|
|
967
|
-
function emojiChar(name) {
|
|
968
|
-
return EMOJI[name.replace(/^:|:$/g, "").replace(/::skin-tone-\d$/, "")];
|
|
969
|
-
}
|
|
970
|
-
function emojiFromUnicode(code) {
|
|
971
|
-
const points = code.split("-").map((p) => Number.parseInt(p, 16));
|
|
972
|
-
if (points.some((p) => !Number.isFinite(p)))
|
|
973
|
-
return;
|
|
974
|
-
try {
|
|
975
|
-
return String.fromCodePoint(...points);
|
|
976
|
-
} catch {
|
|
977
|
-
return;
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
function renderEmoji(html) {
|
|
981
|
-
return html.replace(/:([a-z][a-z0-9_+'-]*|\+1|-1|100):/g, (m, name) => {
|
|
982
|
-
const char = emojiChar(name);
|
|
983
|
-
return char ?? `<span class="sm-emoji-name">${m}</span>`;
|
|
984
|
-
});
|
|
985
|
-
}
|
|
986
|
-
function displayName(user) {
|
|
987
|
-
return user.profile.display_name || user.real_name || user.name;
|
|
988
|
-
}
|
|
989
|
-
function pill(text) {
|
|
990
|
-
return `<span class="sm-pill">${escapeHtml(text)}</span>`;
|
|
991
|
-
}
|
|
992
|
-
function isSafeUrl(url) {
|
|
993
|
-
return /^(https?:\/\/|mailto:|tel:)/i.test(url);
|
|
994
|
-
}
|
|
995
|
-
function renderControl(inner, ctx) {
|
|
996
|
-
const bar = inner.indexOf("|");
|
|
997
|
-
const head = bar >= 0 ? inner.slice(0, bar) : inner;
|
|
998
|
-
const label = bar >= 0 ? inner.slice(bar + 1) : "";
|
|
999
|
-
if (head.startsWith("@")) {
|
|
1000
|
-
const id = head.slice(1);
|
|
1001
|
-
const user = ctx.users.get(id);
|
|
1002
|
-
return pill(`@${user ? displayName(user) : label || id}`);
|
|
1003
|
-
}
|
|
1004
|
-
if (head.startsWith("#")) {
|
|
1005
|
-
const id = head.slice(1);
|
|
1006
|
-
const channel = ctx.channels.get(id);
|
|
1007
|
-
return pill(`#${channel ? channel.name : label.replace(/^#/, "") || id}`);
|
|
1008
|
-
}
|
|
1009
|
-
if (head.startsWith("!")) {
|
|
1010
|
-
const cmd = head.slice(1);
|
|
1011
|
-
if (cmd === "here" || cmd === "channel" || cmd === "everyone")
|
|
1012
|
-
return pill(`@${cmd}`);
|
|
1013
|
-
if (cmd.startsWith("subteam^")) {
|
|
1014
|
-
const handle = label || cmd.slice("subteam^".length);
|
|
1015
|
-
return pill(handle.startsWith("@") ? handle : `@${handle}`);
|
|
1016
|
-
}
|
|
1017
|
-
if (cmd.startsWith("date^")) {
|
|
1018
|
-
if (label)
|
|
1019
|
-
return renderEmoji(reEscape(label));
|
|
1020
|
-
const secs = Number(cmd.split("^")[1]);
|
|
1021
|
-
return Number.isFinite(secs) ? escapeHtml(new Date(secs * 1000).toISOString()) : "";
|
|
1022
|
-
}
|
|
1023
|
-
return pill(`@${cmd}`);
|
|
1024
|
-
}
|
|
1025
|
-
if (isSafeUrl(head)) {
|
|
1026
|
-
const shown = label || head.replace(/^mailto:/i, "");
|
|
1027
|
-
const attrs = /^mailto:/i.test(head) ? "" : ' target="_blank" rel="noopener"';
|
|
1028
|
-
return `<a class="sm-link" href="${escapeHtml(head)}"${attrs}>${renderEmoji(reEscape(shown))}</a>`;
|
|
1029
|
-
}
|
|
1030
|
-
return escapeHtml(`<${unescapeSlack(inner)}>`);
|
|
1031
|
-
}
|
|
1032
|
-
function preHtml(body) {
|
|
1033
|
-
let code = body.replace(/^\n/, "").replace(/\n$/, "");
|
|
1034
|
-
if (code.includes(`
|
|
1035
|
-
`))
|
|
1036
|
-
code = code.replace(/^[a-z][a-z0-9+#-]{0,11}\n/, "");
|
|
1037
|
-
return `<pre class="sm-pre">${reEscape(code)}</pre>`;
|
|
1038
|
-
}
|
|
1039
|
-
var BOUND_L = `(^|[\\s(\\[{'"\\uE000\\uE001])`;
|
|
1040
|
-
var BOUND_R = `(?=$|[\\s.,;:!?)\\]}'"\\uE000\\uE001])`;
|
|
1041
|
-
function styleRule(marker) {
|
|
1042
|
-
const m = marker.replace(/[*]/g, "\\*");
|
|
1043
|
-
return new RegExp(`${BOUND_L}${m}(?![\\s${m}])([^${m}\\n]*[^\\s${m}])${m}${BOUND_R}`, "g");
|
|
1044
|
-
}
|
|
1045
|
-
var RULES = [
|
|
1046
|
-
[styleRule("*"), "strong"],
|
|
1047
|
-
[styleRule("~"), "s"],
|
|
1048
|
-
[styleRule("_"), "em"]
|
|
1049
|
-
];
|
|
1050
|
-
function inlineStyles(s) {
|
|
1051
|
-
let out = s;
|
|
1052
|
-
for (const [re, tag] of RULES)
|
|
1053
|
-
out = out.replace(re, `$1<${tag}>$2</${tag}>`);
|
|
1054
|
-
return out;
|
|
1055
|
-
}
|
|
1056
|
-
var QUOTE = /^\s*(?:>|>)\s?/;
|
|
1057
|
-
var BULLET = /^(\s*)(?:\u2022|-|\*)\s+(.*)$/;
|
|
1058
|
-
var NUMBER = /^(\s*)\d+[.)]\s+(.*)$/;
|
|
1059
|
-
function blockify(text) {
|
|
1060
|
-
const groups = [];
|
|
1061
|
-
const push = (kind, html, indent = 0) => {
|
|
1062
|
-
const last = groups[groups.length - 1];
|
|
1063
|
-
if (last?.kind === kind)
|
|
1064
|
-
last.items.push({ html, indent });
|
|
1065
|
-
else
|
|
1066
|
-
groups.push({ kind, items: [{ html, indent }] });
|
|
1067
|
-
};
|
|
1068
|
-
for (const line of text.split(`
|
|
1069
|
-
`)) {
|
|
1070
|
-
const bullet = BULLET.exec(line);
|
|
1071
|
-
const numbered = bullet ? null : NUMBER.exec(line);
|
|
1072
|
-
if (QUOTE.test(line))
|
|
1073
|
-
push("quote", line.replace(QUOTE, ""));
|
|
1074
|
-
else if (bullet)
|
|
1075
|
-
push("ul", bullet[2] ?? "", Math.min(4, Math.floor((bullet[1]?.length ?? 0) / 2)));
|
|
1076
|
-
else if (numbered)
|
|
1077
|
-
push("ol", numbered[2] ?? "", Math.min(4, Math.floor((numbered[1]?.length ?? 0) / 2)));
|
|
1078
|
-
else
|
|
1079
|
-
push("p", line);
|
|
1080
|
-
}
|
|
1081
|
-
return groups.map((g) => {
|
|
1082
|
-
if (g.kind === "p")
|
|
1083
|
-
return g.items.map((i) => i.html).join("<br>");
|
|
1084
|
-
const inner = g.items.map((i) => `<li${i.indent ? ` style="margin-left:${i.indent * 18}px"` : ""}>${i.html}</li>`).join("");
|
|
1085
|
-
if (g.kind === "quote")
|
|
1086
|
-
return `<blockquote class="sm-quote">${g.items.map((i) => i.html).join("<br>")}</blockquote>`;
|
|
1087
|
-
return `<${g.kind} class="sm-list">${inner}</${g.kind}>`;
|
|
1088
|
-
}).join("");
|
|
1089
|
-
}
|
|
1090
|
-
var BLOCK_TAGS = "pre|blockquote|ul|ol";
|
|
1091
|
-
function tidy(html) {
|
|
1092
|
-
return html.replace(new RegExp(`<br>\\s*(<(?:${BLOCK_TAGS})\\b)`, "g"), "$1").replace(new RegExp(`(</(?:${BLOCK_TAGS})>)\\s*<br>`, "g"), "$1");
|
|
1093
|
-
}
|
|
1094
|
-
function mrkdwnToHtml(text, ctx) {
|
|
1095
|
-
if (!text)
|
|
1096
|
-
return "";
|
|
1097
|
-
const tokens = [];
|
|
1098
|
-
const hold = (html) => {
|
|
1099
|
-
tokens.push(html);
|
|
1100
|
-
return `\uE000${tokens.length - 1}\uE001`;
|
|
1101
|
-
};
|
|
1102
|
-
let s = text.replace(/\r\n/g, `
|
|
1103
|
-
`);
|
|
1104
|
-
s = s.replace(/```([\s\S]*?)```/g, (_m, body) => hold(preHtml(body)));
|
|
1105
|
-
s = s.replace(/`([^`\n]+)`/g, (_m, body) => hold(`<code class="sm-code">${reEscape(body)}</code>`));
|
|
1106
|
-
s = s.replace(/<([^<>\n]*)>/g, (_m, inner) => hold(renderControl(inner, ctx)));
|
|
1107
|
-
s = renderEmoji(reEscape(s));
|
|
1108
|
-
s = inlineStyles(s);
|
|
1109
|
-
s = blockify(s);
|
|
1110
|
-
const expand = (html) => html.replace(/\uE000(\d+)\uE001/g, (_m, i) => expand(tokens[Number(i)] ?? ""));
|
|
1111
|
-
return tidy(expand(s));
|
|
1112
|
-
}
|
|
1113
|
-
function plainTextToHtml(text) {
|
|
1114
|
-
if (!text)
|
|
1115
|
-
return "";
|
|
1116
|
-
return renderEmoji(reEscape(text.replace(/\r\n/g, `
|
|
1117
|
-
`))).replace(/\n/g, "<br>");
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
// src/render/blocks.ts
|
|
1121
|
-
function rec(v) {
|
|
1122
|
-
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : undefined;
|
|
1123
|
-
}
|
|
1124
|
-
function str2(v) {
|
|
1125
|
-
return typeof v === "string" ? v : "";
|
|
1126
|
-
}
|
|
1127
|
-
function arr(v) {
|
|
1128
|
-
return Array.isArray(v) ? v : [];
|
|
1129
|
-
}
|
|
1130
|
-
function safeUrl(url) {
|
|
1131
|
-
return /^(https?:\/\/|\/)/i.test(url) ? escapeHtml(url) : undefined;
|
|
1132
|
-
}
|
|
1133
|
-
function unsupported(what) {
|
|
1134
|
-
return `<div class="sm-unsupported">unsupported: ${escapeHtml(what || "unknown")}</div>`;
|
|
1135
|
-
}
|
|
1136
|
-
function textObject(v, ctx) {
|
|
1137
|
-
if (typeof v === "string")
|
|
1138
|
-
return plainTextToHtml(v);
|
|
1139
|
-
const o = rec(v);
|
|
1140
|
-
if (!o)
|
|
1141
|
-
return "";
|
|
1142
|
-
const text = str2(o.text);
|
|
1143
|
-
return o.type === "mrkdwn" ? mrkdwnToHtml(text, ctx) : plainTextToHtml(text);
|
|
1144
|
-
}
|
|
1145
|
-
function plainOf(v) {
|
|
1146
|
-
const o = rec(v);
|
|
1147
|
-
return plainTextToHtml(o ? str2(o.text) : str2(v));
|
|
1148
|
-
}
|
|
1149
|
-
function image(url, alt, cls) {
|
|
1150
|
-
const src = safeUrl(url);
|
|
1151
|
-
if (!src)
|
|
1152
|
-
return unsupported("image url");
|
|
1153
|
-
return `<img class="${cls}" src="${src}" alt="${escapeHtml(alt)}">`;
|
|
1154
|
-
}
|
|
1155
|
-
function imageSrcOf(block) {
|
|
1156
|
-
const direct = str2(block.image_url);
|
|
1157
|
-
if (direct)
|
|
1158
|
-
return direct;
|
|
1159
|
-
const file = rec(block.slack_file);
|
|
1160
|
-
return file ? str2(file.url) || str2(file.url_private) : "";
|
|
1161
|
-
}
|
|
1162
|
-
function button(el, ctx) {
|
|
1163
|
-
const style = str2(el.style);
|
|
1164
|
-
const cls = `sm-btn${style === "primary" ? " sm-btn-primary" : style === "danger" ? " sm-btn-danger" : ""}`;
|
|
1165
|
-
const label = textObject(el.text, ctx) || "Button";
|
|
1166
|
-
const url = safeUrl(str2(el.url));
|
|
1167
|
-
const confirm = rec(el.confirm) ? '<span class="sm-btn-confirm" title="confirmation dialog">\u26A0</span>' : "";
|
|
1168
|
-
return url ? `<a class="${cls}" href="${url}" target="_blank" rel="noopener">${label}${confirm}</a>` : `<span class="${cls}">${label}${confirm}</span>`;
|
|
1169
|
-
}
|
|
1170
|
-
function selectPill(el, ctx, glyph = "\u25BE") {
|
|
1171
|
-
const initial = rec(el.initial_option);
|
|
1172
|
-
const label = initial && textObject(initial.text, ctx) || textObject(el.placeholder, ctx) || plainTextToHtml(str2(el.type).replace(/_/g, " "));
|
|
1173
|
-
return `<span class="sm-select">${label}<span class="sm-select-caret">${glyph}</span></span>`;
|
|
1174
|
-
}
|
|
1175
|
-
function inputBox(el, ctx) {
|
|
1176
|
-
const multiline = el.multiline === true;
|
|
1177
|
-
const value = str2(el.initial_value);
|
|
1178
|
-
const inner = value ? plainTextToHtml(value) : `<span class="sm-input-ph">${textObject(el.placeholder, ctx)}</span>`;
|
|
1179
|
-
return `<div class="sm-input${multiline ? " sm-input-multi" : ""}">${inner}</div>`;
|
|
1180
|
-
}
|
|
1181
|
-
function renderElement(v, ctx) {
|
|
1182
|
-
const el = rec(v);
|
|
1183
|
-
if (!el)
|
|
1184
|
-
return "";
|
|
1185
|
-
const type = str2(el.type);
|
|
1186
|
-
switch (type) {
|
|
1187
|
-
case "button":
|
|
1188
|
-
return button(el, ctx);
|
|
1189
|
-
case "image":
|
|
1190
|
-
return image(imageSrcOf(el), str2(el.alt_text), "sm-acc-img");
|
|
1191
|
-
case "overflow":
|
|
1192
|
-
return `<span class="sm-select sm-overflow">\u22EF</span>`;
|
|
1193
|
-
case "datepicker":
|
|
1194
|
-
return selectPill({ ...el, placeholder: el.placeholder ?? "Select a date" }, ctx, "\uD83D\uDCC5");
|
|
1195
|
-
case "timepicker":
|
|
1196
|
-
return selectPill({ ...el, placeholder: el.placeholder ?? "Select a time" }, ctx, "\uD83D\uDD50");
|
|
1197
|
-
case "plain_text_input":
|
|
1198
|
-
return inputBox(el, ctx);
|
|
1199
|
-
case "checkboxes":
|
|
1200
|
-
case "radio_buttons":
|
|
1201
|
-
return `<div class="sm-options">${arr(el.options).map((o) => {
|
|
1202
|
-
const opt = rec(o);
|
|
1203
|
-
const mark = type === "checkboxes" ? "\u2610" : "\u25EF";
|
|
1204
|
-
return `<div class="sm-option">${mark} ${opt ? textObject(opt.text, ctx) : ""}</div>`;
|
|
1205
|
-
}).join("")}</div>`;
|
|
1206
|
-
default:
|
|
1207
|
-
if (type.endsWith("_select") || type.startsWith("multi_"))
|
|
1208
|
-
return selectPill(el, ctx);
|
|
1209
|
-
return unsupported(type);
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
function richStyle(html, style) {
|
|
1213
|
-
if (!html || !style)
|
|
1214
|
-
return html;
|
|
1215
|
-
let out = html;
|
|
1216
|
-
if (style.code === true)
|
|
1217
|
-
out = `<code class="sm-code">${out}</code>`;
|
|
1218
|
-
if (style.bold === true)
|
|
1219
|
-
out = `<strong>${out}</strong>`;
|
|
1220
|
-
if (style.italic === true)
|
|
1221
|
-
out = `<em>${out}</em>`;
|
|
1222
|
-
if (style.strike === true)
|
|
1223
|
-
out = `<s>${out}</s>`;
|
|
1224
|
-
return out;
|
|
1225
|
-
}
|
|
1226
|
-
function richElement(v, ctx) {
|
|
1227
|
-
const el = rec(v);
|
|
1228
|
-
if (!el)
|
|
1229
|
-
return "";
|
|
1230
|
-
const style = rec(el.style);
|
|
1231
|
-
switch (str2(el.type)) {
|
|
1232
|
-
case "text":
|
|
1233
|
-
return richStyle(plainTextToHtml(str2(el.text)).replace(/\n/g, "<br>"), style);
|
|
1234
|
-
case "link": {
|
|
1235
|
-
const url = safeUrl(str2(el.url));
|
|
1236
|
-
const label = plainTextToHtml(str2(el.text) || str2(el.url));
|
|
1237
|
-
if (!url)
|
|
1238
|
-
return label;
|
|
1239
|
-
return richStyle(`<a class="sm-link" href="${url}" target="_blank" rel="noopener">${label}</a>`, style);
|
|
1240
|
-
}
|
|
1241
|
-
case "user": {
|
|
1242
|
-
const user = ctx.users.get(str2(el.user_id));
|
|
1243
|
-
return `<span class="sm-pill">@${escapeHtml(user ? displayName(user) : str2(el.user_id))}</span>`;
|
|
1244
|
-
}
|
|
1245
|
-
case "usergroup":
|
|
1246
|
-
return `<span class="sm-pill">@${escapeHtml(str2(el.usergroup_id))}</span>`;
|
|
1247
|
-
case "channel": {
|
|
1248
|
-
const channel = ctx.channels.get(str2(el.channel_id));
|
|
1249
|
-
return `<span class="sm-pill">#${escapeHtml(channel ? channel.name : str2(el.channel_id))}</span>`;
|
|
1250
|
-
}
|
|
1251
|
-
case "broadcast":
|
|
1252
|
-
return `<span class="sm-pill">@${escapeHtml(str2(el.range) || "channel")}</span>`;
|
|
1253
|
-
case "emoji": {
|
|
1254
|
-
const char = emojiChar(str2(el.name)) ?? (el.unicode ? emojiFromUnicode(str2(el.unicode)) : undefined);
|
|
1255
|
-
return char ?? `<span class="sm-emoji-name">:${escapeHtml(str2(el.name))}:</span>`;
|
|
1256
|
-
}
|
|
1257
|
-
case "date":
|
|
1258
|
-
return plainTextToHtml(str2(el.fallback) || str2(el.timestamp));
|
|
1259
|
-
case "color":
|
|
1260
|
-
return plainTextToHtml(str2(el.value));
|
|
1261
|
-
default:
|
|
1262
|
-
return "";
|
|
1263
|
-
}
|
|
1264
|
-
}
|
|
1265
|
-
function richSection(v, ctx) {
|
|
1266
|
-
const block = rec(v);
|
|
1267
|
-
if (!block)
|
|
1268
|
-
return "";
|
|
1269
|
-
const inner = arr(block.elements).map((e) => richElement(e, ctx)).join("");
|
|
1270
|
-
switch (str2(block.type)) {
|
|
1271
|
-
case "rich_text_section":
|
|
1272
|
-
return `<div class="sm-rt-section">${inner}</div>`;
|
|
1273
|
-
case "rich_text_quote":
|
|
1274
|
-
return `<blockquote class="sm-quote">${inner}</blockquote>`;
|
|
1275
|
-
case "rich_text_preformatted":
|
|
1276
|
-
return `<pre class="sm-pre">${inner}</pre>`;
|
|
1277
|
-
case "rich_text_list": {
|
|
1278
|
-
const tag = str2(block.style) === "ordered" ? "ol" : "ul";
|
|
1279
|
-
const indent = Number(block.indent) || 0;
|
|
1280
|
-
const items = arr(block.elements).map((item) => `<li>${richSection(item, ctx)}</li>`).join("");
|
|
1281
|
-
const margin = indent ? ` style="margin-left:${indent * 18}px"` : "";
|
|
1282
|
-
return `<${tag} class="sm-list"${margin}>${items}</${tag}>`;
|
|
1283
|
-
}
|
|
1284
|
-
default:
|
|
1285
|
-
return inner;
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
function sectionBlock(block, ctx) {
|
|
1289
|
-
const parts = [];
|
|
1290
|
-
if (block.text)
|
|
1291
|
-
parts.push(`<div class="sm-section-text">${textObject(block.text, ctx)}</div>`);
|
|
1292
|
-
const fields = arr(block.fields);
|
|
1293
|
-
if (fields.length) {
|
|
1294
|
-
parts.push(`<div class="sm-fields">${fields.map((f) => `<div class="sm-field">${textObject(f, ctx)}</div>`).join("")}</div>`);
|
|
1295
|
-
}
|
|
1296
|
-
const accessory = block.accessory ? renderElement(block.accessory, ctx) : "";
|
|
1297
|
-
const body = parts.join("");
|
|
1298
|
-
if (!accessory)
|
|
1299
|
-
return `<div class="sm-block sm-section">${body}</div>`;
|
|
1300
|
-
return `<div class="sm-block sm-section sm-section-split"><div class="sm-section-main">${body}</div><div class="sm-section-acc">${accessory}</div></div>`;
|
|
1301
|
-
}
|
|
1302
|
-
function contextBlock(block, ctx) {
|
|
1303
|
-
const inner = arr(block.elements).map((e) => {
|
|
1304
|
-
const el = rec(e);
|
|
1305
|
-
if (!el)
|
|
1306
|
-
return "";
|
|
1307
|
-
if (str2(el.type) === "image")
|
|
1308
|
-
return image(imageSrcOf(el), str2(el.alt_text), "sm-context-img");
|
|
1309
|
-
return `<span class="sm-context-text">${textObject(el, ctx)}</span>`;
|
|
1310
|
-
}).join("");
|
|
1311
|
-
return `<div class="sm-block sm-context">${inner}</div>`;
|
|
1312
|
-
}
|
|
1313
|
-
function imageBlock(block, ctx) {
|
|
1314
|
-
const title = block.title ? `<div class="sm-img-title">${textObject(block.title, ctx)}</div>` : "";
|
|
1315
|
-
const src = imageSrcOf(block);
|
|
1316
|
-
const alt = str2(block.alt_text);
|
|
1317
|
-
if (!src) {
|
|
1318
|
-
const file = rec(block.slack_file);
|
|
1319
|
-
return `<div class="sm-block">${title}${unsupported(`image (file ${str2(file?.id) || "?"})`)}</div>`;
|
|
1320
|
-
}
|
|
1321
|
-
return `<div class="sm-block sm-image">${title}${image(src, alt, "sm-img")}</div>`;
|
|
1322
|
-
}
|
|
1323
|
-
function inputBlock(block, ctx) {
|
|
1324
|
-
const label = block.label ? `<div class="sm-label">${plainOf(block.label)}</div>` : "";
|
|
1325
|
-
const element = block.element ? renderElement(block.element, ctx) : "";
|
|
1326
|
-
const hint = block.hint ? `<div class="sm-hint">${plainOf(block.hint)}</div>` : "";
|
|
1327
|
-
return `<div class="sm-block sm-input-block">${label}${element}${hint}</div>`;
|
|
1328
|
-
}
|
|
1329
|
-
function videoBlock(block, ctx) {
|
|
1330
|
-
const thumb = safeUrl(str2(block.thumbnail_url));
|
|
1331
|
-
const url = safeUrl(str2(block.title_url) || str2(block.video_url));
|
|
1332
|
-
const title = textObject(block.title, ctx) || plainTextToHtml(str2(block.alt_text));
|
|
1333
|
-
const inner = `${thumb ? `<img class="sm-video-thumb" src="${thumb}" alt="${escapeHtml(str2(block.alt_text))}">` : ""}<div class="sm-video-title">\u25B6 ${title}</div>`;
|
|
1334
|
-
return `<div class="sm-block sm-video">${url ? `<a class="sm-video-link" href="${url}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`;
|
|
1335
|
-
}
|
|
1336
|
-
function renderBlock(v, ctx) {
|
|
1337
|
-
const block = rec(v);
|
|
1338
|
-
if (!block)
|
|
1339
|
-
return unsupported("block");
|
|
1340
|
-
switch (str2(block.type)) {
|
|
1341
|
-
case "header":
|
|
1342
|
-
return `<div class="sm-block sm-header-block">${textObject(block.text, ctx)}</div>`;
|
|
1343
|
-
case "section":
|
|
1344
|
-
return sectionBlock(block, ctx);
|
|
1345
|
-
case "divider":
|
|
1346
|
-
return `<div class="sm-block"><hr class="sm-hr"></div>`;
|
|
1347
|
-
case "context":
|
|
1348
|
-
return contextBlock(block, ctx);
|
|
1349
|
-
case "actions":
|
|
1350
|
-
return `<div class="sm-block sm-actions">${arr(block.elements).map((e) => renderElement(e, ctx)).join("")}</div>`;
|
|
1351
|
-
case "image":
|
|
1352
|
-
return imageBlock(block, ctx);
|
|
1353
|
-
case "rich_text":
|
|
1354
|
-
return `<div class="sm-block sm-rich-text">${arr(block.elements).map((e) => richSection(e, ctx)).join("")}</div>`;
|
|
1355
|
-
case "input":
|
|
1356
|
-
return inputBlock(block, ctx);
|
|
1357
|
-
case "file":
|
|
1358
|
-
return `<div class="sm-block sm-file-block">\uD83D\uDCCE ${escapeHtml(str2(block.external_id) || str2(block.file_id) || "file")}</div>`;
|
|
1359
|
-
case "video":
|
|
1360
|
-
return videoBlock(block, ctx);
|
|
1361
|
-
default:
|
|
1362
|
-
return `<div class="sm-block">${unsupported(str2(block.type))}</div>`;
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
function renderBlocks(blocks, ctx) {
|
|
1366
|
-
const list = arr(blocks);
|
|
1367
|
-
if (!list.length)
|
|
1368
|
-
return "";
|
|
1369
|
-
return `<div class="sm-blocks">${list.map((b) => renderBlock(b, ctx)).join("")}</div>`;
|
|
1370
|
-
}
|
|
1371
|
-
var NAMED_COLORS = {
|
|
1372
|
-
good: "#2eb67d",
|
|
1373
|
-
warning: "#ecb22e",
|
|
1374
|
-
danger: "#e01e5a"
|
|
1375
|
-
};
|
|
1376
|
-
function barColor(v) {
|
|
1377
|
-
const raw = str2(v).trim();
|
|
1378
|
-
if (!raw)
|
|
1379
|
-
return "#dddddd";
|
|
1380
|
-
const named = NAMED_COLORS[raw.toLowerCase()];
|
|
1381
|
-
if (named)
|
|
1382
|
-
return named;
|
|
1383
|
-
const hex = raw.startsWith("#") ? raw : `#${raw}`;
|
|
1384
|
-
return /^#[0-9a-fA-F]{3,8}$/.test(hex) ? hex : "#dddddd";
|
|
1385
|
-
}
|
|
1386
|
-
function attachmentFields(fields, ctx) {
|
|
1387
|
-
if (!fields.length)
|
|
1388
|
-
return "";
|
|
1389
|
-
return `<div class="sm-fields">${fields.map((f) => {
|
|
1390
|
-
const field = rec(f);
|
|
1391
|
-
if (!field)
|
|
1392
|
-
return "";
|
|
1393
|
-
const wide = field.short === true ? "" : " sm-field-wide";
|
|
1394
|
-
return `<div class="sm-field${wide}"><div class="sm-field-title">${plainTextToHtml(str2(field.title))}</div><div>${mrkdwnToHtml(str2(field.value), ctx)}</div></div>`;
|
|
1395
|
-
}).join("")}</div>`;
|
|
1396
|
-
}
|
|
1397
|
-
function attachment(v, ctx) {
|
|
1398
|
-
const att = rec(v);
|
|
1399
|
-
if (!att)
|
|
1400
|
-
return "";
|
|
1401
|
-
const parts = [];
|
|
1402
|
-
const author = str2(att.author_name);
|
|
1403
|
-
if (author) {
|
|
1404
|
-
const icon = safeUrl(str2(att.author_icon));
|
|
1405
|
-
parts.push(`<div class="sm-att-author">${icon ? `<img class="sm-att-author-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(author)}</div>`);
|
|
1406
|
-
}
|
|
1407
|
-
const title = str2(att.title);
|
|
1408
|
-
if (title) {
|
|
1409
|
-
const link = safeUrl(str2(att.title_link));
|
|
1410
|
-
const inner = plainTextToHtml(title);
|
|
1411
|
-
parts.push(`<div class="sm-att-title">${link ? `<a class="sm-link" href="${link}" target="_blank" rel="noopener">${inner}</a>` : inner}</div>`);
|
|
1412
|
-
}
|
|
1413
|
-
const text = str2(att.text) || (parts.length ? "" : str2(att.fallback));
|
|
1414
|
-
if (text)
|
|
1415
|
-
parts.push(`<div class="sm-att-text">${mrkdwnToHtml(text, ctx)}</div>`);
|
|
1416
|
-
parts.push(attachmentFields(arr(att.fields), ctx));
|
|
1417
|
-
const img = safeUrl(str2(att.image_url));
|
|
1418
|
-
if (img)
|
|
1419
|
-
parts.push(`<img class="sm-att-img" src="${img}" alt="">`);
|
|
1420
|
-
const thumb = !img ? safeUrl(str2(att.thumb_url)) : undefined;
|
|
1421
|
-
if (thumb)
|
|
1422
|
-
parts.push(`<img class="sm-att-thumb" src="${thumb}" alt="">`);
|
|
1423
|
-
const blocks = arr(att.blocks);
|
|
1424
|
-
if (blocks.length)
|
|
1425
|
-
parts.push(renderBlocks(blocks, ctx));
|
|
1426
|
-
const footer = str2(att.footer);
|
|
1427
|
-
const ts = att.ts ? formatAttachmentTs(att.ts) : "";
|
|
1428
|
-
if (footer || ts) {
|
|
1429
|
-
const icon = safeUrl(str2(att.footer_icon));
|
|
1430
|
-
parts.push(`<div class="sm-att-footer">${icon ? `<img class="sm-att-footer-icon" src="${icon}" alt="">` : ""}${plainTextToHtml(footer)}${footer && ts ? " | " : ""}${escapeHtml(ts)}</div>`);
|
|
1431
|
-
}
|
|
1432
|
-
const pretext = str2(att.pretext);
|
|
1433
|
-
const box = `<div class="sm-att" style="border-left-color:${barColor(att.color)}">${parts.join("")}</div>`;
|
|
1434
|
-
return pretext ? `<div class="sm-att-pretext">${mrkdwnToHtml(pretext, ctx)}</div>${box}` : box;
|
|
1435
|
-
}
|
|
1436
|
-
function formatAttachmentTs(v) {
|
|
1437
|
-
const secs = Number(v);
|
|
1438
|
-
if (!Number.isFinite(secs) || secs <= 0)
|
|
1439
|
-
return str2(v);
|
|
1440
|
-
const d = new Date(secs * 1000);
|
|
1441
|
-
const pad = (n) => String(n).padStart(2, "0");
|
|
1442
|
-
return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
1443
|
-
}
|
|
1444
|
-
function renderAttachments(attachments, ctx) {
|
|
1445
|
-
const list = arr(attachments);
|
|
1446
|
-
if (!list.length)
|
|
1447
|
-
return "";
|
|
1448
|
-
return `<div class="sm-atts">${list.map((a) => attachment(a, ctx)).join("")}</div>`;
|
|
602
|
+
function renderAttachments(attachments, ctx) {
|
|
603
|
+
const list = arr(attachments);
|
|
604
|
+
if (!list.length)
|
|
605
|
+
return "";
|
|
606
|
+
return `<div class="sm-atts">${list.map((a) => attachment(a, ctx)).join("")}</div>`;
|
|
1449
607
|
}
|
|
1450
608
|
|
|
1451
609
|
// src/render/index.ts
|
|
@@ -1702,646 +860,1491 @@ a:hover{text-decoration:underline}
|
|
|
1702
860
|
.sm-replies{flex-wrap:wrap}
|
|
1703
861
|
.sm-att-img,.sm-file-img,.sm-video-thumb,.sm-file-card{max-width:100%}
|
|
1704
862
|
}
|
|
1705
|
-
@media (max-width:699px),(hover:none),(pointer:coarse){
|
|
1706
|
-
.sm-message-actions{position:static;opacity:1;justify-content:flex-start;gap:6px;margin-top:6px;padding:0;border:0;box-shadow:none;background:none}
|
|
1707
|
-
.sm-message-actions button,.sm-message-actions a{width:40px;min-height:40px;background:none}
|
|
863
|
+
@media (max-width:699px),(hover:none),(pointer:coarse){
|
|
864
|
+
.sm-message-actions{position:static;opacity:1;justify-content:flex-start;gap:6px;margin-top:6px;padding:0;border:0;box-shadow:none;background:none}
|
|
865
|
+
.sm-message-actions button,.sm-message-actions a{width:40px;min-height:40px;background:none}
|
|
866
|
+
}
|
|
867
|
+
@media (max-width:699px){
|
|
868
|
+
html,body{overflow:hidden;overscroll-behavior:none}
|
|
869
|
+
body{overflow-wrap:anywhere}
|
|
870
|
+
.sm-app{position:fixed;top:var(--sm-viewport-top,0px);left:0;width:100%;height:100dvh;height:var(--sm-viewport-height,100dvh);flex-direction:column}
|
|
871
|
+
.sm-side,.sm-resize{display:none}
|
|
872
|
+
.sm-mobile-bar{display:block;flex:none;background:#3f0e40;color:#fff;padding:calc(8px + env(safe-area-inset-top)) 12px 8px;z-index:10}
|
|
873
|
+
.sm-mobile-heading{display:flex;align-items:center;justify-content:space-between;gap:12px;min-width:0}
|
|
874
|
+
.sm-mobile-workspace{display:flex;align-items:center;min-height:40px;min-width:0;color:#fff;font-size:17px;font-weight:900}
|
|
875
|
+
.sm-mobile-bar .sm-identity{flex:none;max-width:55%;color:#1d1c1d}
|
|
876
|
+
.sm-identity summary{min-height:40px;background:#fff;border-radius:20px}
|
|
877
|
+
.sm-identity-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
878
|
+
.sm-identity-list{width:280px;max-width:calc(100vw - 24px);max-height:calc(var(--sm-viewport-height,100dvh) - 120px);overflow-y:auto}
|
|
879
|
+
.sm-identity-list button{min-height:40px}
|
|
880
|
+
.sm-identity-list button>span{min-width:0;overflow-wrap:anywhere}
|
|
881
|
+
.sm-identity-user{white-space:normal}
|
|
882
|
+
.sm-channel-switch{display:block;width:100%;min-height:40px;margin-top:6px;padding:6px 10px;border:1px solid #79547a;border-radius:5px;background:#522653;color:#fff;font:inherit;font-size:16px}
|
|
883
|
+
.sm-main{min-height:0}
|
|
884
|
+
.sm-app-thread .sm-main{display:none}
|
|
885
|
+
.sm-panel{width:100%;min-width:0;min-height:0;flex:1;border-left:0}
|
|
886
|
+
.sm-top{padding:6px 12px;align-items:center;gap:10px;flex-wrap:wrap}
|
|
887
|
+
.sm-top h1{font-size:16px;min-width:0}
|
|
888
|
+
.sm-top .sm-sub,.sm-top .sm-identity,.sm-live{display:none}
|
|
889
|
+
.sm-panel-top{padding:6px 12px;align-items:center}
|
|
890
|
+
.sm-panel-back{display:inline-flex;align-items:center;justify-content:center;width:40px;min-height:40px;padding:0;border:0;font-size:22px}
|
|
891
|
+
.sm-panel-close,.sm-panel-expand{display:none}
|
|
892
|
+
.sm-main .sm-page,.sm-panel-body{min-height:0;padding:8px 12px 20px;overscroll-behavior:contain}
|
|
893
|
+
.sm-msg{padding:8px 0;gap:10px}
|
|
894
|
+
.sm-msg-body,.sm-card-v,.sm-field,.sm-att,.sm-context,.sm-file-card{overflow-wrap:anywhere}
|
|
895
|
+
.sm-text,.sm-blocks,.sm-field,.sm-att-text,.sm-att-pretext{font-size:16px;line-height:24px}
|
|
896
|
+
.sm-name{font-size:14px}
|
|
897
|
+
.sm-time{font-size:12px}
|
|
898
|
+
.sm-composer,.sm-panel .sm-composer{padding:8px 12px calc(10px + env(safe-area-inset-bottom))}
|
|
899
|
+
.sm-composer-text{font-size:16px;line-height:22px;resize:none;max-height:30vh}
|
|
900
|
+
.sm-composer-lock input{font-size:16px;min-height:40px;flex:1;min-width:0}
|
|
901
|
+
.sm-composer-lock>span:first-child{width:100%}
|
|
902
|
+
.sm-mentions,.sm-panel .sm-mentions{left:12px;right:12px;max-height:min(260px,40vh)}
|
|
903
|
+
.sm-mention{min-height:40px}
|
|
904
|
+
.sm-btn,.sm-select,.sm-reaction,.sm-prompt,.sm-replies,.sm-back,.sm-icon-btn,.sm-composer-send,.sm-composer-lock .sm-lock-go,.sm-lock-out{min-height:40px;display:inline-flex;align-items:center}
|
|
905
|
+
.sm-btn,.sm-select{max-width:100%;white-space:normal}
|
|
906
|
+
.sm-replies{flex-wrap:wrap;gap:4px 8px}
|
|
907
|
+
.sm-section-split{flex-wrap:wrap}
|
|
908
|
+
.sm-section-main{flex-basis:100%}
|
|
909
|
+
.sm-section-acc,.sm-context>*{min-width:0;max-width:100%}
|
|
910
|
+
.sm-att-img,.sm-file-img,.sm-video-thumb{max-width:100%;height:auto}
|
|
911
|
+
.sm-file-card,.sm-unsupported{max-width:100%}
|
|
912
|
+
.sm-file-card>div:last-child{min-width:0}
|
|
913
|
+
.sm-cards{gap:10px}
|
|
914
|
+
.sm-card{flex:1 1 calc(50% - 5px);min-width:0}
|
|
915
|
+
.sm-table,.sm-table tbody{display:block;width:100%}
|
|
916
|
+
.sm-table tr{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px 14px;padding:12px 0;border-bottom:1px solid #e8e8e8}
|
|
917
|
+
.sm-table tr:first-child{display:none}
|
|
918
|
+
.sm-table td{display:block;padding:0;border:0;overflow-wrap:anywhere}
|
|
919
|
+
.sm-table td:before{content:attr(data-label);display:block;font-size:11px;color:#616061;text-transform:uppercase;letter-spacing:.4px}
|
|
920
|
+
.sm-table td a{display:inline-flex;align-items:center;min-height:40px}
|
|
921
|
+
.sm-shot{width:100%;padding:12px}
|
|
922
|
+
}
|
|
923
|
+
`;
|
|
924
|
+
function ctxOf(store) {
|
|
925
|
+
return { users: store.users, channels: store.channels, botUserId: store.bot.userId };
|
|
926
|
+
}
|
|
927
|
+
function panelWidth(opts) {
|
|
928
|
+
const raw = (opts.panelWidth ?? "").trim();
|
|
929
|
+
return /^\d{1,4}(%|px|vw|rem)$/.test(raw) ? raw : DEFAULT_PANEL_WIDTH;
|
|
930
|
+
}
|
|
931
|
+
function panelParam(opts) {
|
|
932
|
+
const width = (opts.panelWidth ?? "").trim();
|
|
933
|
+
if (!/^\d{1,4}(%|px|vw|rem)$/.test(width) || width === DEFAULT_PANEL_WIDTH)
|
|
934
|
+
return "";
|
|
935
|
+
return width.endsWith("%") ? width.slice(0, -1) : width;
|
|
936
|
+
}
|
|
937
|
+
function query(opts, over = {}) {
|
|
938
|
+
if (opts.screenshot)
|
|
939
|
+
return "?screenshot";
|
|
940
|
+
const parts = [];
|
|
941
|
+
if (opts.refreshSec)
|
|
942
|
+
parts.push(`refresh=${opts.refreshSec}`);
|
|
943
|
+
const panel = panelParam(opts);
|
|
944
|
+
if (panel)
|
|
945
|
+
parts.push(`panel=${panel}`);
|
|
946
|
+
if (over.full ?? opts.threadView === "full")
|
|
947
|
+
parts.push("full");
|
|
948
|
+
return parts.length ? `?${parts.join("&")}` : "";
|
|
949
|
+
}
|
|
950
|
+
function channelHref(channelId, opts) {
|
|
951
|
+
return `/c/${encodeURIComponent(channelId)}${query(opts, { full: false })}`;
|
|
952
|
+
}
|
|
953
|
+
function homeHref(opts) {
|
|
954
|
+
return `/${query(opts, { full: false })}`;
|
|
955
|
+
}
|
|
956
|
+
function threadHref(channelId, ts, opts, over = {}) {
|
|
957
|
+
return `/c/${encodeURIComponent(channelId)}/t/${encodeURIComponent(ts)}${query(opts, over)}`;
|
|
958
|
+
}
|
|
959
|
+
function pad(n) {
|
|
960
|
+
return String(n).padStart(2, "0");
|
|
961
|
+
}
|
|
962
|
+
function timeOf(ts) {
|
|
963
|
+
const d = new Date(Number(ts) * 1000);
|
|
964
|
+
if (Number.isNaN(d.getTime()))
|
|
965
|
+
return "";
|
|
966
|
+
return `${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
967
|
+
}
|
|
968
|
+
function dayKey(d) {
|
|
969
|
+
return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`;
|
|
970
|
+
}
|
|
971
|
+
function dayLabel(ts) {
|
|
972
|
+
const d = new Date(Number(ts) * 1000);
|
|
973
|
+
if (Number.isNaN(d.getTime()))
|
|
974
|
+
return "";
|
|
975
|
+
const now = new Date;
|
|
976
|
+
if (dayKey(d) === dayKey(now))
|
|
977
|
+
return "Today";
|
|
978
|
+
const yesterday = new Date(now.getTime() - 86400000);
|
|
979
|
+
if (dayKey(d) === dayKey(yesterday))
|
|
980
|
+
return "Yesterday";
|
|
981
|
+
return `${MONTHS[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
|
|
982
|
+
}
|
|
983
|
+
function fileSize(bytes) {
|
|
984
|
+
if (bytes < 1024)
|
|
985
|
+
return `${bytes} B`;
|
|
986
|
+
if (bytes < 1024 * 1024)
|
|
987
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
988
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
989
|
+
}
|
|
990
|
+
function hashIndex(s, buckets) {
|
|
991
|
+
let h = 0;
|
|
992
|
+
for (let i = 0;i < s.length; i++)
|
|
993
|
+
h = h * 31 + s.charCodeAt(i) >>> 0;
|
|
994
|
+
return h % buckets;
|
|
995
|
+
}
|
|
996
|
+
function initials(name) {
|
|
997
|
+
const words = name.replace(/[^\p{L}\p{N} ]/gu, " ").trim().split(/\s+/);
|
|
998
|
+
const letters = words.slice(0, 2).map((w) => w[0] ?? "").join("");
|
|
999
|
+
return (letters || name.slice(0, 2) || "?").toUpperCase();
|
|
1000
|
+
}
|
|
1001
|
+
function channelLabel(store, c) {
|
|
1002
|
+
if (!c.is_im)
|
|
1003
|
+
return `#${c.name}`;
|
|
1004
|
+
const peer = c.user ? store.users.get(c.user) : undefined;
|
|
1005
|
+
return peer ? displayName(peer) : c.name;
|
|
1006
|
+
}
|
|
1007
|
+
function channelIcon(c) {
|
|
1008
|
+
if (c.is_im)
|
|
1009
|
+
return "@";
|
|
1010
|
+
if (c.is_private)
|
|
1011
|
+
return "\uD83D\uDD12";
|
|
1012
|
+
return "#";
|
|
1013
|
+
}
|
|
1014
|
+
function channelKind(c) {
|
|
1015
|
+
if (c.is_im)
|
|
1016
|
+
return "DM";
|
|
1017
|
+
if (c.is_private)
|
|
1018
|
+
return "private";
|
|
1019
|
+
return "public";
|
|
1020
|
+
}
|
|
1021
|
+
function authorName(store, m) {
|
|
1022
|
+
if (m.username)
|
|
1023
|
+
return m.username;
|
|
1024
|
+
const user = m.user ? store.users.get(m.user) : undefined;
|
|
1025
|
+
if (user)
|
|
1026
|
+
return user.real_name || user.name;
|
|
1027
|
+
return m.bot_profile?.name ?? m.user ?? "unknown";
|
|
1028
|
+
}
|
|
1029
|
+
function avatarHtml(m, name) {
|
|
1030
|
+
const emojiName = m.icons?.emoji;
|
|
1031
|
+
if (emojiName) {
|
|
1032
|
+
const char = emojiChar(emojiName) ?? emojiName.replace(/:/g, "");
|
|
1033
|
+
return `<div class="sm-avatar sm-avatar-emoji">${escapeHtml(char)}</div>`;
|
|
1034
|
+
}
|
|
1035
|
+
const imageUrl = m.icons?.image_72 ?? m.icons?.image_48 ?? m.icons?.image_64;
|
|
1036
|
+
if (imageUrl && /^(https?:\/\/|\/)/.test(imageUrl)) {
|
|
1037
|
+
return `<div class="sm-avatar"><img src="${escapeHtml(imageUrl)}" alt=""></div>`;
|
|
1038
|
+
}
|
|
1039
|
+
const seed = m.user ?? m.bot_id ?? name;
|
|
1040
|
+
const color = AVATAR_COLORS[hashIndex(seed, AVATAR_COLORS.length)];
|
|
1041
|
+
return `<div class="sm-avatar" style="background:${color}">${escapeHtml(initials(name))}</div>`;
|
|
1042
|
+
}
|
|
1043
|
+
function reactionsHtml(store, m, interactive, locked) {
|
|
1044
|
+
if (!m.reactions?.length)
|
|
1045
|
+
return "";
|
|
1046
|
+
const chips = m.reactions.map((r) => {
|
|
1047
|
+
const who = r.users.map((id) => {
|
|
1048
|
+
const user = store.users.get(id);
|
|
1049
|
+
return user ? displayName(user) : id;
|
|
1050
|
+
}).join(", ");
|
|
1051
|
+
const char = emojiChar(r.name) ?? `:${r.name}:`;
|
|
1052
|
+
const content = `${escapeHtml(char)}<span class="sm-reaction-count">${r.count}</span>`;
|
|
1053
|
+
return interactive ? `<button type="button" class="sm-reaction" title="${escapeHtml(who)}" data-reaction="${escapeHtml(r.name)}" data-users="${escapeHtml(JSON.stringify(r.users))}" aria-label="Add :${escapeHtml(r.name)}: reaction, ${r.count} ${r.count === 1 ? "person" : "people"} reacted" aria-pressed="false"${locked ? " disabled" : ""}>${content}</button>` : `<span class="sm-reaction" title="${escapeHtml(who)}">${content}</span>`;
|
|
1054
|
+
});
|
|
1055
|
+
return `<div class="sm-reactions">${chips.join("")}</div>`;
|
|
1056
|
+
}
|
|
1057
|
+
function messageActions(m, opts) {
|
|
1058
|
+
const thread = m.thread_ts ?? m.ts;
|
|
1059
|
+
const reactIcon = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" aria-hidden="true"><circle cx="11" cy="12" r="8"/><path d="M7 14c2 3 6 3 8 0M8 9h.01M14 9h.01M20 2v6M17 5h6" stroke-linecap="round"/></svg>`;
|
|
1060
|
+
const replyIcon = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" aria-hidden="true"><path d="M5 4h14a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H9l-6 4V6a2 2 0 0 1 2-2Z" stroke-linejoin="round"/><path d="M7 9h10M7 13h6" stroke-linecap="round"/></svg>`;
|
|
1061
|
+
return `<div class="sm-message-actions"><button type="button" class="sm-add-reaction" aria-label="Add reaction" aria-haspopup="dialog" aria-controls="sm-reaction-picker" title="Add reaction"${opts.writeGated ? " disabled" : ""}>${reactIcon}</button><a class="sm-reply-action" href="${threadHref(m.channel, thread, opts)}" data-thread="${escapeHtml(thread)}" aria-label="Open thread" title="Open thread">${replyIcon}</a></div><div class="sm-action-error" role="alert" hidden></div>`;
|
|
1062
|
+
}
|
|
1063
|
+
function reactionPicker() {
|
|
1064
|
+
const names = [
|
|
1065
|
+
"thumbsup",
|
|
1066
|
+
"green_heart",
|
|
1067
|
+
"eyes",
|
|
1068
|
+
"white_check_mark",
|
|
1069
|
+
"tada",
|
|
1070
|
+
"rocket",
|
|
1071
|
+
"star",
|
|
1072
|
+
"thinking_face",
|
|
1073
|
+
"sparkles",
|
|
1074
|
+
"fire",
|
|
1075
|
+
"raised_hands",
|
|
1076
|
+
"clap"
|
|
1077
|
+
];
|
|
1078
|
+
const buttons = names.map((name) => `<button type="button" data-reaction="${name}" aria-label="React with ${name.replaceAll("_", " ")}" title=":${name}:">${escapeHtml(emojiChar(name) ?? `:${name}:`)}</button>`).join("");
|
|
1079
|
+
return `<dialog class="sm-reaction-picker" id="sm-reaction-picker" aria-labelledby="sm-reaction-title"><div class="sm-reaction-picker-head"><h2 id="sm-reaction-title">Add a reaction</h2><button type="button" class="sm-reaction-close" aria-label="Close reactions">\u2715</button></div><div class="sm-reaction-choices">${buttons}</div><div class="sm-action-error" role="alert" hidden></div></dialog>`;
|
|
1080
|
+
}
|
|
1081
|
+
function fileUrl(f, token) {
|
|
1082
|
+
if (!f.url_private)
|
|
1083
|
+
return "";
|
|
1084
|
+
return escapeHtml(`${f.url_private}${f.url_private.includes("?") ? "&" : "?"}t=${token}`);
|
|
1085
|
+
}
|
|
1086
|
+
function filesHtml(files, token) {
|
|
1087
|
+
if (!files?.length)
|
|
1088
|
+
return "";
|
|
1089
|
+
const cards = files.map((f) => {
|
|
1090
|
+
if (f.mimetype?.startsWith("image/") && f.url_private) {
|
|
1091
|
+
return `<img class="sm-file-img" src="${fileUrl(f, token)}" alt="${escapeHtml(f.title || f.name)}">`;
|
|
1092
|
+
}
|
|
1093
|
+
const href = fileUrl(f, token);
|
|
1094
|
+
const name = escapeHtml(f.title || f.name);
|
|
1095
|
+
const meta = `${escapeHtml(f.pretty_type || f.filetype || "File")} \xB7 ${fileSize(f.size ?? 0)}`;
|
|
1096
|
+
const label = href ? `<a href="${href}">${name}</a>` : name;
|
|
1097
|
+
return `<div class="sm-file-card"><div class="sm-file-icon">\uD83D\uDCC4</div><div><div class="sm-file-name">${label}</div><div class="sm-file-meta">${meta}</div></div></div>`;
|
|
1098
|
+
});
|
|
1099
|
+
return `<div class="sm-files">${cards.join("")}</div>`;
|
|
1100
|
+
}
|
|
1101
|
+
function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
1102
|
+
const name = authorName(store, m);
|
|
1103
|
+
const isApp = Boolean(m.bot_id);
|
|
1104
|
+
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
1105
|
+
const interactive = !opts.screenshot && !flags.ephemeral && humanUsers(store).length > 0;
|
|
1106
|
+
const body = [];
|
|
1107
|
+
if (m.streaming_state !== undefined) {
|
|
1108
|
+
if (m.text)
|
|
1109
|
+
body.push(`<div class="sm-text">${mrkdwnToHtml(markdownToMrkdwn(m.text), ctx)}</div>`);
|
|
1110
|
+
if (blocks)
|
|
1111
|
+
body.push(renderBlocks(blocks, ctx));
|
|
1112
|
+
} else if (blocks)
|
|
1113
|
+
body.push(renderBlocks(blocks, ctx));
|
|
1114
|
+
else if (m.text)
|
|
1115
|
+
body.push(`<div class="sm-text">${mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1116
|
+
if (m.edited)
|
|
1117
|
+
body.push(`<div class="sm-edited">(edited)</div>`);
|
|
1118
|
+
if (m.attachments?.length)
|
|
1119
|
+
body.push(renderAttachments(m.attachments, ctx));
|
|
1120
|
+
body.push(filesHtml(m.files, store.bot.token));
|
|
1121
|
+
body.push(reactionsHtml(store, m, interactive, Boolean(opts.writeGated)));
|
|
1122
|
+
if (flags.ephemeral)
|
|
1123
|
+
body.push(`<div class="sm-eph-note">Only visible to you</div>`);
|
|
1124
|
+
if (flags.threadLink && m.reply_count) {
|
|
1125
|
+
const count = `${m.reply_count} ${m.reply_count === 1 ? "reply" : "replies"}`;
|
|
1126
|
+
const last = m.latest_reply ? `<span class="sm-replies-time">Last reply ${timeOf(m.latest_reply)}</span>` : "";
|
|
1127
|
+
body.push(`<a class="sm-replies" href="${threadHref(m.channel, m.thread_ts ?? m.ts, opts)}">${count}${last}</a>`);
|
|
1128
|
+
}
|
|
1129
|
+
if (interactive)
|
|
1130
|
+
body.push(messageActions(m, opts));
|
|
1131
|
+
const streaming = m.streaming_state === "in_progress" ? `<span class="sm-stream" title="streaming">\u2022\u2022\u2022</span>` : "";
|
|
1132
|
+
const open = flags.openTs && flags.openTs === m.ts ? " sm-msg-open" : "";
|
|
1133
|
+
return `<div class="sm-msg${flags.ephemeral ? " sm-msg-eph" : ""}${open}" data-ts="${escapeHtml(m.ts)}" data-channel="${escapeHtml(m.channel)}">${avatarHtml(m, name)}<div class="sm-msg-body"><div class="sm-msg-head"><span class="sm-name">${escapeHtml(name)}</span>${isApp ? `<span class="sm-badge">APP</span>` : ""}<span class="sm-time">${timeOf(m.ts)}</span>${streaming}</div>${body.join("")}</div></div>`;
|
|
1134
|
+
}
|
|
1135
|
+
function markdownToMrkdwn(text) {
|
|
1136
|
+
return text.split(/(```[\s\S]*?```)/).map((part, i) => i % 2 === 1 ? part : part.replace(/\*\*(.+?)\*\*/g, "*$1*").replace(/__(.+?)__/g, "*$1*").replace(/\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g, "<$2|$1>").replace(/^#{1,6}\s+(.+)$/gm, "*$1*").replace(/^(\s*)[-*]\s+/gm, "$1\u2022 ")).join("");
|
|
1708
1137
|
}
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
.
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
.
|
|
1724
|
-
.sm-
|
|
1725
|
-
.
|
|
1726
|
-
|
|
1727
|
-
.sm-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
.
|
|
1731
|
-
.sm-panel-
|
|
1732
|
-
|
|
1733
|
-
.
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
.sm-
|
|
1738
|
-
.sm-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
.sm-
|
|
1752
|
-
.
|
|
1753
|
-
.
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
.
|
|
1757
|
-
.sm-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1138
|
+
function renderMessageList(store, ctx, messages, opts, flags = {}) {
|
|
1139
|
+
const out = [];
|
|
1140
|
+
let day = "";
|
|
1141
|
+
for (const m of messages) {
|
|
1142
|
+
const label = flags.dividers === false ? "" : dayLabel(m.ts);
|
|
1143
|
+
if (label && label !== day) {
|
|
1144
|
+
day = label;
|
|
1145
|
+
out.push(`<div class="sm-daydiv"><span>${escapeHtml(label)}</span></div>`);
|
|
1146
|
+
}
|
|
1147
|
+
out.push(renderMessage(store, ctx, m, opts, { ...flags, ephemeral: m._ephemeral === true }));
|
|
1148
|
+
}
|
|
1149
|
+
return out.join("");
|
|
1150
|
+
}
|
|
1151
|
+
function sidebar(store, opts, active) {
|
|
1152
|
+
const channels = [...store.channels.values()].sort((a, b) => channelLabel(store, a).localeCompare(channelLabel(store, b)));
|
|
1153
|
+
const links = channels.map((c) => `<a class="${c.id === active ? "sm-active" : ""}" href="${channelHref(c.id, opts)}">${escapeHtml(channelIcon(c))} ${escapeHtml(c.is_im ? channelLabel(store, c) : c.name)}</a>`).join("");
|
|
1154
|
+
const options = channels.map((c) => `<option value="${escapeHtml(channelHref(c.id, opts))}"${c.id === active ? " selected" : ""}>${escapeHtml(channelLabel(store, c))}</option>`).join("");
|
|
1155
|
+
const mobile = `<div class="sm-mobile-bar"><div class="sm-mobile-heading"><a class="sm-mobile-workspace" href="${homeHref(opts)}">${escapeHtml(store.team.name)}</a>${identityChip(store, opts)}</div><select class="sm-channel-switch" aria-label="Channel"><option value="${homeHref(opts)}"${active ? "" : " selected"}>Workspace</option>${options}</select></div>`;
|
|
1156
|
+
return `${mobile}<nav class="sm-side"><div class="sm-side-title"><a href="${homeHref(opts)}" style="color:#fff">${escapeHtml(store.team.name)}</a><span class="sm-side-team">${escapeHtml(store.team.id)} \xB7 slack-mock</span></div><div class="sm-side-h">Channels</div>${links}</nav>`;
|
|
1157
|
+
}
|
|
1158
|
+
function shell(store, opts, parts) {
|
|
1159
|
+
const refresh = opts.refreshSec && !opts.screenshot ? `<meta http-equiv="refresh" content="${Math.max(1, Math.floor(opts.refreshSec))}">` : "";
|
|
1160
|
+
const style = opts.screenshot ? "" : ` style="--sm-panel-w:${panelWidth(opts)}"`;
|
|
1161
|
+
const head = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"><title>${escapeHtml(parts.title)}</title>${refresh}<style>${CSS}</style></head>`;
|
|
1162
|
+
if (opts.screenshot) {
|
|
1163
|
+
return `${head}<body><div class="sm-shot">${parts.header}<div class="sm-page">${parts.main}</div></div></body></html>`;
|
|
1164
|
+
}
|
|
1165
|
+
const app = `sm-app${parts.panel ? " sm-app-thread" : ""}`;
|
|
1166
|
+
const handle = parts.panel ? `<div class="sm-resize" title="Drag to resize, double-click to reset"></div>` : "";
|
|
1167
|
+
return `${head}<body${style}><div class="${app}">${sidebar(store, opts, parts.active)}<div class="sm-main">${parts.header}<div class="sm-page sm-scroll">${parts.main}</div>${parts.footer ?? ""}</div>${handle}${parts.panel ?? ""}</div>${parts.script ?? ""}</body></html>`;
|
|
1168
|
+
}
|
|
1169
|
+
function topbar(title, sub, back, actions) {
|
|
1170
|
+
const backLink = back ? `<a class="sm-back" href="${back.href}">${escapeHtml(back.label)}</a>` : "";
|
|
1171
|
+
const right = actions ? `<div class="sm-top-actions">${actions}</div>` : "";
|
|
1172
|
+
return `<div class="sm-top">${backLink}<h1>${title}</h1>${sub ? `<div class="sm-sub">${sub}</div>` : ""}${right}</div>`;
|
|
1173
|
+
}
|
|
1174
|
+
function indexView(store, opts) {
|
|
1175
|
+
const cards = [
|
|
1176
|
+
["Workspace", escapeHtml(store.team.name)],
|
|
1177
|
+
["Team id", escapeHtml(store.team.id)],
|
|
1178
|
+
["Bot user", escapeHtml(`${store.app.name} (${store.bot.userId})`)],
|
|
1179
|
+
["Connections", String(opts.connections ?? 0)]
|
|
1180
|
+
].map(([k, v]) => `<div class="sm-card"><div class="sm-card-k">${k}</div><div class="sm-card-v">${v}</div></div>`).join("");
|
|
1181
|
+
const channels = [...store.channels.values()];
|
|
1182
|
+
const channelRows = channels.map((c) => {
|
|
1183
|
+
const count = store.messages.get(c.id)?.length ?? 0;
|
|
1184
|
+
return `<tr><td data-label="Name"><a href="${channelHref(c.id, opts)}">${escapeHtml(channelIcon(c))} ${escapeHtml(c.is_im ? channelLabel(store, c) : c.name)}</a></td><td data-label="Type"><span class="sm-tag">${channelKind(c)}</span></td><td class="sm-muted" data-label="Id">${c.id}</td><td data-label="Messages">${count}</td></tr>`;
|
|
1185
|
+
}).join("");
|
|
1186
|
+
const userRows = [...store.users.values()].map((u) => `<tr><td data-label="Name">${escapeHtml(u.name)}${userStatus(u)}</td><td data-label="Real name">${escapeHtml(u.real_name)}</td><td class="sm-muted" data-label="Title">${escapeHtml(u.profile.title ?? "")}</td><td class="sm-muted" data-label="Email">${escapeHtml(u.profile.email ?? "")}</td><td class="sm-muted" data-label="Id">${u.id}</td><td data-label="Type">${u.is_bot ? `<span class="sm-tag">bot</span>` : ""}</td></tr>`).join("");
|
|
1187
|
+
const main = `<div class="sm-cards">${cards}</div>
|
|
1188
|
+
<div class="sm-h2">Channels</div>
|
|
1189
|
+
${channels.length ? `<table class="sm-table"><tr><th>Name</th><th>Type</th><th>Id</th><th>Messages</th></tr>${channelRows}</table>` : `<div class="sm-empty">No channels yet</div>`}
|
|
1190
|
+
<div class="sm-h2">Users</div>
|
|
1191
|
+
<table class="sm-table"><tr><th>Name</th><th>Real name</th><th>Title</th><th>Email</th><th>Id</th><th></th></tr>${userRows}</table>`;
|
|
1192
|
+
return shell(store, opts, {
|
|
1193
|
+
title: `${store.team.name} \xB7 slack-mock`,
|
|
1194
|
+
header: topbar(escapeHtml(store.team.name), "slack-mock workspace", undefined, identityChip(store, opts)),
|
|
1195
|
+
main,
|
|
1196
|
+
script: opts.screenshot ? "" : uiScript(store, opts, undefined, false)
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
function channelMessages(store, channel) {
|
|
1200
|
+
const history = store.history(channel.id, { limit: 1000 }).items;
|
|
1201
|
+
const ephemerals = store.ephemerals.filter((e) => e.channel === channel.id).map((e) => ({ ...e, _ephemeral: true }));
|
|
1202
|
+
return [...history, ...ephemerals].sort((a, b) => Number(a.ts) - Number(b.ts));
|
|
1203
|
+
}
|
|
1204
|
+
function channelColumn(store, ctx, opts, channel, openTs) {
|
|
1205
|
+
const messages = channelMessages(store, channel);
|
|
1206
|
+
if (!messages.length)
|
|
1207
|
+
return `<div class="sm-empty">No messages yet</div>`;
|
|
1208
|
+
return renderMessageList(store, ctx, messages, opts, {
|
|
1209
|
+
threadLink: true,
|
|
1210
|
+
dividers: true,
|
|
1211
|
+
openTs
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1214
|
+
function channelHeader(store, opts, channel) {
|
|
1215
|
+
const sub = [channel.topic.value, channel.purpose.value].filter(Boolean).join(" \xB7 ");
|
|
1216
|
+
const back = opts.screenshot ? undefined : { href: homeHref(opts), label: "\u2190 Workspace" };
|
|
1217
|
+
return topbar(escapeHtml(channelLabel(store, channel)), escapeHtml(sub), back, identityChip(store, opts));
|
|
1218
|
+
}
|
|
1219
|
+
function humanUsers(store) {
|
|
1220
|
+
return [...store.users.values()].filter((u) => !u.is_bot && !u.deleted);
|
|
1221
|
+
}
|
|
1222
|
+
function userStatus(user) {
|
|
1223
|
+
return user.profile.status_text ? `<span class="sm-user-status">${escapeHtml(user.profile.status_text)}</span>` : "";
|
|
1224
|
+
}
|
|
1225
|
+
function identityChip(store, opts) {
|
|
1226
|
+
if (opts.screenshot)
|
|
1227
|
+
return "";
|
|
1228
|
+
const users = humanUsers(store);
|
|
1229
|
+
const selected = users[0];
|
|
1230
|
+
if (!selected)
|
|
1231
|
+
return "";
|
|
1232
|
+
const name = selected.real_name || selected.name;
|
|
1233
|
+
const avatar = `<span class="sm-identity-avatar" style="background:${AVATAR_COLORS[hashIndex(selected.id, AVATAR_COLORS.length)]}">${escapeHtml(initials(name))}</span>`;
|
|
1234
|
+
const items = users.map((u) => {
|
|
1235
|
+
const real = u.real_name || u.name;
|
|
1236
|
+
const color = AVATAR_COLORS[hashIndex(u.id, AVATAR_COLORS.length)];
|
|
1237
|
+
return `<li><button type="button" data-user="${escapeHtml(u.id)}"><span class="sm-identity-avatar" style="background:${color}">${escapeHtml(initials(real))}</span><span>${escapeHtml(real)}${userStatus(u)}</span><span class="sm-identity-user">@${escapeHtml(u.name)}</span></button></li>`;
|
|
1238
|
+
}).join("");
|
|
1239
|
+
const hidden = opts.writeGated ? " hidden" : "";
|
|
1240
|
+
return `<details class="sm-identity"${hidden}><summary title="Change posting identity" aria-label="Posting as ${escapeHtml(name)}">${avatar}<span class="sm-identity-name">${escapeHtml(name)}</span></summary><ul class="sm-identity-list">${items}</ul></details>`;
|
|
1241
|
+
}
|
|
1242
|
+
function composer(store, opts, channel, threadTs) {
|
|
1243
|
+
if (opts.screenshot)
|
|
1244
|
+
return "";
|
|
1245
|
+
const users = humanUsers(store);
|
|
1246
|
+
if (!users.length)
|
|
1247
|
+
return "";
|
|
1248
|
+
const placeholder = threadTs ? "Reply in thread" : `Message ${channelLabel(store, channel)}`;
|
|
1249
|
+
const thread = threadTs ? ` data-thread="${escapeHtml(threadTs)}"` : "";
|
|
1250
|
+
const gated = opts.writeGated ? ' data-gated="1"' : "";
|
|
1251
|
+
const lock = opts.writeGated ? `<div class="sm-composer-lock" hidden><span>Reading is open. Sign in to post.</span><input class="sm-lock-user" placeholder="user" autocomplete="username"><input class="sm-lock-pass" type="password" placeholder="password" autocomplete="current-password"><button type="button" class="sm-lock-go">Sign in</button><span class="sm-err"></span></div>` : "";
|
|
1252
|
+
return `<div class="sm-composer" data-channel="${escapeHtml(channel.id)}"${thread}${gated}><div class="sm-mentions"></div><div class="sm-composer-box"><textarea class="sm-composer-text" rows="2" placeholder="${escapeHtml(placeholder)}" data-placeholder="${escapeHtml(placeholder)}"></textarea><div class="sm-composer-row"><button type="button" class="sm-composer-send">Send</button></div></div>${lock}<div class="sm-composer-hint">Enter to send, Shift+Enter for a new line, @name to mention<span class="sm-lock-state"></span></div></div>`;
|
|
1253
|
+
}
|
|
1254
|
+
function mentionData(store) {
|
|
1255
|
+
const users = [...store.users.values()].filter((u) => !u.deleted).map((u) => ({
|
|
1256
|
+
id: u.id,
|
|
1257
|
+
name: u.name,
|
|
1258
|
+
real: u.real_name || u.name,
|
|
1259
|
+
initials: initials(u.real_name || u.name),
|
|
1260
|
+
color: AVATAR_COLORS[hashIndex(u.id, AVATAR_COLORS.length)],
|
|
1261
|
+
bot: u.is_bot
|
|
1262
|
+
}));
|
|
1263
|
+
const json = JSON.stringify(users).replace(/</g, "\\u003c");
|
|
1264
|
+
return `<script type="application/json" id="sm-users">${json}</script>`;
|
|
1265
|
+
}
|
|
1266
|
+
function uiScript(store, opts, closeUrl, autoScroll = true) {
|
|
1267
|
+
const esc = closeUrl ? `document.addEventListener("keydown",function(e){if(e.key==="Escape"&&!/^(TEXTAREA|INPUT|SELECT)$/.test(document.activeElement&&document.activeElement.tagName||""))location.href=${JSON.stringify(closeUrl)}});` : "";
|
|
1268
|
+
const body = `(function(){
|
|
1269
|
+
var viewport=window.visualViewport;
|
|
1270
|
+
function fitViewport(){if(viewport&&viewport.scale!==1)return;var style=document.documentElement.style;style.setProperty("--sm-viewport-height",(viewport?viewport.height:innerHeight)+"px");style.setProperty("--sm-viewport-top",(viewport?viewport.offsetTop:0)+"px")}
|
|
1271
|
+
fitViewport();addEventListener("resize",fitViewport);
|
|
1272
|
+
if(viewport){viewport.addEventListener("resize",fitViewport);viewport.addEventListener("scroll",fitViewport)}
|
|
1273
|
+
document.addEventListener("change",function(e){if(e.target.classList.contains("sm-channel-switch"))location.href=e.target.value});
|
|
1274
|
+
var W="sm-panel-w",DEF=${JSON.stringify(panelWidth(opts))},FIXED=${opts.panelWidth ? "true" : "false"};
|
|
1275
|
+
${autoScroll ? `function scroll(){document.querySelectorAll(".sm-scroll").forEach(function(el){el.scrollTop=el.scrollHeight})}
|
|
1276
|
+
scroll();addEventListener("load",scroll);` : ""}
|
|
1277
|
+
var handle=document.querySelector(".sm-resize");
|
|
1278
|
+
if(handle){
|
|
1279
|
+
var saved=null;try{saved=localStorage.getItem(W)}catch(e){}
|
|
1280
|
+
if(!FIXED&&saved)document.body.style.setProperty("--sm-panel-w",saved);
|
|
1281
|
+
var dragging=false;
|
|
1282
|
+
handle.addEventListener("mousedown",function(e){dragging=true;handle.classList.add("sm-dragging");e.preventDefault()});
|
|
1283
|
+
document.addEventListener("mousemove",function(e){if(!dragging)return;var w=Math.round(Math.min(Math.max(innerWidth-e.clientX,360),Math.max(innerWidth-320,360)));document.body.style.setProperty("--sm-panel-w",w+"px")});
|
|
1284
|
+
document.addEventListener("mouseup",function(){if(!dragging)return;dragging=false;handle.classList.remove("sm-dragging");try{localStorage.setItem(W,document.body.style.getPropertyValue("--sm-panel-w"))}catch(e){}});
|
|
1285
|
+
handle.addEventListener("dblclick",function(){try{localStorage.removeItem(W)}catch(e){}document.body.style.setProperty("--sm-panel-w",DEF)});
|
|
1286
|
+
}
|
|
1287
|
+
var raw=document.getElementById("sm-users");
|
|
1288
|
+
var USERS=raw?JSON.parse(raw.textContent||"[]"):[],HUMANS=USERS.filter(function(u){return !u.bot});
|
|
1289
|
+
var PEOPLE=USERS.concat([{name:"here",real:"Notify everyone online",initials:"@",color:"#616061"},{name:"channel",real:"Notify everyone in the channel",initials:"@",color:"#616061"}]);
|
|
1290
|
+
function esc(s){return String(s).replace(/[&<>"]/g,function(c){return c==="&"?"&":c==="<"?"<":c===">"?">":"""})}
|
|
1291
|
+
var GATED=${opts.writeGated ? "true" : "false"},LIVE=${opts.live !== false && !opts.refreshSec ? "true" : "false"},CK="sm-presenter",IK="sm-identity",currentIdentity="",identityStorage=true;
|
|
1292
|
+
function cred(){try{return localStorage.getItem(CK)||""}catch(e){return ""}}
|
|
1293
|
+
function setCred(v){try{if(v)localStorage.setItem(CK,v);else localStorage.removeItem(CK)}catch(e){}}
|
|
1294
|
+
function setIdentity(v){currentIdentity=v;try{if(v)localStorage.setItem(IK,v);else localStorage.removeItem(IK)}catch(e){identityStorage=false}}
|
|
1295
|
+
function findIdentity(value,key){for(var i=0;i<HUMANS.length;i++)if(HUMANS[i][key]===value)return HUMANS[i];return null}
|
|
1296
|
+
function identity(){var id=currentIdentity;if(identityStorage)try{id=localStorage.getItem(IK)||currentIdentity}catch(e){identityStorage=false}currentIdentity=id;var u=findIdentity(id,"id");if(!u&&HUMANS.length){u=HUMANS[0];setIdentity(u.id)}return u}
|
|
1297
|
+
(function(){var url=null,name=null;try{url=new URL(location.href);name=url.searchParams.get("as")}catch(e){}if(url&&name!==null){var u=findIdentity(name,"name");if(u)setIdentity(u.id);url.searchParams.delete("as");var search=url.searchParams.toString();history.replaceState(null,"",url.pathname+(search?"?"+search:"")+url.hash)}})();
|
|
1298
|
+
(function(){var m=/[#&]presenter=([^&]+)/.exec(location.hash||"");if(m){setCred(btoa(decodeURIComponent(m[1])));history.replaceState(null,"",location.pathname+location.search)}})();
|
|
1299
|
+
function authHeaders(h){var c=cred();if(c)h.authorization="Basic "+c;return h}
|
|
1300
|
+
var reactionDialog=document.querySelector(".sm-reaction-picker"),reactionTarget=null,reactionScope=null,reactionBusy=false;
|
|
1301
|
+
function closeReactions(restoreFocus){var button=reactionTarget&&reactionTarget.querySelector(".sm-add-reaction");if(reactionDialog&&reactionDialog.open)reactionDialog.close();reactionTarget=null;reactionScope=null;if(restoreFocus&&button&&button.isConnected&&!button.disabled)button.focus({preventScroll:true})}
|
|
1302
|
+
function findMessageElement(scope,ts){var found=null;if(scope)scope.querySelectorAll(".sm-msg[data-ts]").forEach(function(message){if(message.dataset.ts===ts)found=message});return found}
|
|
1303
|
+
function positionReactions(){
|
|
1304
|
+
if(!reactionDialog||!reactionDialog.open||!reactionTarget)return;
|
|
1305
|
+
if(!reactionTarget.isConnected){reactionTarget=findMessageElement(reactionScope,reactionTarget.dataset.ts);if(!reactionTarget){closeReactions();return}}
|
|
1306
|
+
var button=reactionTarget.querySelector(".sm-add-reaction"),anchor=button.getBoundingClientRect(),v=window.visualViewport,left=v?v.offsetLeft:0,top=v?v.offsetTop:0,width=v?v.width:innerWidth,height=v?v.height:innerHeight,scope=reactionScope.getBoundingClientRect(),pad=12,gap=8;
|
|
1307
|
+
if(anchor.bottom<=Math.max(top,scope.top)||anchor.top>=Math.min(top+height,scope.bottom)){closeReactions();return}
|
|
1308
|
+
reactionDialog.style.maxWidth=Math.max(0,width-pad*2)+"px";reactionDialog.style.maxHeight=Math.max(0,height-pad*2)+"px";
|
|
1309
|
+
var rect=reactionDialog.getBoundingClientRect(),x=Math.max(left+pad,Math.min(anchor.right-rect.width,left+width-rect.width-pad)),y=anchor.bottom+gap;
|
|
1310
|
+
if(y+rect.height>top+height-pad)y=anchor.top-rect.height-gap;
|
|
1311
|
+
reactionDialog.style.left=x+"px";reactionDialog.style.top=Math.max(top+pad,Math.min(y,top+height-rect.height-pad))+"px";
|
|
1312
|
+
}
|
|
1313
|
+
document.addEventListener("scroll",function(e){if(!reactionDialog.contains(e.target))positionReactions()},true);
|
|
1314
|
+
addEventListener("resize",positionReactions);
|
|
1315
|
+
if(window.visualViewport){window.visualViewport.addEventListener("resize",positionReactions);window.visualViewport.addEventListener("scroll",positionReactions)}
|
|
1316
|
+
document.addEventListener("pointerdown",function(e){if(reactionDialog.open&&!reactionDialog.contains(e.target)&&!e.target.closest(".sm-add-reaction"))closeReactions()});
|
|
1317
|
+
document.addEventListener("focusin",function(e){if(reactionDialog.open&&!reactionDialog.contains(e.target)&&(!reactionTarget||e.target!==reactionTarget.querySelector(".sm-add-reaction")))closeReactions()});
|
|
1318
|
+
function hasReaction(button,user){return JSON.parse(button.dataset.users||"[]").indexOf(user)!==-1}
|
|
1319
|
+
function reactionUi(root){var u=identity(),locked=(GATED&&!cred())||!u;(root||document).querySelectorAll(".sm-add-reaction,.sm-reaction[data-reaction],.sm-reaction-choices button").forEach(function(button){button.disabled=locked||reactionBusy;if(button.classList.contains("sm-reaction")){var mine=Boolean(u&&hasReaction(button,u.id)),count=button.querySelector(".sm-reaction-count").textContent;button.setAttribute("aria-pressed",String(mine));button.setAttribute("aria-label",(mine?"Remove":"Add")+" :"+button.dataset.reaction+": reaction, "+count+" "+(count==="1"?"person":"people")+" reacted")}});if(locked)closeReactions()}
|
|
1320
|
+
function identityUi(){var u=identity();document.querySelectorAll(".sm-identity").forEach(function(chip){chip.hidden=GATED&&!cred();if(!u)return;var av=chip.querySelector("summary .sm-identity-avatar"),name=chip.querySelector(".sm-identity-name"),summary=chip.querySelector("summary");if(av){av.textContent=u.initials;av.style.backgroundColor=u.color}if(name)name.textContent=u.real;if(summary)summary.setAttribute("aria-label","Posting as "+u.real);chip.querySelectorAll("[data-user]").forEach(function(button){if(button.dataset.user===u.id)button.setAttribute("aria-current","true");else button.removeAttribute("aria-current")})});reactionUi()}
|
|
1321
|
+
function lockUi(){document.querySelectorAll(".sm-composer").forEach(function(box){var locked=GATED&&!cred(),lock=box.querySelector(".sm-composer-lock"),ta=box.querySelector(".sm-composer-text"),btn=box.querySelector(".sm-composer-send"),state=box.querySelector(".sm-lock-state");if(lock)lock.hidden=!locked;ta.disabled=locked;btn.disabled=locked;ta.placeholder=locked?"Sign in to post":(ta.dataset.placeholder||"");if(state)state.innerHTML=GATED&&!locked?' \xB7 signed in \xB7 <a href="#" class="sm-lock-out">Sign out</a>':""});identityUi()}
|
|
1322
|
+
function signIn(box){if(!box)return;var u=box.querySelector(".sm-lock-user").value.trim(),p=box.querySelector(".sm-lock-pass").value,err=box.querySelector(".sm-err");if(!u){err.textContent="Enter the presenter user";return}var c=btoa(u+":"+p);err.textContent="";fetch("/mock/presenter",{headers:{authorization:"Basic "+c}}).then(function(r){if(!r.ok)throw new Error(r.status===401||r.status===403?"Wrong user or password":"Sign-in failed ("+r.status+")");setCred(c);lockUi();box.querySelector(".sm-composer-text").focus()}).catch(function(e){err.textContent=e.message})}
|
|
1323
|
+
document.addEventListener("click",function(e){var pick=e.target.closest(".sm-identity-list button");if(pick){setIdentity(pick.dataset.user);identityUi();pick.closest(".sm-identity").open=false;return}if(!e.target.closest(".sm-identity"))document.querySelectorAll(".sm-identity[open]").forEach(function(chip){chip.open=false});var out=e.target.closest(".sm-lock-out");if(out){e.preventDefault();setCred("");lockUi();return}var go=e.target.closest(".sm-lock-go");if(go)signIn(go.closest(".sm-composer"))});
|
|
1324
|
+
document.addEventListener("keydown",function(e){if(e.key!=="Escape")return;var chip=document.querySelector(".sm-identity[open]");if(!chip)return;e.preventDefault();e.stopImmediatePropagation();chip.open=false;chip.querySelector("summary").focus()});
|
|
1325
|
+
document.addEventListener("keydown",function(e){if(e.key==="Escape"&&reactionDialog&&reactionDialog.open){e.preventDefault();e.stopImmediatePropagation();closeReactions(true)}});
|
|
1326
|
+
document.addEventListener("keydown",function(e){var c=e.target.classList;if(e.key==="Enter"&&c&&(c.contains("sm-lock-pass")||c.contains("sm-lock-user"))){e.preventDefault();signIn(e.target.closest(".sm-composer"))}});
|
|
1327
|
+
addEventListener("storage",function(e){if(e.key===IK){currentIdentity=e.newValue||"";identityUi()}});
|
|
1328
|
+
lockUi();
|
|
1329
|
+
var pending=null;
|
|
1330
|
+
function refresh(){if(pending)return;pending=setTimeout(function(){pending=null;fetch(location.href,{headers:{accept:"text/html"},credentials:"same-origin"}).then(function(r){return r.ok?r.text():Promise.reject(r.status)}).then(apply).catch(function(){reactionUi()})},120)}
|
|
1331
|
+
function apply(html){
|
|
1332
|
+
var doc=new DOMParser().parseFromString(html,"text/html"),opener=document.activeElement,focusedMessage=opener&&opener.classList.contains("sm-add-reaction")?opener.closest(".sm-msg"):null,focusedScope=focusedMessage&&focusedMessage.closest(".sm-page,.sm-panel-body");
|
|
1333
|
+
reactionUi();reactionUi(doc);[".sm-side",".sm-channel-switch",".sm-page",".sm-panel-body"].forEach(function(sel){var cur=document.querySelector(sel),nxt=doc.querySelector(sel);if(!cur||!nxt)return;var seen={};cur.querySelectorAll(".sm-msg[data-ts]").forEach(function(el){el.classList.remove("sm-msg-new","sm-msg-changed");seen[el.dataset.ts]=el.outerHTML});var atBottom=cur.scrollHeight-cur.scrollTop-cur.clientHeight<80;cur.innerHTML=nxt.innerHTML;cur.querySelectorAll(".sm-msg[data-ts]").forEach(function(el){var prev=seen[el.dataset.ts];if(prev===undefined)el.classList.add("sm-msg-new");else if(prev!==el.outerHTML)el.classList.add("sm-msg-changed")});if(atBottom)cur.scrollTop=cur.scrollHeight});identityUi();positionReactions();if(doc.title)document.title=doc.title;
|
|
1334
|
+
if(focusedMessage&&!opener.isConnected){var message=findMessageElement(focusedScope,focusedMessage.dataset.ts);if(message)message.querySelector(".sm-add-reaction").focus({preventScroll:true})}
|
|
1764
1335
|
}
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1336
|
+
if(LIVE&&window.EventSource){
|
|
1337
|
+
var box0=document.querySelector(".sm-composer"),pm=/^\\/c\\/([^/]+)/.exec(location.pathname),chan=box0?box0.dataset.channel:(pm?decodeURIComponent(pm[1]):null);
|
|
1338
|
+
if(chan){
|
|
1339
|
+
var h1=document.querySelector(".sm-top h1"),dot=null;
|
|
1340
|
+
if(h1){dot=document.createElement("span");dot.className="sm-live";dot.textContent="\u25CF live";h1.appendChild(dot)}
|
|
1341
|
+
var es=new EventSource("/c/"+encodeURIComponent(chan)+"/events"),down=false;
|
|
1342
|
+
es.addEventListener("change",refresh);
|
|
1343
|
+
es.onopen=function(){if(dot){dot.textContent="\u25CF live";dot.classList.remove("sm-live-off")}if(down){down=false;refresh()}};
|
|
1344
|
+
es.onerror=function(){down=true;if(dot){dot.textContent="\u25CF reconnecting";dot.classList.add("sm-live-off")}};
|
|
1345
|
+
}
|
|
1768
1346
|
}
|
|
1769
|
-
function
|
|
1770
|
-
|
|
1771
|
-
|
|
1347
|
+
function react(message,name){
|
|
1348
|
+
var u=identity();if(!message||!u||reactionBusy||(GATED&&!cred()))return;
|
|
1349
|
+
var action="add";message.querySelectorAll(".sm-reaction[data-reaction]").forEach(function(button){if(button.dataset.reaction===name&&hasReaction(button,u.id))action="remove"});
|
|
1350
|
+
var err=(reactionDialog&&reactionDialog.open?reactionDialog:message).querySelector(".sm-action-error");if(err){err.hidden=true;err.textContent=""}
|
|
1351
|
+
reactionBusy=true;reactionUi();
|
|
1352
|
+
fetch("/mock/reactions",{method:"POST",headers:authHeaders({"content-type":"application/json"}),body:JSON.stringify({channel:message.dataset.channel,ts:message.dataset.ts,name:name,user:u.id,action:action})}).then(function(r){
|
|
1353
|
+
if(r.status===401||r.status===403){setCred("");lockUi();throw new Error("Sign in to react.")}
|
|
1354
|
+
return r.json().then(function(data){if(!r.ok&&data.error!=="already_reacted"&&data.error!=="no_reaction")throw new Error("Could not save the reaction. Try again.")})
|
|
1355
|
+
}).then(function(){reactionBusy=false;reactionUi();closeReactions(true);refresh()}).catch(function(e){reactionBusy=false;reactionUi();if(err){if(err.closest("dialog")&&!reactionDialog.open)err=message.querySelector(".sm-action-error");err.textContent=e.message==="Sign in to react."?e.message:"Could not save the reaction. Try again.";err.hidden=false;positionReactions()}})
|
|
1772
1356
|
}
|
|
1773
|
-
function
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1357
|
+
document.addEventListener("click",function(e){
|
|
1358
|
+
var add=e.target.closest(".sm-add-reaction");if(add&&!add.disabled){var message=add.closest(".sm-msg");if(reactionDialog.open&&reactionTarget===message){closeReactions(true);return}reactionTarget=message;reactionScope=message.closest(".sm-page,.sm-panel-body");var err=reactionDialog.querySelector(".sm-action-error");err.hidden=true;err.textContent="";if(!reactionDialog.open)reactionDialog.show();positionReactions();return}
|
|
1359
|
+
var choice=e.target.closest("button[data-reaction]");if(choice&&!choice.disabled){react(choice.closest(".sm-reaction-picker")?reactionTarget:choice.closest(".sm-msg"),choice.dataset.reaction);return}
|
|
1360
|
+
if(e.target.closest(".sm-reaction-close")){closeReactions(true);return}
|
|
1361
|
+
var reply=e.target.closest(".sm-reply-action"),box=document.querySelector(".sm-composer[data-thread]");if(reply&&box&&box.dataset.thread===reply.dataset.thread&&box.dataset.channel===reply.closest(".sm-msg").dataset.channel)e.preventDefault()
|
|
1362
|
+
});
|
|
1363
|
+
function send(box){var t=box.querySelector(".sm-composer-text"),text=t.value.trim(),user=identity();if(!text||t.disabled||!user)return;var body={channel:box.dataset.channel,user:user.id,text:text};if(box.dataset.thread)body.thread_ts=box.dataset.thread;t.disabled=true;fetch("/mock/messages",{method:"POST",headers:authHeaders({"content-type":"application/json"}),body:JSON.stringify(body)}).then(function(r){t.disabled=false;if(r.status===401||r.status===403){setCred("");lockUi();var err=box.querySelector(".sm-err");if(err)err.textContent="Not signed in, or the credential changed. Sign in to post.";return}if(!r.ok)throw new Error("send failed "+r.status);t.value="";if(LIVE)refresh();else location.reload()}).catch(function(){t.disabled=false})}
|
|
1364
|
+
document.querySelectorAll(".sm-composer").forEach(function(box){
|
|
1365
|
+
var ta=box.querySelector(".sm-composer-text"),menu=box.querySelector(".sm-mentions"),items=[],active=0;
|
|
1366
|
+
function close(){items=[];menu.style.display="none"}
|
|
1367
|
+
function prefix(){var m=/(?:^|\\s)@(\\w*)$/.exec(ta.value.slice(0,ta.selectionStart));return m?m[1]:null}
|
|
1368
|
+
function paint(){menu.querySelectorAll(".sm-mention").forEach(function(el,i){el.className="sm-mention"+(i===active?" sm-mention-on":"")})}
|
|
1369
|
+
function show(){
|
|
1370
|
+
var q=prefix();if(q===null)return close();
|
|
1371
|
+
var low=q.toLowerCase();
|
|
1372
|
+
var list=PEOPLE.filter(function(u){return !low||u.name.toLowerCase().indexOf(low)===0||u.real.toLowerCase().indexOf(low)===0}).slice(0,8);
|
|
1373
|
+
if(!list.length)return close();
|
|
1374
|
+
items=list;active=0;
|
|
1375
|
+
menu.innerHTML=list.map(function(u,i){return '<div class="sm-mention'+(i?"":" sm-mention-on")+'" data-i="'+i+'"><span class="sm-mention-av" style="background:'+esc(u.color)+'">'+esc(u.initials)+'</span><span class="sm-mention-name">@'+esc(u.name)+'</span><span class="sm-mention-real">'+esc(u.real)+'</span></div>'}).join("");
|
|
1376
|
+
menu.style.display="block";
|
|
1377
|
+
}
|
|
1378
|
+
function pick(i){var u=items[i];if(!u)return;var pos=ta.selectionStart,before=ta.value.slice(0,pos).replace(/@\\w*$/,"@"+u.name+" "),after=ta.value.slice(pos);ta.value=before+after;ta.selectionStart=ta.selectionEnd=before.length;close();ta.focus()}
|
|
1379
|
+
ta.addEventListener("input",show);
|
|
1380
|
+
ta.addEventListener("blur",function(){setTimeout(close,150)});
|
|
1381
|
+
menu.addEventListener("mousedown",function(e){var el=e.target.closest(".sm-mention");if(el){e.preventDefault();pick(Number(el.dataset.i))}});
|
|
1382
|
+
ta.addEventListener("keydown",function(e){
|
|
1383
|
+
if(e.key==="Backspace"&&ta.selectionStart===ta.selectionEnd&&ta.selectionStart>0){
|
|
1384
|
+
var pos=ta.selectionStart,before=ta.value.slice(0,pos),hit="";
|
|
1385
|
+
for(var k=0;k<PEOPLE.length;k++){var tok="@"+PEOPLE[k].name;var cand=before.endsWith(tok)?tok:"";if(cand&&cand.length>hit.length){var at=before.length-cand.length;if(at===0||/s/.test(before.charAt(at-1)))hit=cand}}
|
|
1386
|
+
if(hit){e.preventDefault();ta.value=before.slice(0,before.length-hit.length)+ta.value.slice(pos);ta.selectionStart=ta.selectionEnd=pos-hit.length;close();return}
|
|
1387
|
+
}
|
|
1388
|
+
if(items.length){
|
|
1389
|
+
if(e.key==="ArrowDown"){e.preventDefault();active=(active+1)%items.length;paint();return}
|
|
1390
|
+
if(e.key==="ArrowUp"){e.preventDefault();active=(active-1+items.length)%items.length;paint();return}
|
|
1391
|
+
if(e.key==="Enter"||e.key==="Tab"){e.preventDefault();pick(active);return}
|
|
1392
|
+
if(e.key==="Escape"){e.preventDefault();e.stopPropagation();close();return}
|
|
1393
|
+
}
|
|
1394
|
+
if(e.key==="Enter"&&!e.shiftKey){e.preventDefault();send(box)}
|
|
1395
|
+
});
|
|
1396
|
+
box.querySelector(".sm-composer-send").addEventListener("click",function(){send(box)});
|
|
1397
|
+
});
|
|
1398
|
+
})();`;
|
|
1399
|
+
return `${mentionData(store)}${reactionPicker()}<script>${body}${esc}</script>`;
|
|
1778
1400
|
}
|
|
1779
|
-
function
|
|
1780
|
-
|
|
1781
|
-
|
|
1401
|
+
function channelView(store, opts, channelId) {
|
|
1402
|
+
const channel = store.channels.get(channelId);
|
|
1403
|
+
const ctx = ctxOf(store);
|
|
1404
|
+
if (!channel)
|
|
1405
|
+
return notFound(store, opts, "Channel not found");
|
|
1406
|
+
const label = channelLabel(store, channel);
|
|
1407
|
+
return shell(store, opts, {
|
|
1408
|
+
title: `${label} \xB7 slack-mock`,
|
|
1409
|
+
header: channelHeader(store, opts, channel),
|
|
1410
|
+
main: channelColumn(store, ctx, opts, channel),
|
|
1411
|
+
active: channel.id,
|
|
1412
|
+
footer: composer(store, opts, channel),
|
|
1413
|
+
script: opts.screenshot ? "" : uiScript(store, opts)
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
function assistantPanel(store, channelId, ts) {
|
|
1417
|
+
const thread = store.assistantThreads.get(`${channelId}:${ts}`);
|
|
1418
|
+
if (!thread)
|
|
1419
|
+
return "";
|
|
1782
1420
|
const parts = [];
|
|
1783
|
-
if (
|
|
1784
|
-
parts.push(
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
if (
|
|
1789
|
-
|
|
1790
|
-
|
|
1421
|
+
if (thread.title)
|
|
1422
|
+
parts.push(`<div class="sm-assist-title">${plainTextToHtml(thread.title)}</div>`);
|
|
1423
|
+
if (thread.status)
|
|
1424
|
+
parts.push(`<div class="sm-assist-status">${plainTextToHtml(thread.status)}<span class="sm-stream">\u2022\u2022\u2022</span></div>`);
|
|
1425
|
+
const prompts = Array.isArray(thread.prompts) ? thread.prompts : [];
|
|
1426
|
+
if (prompts.length) {
|
|
1427
|
+
const chips = prompts.map((p) => {
|
|
1428
|
+
const prompt = typeof p === "object" && p !== null ? p : {};
|
|
1429
|
+
const title = typeof prompt.title === "string" ? prompt.title : "";
|
|
1430
|
+
const message = typeof prompt.message === "string" ? prompt.message : "";
|
|
1431
|
+
return `<span class="sm-prompt" title="${escapeHtml(message)}">${plainTextToHtml(title || message)}</span>`;
|
|
1432
|
+
}).join("");
|
|
1433
|
+
const heading = thread.promptsTitle ? `<div class="sm-assist-status">${plainTextToHtml(thread.promptsTitle)}</div>` : "";
|
|
1434
|
+
parts.push(`${heading}<div class="sm-prompts">${chips}</div>`);
|
|
1435
|
+
}
|
|
1436
|
+
if (!parts.length)
|
|
1437
|
+
return "";
|
|
1438
|
+
return `<div class="sm-assist">${parts.join("")}</div>`;
|
|
1791
1439
|
}
|
|
1792
|
-
function
|
|
1793
|
-
|
|
1440
|
+
function threadView(store, opts, channelId, ts) {
|
|
1441
|
+
const channel = store.channels.get(channelId);
|
|
1442
|
+
const ctx = ctxOf(store);
|
|
1443
|
+
if (!channel)
|
|
1444
|
+
return notFound(store, opts, "Channel not found");
|
|
1445
|
+
const parent = store.findMessage(channel.id, ts);
|
|
1446
|
+
if (!parent)
|
|
1447
|
+
return notFound(store, opts, "Message not found");
|
|
1448
|
+
const items = store.replies(channel.id, ts, { limit: 1000 }).items;
|
|
1449
|
+
const replies = items.slice(1);
|
|
1450
|
+
const label = channelLabel(store, channel);
|
|
1451
|
+
const repliesBar = replies.length ? `<div class="sm-daydiv"><span>${replies.length} ${replies.length === 1 ? "reply" : "replies"}</span></div>` : `<div class="sm-empty">No replies yet</div>`;
|
|
1452
|
+
const body = `${assistantPanel(store, channel.id, ts)}${renderMessage(store, ctx, parent, opts)}${repliesBar}${renderMessageList(store, ctx, replies, opts, { dividers: false })}`;
|
|
1453
|
+
if (opts.screenshot) {
|
|
1454
|
+
const header = topbar(`Thread <span class="sm-muted">\xB7</span> <a href="${channelHref(channel.id, opts)}">${escapeHtml(label)}</a>`, "");
|
|
1455
|
+
return shell(store, opts, {
|
|
1456
|
+
title: `Thread \xB7 ${label}`,
|
|
1457
|
+
header,
|
|
1458
|
+
main: body,
|
|
1459
|
+
active: channel.id
|
|
1460
|
+
});
|
|
1461
|
+
}
|
|
1462
|
+
const close = channelHref(channel.id, opts);
|
|
1463
|
+
const title = `Thread \xB7 ${label}`;
|
|
1464
|
+
if (opts.threadView === "full") {
|
|
1465
|
+
const header = topbar(`Thread <span class="sm-muted">\xB7</span> ${escapeHtml(label)}`, "", { href: close, label: `\u2190 ${label}` }, `<a class="sm-icon-btn" href="${threadHref(channel.id, ts, opts, { full: false })}" title="Collapse to panel" aria-label="Collapse to panel">\u2921</a>${identityChip(store, opts)}`);
|
|
1466
|
+
return shell(store, opts, {
|
|
1467
|
+
title,
|
|
1468
|
+
header,
|
|
1469
|
+
main: body,
|
|
1470
|
+
active: channel.id,
|
|
1471
|
+
footer: composer(store, opts, channel, ts),
|
|
1472
|
+
script: uiScript(store, opts, close)
|
|
1473
|
+
});
|
|
1474
|
+
}
|
|
1475
|
+
const expand = threadHref(channel.id, ts, opts, { full: true });
|
|
1476
|
+
const panel = `<aside class="sm-panel"><div class="sm-panel-top"><a class="sm-panel-back" href="${close}" aria-label="Back to ${escapeHtml(label)}">\u2190</a><div class="sm-panel-heading"><h2 class="sm-panel-title">Thread</h2><div class="sm-panel-sub">${escapeHtml(label)}</div></div><a class="sm-panel-expand" href="${expand}" title="Expand thread">\u2922</a><a class="sm-panel-close" href="${close}" title="Close thread">\xD7</a></div><div class="sm-panel-body sm-scroll">${body}</div>${composer(store, opts, channel, ts)}</aside>`;
|
|
1477
|
+
return shell(store, opts, {
|
|
1478
|
+
title,
|
|
1479
|
+
header: channelHeader(store, opts, channel),
|
|
1480
|
+
main: channelColumn(store, ctx, opts, channel, ts),
|
|
1481
|
+
active: channel.id,
|
|
1482
|
+
panel,
|
|
1483
|
+
footer: composer(store, opts, channel),
|
|
1484
|
+
script: uiScript(store, opts, close)
|
|
1485
|
+
});
|
|
1794
1486
|
}
|
|
1795
|
-
function
|
|
1796
|
-
return
|
|
1487
|
+
function notFound(store, opts, message) {
|
|
1488
|
+
return shell(store, opts, {
|
|
1489
|
+
title: message,
|
|
1490
|
+
header: topbar(escapeHtml(message), ""),
|
|
1491
|
+
main: `<div class="sm-empty">${escapeHtml(message)}</div>`
|
|
1492
|
+
});
|
|
1797
1493
|
}
|
|
1798
|
-
function
|
|
1799
|
-
|
|
1494
|
+
function renderPage(store, view, opts = {}) {
|
|
1495
|
+
switch (view.kind) {
|
|
1496
|
+
case "channel":
|
|
1497
|
+
return channelView(store, opts, view.channel);
|
|
1498
|
+
case "thread":
|
|
1499
|
+
return threadView(store, opts, view.channel, view.ts);
|
|
1500
|
+
default:
|
|
1501
|
+
return indexView(store, opts);
|
|
1502
|
+
}
|
|
1800
1503
|
}
|
|
1801
|
-
|
|
1802
|
-
|
|
1504
|
+
|
|
1505
|
+
// src/server.ts
|
|
1506
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
1507
|
+
|
|
1508
|
+
// src/body.ts
|
|
1509
|
+
var JSON_FIELDS = new Set([
|
|
1510
|
+
"blocks",
|
|
1511
|
+
"attachments",
|
|
1512
|
+
"metadata",
|
|
1513
|
+
"files",
|
|
1514
|
+
"view",
|
|
1515
|
+
"prompts",
|
|
1516
|
+
"icons",
|
|
1517
|
+
"user_ids",
|
|
1518
|
+
"types",
|
|
1519
|
+
"chunks",
|
|
1520
|
+
"loading_messages"
|
|
1521
|
+
]);
|
|
1522
|
+
function coerce(key, value) {
|
|
1523
|
+
if (JSON_FIELDS.has(key)) {
|
|
1524
|
+
try {
|
|
1525
|
+
return JSON.parse(value);
|
|
1526
|
+
} catch {
|
|
1527
|
+
return value;
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
if (value === "true")
|
|
1531
|
+
return true;
|
|
1532
|
+
if (value === "false")
|
|
1533
|
+
return false;
|
|
1534
|
+
return value;
|
|
1803
1535
|
}
|
|
1804
|
-
function
|
|
1805
|
-
const
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1536
|
+
async function parseArgs(req) {
|
|
1537
|
+
const url = new URL(req.url);
|
|
1538
|
+
const args = {};
|
|
1539
|
+
for (const [k, v] of url.searchParams)
|
|
1540
|
+
args[k] = coerce(k, v);
|
|
1541
|
+
if (req.method !== "POST")
|
|
1542
|
+
return args;
|
|
1543
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
1544
|
+
if (contentType.includes("application/json")) {
|
|
1545
|
+
const text = await req.text();
|
|
1546
|
+
if (text.trim())
|
|
1547
|
+
Object.assign(args, JSON.parse(text));
|
|
1548
|
+
return args;
|
|
1549
|
+
}
|
|
1550
|
+
if (contentType.includes("multipart/form-data")) {
|
|
1551
|
+
const form = await req.formData();
|
|
1552
|
+
form.forEach((v, k) => {
|
|
1553
|
+
args[k] = typeof v === "string" ? coerce(k, v) : v;
|
|
1554
|
+
});
|
|
1555
|
+
return args;
|
|
1556
|
+
}
|
|
1557
|
+
const text = await req.text();
|
|
1558
|
+
if (!text)
|
|
1559
|
+
return args;
|
|
1560
|
+
for (const [k, v] of new URLSearchParams(text))
|
|
1561
|
+
args[k] = coerce(k, v);
|
|
1562
|
+
return args;
|
|
1809
1563
|
}
|
|
1810
|
-
function
|
|
1811
|
-
|
|
1564
|
+
function bearerToken(req, args) {
|
|
1565
|
+
const header = req.headers.get("authorization");
|
|
1566
|
+
if (header?.toLowerCase().startsWith("bearer "))
|
|
1567
|
+
return header.slice(7).trim();
|
|
1568
|
+
if (typeof args.token === "string")
|
|
1569
|
+
return args.token;
|
|
1570
|
+
return;
|
|
1812
1571
|
}
|
|
1813
|
-
function
|
|
1814
|
-
const
|
|
1815
|
-
|
|
1816
|
-
return "";
|
|
1817
|
-
const now = new Date;
|
|
1818
|
-
if (dayKey(d) === dayKey(now))
|
|
1819
|
-
return "Today";
|
|
1820
|
-
const yesterday = new Date(now.getTime() - 86400000);
|
|
1821
|
-
if (dayKey(d) === dayKey(yesterday))
|
|
1822
|
-
return "Yesterday";
|
|
1823
|
-
return `${MONTHS[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
|
|
1572
|
+
function str2(args, key) {
|
|
1573
|
+
const v = args[key];
|
|
1574
|
+
return typeof v === "string" ? v : v == null ? undefined : String(v);
|
|
1824
1575
|
}
|
|
1825
|
-
function
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
1830
|
-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
1576
|
+
function num(args, key, fallback) {
|
|
1577
|
+
const v = args[key];
|
|
1578
|
+
const n = typeof v === "number" ? v : typeof v === "string" ? Number.parseInt(v, 10) : Number.NaN;
|
|
1579
|
+
return Number.isFinite(n) ? n : fallback;
|
|
1831
1580
|
}
|
|
1832
|
-
function
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1581
|
+
function bool(args, key) {
|
|
1582
|
+
const v = args[key];
|
|
1583
|
+
if (typeof v === "boolean")
|
|
1584
|
+
return v;
|
|
1585
|
+
if (v === "true" || v === "1" || v === 1)
|
|
1586
|
+
return true;
|
|
1587
|
+
if (v === "false" || v === "0" || v === 0)
|
|
1588
|
+
return false;
|
|
1589
|
+
return;
|
|
1837
1590
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1591
|
+
|
|
1592
|
+
// src/ids.ts
|
|
1593
|
+
var ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ0123456789";
|
|
1594
|
+
function slackId(prefix, length = 8) {
|
|
1595
|
+
let out = prefix;
|
|
1596
|
+
for (let i = 0;i < length; i++)
|
|
1597
|
+
out += ALPHABET[Math.floor(Math.random() * ALPHABET.length)];
|
|
1598
|
+
return out;
|
|
1842
1599
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1600
|
+
var lastMicros = 0;
|
|
1601
|
+
function nextTs() {
|
|
1602
|
+
let micros = Date.now() * 1000;
|
|
1603
|
+
if (micros <= lastMicros)
|
|
1604
|
+
micros = lastMicros + 1;
|
|
1605
|
+
lastMicros = micros;
|
|
1606
|
+
const seconds = Math.floor(micros / 1e6);
|
|
1607
|
+
const fraction = (micros % 1e6).toString().padStart(6, "0");
|
|
1608
|
+
return `${seconds}.${fraction}`;
|
|
1848
1609
|
}
|
|
1849
|
-
function
|
|
1850
|
-
|
|
1851
|
-
return "@";
|
|
1852
|
-
if (c.is_private)
|
|
1853
|
-
return "\uD83D\uDD12";
|
|
1854
|
-
return "#";
|
|
1610
|
+
function nowUnix() {
|
|
1611
|
+
return Math.floor(Date.now() / 1000);
|
|
1855
1612
|
}
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1613
|
+
|
|
1614
|
+
// src/store.ts
|
|
1615
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
|
|
1616
|
+
import { dirname } from "path";
|
|
1617
|
+
class SlackApiError2 extends Error {
|
|
1618
|
+
code;
|
|
1619
|
+
extra;
|
|
1620
|
+
constructor(code, extra = {}) {
|
|
1621
|
+
super(code);
|
|
1622
|
+
this.code = code;
|
|
1623
|
+
this.extra = extra;
|
|
1624
|
+
}
|
|
1862
1625
|
}
|
|
1863
|
-
function
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
const
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1626
|
+
function parseJournal(text, source = "journal") {
|
|
1627
|
+
const entries = [];
|
|
1628
|
+
let line = 0;
|
|
1629
|
+
for (const raw of text.split(`
|
|
1630
|
+
`)) {
|
|
1631
|
+
line += 1;
|
|
1632
|
+
if (!raw.trim())
|
|
1633
|
+
continue;
|
|
1634
|
+
try {
|
|
1635
|
+
entries.push({ line, change: JSON.parse(raw) });
|
|
1636
|
+
} catch (e) {
|
|
1637
|
+
console.warn(`[slack-mock] skipping corrupt journal line ${line} in ${source}: ${e instanceof Error ? e.message : e}`);
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
return entries;
|
|
1870
1641
|
}
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1642
|
+
|
|
1643
|
+
class Store2 {
|
|
1644
|
+
team;
|
|
1645
|
+
app;
|
|
1646
|
+
bot;
|
|
1647
|
+
users = new Map;
|
|
1648
|
+
channels = new Map;
|
|
1649
|
+
messages = new Map;
|
|
1650
|
+
files = new Map;
|
|
1651
|
+
views = new Map;
|
|
1652
|
+
ephemerals = [];
|
|
1653
|
+
apiCalls = [];
|
|
1654
|
+
assistantThreads = new Map;
|
|
1655
|
+
listeners = new Set;
|
|
1656
|
+
dataFile;
|
|
1657
|
+
replaying = false;
|
|
1658
|
+
constructor(opts = {}) {
|
|
1659
|
+
this.team = {
|
|
1660
|
+
id: opts.teamId ?? "T0MOCK0000",
|
|
1661
|
+
name: opts.teamName ?? "Mock Workspace",
|
|
1662
|
+
domain: opts.teamDomain ?? "mock"
|
|
1663
|
+
};
|
|
1664
|
+
this.app = { id: opts.appId ?? "A0MOCK0000", name: opts.appName ?? "mock-bot" };
|
|
1665
|
+
this.bot = {
|
|
1666
|
+
userId: opts.botUserId ?? "U0BOT00000",
|
|
1667
|
+
botId: opts.botId ?? "B0BOT00000",
|
|
1668
|
+
token: opts.botToken ?? "xoxb-mock-bot-token",
|
|
1669
|
+
appToken: opts.appToken ?? "xapp-mock-app-token",
|
|
1670
|
+
userToken: opts.userToken
|
|
1671
|
+
};
|
|
1672
|
+
this.dataFile = opts.dataFile;
|
|
1673
|
+
if (this.dataFile && existsSync(this.dataFile))
|
|
1674
|
+
this.replay(parseJournal(readFileSync(this.dataFile, "utf8"), this.dataFile).map((e) => e.change));
|
|
1675
|
+
if (opts.replay)
|
|
1676
|
+
this.replay(opts.replay);
|
|
1677
|
+
if (!this.users.has(this.bot.userId)) {
|
|
1678
|
+
this.addUser({
|
|
1679
|
+
id: this.bot.userId,
|
|
1680
|
+
name: this.app.name,
|
|
1681
|
+
real_name: this.app.name,
|
|
1682
|
+
is_bot: true
|
|
1683
|
+
});
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
onChange(listener) {
|
|
1687
|
+
this.listeners.add(listener);
|
|
1688
|
+
return () => this.listeners.delete(listener);
|
|
1689
|
+
}
|
|
1690
|
+
onChangeEmit(change) {
|
|
1691
|
+
this.emit(change);
|
|
1692
|
+
}
|
|
1693
|
+
emit(change) {
|
|
1694
|
+
if (this.dataFile && !this.replaying && change.kind !== "api.call") {
|
|
1695
|
+
appendFileSync(this.dataFile, `${JSON.stringify({ at: new Date().toISOString(), ...stripBytes(change) })}
|
|
1696
|
+
`);
|
|
1697
|
+
}
|
|
1698
|
+
for (const l of this.listeners)
|
|
1699
|
+
l(change);
|
|
1700
|
+
}
|
|
1701
|
+
replay(changes) {
|
|
1702
|
+
this.replaying = true;
|
|
1703
|
+
try {
|
|
1704
|
+
for (const change of changes)
|
|
1705
|
+
this.applyReplayed(change);
|
|
1706
|
+
} finally {
|
|
1707
|
+
this.replaying = false;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
applyReplayed(change) {
|
|
1711
|
+
switch (change.kind) {
|
|
1712
|
+
case "user.add":
|
|
1713
|
+
this.users.set(change.user.id, change.user);
|
|
1714
|
+
break;
|
|
1715
|
+
case "channel.add":
|
|
1716
|
+
case "channel.update":
|
|
1717
|
+
this.channels.set(change.channel.id, change.channel);
|
|
1718
|
+
break;
|
|
1719
|
+
case "member.join": {
|
|
1720
|
+
const c = this.channels.get(change.channel.id);
|
|
1721
|
+
if (c && !c.members.includes(change.user))
|
|
1722
|
+
c.members.push(change.user);
|
|
1723
|
+
break;
|
|
1724
|
+
}
|
|
1725
|
+
case "message.add":
|
|
1726
|
+
this.insertMessage(change.message);
|
|
1727
|
+
break;
|
|
1728
|
+
case "message.update":
|
|
1729
|
+
case "reaction.add":
|
|
1730
|
+
case "reaction.remove": {
|
|
1731
|
+
const list = this.messages.get(change.message.channel);
|
|
1732
|
+
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
1733
|
+
if (list && i >= 0)
|
|
1734
|
+
list[i] = change.message;
|
|
1735
|
+
break;
|
|
1736
|
+
}
|
|
1737
|
+
case "message.delete": {
|
|
1738
|
+
const list = this.messages.get(change.message.channel);
|
|
1739
|
+
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
1740
|
+
if (list && i >= 0)
|
|
1741
|
+
list.splice(i, 1);
|
|
1742
|
+
break;
|
|
1743
|
+
}
|
|
1744
|
+
case "file.add":
|
|
1745
|
+
this.files.set(change.file.id, change.file);
|
|
1746
|
+
break;
|
|
1747
|
+
default:
|
|
1748
|
+
break;
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
addUser(input) {
|
|
1752
|
+
const id = input.id ?? slackId("U");
|
|
1753
|
+
const realName = input.real_name ?? input.name;
|
|
1754
|
+
const user = {
|
|
1755
|
+
id,
|
|
1756
|
+
team_id: this.team.id,
|
|
1757
|
+
name: input.name,
|
|
1758
|
+
real_name: realName,
|
|
1759
|
+
deleted: false,
|
|
1760
|
+
is_bot: input.is_bot ?? false,
|
|
1761
|
+
is_admin: input.is_admin ?? false,
|
|
1762
|
+
is_app_user: false,
|
|
1763
|
+
tz: input.tz ?? "UTC",
|
|
1764
|
+
profile: {
|
|
1765
|
+
real_name: realName,
|
|
1766
|
+
display_name: input.name,
|
|
1767
|
+
real_name_normalized: realName,
|
|
1768
|
+
display_name_normalized: input.name,
|
|
1769
|
+
email: input.email,
|
|
1770
|
+
title: input.title,
|
|
1771
|
+
status_text: input.status,
|
|
1772
|
+
team: this.team.id
|
|
1773
|
+
}
|
|
1774
|
+
};
|
|
1775
|
+
this.users.set(id, user);
|
|
1776
|
+
this.emit({ kind: "user.add", user });
|
|
1777
|
+
return user;
|
|
1876
1778
|
}
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1779
|
+
user(id) {
|
|
1780
|
+
const u = this.users.get(id);
|
|
1781
|
+
if (!u)
|
|
1782
|
+
throw new SlackApiError2("user_not_found");
|
|
1783
|
+
return u;
|
|
1880
1784
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
"
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1785
|
+
userByEmail(email) {
|
|
1786
|
+
for (const u of this.users.values())
|
|
1787
|
+
if (u.profile.email === email)
|
|
1788
|
+
return u;
|
|
1789
|
+
return;
|
|
1790
|
+
}
|
|
1791
|
+
isBotUser(userId) {
|
|
1792
|
+
return userId === this.bot.userId;
|
|
1793
|
+
}
|
|
1794
|
+
addChannel(input) {
|
|
1795
|
+
const name = input.name.replace(/^#/, "");
|
|
1796
|
+
for (const c of this.channels.values()) {
|
|
1797
|
+
if (c.name === name && !c.is_im)
|
|
1798
|
+
throw new SlackApiError2("name_taken");
|
|
1799
|
+
}
|
|
1800
|
+
const id = input.id ?? slackId(input.is_private ? "G" : "C");
|
|
1801
|
+
const creator = input.creator ?? this.bot.userId;
|
|
1802
|
+
const members = [...new Set([creator, ...input.members ?? []])];
|
|
1803
|
+
const channel = {
|
|
1804
|
+
id,
|
|
1805
|
+
name,
|
|
1806
|
+
is_channel: !input.is_private,
|
|
1807
|
+
is_group: !!input.is_private,
|
|
1808
|
+
is_im: false,
|
|
1809
|
+
is_mpim: false,
|
|
1810
|
+
is_private: !!input.is_private,
|
|
1811
|
+
is_archived: false,
|
|
1812
|
+
is_general: name === "general",
|
|
1813
|
+
created: nowUnix(),
|
|
1814
|
+
creator,
|
|
1815
|
+
topic: { value: input.topic ?? "", creator, last_set: 0 },
|
|
1816
|
+
purpose: { value: input.purpose ?? "", creator, last_set: 0 },
|
|
1817
|
+
members
|
|
1818
|
+
};
|
|
1819
|
+
this.channels.set(id, channel);
|
|
1820
|
+
this.messages.set(id, []);
|
|
1821
|
+
this.emit({ kind: "channel.add", channel });
|
|
1822
|
+
return channel;
|
|
1823
|
+
}
|
|
1824
|
+
openDm(userId) {
|
|
1825
|
+
this.user(userId);
|
|
1826
|
+
for (const c of this.channels.values())
|
|
1827
|
+
if (c.is_im && c.user === userId)
|
|
1828
|
+
return c;
|
|
1829
|
+
const id = slackId("D");
|
|
1830
|
+
const channel = {
|
|
1831
|
+
id,
|
|
1832
|
+
name: id,
|
|
1833
|
+
is_channel: false,
|
|
1834
|
+
is_group: false,
|
|
1835
|
+
is_im: true,
|
|
1836
|
+
is_mpim: false,
|
|
1837
|
+
is_private: true,
|
|
1838
|
+
is_archived: false,
|
|
1839
|
+
is_general: false,
|
|
1840
|
+
created: nowUnix(),
|
|
1841
|
+
creator: userId,
|
|
1842
|
+
user: userId,
|
|
1843
|
+
topic: { value: "", creator: "", last_set: 0 },
|
|
1844
|
+
purpose: { value: "", creator: "", last_set: 0 },
|
|
1845
|
+
members: [userId, this.bot.userId]
|
|
1846
|
+
};
|
|
1847
|
+
this.channels.set(id, channel);
|
|
1848
|
+
this.messages.set(id, []);
|
|
1849
|
+
this.emit({ kind: "channel.add", channel });
|
|
1850
|
+
return channel;
|
|
1851
|
+
}
|
|
1852
|
+
channel(id) {
|
|
1853
|
+
const c = this.channels.get(id);
|
|
1854
|
+
if (!c)
|
|
1855
|
+
throw new SlackApiError2("channel_not_found");
|
|
1856
|
+
return c;
|
|
1857
|
+
}
|
|
1858
|
+
channelByName(name) {
|
|
1859
|
+
const n = name.replace(/^#/, "");
|
|
1860
|
+
for (const c of this.channels.values())
|
|
1861
|
+
if (c.name === n)
|
|
1862
|
+
return c;
|
|
1863
|
+
return;
|
|
1864
|
+
}
|
|
1865
|
+
channelType(c) {
|
|
1866
|
+
if (c.is_im)
|
|
1867
|
+
return "im";
|
|
1868
|
+
if (c.is_mpim)
|
|
1869
|
+
return "mpim";
|
|
1870
|
+
if (c.is_private)
|
|
1871
|
+
return "group";
|
|
1872
|
+
return "channel";
|
|
1873
|
+
}
|
|
1874
|
+
join(channelId, userId, inviter) {
|
|
1875
|
+
const channel = this.channel(channelId);
|
|
1876
|
+
if (channel.is_archived)
|
|
1877
|
+
throw new SlackApiError2("is_archived");
|
|
1878
|
+
if (channel.members.includes(userId))
|
|
1879
|
+
return { alreadyMember: true };
|
|
1880
|
+
channel.members.push(userId);
|
|
1881
|
+
this.emit({ kind: "member.join", channel, user: userId, inviter });
|
|
1882
|
+
return { alreadyMember: false };
|
|
1883
|
+
}
|
|
1884
|
+
archive(channelId) {
|
|
1885
|
+
const channel = this.channel(channelId);
|
|
1886
|
+
if (channel.is_archived)
|
|
1887
|
+
throw new SlackApiError2("already_archived");
|
|
1888
|
+
if (channel.is_general)
|
|
1889
|
+
throw new SlackApiError2("cant_archive_general");
|
|
1890
|
+
channel.is_archived = true;
|
|
1891
|
+
this.emit({ kind: "channel.update", channel });
|
|
1892
|
+
}
|
|
1893
|
+
insertMessage(message) {
|
|
1894
|
+
const list = this.messages.get(message.channel) ?? [];
|
|
1895
|
+
this.messages.set(message.channel, list);
|
|
1896
|
+
let i = list.length;
|
|
1897
|
+
while (i > 0 && Number(list[i - 1].ts) > Number(message.ts))
|
|
1898
|
+
i--;
|
|
1899
|
+
list.splice(i, 0, message);
|
|
1900
|
+
if (message.thread_ts && message.thread_ts !== message.ts) {
|
|
1901
|
+
const parent = this.findMessage(message.channel, message.thread_ts);
|
|
1902
|
+
if (parent) {
|
|
1903
|
+
parent.reply_count = (parent.reply_count ?? 0) + 1;
|
|
1904
|
+
const users = new Set(parent.reply_users ?? []);
|
|
1905
|
+
if (message.user)
|
|
1906
|
+
users.add(message.user);
|
|
1907
|
+
parent.reply_users = [...users];
|
|
1908
|
+
parent.reply_users_count = users.size;
|
|
1909
|
+
parent.latest_reply = message.ts;
|
|
1910
|
+
if (parent.user)
|
|
1911
|
+
message.parent_user_id = parent.user;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
findMessage(channelId, ts) {
|
|
1916
|
+
return this.messages.get(channelId)?.find((m) => m.ts === ts);
|
|
1917
|
+
}
|
|
1918
|
+
findAnyMessage(channelId, ts) {
|
|
1919
|
+
return this.findMessage(channelId, ts) ?? this.ephemerals.find((m) => m.channel === channelId && m.ts === ts);
|
|
1920
|
+
}
|
|
1921
|
+
updateEphemeral(channelId, ts, patch) {
|
|
1922
|
+
const m = this.ephemerals.find((x) => x.channel === channelId && x.ts === ts);
|
|
1923
|
+
if (!m)
|
|
1924
|
+
throw new SlackApiError2("message_not_found");
|
|
1925
|
+
Object.assign(m, patch);
|
|
1926
|
+
return m;
|
|
1927
|
+
}
|
|
1928
|
+
deleteEphemeral(channelId, ts) {
|
|
1929
|
+
const i = this.ephemerals.findIndex((x) => x.channel === channelId && x.ts === ts);
|
|
1930
|
+
if (i < 0)
|
|
1931
|
+
throw new SlackApiError2("message_not_found");
|
|
1932
|
+
this.ephemerals.splice(i, 1);
|
|
1933
|
+
}
|
|
1934
|
+
message(channelId, ts) {
|
|
1935
|
+
const m = this.findMessage(channelId, ts);
|
|
1936
|
+
if (!m)
|
|
1937
|
+
throw new SlackApiError2("message_not_found");
|
|
1938
|
+
return m;
|
|
1939
|
+
}
|
|
1940
|
+
addMessage(input) {
|
|
1941
|
+
const channel = this.channel(input.channel);
|
|
1942
|
+
if (channel.is_archived)
|
|
1943
|
+
throw new SlackApiError2("is_archived");
|
|
1944
|
+
if (input.thread_ts && !this.findMessage(input.channel, input.thread_ts)) {
|
|
1945
|
+
throw new SlackApiError2("thread_not_found");
|
|
1946
|
+
}
|
|
1947
|
+
const message = {
|
|
1948
|
+
type: "message",
|
|
1949
|
+
team: this.team.id,
|
|
1950
|
+
ts: input.ts ?? nextTs(),
|
|
1951
|
+
...input
|
|
1952
|
+
};
|
|
1953
|
+
if (message.ephemeral_user)
|
|
1954
|
+
this.ephemerals.push(message);
|
|
1955
|
+
else
|
|
1956
|
+
this.insertMessage(message);
|
|
1957
|
+
this.emit({ kind: "message.add", message });
|
|
1958
|
+
return message;
|
|
1959
|
+
}
|
|
1960
|
+
updateMessage(channelId, ts, patch, editor, opts = {}) {
|
|
1961
|
+
const message = this.message(channelId, ts);
|
|
1962
|
+
const previous = structuredClone(message);
|
|
1963
|
+
Object.assign(message, patch, opts.markEdited === false ? {} : { edited: { user: editor, ts: nextTs() } });
|
|
1964
|
+
this.emit({ kind: "message.update", message, previous });
|
|
1965
|
+
return message;
|
|
1966
|
+
}
|
|
1967
|
+
deleteMessage(channelId, ts) {
|
|
1968
|
+
const list = this.messages.get(channelId);
|
|
1969
|
+
const i = list?.findIndex((m) => m.ts === ts) ?? -1;
|
|
1970
|
+
if (!list || i < 0)
|
|
1971
|
+
throw new SlackApiError2("message_not_found");
|
|
1972
|
+
const [message] = list.splice(i, 1);
|
|
1973
|
+
if (message.thread_ts && message.thread_ts !== ts) {
|
|
1974
|
+
const parent = this.findMessage(channelId, message.thread_ts);
|
|
1975
|
+
if (parent?.reply_count)
|
|
1976
|
+
parent.reply_count -= 1;
|
|
1934
1977
|
}
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
const meta = `${escapeHtml(f.pretty_type || f.filetype || "File")} \xB7 ${fileSize(f.size ?? 0)}`;
|
|
1938
|
-
const label = href ? `<a href="${href}">${name}</a>` : name;
|
|
1939
|
-
return `<div class="sm-file-card"><div class="sm-file-icon">\uD83D\uDCC4</div><div><div class="sm-file-name">${label}</div><div class="sm-file-meta">${meta}</div></div></div>`;
|
|
1940
|
-
});
|
|
1941
|
-
return `<div class="sm-files">${cards.join("")}</div>`;
|
|
1942
|
-
}
|
|
1943
|
-
function renderMessage(store, ctx, m, opts, flags = {}) {
|
|
1944
|
-
const name = authorName(store, m);
|
|
1945
|
-
const isApp = Boolean(m.bot_id);
|
|
1946
|
-
const blocks = Array.isArray(m.blocks) && m.blocks.length ? m.blocks : undefined;
|
|
1947
|
-
const interactive = !opts.screenshot && !flags.ephemeral && humanUsers(store).length > 0;
|
|
1948
|
-
const body = [];
|
|
1949
|
-
if (m.streaming_state !== undefined) {
|
|
1950
|
-
if (m.text)
|
|
1951
|
-
body.push(`<div class="sm-text">${mrkdwnToHtml(markdownToMrkdwn(m.text), ctx)}</div>`);
|
|
1952
|
-
if (blocks)
|
|
1953
|
-
body.push(renderBlocks(blocks, ctx));
|
|
1954
|
-
} else if (blocks)
|
|
1955
|
-
body.push(renderBlocks(blocks, ctx));
|
|
1956
|
-
else if (m.text)
|
|
1957
|
-
body.push(`<div class="sm-text">${mrkdwnToHtml(m.text, ctx)}</div>`);
|
|
1958
|
-
if (m.edited)
|
|
1959
|
-
body.push(`<div class="sm-edited">(edited)</div>`);
|
|
1960
|
-
if (m.attachments?.length)
|
|
1961
|
-
body.push(renderAttachments(m.attachments, ctx));
|
|
1962
|
-
body.push(filesHtml(m.files, store.bot.token));
|
|
1963
|
-
body.push(reactionsHtml(store, m, interactive, Boolean(opts.writeGated)));
|
|
1964
|
-
if (flags.ephemeral)
|
|
1965
|
-
body.push(`<div class="sm-eph-note">Only visible to you</div>`);
|
|
1966
|
-
if (flags.threadLink && m.reply_count) {
|
|
1967
|
-
const count = `${m.reply_count} ${m.reply_count === 1 ? "reply" : "replies"}`;
|
|
1968
|
-
const last = m.latest_reply ? `<span class="sm-replies-time">Last reply ${timeOf(m.latest_reply)}</span>` : "";
|
|
1969
|
-
body.push(`<a class="sm-replies" href="${threadHref(m.channel, m.thread_ts ?? m.ts, opts)}">${count}${last}</a>`);
|
|
1978
|
+
this.emit({ kind: "message.delete", message });
|
|
1979
|
+
return message;
|
|
1970
1980
|
}
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
function renderMessageList(store, ctx, messages, opts, flags = {}) {
|
|
1981
|
-
const out = [];
|
|
1982
|
-
let day = "";
|
|
1983
|
-
for (const m of messages) {
|
|
1984
|
-
const label = flags.dividers === false ? "" : dayLabel(m.ts);
|
|
1985
|
-
if (label && label !== day) {
|
|
1986
|
-
day = label;
|
|
1987
|
-
out.push(`<div class="sm-daydiv"><span>${escapeHtml(label)}</span></div>`);
|
|
1981
|
+
addReaction(channelId, ts, name, userId) {
|
|
1982
|
+
const message = this.message(channelId, ts);
|
|
1983
|
+
message.reactions ??= [];
|
|
1984
|
+
let reaction = message.reactions.find((r) => r.name === name);
|
|
1985
|
+
if (reaction?.users.includes(userId))
|
|
1986
|
+
throw new SlackApiError2("already_reacted");
|
|
1987
|
+
if (!reaction) {
|
|
1988
|
+
reaction = { name, users: [], count: 0 };
|
|
1989
|
+
message.reactions.push(reaction);
|
|
1988
1990
|
}
|
|
1989
|
-
|
|
1991
|
+
reaction.users.push(userId);
|
|
1992
|
+
reaction.count = reaction.users.length;
|
|
1993
|
+
this.emit({ kind: "reaction.add", message, name, user: userId });
|
|
1994
|
+
return message;
|
|
1990
1995
|
}
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
1996
|
+
removeReaction(channelId, ts, name, userId) {
|
|
1997
|
+
const message = this.message(channelId, ts);
|
|
1998
|
+
const reaction = message.reactions?.find((r) => r.name === name);
|
|
1999
|
+
if (!reaction?.users.includes(userId))
|
|
2000
|
+
throw new SlackApiError2("no_reaction");
|
|
2001
|
+
reaction.users = reaction.users.filter((u) => u !== userId);
|
|
2002
|
+
reaction.count = reaction.users.length;
|
|
2003
|
+
message.reactions = message.reactions.filter((r) => r.count > 0);
|
|
2004
|
+
if (message.reactions.length === 0)
|
|
2005
|
+
message.reactions = undefined;
|
|
2006
|
+
this.emit({ kind: "reaction.remove", message, name, user: userId });
|
|
2007
|
+
return message;
|
|
2008
|
+
}
|
|
2009
|
+
history(channelId, q = {}) {
|
|
2010
|
+
this.channel(channelId);
|
|
2011
|
+
const all = (this.messages.get(channelId) ?? []).filter((m) => !m.thread_ts || m.thread_ts === m.ts || m.subtype === "thread_broadcast");
|
|
2012
|
+
return paginate(all, q);
|
|
2013
|
+
}
|
|
2014
|
+
replies(channelId, threadTs, q = {}) {
|
|
2015
|
+
const parent = this.message(channelId, threadTs);
|
|
2016
|
+
const all = [
|
|
2017
|
+
parent,
|
|
2018
|
+
...(this.messages.get(channelId) ?? []).filter((m) => m.thread_ts === threadTs && m.ts !== threadTs)
|
|
2019
|
+
];
|
|
2020
|
+
return paginate(all, q);
|
|
2021
|
+
}
|
|
2022
|
+
addFile(input) {
|
|
2023
|
+
const id = input.id ?? slackId("F");
|
|
2024
|
+
const ext = input.name.includes(".") ? input.name.split(".").pop().toLowerCase() : "";
|
|
2025
|
+
const mimetype = input.mimetype ?? guessMime(ext);
|
|
2026
|
+
const now = nowUnix();
|
|
2027
|
+
const file = {
|
|
2028
|
+
id,
|
|
2029
|
+
created: now,
|
|
2030
|
+
timestamp: now,
|
|
2031
|
+
name: input.name,
|
|
2032
|
+
title: input.title ?? input.name,
|
|
2033
|
+
mimetype,
|
|
2034
|
+
filetype: ext || "binary",
|
|
2035
|
+
pretty_type: ext ? ext.toUpperCase() : "Binary",
|
|
2036
|
+
user: input.user,
|
|
2037
|
+
size: input.bytes.byteLength,
|
|
2038
|
+
mode: "hosted",
|
|
2039
|
+
is_external: false,
|
|
2040
|
+
is_public: false,
|
|
2041
|
+
url_private: `${input.baseUrl}/files-pri/${this.team.id}-${id}/${encodeURIComponent(input.name)}`,
|
|
2042
|
+
url_private_download: `${input.baseUrl}/files-pri/${this.team.id}-${id}/download/${encodeURIComponent(input.name)}`,
|
|
2043
|
+
permalink: `${input.baseUrl}/files/${input.user}/${id}/${encodeURIComponent(input.name)}`,
|
|
2044
|
+
channels: [],
|
|
2045
|
+
bytes: input.bytes
|
|
2046
|
+
};
|
|
2047
|
+
if (mimetype.startsWith("image/")) {
|
|
2048
|
+
const thumbs = file;
|
|
2049
|
+
for (const size of [64, 80, 160, 360, 480])
|
|
2050
|
+
thumbs[`thumb_${size}`] = file.url_private;
|
|
2051
|
+
}
|
|
2052
|
+
this.files.set(id, file);
|
|
2053
|
+
this.emit({ kind: "file.add", file });
|
|
2054
|
+
return file;
|
|
2055
|
+
}
|
|
2056
|
+
file(id) {
|
|
2057
|
+
const f = this.files.get(id);
|
|
2058
|
+
if (!f)
|
|
2059
|
+
throw new SlackApiError2("file_not_found");
|
|
2060
|
+
return f;
|
|
2061
|
+
}
|
|
2062
|
+
openView(view) {
|
|
2063
|
+
const id = view.id ?? slackId("V");
|
|
2064
|
+
const full = {
|
|
2065
|
+
...view,
|
|
2066
|
+
id,
|
|
2067
|
+
team_id: this.team.id,
|
|
2068
|
+
app_id: this.app.id,
|
|
2069
|
+
bot_id: this.bot.botId,
|
|
2070
|
+
hash: `${nowUnix()}.${slackId("", 6).toLowerCase()}`,
|
|
2071
|
+
root_view_id: id
|
|
2072
|
+
};
|
|
2073
|
+
this.views.set(id, full);
|
|
2074
|
+
this.emit({ kind: "view.open", view: full });
|
|
2075
|
+
return full;
|
|
2076
|
+
}
|
|
2077
|
+
recordApiCall(call) {
|
|
2078
|
+
this.apiCalls.push(call);
|
|
2079
|
+
this.emit({ kind: "api.call", call });
|
|
2080
|
+
}
|
|
2081
|
+
assistantThread(channel, threadTs) {
|
|
2082
|
+
const key = `${channel}:${threadTs}`;
|
|
2083
|
+
let t = this.assistantThreads.get(key);
|
|
2084
|
+
if (!t) {
|
|
2085
|
+
t = {};
|
|
2086
|
+
this.assistantThreads.set(key, t);
|
|
2087
|
+
}
|
|
2088
|
+
return t;
|
|
2006
2089
|
}
|
|
2007
|
-
const app = `sm-app${parts.panel ? " sm-app-thread" : ""}`;
|
|
2008
|
-
const handle = parts.panel ? `<div class="sm-resize" title="Drag to resize, double-click to reset"></div>` : "";
|
|
2009
|
-
return `${head}<body${style}><div class="${app}">${sidebar(store, opts, parts.active)}<div class="sm-main">${parts.header}<div class="sm-page sm-scroll">${parts.main}</div>${parts.footer ?? ""}</div>${handle}${parts.panel ?? ""}</div>${parts.script ?? ""}</body></html>`;
|
|
2010
|
-
}
|
|
2011
|
-
function topbar(title, sub, back, actions) {
|
|
2012
|
-
const backLink = back ? `<a class="sm-back" href="${back.href}">${escapeHtml(back.label)}</a>` : "";
|
|
2013
|
-
const right = actions ? `<div class="sm-top-actions">${actions}</div>` : "";
|
|
2014
|
-
return `<div class="sm-top">${backLink}<h1>${title}</h1>${sub ? `<div class="sm-sub">${sub}</div>` : ""}${right}</div>`;
|
|
2015
|
-
}
|
|
2016
|
-
function indexView(store, opts) {
|
|
2017
|
-
const cards = [
|
|
2018
|
-
["Workspace", escapeHtml(store.team.name)],
|
|
2019
|
-
["Team id", escapeHtml(store.team.id)],
|
|
2020
|
-
["Bot user", escapeHtml(`${store.app.name} (${store.bot.userId})`)],
|
|
2021
|
-
["Connections", String(opts.connections ?? 0)]
|
|
2022
|
-
].map(([k, v]) => `<div class="sm-card"><div class="sm-card-k">${k}</div><div class="sm-card-v">${v}</div></div>`).join("");
|
|
2023
|
-
const channels = [...store.channels.values()];
|
|
2024
|
-
const channelRows = channels.map((c) => {
|
|
2025
|
-
const count = store.messages.get(c.id)?.length ?? 0;
|
|
2026
|
-
return `<tr><td data-label="Name"><a href="${channelHref(c.id, opts)}">${escapeHtml(channelIcon(c))} ${escapeHtml(c.is_im ? channelLabel(store, c) : c.name)}</a></td><td data-label="Type"><span class="sm-tag">${channelKind(c)}</span></td><td class="sm-muted" data-label="Id">${c.id}</td><td data-label="Messages">${count}</td></tr>`;
|
|
2027
|
-
}).join("");
|
|
2028
|
-
const userRows = [...store.users.values()].map((u) => `<tr><td data-label="Name">${escapeHtml(u.name)}${userStatus(u)}</td><td data-label="Real name">${escapeHtml(u.real_name)}</td><td class="sm-muted" data-label="Title">${escapeHtml(u.profile.title ?? "")}</td><td class="sm-muted" data-label="Email">${escapeHtml(u.profile.email ?? "")}</td><td class="sm-muted" data-label="Id">${u.id}</td><td data-label="Type">${u.is_bot ? `<span class="sm-tag">bot</span>` : ""}</td></tr>`).join("");
|
|
2029
|
-
const main = `<div class="sm-cards">${cards}</div>
|
|
2030
|
-
<div class="sm-h2">Channels</div>
|
|
2031
|
-
${channels.length ? `<table class="sm-table"><tr><th>Name</th><th>Type</th><th>Id</th><th>Messages</th></tr>${channelRows}</table>` : `<div class="sm-empty">No channels yet</div>`}
|
|
2032
|
-
<div class="sm-h2">Users</div>
|
|
2033
|
-
<table class="sm-table"><tr><th>Name</th><th>Real name</th><th>Title</th><th>Email</th><th>Id</th><th></th></tr>${userRows}</table>`;
|
|
2034
|
-
return shell(store, opts, {
|
|
2035
|
-
title: `${store.team.name} \xB7 slack-mock`,
|
|
2036
|
-
header: topbar(escapeHtml(store.team.name), "slack-mock workspace", undefined, identityChip(store, opts)),
|
|
2037
|
-
main,
|
|
2038
|
-
script: opts.screenshot ? "" : uiScript(store, opts, undefined, false)
|
|
2039
|
-
});
|
|
2040
|
-
}
|
|
2041
|
-
function channelMessages(store, channel) {
|
|
2042
|
-
const history = store.history(channel.id, { limit: 1000 }).items;
|
|
2043
|
-
const ephemerals = store.ephemerals.filter((e) => e.channel === channel.id).map((e) => ({ ...e, _ephemeral: true }));
|
|
2044
|
-
return [...history, ...ephemerals].sort((a, b) => Number(a.ts) - Number(b.ts));
|
|
2045
|
-
}
|
|
2046
|
-
function channelColumn(store, ctx, opts, channel, openTs) {
|
|
2047
|
-
const messages = channelMessages(store, channel);
|
|
2048
|
-
if (!messages.length)
|
|
2049
|
-
return `<div class="sm-empty">No messages yet</div>`;
|
|
2050
|
-
return renderMessageList(store, ctx, messages, opts, {
|
|
2051
|
-
threadLink: true,
|
|
2052
|
-
dividers: true,
|
|
2053
|
-
openTs
|
|
2054
|
-
});
|
|
2055
|
-
}
|
|
2056
|
-
function channelHeader(store, opts, channel) {
|
|
2057
|
-
const sub = [channel.topic.value, channel.purpose.value].filter(Boolean).join(" \xB7 ");
|
|
2058
|
-
const back = opts.screenshot ? undefined : { href: homeHref(opts), label: "\u2190 Workspace" };
|
|
2059
|
-
return topbar(escapeHtml(channelLabel(store, channel)), escapeHtml(sub), back, identityChip(store, opts));
|
|
2060
|
-
}
|
|
2061
|
-
function humanUsers(store) {
|
|
2062
|
-
return [...store.users.values()].filter((u) => !u.is_bot && !u.deleted);
|
|
2063
|
-
}
|
|
2064
|
-
function userStatus(user) {
|
|
2065
|
-
return user.profile.status_text ? `<span class="sm-user-status">${escapeHtml(user.profile.status_text)}</span>` : "";
|
|
2066
|
-
}
|
|
2067
|
-
function identityChip(store, opts) {
|
|
2068
|
-
if (opts.screenshot)
|
|
2069
|
-
return "";
|
|
2070
|
-
const users = humanUsers(store);
|
|
2071
|
-
const selected = users[0];
|
|
2072
|
-
if (!selected)
|
|
2073
|
-
return "";
|
|
2074
|
-
const name = selected.real_name || selected.name;
|
|
2075
|
-
const avatar = `<span class="sm-identity-avatar" style="background:${AVATAR_COLORS[hashIndex(selected.id, AVATAR_COLORS.length)]}">${escapeHtml(initials(name))}</span>`;
|
|
2076
|
-
const items = users.map((u) => {
|
|
2077
|
-
const real = u.real_name || u.name;
|
|
2078
|
-
const color = AVATAR_COLORS[hashIndex(u.id, AVATAR_COLORS.length)];
|
|
2079
|
-
return `<li><button type="button" data-user="${escapeHtml(u.id)}"><span class="sm-identity-avatar" style="background:${color}">${escapeHtml(initials(real))}</span><span>${escapeHtml(real)}${userStatus(u)}</span><span class="sm-identity-user">@${escapeHtml(u.name)}</span></button></li>`;
|
|
2080
|
-
}).join("");
|
|
2081
|
-
const hidden = opts.writeGated ? " hidden" : "";
|
|
2082
|
-
return `<details class="sm-identity"${hidden}><summary title="Change posting identity" aria-label="Posting as ${escapeHtml(name)}">${avatar}<span class="sm-identity-name">${escapeHtml(name)}</span></summary><ul class="sm-identity-list">${items}</ul></details>`;
|
|
2083
2090
|
}
|
|
2084
|
-
function
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
if (
|
|
2089
|
-
|
|
2090
|
-
const
|
|
2091
|
-
const
|
|
2092
|
-
const
|
|
2093
|
-
const
|
|
2094
|
-
return
|
|
2091
|
+
function paginate(all, q) {
|
|
2092
|
+
let items = all;
|
|
2093
|
+
if (q.oldest)
|
|
2094
|
+
items = items.filter((m) => q.inclusive ? Number(m.ts) >= Number(q.oldest) : Number(m.ts) > Number(q.oldest));
|
|
2095
|
+
if (q.latest)
|
|
2096
|
+
items = items.filter((m) => q.inclusive ? Number(m.ts) <= Number(q.latest) : Number(m.ts) < Number(q.latest));
|
|
2097
|
+
const start = q.cursor ? Number.parseInt(Buffer.from(q.cursor, "base64").toString("utf8").replace("offset:", ""), 10) || 0 : 0;
|
|
2098
|
+
const limit = Math.max(1, Math.min(q.limit ?? 100, 1000));
|
|
2099
|
+
const page = items.slice(start, start + limit);
|
|
2100
|
+
const has_more = start + limit < items.length;
|
|
2101
|
+
return {
|
|
2102
|
+
items: page,
|
|
2103
|
+
has_more,
|
|
2104
|
+
next_cursor: has_more ? Buffer.from(`offset:${start + limit}`).toString("base64") : ""
|
|
2105
|
+
};
|
|
2095
2106
|
}
|
|
2096
|
-
function
|
|
2097
|
-
const
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
+
function guessMime(ext) {
|
|
2108
|
+
const map = {
|
|
2109
|
+
png: "image/png",
|
|
2110
|
+
jpg: "image/jpeg",
|
|
2111
|
+
jpeg: "image/jpeg",
|
|
2112
|
+
gif: "image/gif",
|
|
2113
|
+
webp: "image/webp",
|
|
2114
|
+
svg: "image/svg+xml",
|
|
2115
|
+
txt: "text/plain",
|
|
2116
|
+
md: "text/markdown",
|
|
2117
|
+
log: "text/plain",
|
|
2118
|
+
json: "application/json",
|
|
2119
|
+
csv: "text/csv",
|
|
2120
|
+
pdf: "application/pdf",
|
|
2121
|
+
html: "text/html",
|
|
2122
|
+
zip: "application/zip"
|
|
2123
|
+
};
|
|
2124
|
+
return map[ext] ?? "application/octet-stream";
|
|
2107
2125
|
}
|
|
2108
|
-
function
|
|
2109
|
-
const
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
fitViewport();addEventListener("resize",fitViewport);
|
|
2114
|
-
if(viewport){viewport.addEventListener("resize",fitViewport);viewport.addEventListener("scroll",fitViewport)}
|
|
2115
|
-
document.addEventListener("change",function(e){if(e.target.classList.contains("sm-channel-switch"))location.href=e.target.value});
|
|
2116
|
-
var W="sm-panel-w",DEF=${JSON.stringify(panelWidth(opts))},FIXED=${opts.panelWidth ? "true" : "false"};
|
|
2117
|
-
${autoScroll ? `function scroll(){document.querySelectorAll(".sm-scroll").forEach(function(el){el.scrollTop=el.scrollHeight})}
|
|
2118
|
-
scroll();addEventListener("load",scroll);` : ""}
|
|
2119
|
-
var handle=document.querySelector(".sm-resize");
|
|
2120
|
-
if(handle){
|
|
2121
|
-
var saved=null;try{saved=localStorage.getItem(W)}catch(e){}
|
|
2122
|
-
if(!FIXED&&saved)document.body.style.setProperty("--sm-panel-w",saved);
|
|
2123
|
-
var dragging=false;
|
|
2124
|
-
handle.addEventListener("mousedown",function(e){dragging=true;handle.classList.add("sm-dragging");e.preventDefault()});
|
|
2125
|
-
document.addEventListener("mousemove",function(e){if(!dragging)return;var w=Math.round(Math.min(Math.max(innerWidth-e.clientX,360),Math.max(innerWidth-320,360)));document.body.style.setProperty("--sm-panel-w",w+"px")});
|
|
2126
|
-
document.addEventListener("mouseup",function(){if(!dragging)return;dragging=false;handle.classList.remove("sm-dragging");try{localStorage.setItem(W,document.body.style.getPropertyValue("--sm-panel-w"))}catch(e){}});
|
|
2127
|
-
handle.addEventListener("dblclick",function(){try{localStorage.removeItem(W)}catch(e){}document.body.style.setProperty("--sm-panel-w",DEF)});
|
|
2126
|
+
function wireMessage(m) {
|
|
2127
|
+
const { ephemeral_user: _e, ...rest } = m;
|
|
2128
|
+
if (rest.files)
|
|
2129
|
+
rest.files = rest.files.map(wireFile);
|
|
2130
|
+
return rest;
|
|
2128
2131
|
}
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
function esc(s){return String(s).replace(/[&<>"]/g,function(c){return c==="&"?"&":c==="<"?"<":c===">"?">":"""})}
|
|
2133
|
-
var GATED=${opts.writeGated ? "true" : "false"},LIVE=${opts.live !== false && !opts.refreshSec ? "true" : "false"},CK="sm-presenter",IK="sm-identity",currentIdentity="",identityStorage=true;
|
|
2134
|
-
function cred(){try{return localStorage.getItem(CK)||""}catch(e){return ""}}
|
|
2135
|
-
function setCred(v){try{if(v)localStorage.setItem(CK,v);else localStorage.removeItem(CK)}catch(e){}}
|
|
2136
|
-
function setIdentity(v){currentIdentity=v;try{if(v)localStorage.setItem(IK,v);else localStorage.removeItem(IK)}catch(e){identityStorage=false}}
|
|
2137
|
-
function findIdentity(value,key){for(var i=0;i<HUMANS.length;i++)if(HUMANS[i][key]===value)return HUMANS[i];return null}
|
|
2138
|
-
function identity(){var id=currentIdentity;if(identityStorage)try{id=localStorage.getItem(IK)||currentIdentity}catch(e){identityStorage=false}currentIdentity=id;var u=findIdentity(id,"id");if(!u&&HUMANS.length){u=HUMANS[0];setIdentity(u.id)}return u}
|
|
2139
|
-
(function(){var url=null,name=null;try{url=new URL(location.href);name=url.searchParams.get("as")}catch(e){}if(url&&name!==null){var u=findIdentity(name,"name");if(u)setIdentity(u.id);url.searchParams.delete("as");var search=url.searchParams.toString();history.replaceState(null,"",url.pathname+(search?"?"+search:"")+url.hash)}})();
|
|
2140
|
-
(function(){var m=/[#&]presenter=([^&]+)/.exec(location.hash||"");if(m){setCred(btoa(decodeURIComponent(m[1])));history.replaceState(null,"",location.pathname+location.search)}})();
|
|
2141
|
-
function authHeaders(h){var c=cred();if(c)h.authorization="Basic "+c;return h}
|
|
2142
|
-
var reactionDialog=document.querySelector(".sm-reaction-picker"),reactionTarget=null,reactionScope=null,reactionBusy=false;
|
|
2143
|
-
function closeReactions(restoreFocus){var button=reactionTarget&&reactionTarget.querySelector(".sm-add-reaction");if(reactionDialog&&reactionDialog.open)reactionDialog.close();reactionTarget=null;reactionScope=null;if(restoreFocus&&button&&button.isConnected&&!button.disabled)button.focus({preventScroll:true})}
|
|
2144
|
-
function findMessageElement(scope,ts){var found=null;if(scope)scope.querySelectorAll(".sm-msg[data-ts]").forEach(function(message){if(message.dataset.ts===ts)found=message});return found}
|
|
2145
|
-
function positionReactions(){
|
|
2146
|
-
if(!reactionDialog||!reactionDialog.open||!reactionTarget)return;
|
|
2147
|
-
if(!reactionTarget.isConnected){reactionTarget=findMessageElement(reactionScope,reactionTarget.dataset.ts);if(!reactionTarget){closeReactions();return}}
|
|
2148
|
-
var button=reactionTarget.querySelector(".sm-add-reaction"),anchor=button.getBoundingClientRect(),v=window.visualViewport,left=v?v.offsetLeft:0,top=v?v.offsetTop:0,width=v?v.width:innerWidth,height=v?v.height:innerHeight,scope=reactionScope.getBoundingClientRect(),pad=12,gap=8;
|
|
2149
|
-
if(anchor.bottom<=Math.max(top,scope.top)||anchor.top>=Math.min(top+height,scope.bottom)){closeReactions();return}
|
|
2150
|
-
reactionDialog.style.maxWidth=Math.max(0,width-pad*2)+"px";reactionDialog.style.maxHeight=Math.max(0,height-pad*2)+"px";
|
|
2151
|
-
var rect=reactionDialog.getBoundingClientRect(),x=Math.max(left+pad,Math.min(anchor.right-rect.width,left+width-rect.width-pad)),y=anchor.bottom+gap;
|
|
2152
|
-
if(y+rect.height>top+height-pad)y=anchor.top-rect.height-gap;
|
|
2153
|
-
reactionDialog.style.left=x+"px";reactionDialog.style.top=Math.max(top+pad,Math.min(y,top+height-rect.height-pad))+"px";
|
|
2132
|
+
function wireFile(f) {
|
|
2133
|
+
const { bytes: _b, ...rest } = f;
|
|
2134
|
+
return rest;
|
|
2154
2135
|
}
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
document.addEventListener("click",function(e){var pick=e.target.closest(".sm-identity-list button");if(pick){setIdentity(pick.dataset.user);identityUi();pick.closest(".sm-identity").open=false;return}if(!e.target.closest(".sm-identity"))document.querySelectorAll(".sm-identity[open]").forEach(function(chip){chip.open=false});var out=e.target.closest(".sm-lock-out");if(out){e.preventDefault();setCred("");lockUi();return}var go=e.target.closest(".sm-lock-go");if(go)signIn(go.closest(".sm-composer"))});
|
|
2166
|
-
document.addEventListener("keydown",function(e){if(e.key!=="Escape")return;var chip=document.querySelector(".sm-identity[open]");if(!chip)return;e.preventDefault();e.stopImmediatePropagation();chip.open=false;chip.querySelector("summary").focus()});
|
|
2167
|
-
document.addEventListener("keydown",function(e){if(e.key==="Escape"&&reactionDialog&&reactionDialog.open){e.preventDefault();e.stopImmediatePropagation();closeReactions(true)}});
|
|
2168
|
-
document.addEventListener("keydown",function(e){var c=e.target.classList;if(e.key==="Enter"&&c&&(c.contains("sm-lock-pass")||c.contains("sm-lock-user"))){e.preventDefault();signIn(e.target.closest(".sm-composer"))}});
|
|
2169
|
-
addEventListener("storage",function(e){if(e.key===IK){currentIdentity=e.newValue||"";identityUi()}});
|
|
2170
|
-
lockUi();
|
|
2171
|
-
var pending=null;
|
|
2172
|
-
function refresh(){if(pending)return;pending=setTimeout(function(){pending=null;fetch(location.href,{headers:{accept:"text/html"},credentials:"same-origin"}).then(function(r){return r.ok?r.text():Promise.reject(r.status)}).then(apply).catch(function(){reactionUi()})},120)}
|
|
2173
|
-
function apply(html){
|
|
2174
|
-
var doc=new DOMParser().parseFromString(html,"text/html"),opener=document.activeElement,focusedMessage=opener&&opener.classList.contains("sm-add-reaction")?opener.closest(".sm-msg"):null,focusedScope=focusedMessage&&focusedMessage.closest(".sm-page,.sm-panel-body");
|
|
2175
|
-
reactionUi();reactionUi(doc);[".sm-side",".sm-channel-switch",".sm-page",".sm-panel-body"].forEach(function(sel){var cur=document.querySelector(sel),nxt=doc.querySelector(sel);if(!cur||!nxt)return;var seen={};cur.querySelectorAll(".sm-msg[data-ts]").forEach(function(el){el.classList.remove("sm-msg-new","sm-msg-changed");seen[el.dataset.ts]=el.outerHTML});var atBottom=cur.scrollHeight-cur.scrollTop-cur.clientHeight<80;cur.innerHTML=nxt.innerHTML;cur.querySelectorAll(".sm-msg[data-ts]").forEach(function(el){var prev=seen[el.dataset.ts];if(prev===undefined)el.classList.add("sm-msg-new");else if(prev!==el.outerHTML)el.classList.add("sm-msg-changed")});if(atBottom)cur.scrollTop=cur.scrollHeight});identityUi();positionReactions();if(doc.title)document.title=doc.title;
|
|
2176
|
-
if(focusedMessage&&!opener.isConnected){var message=findMessageElement(focusedScope,focusedMessage.dataset.ts);if(message)message.querySelector(".sm-add-reaction").focus({preventScroll:true})}
|
|
2136
|
+
function stripBytes(change) {
|
|
2137
|
+
if (change.kind === "file.add")
|
|
2138
|
+
return { ...change, file: wireFile(change.file) };
|
|
2139
|
+
if ("message" in change && change.message.files) {
|
|
2140
|
+
return {
|
|
2141
|
+
...change,
|
|
2142
|
+
message: { ...change.message, files: change.message.files.map(wireFile) }
|
|
2143
|
+
};
|
|
2144
|
+
}
|
|
2145
|
+
return change;
|
|
2177
2146
|
}
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
if(chan){
|
|
2181
|
-
var h1=document.querySelector(".sm-top h1"),dot=null;
|
|
2182
|
-
if(h1){dot=document.createElement("span");dot.className="sm-live";dot.textContent="\u25CF live";h1.appendChild(dot)}
|
|
2183
|
-
var es=new EventSource("/c/"+encodeURIComponent(chan)+"/events"),down=false;
|
|
2184
|
-
es.addEventListener("change",refresh);
|
|
2185
|
-
es.onopen=function(){if(dot){dot.textContent="\u25CF live";dot.classList.remove("sm-live-off")}if(down){down=false;refresh()}};
|
|
2186
|
-
es.onerror=function(){down=true;if(dot){dot.textContent="\u25CF reconnecting";dot.classList.add("sm-live-off")}};
|
|
2187
|
-
}
|
|
2147
|
+
function ensureDir(file) {
|
|
2148
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
2188
2149
|
}
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2150
|
+
|
|
2151
|
+
// src/events.ts
|
|
2152
|
+
var VERIFICATION_TOKEN = "mock-verification-token";
|
|
2153
|
+
function eventCallback(store, event) {
|
|
2154
|
+
return {
|
|
2155
|
+
token: VERIFICATION_TOKEN,
|
|
2156
|
+
team_id: store.team.id,
|
|
2157
|
+
context_team_id: store.team.id,
|
|
2158
|
+
context_enterprise_id: null,
|
|
2159
|
+
api_app_id: store.app.id,
|
|
2160
|
+
event,
|
|
2161
|
+
type: "event_callback",
|
|
2162
|
+
event_id: slackId("Ev", 9),
|
|
2163
|
+
event_time: nowUnix(),
|
|
2164
|
+
authorizations: [
|
|
2165
|
+
{
|
|
2166
|
+
enterprise_id: null,
|
|
2167
|
+
team_id: store.team.id,
|
|
2168
|
+
user_id: store.bot.userId,
|
|
2169
|
+
is_bot: true,
|
|
2170
|
+
is_enterprise_install: false
|
|
2171
|
+
}
|
|
2172
|
+
],
|
|
2173
|
+
is_ext_shared_channel: false,
|
|
2174
|
+
event_context: Buffer.from(`${store.team.id}:${store.app.id}:${event.channel ?? ""}`).toString("base64")
|
|
2175
|
+
};
|
|
2198
2176
|
}
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
});
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
if(
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
menu.style.display="block";
|
|
2219
|
-
}
|
|
2220
|
-
function pick(i){var u=items[i];if(!u)return;var pos=ta.selectionStart,before=ta.value.slice(0,pos).replace(/@\\w*$/,"@"+u.name+" "),after=ta.value.slice(pos);ta.value=before+after;ta.selectionStart=ta.selectionEnd=before.length;close();ta.focus()}
|
|
2221
|
-
ta.addEventListener("input",show);
|
|
2222
|
-
ta.addEventListener("blur",function(){setTimeout(close,150)});
|
|
2223
|
-
menu.addEventListener("mousedown",function(e){var el=e.target.closest(".sm-mention");if(el){e.preventDefault();pick(Number(el.dataset.i))}});
|
|
2224
|
-
ta.addEventListener("keydown",function(e){
|
|
2225
|
-
if(e.key==="Backspace"&&ta.selectionStart===ta.selectionEnd&&ta.selectionStart>0){
|
|
2226
|
-
var pos=ta.selectionStart,before=ta.value.slice(0,pos),hit="";
|
|
2227
|
-
for(var k=0;k<PEOPLE.length;k++){var tok="@"+PEOPLE[k].name;var cand=before.endsWith(tok)?tok:"";if(cand&&cand.length>hit.length){var at=before.length-cand.length;if(at===0||/s/.test(before.charAt(at-1)))hit=cand}}
|
|
2228
|
-
if(hit){e.preventDefault();ta.value=before.slice(0,before.length-hit.length)+ta.value.slice(pos);ta.selectionStart=ta.selectionEnd=pos-hit.length;close();return}
|
|
2229
|
-
}
|
|
2230
|
-
if(items.length){
|
|
2231
|
-
if(e.key==="ArrowDown"){e.preventDefault();active=(active+1)%items.length;paint();return}
|
|
2232
|
-
if(e.key==="ArrowUp"){e.preventDefault();active=(active-1+items.length)%items.length;paint();return}
|
|
2233
|
-
if(e.key==="Enter"||e.key==="Tab"){e.preventDefault();pick(active);return}
|
|
2234
|
-
if(e.key==="Escape"){e.preventDefault();e.stopPropagation();close();return}
|
|
2177
|
+
function messageEvent(store, m, channel) {
|
|
2178
|
+
const {
|
|
2179
|
+
channel: _c,
|
|
2180
|
+
team,
|
|
2181
|
+
...rest
|
|
2182
|
+
} = wireMessage(m);
|
|
2183
|
+
const event = {
|
|
2184
|
+
...rest,
|
|
2185
|
+
type: "message",
|
|
2186
|
+
channel: m.channel,
|
|
2187
|
+
event_ts: m.ts,
|
|
2188
|
+
channel_type: store.channelType(channel),
|
|
2189
|
+
team
|
|
2190
|
+
};
|
|
2191
|
+
if (m.files?.length && !m.subtype)
|
|
2192
|
+
event.subtype = "file_share";
|
|
2193
|
+
if (m.files?.length) {
|
|
2194
|
+
event.upload = false;
|
|
2195
|
+
event.display_as_bot = false;
|
|
2235
2196
|
}
|
|
2236
|
-
|
|
2237
|
-
});
|
|
2238
|
-
box.querySelector(".sm-composer-send").addEventListener("click",function(){send(box)});
|
|
2239
|
-
});
|
|
2240
|
-
})();`;
|
|
2241
|
-
return `${mentionData(store)}${reactionPicker()}<script>${body}${esc}</script>`;
|
|
2197
|
+
return event;
|
|
2242
2198
|
}
|
|
2243
|
-
function
|
|
2244
|
-
const
|
|
2245
|
-
|
|
2246
|
-
if (!channel)
|
|
2247
|
-
return notFound(store, opts, "Channel not found");
|
|
2248
|
-
const label = channelLabel(store, channel);
|
|
2249
|
-
return shell(store, opts, {
|
|
2250
|
-
title: `${label} \xB7 slack-mock`,
|
|
2251
|
-
header: channelHeader(store, opts, channel),
|
|
2252
|
-
main: channelColumn(store, ctx, opts, channel),
|
|
2253
|
-
active: channel.id,
|
|
2254
|
-
footer: composer(store, opts, channel),
|
|
2255
|
-
script: opts.screenshot ? "" : uiScript(store, opts)
|
|
2256
|
-
});
|
|
2199
|
+
function appMentionEvent(store, m) {
|
|
2200
|
+
const { channel_type: _ct, ...event } = messageEvent(store, m, store.channel(m.channel));
|
|
2201
|
+
return { ...event, type: "app_mention" };
|
|
2257
2202
|
}
|
|
2258
|
-
function
|
|
2259
|
-
const
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
}
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
}
|
|
2278
|
-
if (!parts.length)
|
|
2279
|
-
return "";
|
|
2280
|
-
return `<div class="sm-assist">${parts.join("")}</div>`;
|
|
2203
|
+
function messageChangedEvent(store, m, previous) {
|
|
2204
|
+
const channel = store.channel(m.channel);
|
|
2205
|
+
const ts = nextTs();
|
|
2206
|
+
return {
|
|
2207
|
+
type: "message",
|
|
2208
|
+
subtype: "message_changed",
|
|
2209
|
+
channel: m.channel,
|
|
2210
|
+
channel_type: store.channelType(channel),
|
|
2211
|
+
hidden: true,
|
|
2212
|
+
ts,
|
|
2213
|
+
event_ts: ts,
|
|
2214
|
+
message: {
|
|
2215
|
+
...wireMessage(m),
|
|
2216
|
+
channel: undefined,
|
|
2217
|
+
source_team: store.team.id,
|
|
2218
|
+
user_team: store.team.id
|
|
2219
|
+
},
|
|
2220
|
+
previous_message: { ...wireMessage(previous), channel: undefined }
|
|
2221
|
+
};
|
|
2281
2222
|
}
|
|
2282
|
-
function
|
|
2283
|
-
const channel = store.
|
|
2284
|
-
const
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
const header = topbar(`Thread <span class="sm-muted">\xB7</span> <a href="${channelHref(channel.id, opts)}">${escapeHtml(label)}</a>`, "");
|
|
2297
|
-
return shell(store, opts, {
|
|
2298
|
-
title: `Thread \xB7 ${label}`,
|
|
2299
|
-
header,
|
|
2300
|
-
main: body,
|
|
2301
|
-
active: channel.id
|
|
2302
|
-
});
|
|
2303
|
-
}
|
|
2304
|
-
const close = channelHref(channel.id, opts);
|
|
2305
|
-
const title = `Thread \xB7 ${label}`;
|
|
2306
|
-
if (opts.threadView === "full") {
|
|
2307
|
-
const header = topbar(`Thread <span class="sm-muted">\xB7</span> ${escapeHtml(label)}`, "", { href: close, label: `\u2190 ${label}` }, `<a class="sm-icon-btn" href="${threadHref(channel.id, ts, opts, { full: false })}" title="Collapse to panel" aria-label="Collapse to panel">\u2921</a>${identityChip(store, opts)}`);
|
|
2308
|
-
return shell(store, opts, {
|
|
2309
|
-
title,
|
|
2310
|
-
header,
|
|
2311
|
-
main: body,
|
|
2312
|
-
active: channel.id,
|
|
2313
|
-
footer: composer(store, opts, channel, ts),
|
|
2314
|
-
script: uiScript(store, opts, close)
|
|
2315
|
-
});
|
|
2316
|
-
}
|
|
2317
|
-
const expand = threadHref(channel.id, ts, opts, { full: true });
|
|
2318
|
-
const panel = `<aside class="sm-panel"><div class="sm-panel-top"><a class="sm-panel-back" href="${close}" aria-label="Back to ${escapeHtml(label)}">\u2190</a><div class="sm-panel-heading"><h2 class="sm-panel-title">Thread</h2><div class="sm-panel-sub">${escapeHtml(label)}</div></div><a class="sm-panel-expand" href="${expand}" title="Expand thread">\u2922</a><a class="sm-panel-close" href="${close}" title="Close thread">\xD7</a></div><div class="sm-panel-body sm-scroll">${body}</div>${composer(store, opts, channel, ts)}</aside>`;
|
|
2319
|
-
return shell(store, opts, {
|
|
2320
|
-
title,
|
|
2321
|
-
header: channelHeader(store, opts, channel),
|
|
2322
|
-
main: channelColumn(store, ctx, opts, channel, ts),
|
|
2323
|
-
active: channel.id,
|
|
2324
|
-
panel,
|
|
2325
|
-
footer: composer(store, opts, channel),
|
|
2326
|
-
script: uiScript(store, opts, close)
|
|
2327
|
-
});
|
|
2223
|
+
function messageDeletedEvent(store, m) {
|
|
2224
|
+
const channel = store.channel(m.channel);
|
|
2225
|
+
const ts = nextTs();
|
|
2226
|
+
return {
|
|
2227
|
+
type: "message",
|
|
2228
|
+
subtype: "message_deleted",
|
|
2229
|
+
channel: m.channel,
|
|
2230
|
+
channel_type: store.channelType(channel),
|
|
2231
|
+
hidden: true,
|
|
2232
|
+
deleted_ts: m.ts,
|
|
2233
|
+
ts,
|
|
2234
|
+
event_ts: ts,
|
|
2235
|
+
previous_message: { ...wireMessage(m), channel: undefined }
|
|
2236
|
+
};
|
|
2328
2237
|
}
|
|
2329
|
-
function
|
|
2330
|
-
return
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2238
|
+
function reactionEvent(kind, m, name, user) {
|
|
2239
|
+
return {
|
|
2240
|
+
type: kind,
|
|
2241
|
+
user,
|
|
2242
|
+
reaction: name,
|
|
2243
|
+
item_user: m.user,
|
|
2244
|
+
item: { type: "message", channel: m.channel, ts: m.ts },
|
|
2245
|
+
event_ts: nextTs()
|
|
2246
|
+
};
|
|
2335
2247
|
}
|
|
2336
|
-
function
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2248
|
+
function memberJoinedEvent(store, channel, user, inviter) {
|
|
2249
|
+
return {
|
|
2250
|
+
type: "member_joined_channel",
|
|
2251
|
+
user,
|
|
2252
|
+
channel: channel.id,
|
|
2253
|
+
channel_type: store.channelType(channel) === "channel" ? "C" : "G",
|
|
2254
|
+
team: store.team.id,
|
|
2255
|
+
inviter,
|
|
2256
|
+
event_ts: nextTs()
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
function assistantThreadStartedEvent(store, dm, user, threadTs, context = {}) {
|
|
2260
|
+
return {
|
|
2261
|
+
type: "assistant_thread_started",
|
|
2262
|
+
assistant_thread: {
|
|
2263
|
+
user_id: user,
|
|
2264
|
+
context: { team_id: store.team.id, enterprise_id: null, ...context },
|
|
2265
|
+
channel_id: dm.id,
|
|
2266
|
+
thread_ts: threadTs
|
|
2267
|
+
},
|
|
2268
|
+
event_ts: nextTs()
|
|
2269
|
+
};
|
|
2270
|
+
}
|
|
2271
|
+
function assistantThreadContextChangedEvent(store, dm, user, threadTs, context) {
|
|
2272
|
+
return {
|
|
2273
|
+
type: "assistant_thread_context_changed",
|
|
2274
|
+
assistant_thread: {
|
|
2275
|
+
user_id: user,
|
|
2276
|
+
context: { team_id: store.team.id, enterprise_id: null, ...context },
|
|
2277
|
+
channel_id: dm.id,
|
|
2278
|
+
thread_ts: threadTs
|
|
2279
|
+
},
|
|
2280
|
+
event_ts: nextTs()
|
|
2281
|
+
};
|
|
2282
|
+
}
|
|
2283
|
+
function slashCommandPayload(store, input) {
|
|
2284
|
+
return {
|
|
2285
|
+
token: VERIFICATION_TOKEN,
|
|
2286
|
+
team_id: store.team.id,
|
|
2287
|
+
team_domain: store.team.domain,
|
|
2288
|
+
channel_id: input.channel.id,
|
|
2289
|
+
channel_name: input.channel.is_im ? "directmessage" : input.channel.name,
|
|
2290
|
+
user_id: input.user.id,
|
|
2291
|
+
user_name: input.user.name,
|
|
2292
|
+
command: input.command,
|
|
2293
|
+
text: input.text,
|
|
2294
|
+
api_app_id: store.app.id,
|
|
2295
|
+
is_enterprise_install: "false",
|
|
2296
|
+
response_url: input.responseUrl,
|
|
2297
|
+
trigger_id: input.triggerId
|
|
2298
|
+
};
|
|
2299
|
+
}
|
|
2300
|
+
function blockActionsPayload(store, input) {
|
|
2301
|
+
return {
|
|
2302
|
+
type: "block_actions",
|
|
2303
|
+
user: {
|
|
2304
|
+
id: input.user.id,
|
|
2305
|
+
username: input.user.name,
|
|
2306
|
+
name: input.user.name,
|
|
2307
|
+
team_id: store.team.id
|
|
2308
|
+
},
|
|
2309
|
+
api_app_id: store.app.id,
|
|
2310
|
+
token: VERIFICATION_TOKEN,
|
|
2311
|
+
container: {
|
|
2312
|
+
type: "message",
|
|
2313
|
+
message_ts: input.message.ts,
|
|
2314
|
+
channel_id: input.channel.id,
|
|
2315
|
+
is_ephemeral: !!input.message.ephemeral_user,
|
|
2316
|
+
...input.message.thread_ts ? { thread_ts: input.message.thread_ts } : {}
|
|
2317
|
+
},
|
|
2318
|
+
trigger_id: input.triggerId,
|
|
2319
|
+
team: { id: store.team.id, domain: store.team.domain },
|
|
2320
|
+
enterprise: null,
|
|
2321
|
+
is_enterprise_install: false,
|
|
2322
|
+
channel: { id: input.channel.id, name: input.channel.name },
|
|
2323
|
+
message: wireMessage(input.message),
|
|
2324
|
+
state: { values: {} },
|
|
2325
|
+
response_url: input.responseUrl,
|
|
2326
|
+
actions: input.actions.map((a) => ({ action_ts: nextTs(), ...a }))
|
|
2327
|
+
};
|
|
2328
|
+
}
|
|
2329
|
+
function viewSubmissionPayload(store, input) {
|
|
2330
|
+
const { opened_by: _o, trigger_id: _t, ...view } = input.view;
|
|
2331
|
+
return {
|
|
2332
|
+
type: "view_submission",
|
|
2333
|
+
team: { id: store.team.id, domain: store.team.domain },
|
|
2334
|
+
user: {
|
|
2335
|
+
id: input.user.id,
|
|
2336
|
+
username: input.user.name,
|
|
2337
|
+
name: input.user.name,
|
|
2338
|
+
team_id: store.team.id
|
|
2339
|
+
},
|
|
2340
|
+
api_app_id: store.app.id,
|
|
2341
|
+
token: VERIFICATION_TOKEN,
|
|
2342
|
+
trigger_id: input.triggerId,
|
|
2343
|
+
view: { ...view, state: { values: input.values } },
|
|
2344
|
+
response_urls: [],
|
|
2345
|
+
is_enterprise_install: false,
|
|
2346
|
+
enterprise: null
|
|
2347
|
+
};
|
|
2345
2348
|
}
|
|
2346
2349
|
|
|
2347
2350
|
// src/socket-mode.ts
|
|
@@ -2520,7 +2523,7 @@ class SocketModeHub {
|
|
|
2520
2523
|
|
|
2521
2524
|
// src/web-api.ts
|
|
2522
2525
|
function required(args, key) {
|
|
2523
|
-
const v =
|
|
2526
|
+
const v = str2(args, key);
|
|
2524
2527
|
if (!v)
|
|
2525
2528
|
throw new SlackApiError2("invalid_arguments", {
|
|
2526
2529
|
response_metadata: { messages: [`[ERROR] missing required field: ${key}`] }
|
|
@@ -2557,8 +2560,16 @@ function botFields(ctx) {
|
|
|
2557
2560
|
}
|
|
2558
2561
|
};
|
|
2559
2562
|
}
|
|
2563
|
+
function markdownText(args) {
|
|
2564
|
+
const markdown = str2(args, "markdown_text");
|
|
2565
|
+
if (markdown === undefined)
|
|
2566
|
+
return;
|
|
2567
|
+
if (str2(args, "text") || Array.isArray(args.blocks) && args.blocks.length)
|
|
2568
|
+
throw new SlackApiError2("markdown_text_conflict");
|
|
2569
|
+
return markdown;
|
|
2570
|
+
}
|
|
2560
2571
|
function messageContent(args) {
|
|
2561
|
-
const text =
|
|
2572
|
+
const text = markdownText(args) ?? str2(args, "text") ?? "";
|
|
2562
2573
|
const blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2563
2574
|
const attachments = Array.isArray(args.attachments) ? args.attachments : undefined;
|
|
2564
2575
|
if (!text && !blocks?.length && !attachments?.length)
|
|
@@ -2627,18 +2638,18 @@ var handlers = {
|
|
|
2627
2638
|
"chat.postMessage": (ctx, args) => {
|
|
2628
2639
|
const channel = resolveChannel(ctx.store, required(args, "channel"));
|
|
2629
2640
|
requireMember(channel, ctx.actor);
|
|
2630
|
-
const threadTs =
|
|
2641
|
+
const threadTs = str2(args, "thread_ts");
|
|
2631
2642
|
const message = ctx.store.addMessage({
|
|
2632
2643
|
channel: channel.id,
|
|
2633
2644
|
user: ctx.actor.userId,
|
|
2634
2645
|
...messageContent(args),
|
|
2635
2646
|
...threadTs ? { thread_ts: threadTs } : {},
|
|
2636
2647
|
...bool(args, "reply_broadcast") && threadTs ? { subtype: "thread_broadcast" } : {},
|
|
2637
|
-
...
|
|
2638
|
-
...
|
|
2648
|
+
...str2(args, "username") ? { username: str2(args, "username") } : {},
|
|
2649
|
+
...str2(args, "icon_emoji") || str2(args, "icon_url") ? {
|
|
2639
2650
|
icons: {
|
|
2640
|
-
...
|
|
2641
|
-
...
|
|
2651
|
+
...str2(args, "icon_emoji") ? { emoji: str2(args, "icon_emoji") } : {},
|
|
2652
|
+
...str2(args, "icon_url") ? { image_64: str2(args, "icon_url") } : {}
|
|
2642
2653
|
}
|
|
2643
2654
|
} : {},
|
|
2644
2655
|
...botFields(ctx)
|
|
@@ -2649,7 +2660,7 @@ var handlers = {
|
|
|
2649
2660
|
const channel = resolveChannel(ctx.store, required(args, "channel"));
|
|
2650
2661
|
const user = required(args, "user");
|
|
2651
2662
|
ctx.store.user(user);
|
|
2652
|
-
const threadTs =
|
|
2663
|
+
const threadTs = str2(args, "thread_ts");
|
|
2653
2664
|
const message = ctx.store.addMessage({
|
|
2654
2665
|
channel: channel.id,
|
|
2655
2666
|
user: ctx.actor.userId,
|
|
@@ -2667,8 +2678,11 @@ var handlers = {
|
|
|
2667
2678
|
if (existing.user !== ctx.actor.userId && !ctx.actor.isBot)
|
|
2668
2679
|
throw new SlackApiError2("cant_update_message");
|
|
2669
2680
|
const patch = {};
|
|
2670
|
-
|
|
2671
|
-
|
|
2681
|
+
const markdown = markdownText(args);
|
|
2682
|
+
if (markdown !== undefined)
|
|
2683
|
+
patch.text = markdown;
|
|
2684
|
+
else if (args.text !== undefined)
|
|
2685
|
+
patch.text = str2(args, "text") ?? "";
|
|
2672
2686
|
if (args.blocks !== undefined)
|
|
2673
2687
|
patch.blocks = Array.isArray(args.blocks) ? args.blocks : undefined;
|
|
2674
2688
|
if (args.attachments !== undefined)
|
|
@@ -2689,15 +2703,15 @@ var handlers = {
|
|
|
2689
2703
|
"chat.startStream": (ctx, args) => {
|
|
2690
2704
|
const channel = resolveChannel(ctx.store, required(args, "channel"));
|
|
2691
2705
|
requireMember(channel, ctx.actor);
|
|
2692
|
-
const threadTs =
|
|
2706
|
+
const threadTs = str2(args, "thread_ts");
|
|
2693
2707
|
const message = ctx.store.addMessage({
|
|
2694
2708
|
channel: channel.id,
|
|
2695
2709
|
user: ctx.actor.userId,
|
|
2696
2710
|
text: streamText(args),
|
|
2697
2711
|
streaming_state: "in_progress",
|
|
2698
2712
|
...threadTs ? { thread_ts: threadTs } : {},
|
|
2699
|
-
...
|
|
2700
|
-
...
|
|
2713
|
+
...str2(args, "username") ? { username: str2(args, "username") } : {},
|
|
2714
|
+
...str2(args, "icon_emoji") ? { icons: { emoji: str2(args, "icon_emoji") } } : {},
|
|
2701
2715
|
...botFields(ctx)
|
|
2702
2716
|
});
|
|
2703
2717
|
return { channel: channel.id, ts: message.ts };
|
|
@@ -2738,7 +2752,7 @@ var handlers = {
|
|
|
2738
2752
|
};
|
|
2739
2753
|
},
|
|
2740
2754
|
"conversations.list": (ctx, args) => {
|
|
2741
|
-
const types = new Set((
|
|
2755
|
+
const types = new Set((str2(args, "types") ?? "public_channel").split(",").map((t) => t.trim()));
|
|
2742
2756
|
const excludeArchived = bool(args, "exclude_archived") ?? false;
|
|
2743
2757
|
const all = [...ctx.store.channels.values()].filter((c) => {
|
|
2744
2758
|
if (excludeArchived && c.is_archived)
|
|
@@ -2752,7 +2766,7 @@ var handlers = {
|
|
|
2752
2766
|
return types.has("public_channel");
|
|
2753
2767
|
});
|
|
2754
2768
|
const limit = num(args, "limit", 100);
|
|
2755
|
-
const start = cursorOffset(
|
|
2769
|
+
const start = cursorOffset(str2(args, "cursor"));
|
|
2756
2770
|
const slice = all.slice(start, start + limit);
|
|
2757
2771
|
const hasMore = start + limit < all.length;
|
|
2758
2772
|
return {
|
|
@@ -2814,7 +2828,7 @@ var handlers = {
|
|
|
2814
2828
|
return { members: channel.members, response_metadata: { next_cursor: "" } };
|
|
2815
2829
|
},
|
|
2816
2830
|
"conversations.open": (ctx, args) => {
|
|
2817
|
-
const users = (
|
|
2831
|
+
const users = (str2(args, "users") ?? "").split(",").map((u) => u.trim()).filter(Boolean);
|
|
2818
2832
|
if (users.length !== 1)
|
|
2819
2833
|
throw new SlackApiError2("users_list_not_supplied");
|
|
2820
2834
|
const dm = ctx.store.openDm(users[0]);
|
|
@@ -2824,11 +2838,11 @@ var handlers = {
|
|
|
2824
2838
|
const channel = ctx.store.channel(required(args, "channel"));
|
|
2825
2839
|
requireMember(channel, ctx.actor);
|
|
2826
2840
|
const p = ctx.store.history(channel.id, {
|
|
2827
|
-
oldest:
|
|
2828
|
-
latest:
|
|
2841
|
+
oldest: str2(args, "oldest"),
|
|
2842
|
+
latest: str2(args, "latest"),
|
|
2829
2843
|
inclusive: bool(args, "inclusive"),
|
|
2830
2844
|
limit: num(args, "limit", 100),
|
|
2831
|
-
cursor:
|
|
2845
|
+
cursor: str2(args, "cursor")
|
|
2832
2846
|
});
|
|
2833
2847
|
return {
|
|
2834
2848
|
messages: p.items.map((m) => historyMessage(m, bool(args, "include_all_metadata"))),
|
|
@@ -2844,11 +2858,11 @@ var handlers = {
|
|
|
2844
2858
|
if (!ctx.store.findMessage(channel.id, ts))
|
|
2845
2859
|
throw new SlackApiError2("thread_not_found");
|
|
2846
2860
|
const p = ctx.store.replies(channel.id, ts, {
|
|
2847
|
-
oldest:
|
|
2848
|
-
latest:
|
|
2861
|
+
oldest: str2(args, "oldest"),
|
|
2862
|
+
latest: str2(args, "latest"),
|
|
2849
2863
|
inclusive: bool(args, "inclusive"),
|
|
2850
2864
|
limit: num(args, "limit", 1000),
|
|
2851
|
-
cursor:
|
|
2865
|
+
cursor: str2(args, "cursor")
|
|
2852
2866
|
});
|
|
2853
2867
|
return {
|
|
2854
2868
|
messages: p.items.map((m) => historyMessage(m, bool(args, "include_all_metadata"))),
|
|
@@ -2868,7 +2882,7 @@ var handlers = {
|
|
|
2868
2882
|
return { user };
|
|
2869
2883
|
},
|
|
2870
2884
|
"users.conversations": (ctx, args) => {
|
|
2871
|
-
const user =
|
|
2885
|
+
const user = str2(args, "user") ?? ctx.actor.userId;
|
|
2872
2886
|
const channels = [...ctx.store.channels.values()].filter((c) => c.members.includes(user) && !c.is_im);
|
|
2873
2887
|
return {
|
|
2874
2888
|
channels: channels.map((c) => channelInfo(ctx.store, c, ctx.actor)),
|
|
@@ -2905,7 +2919,7 @@ var handlers = {
|
|
|
2905
2919
|
ctx.pendingUploads.set(id, {
|
|
2906
2920
|
name: filename,
|
|
2907
2921
|
length,
|
|
2908
|
-
alt_text:
|
|
2922
|
+
alt_text: str2(args, "alt_text"),
|
|
2909
2923
|
user: ctx.actor.userId
|
|
2910
2924
|
});
|
|
2911
2925
|
return { upload_url: `${ctx.baseUrl}/upload/v1/${id}`, file_id: id };
|
|
@@ -2922,19 +2936,19 @@ var handlers = {
|
|
|
2922
2936
|
f.title = title;
|
|
2923
2937
|
return f;
|
|
2924
2938
|
});
|
|
2925
|
-
const channelId =
|
|
2939
|
+
const channelId = str2(args, "channel_id");
|
|
2926
2940
|
if (channelId) {
|
|
2927
2941
|
const channel = ctx.store.channel(channelId);
|
|
2928
2942
|
requireMember(channel, ctx.actor);
|
|
2929
2943
|
for (const f of files)
|
|
2930
2944
|
if (!f.channels.includes(channel.id))
|
|
2931
2945
|
f.channels.push(channel.id);
|
|
2932
|
-
const threadTs =
|
|
2946
|
+
const threadTs = str2(args, "thread_ts");
|
|
2933
2947
|
ctx.store.addMessage({
|
|
2934
2948
|
channel: channel.id,
|
|
2935
2949
|
user: ctx.actor.userId,
|
|
2936
2950
|
subtype: "file_share",
|
|
2937
|
-
text:
|
|
2951
|
+
text: str2(args, "initial_comment") ?? "",
|
|
2938
2952
|
files,
|
|
2939
2953
|
...threadTs ? { thread_ts: threadTs } : {},
|
|
2940
2954
|
...botFields(ctx)
|
|
@@ -2962,13 +2976,13 @@ var handlers = {
|
|
|
2962
2976
|
});
|
|
2963
2977
|
const stored = ctx.store.openView({
|
|
2964
2978
|
type: "modal",
|
|
2965
|
-
callback_id:
|
|
2966
|
-
private_metadata:
|
|
2979
|
+
callback_id: str2(view, "callback_id"),
|
|
2980
|
+
private_metadata: str2(view, "private_metadata") ?? "",
|
|
2967
2981
|
title: view.title,
|
|
2968
2982
|
submit: view.submit,
|
|
2969
2983
|
close: view.close,
|
|
2970
2984
|
blocks: Array.isArray(view.blocks) ? view.blocks : [],
|
|
2971
|
-
external_id:
|
|
2985
|
+
external_id: str2(view, "external_id"),
|
|
2972
2986
|
opened_by: "",
|
|
2973
2987
|
trigger_id: triggerId,
|
|
2974
2988
|
state: { values: {} },
|
|
@@ -2984,8 +2998,8 @@ var handlers = {
|
|
|
2984
2998
|
throw new SlackApiError2("not_found");
|
|
2985
2999
|
const view = viewArg(args);
|
|
2986
3000
|
Object.assign(existing, {
|
|
2987
|
-
callback_id:
|
|
2988
|
-
private_metadata:
|
|
3001
|
+
callback_id: str2(view, "callback_id") ?? existing.callback_id,
|
|
3002
|
+
private_metadata: str2(view, "private_metadata") ?? existing.private_metadata,
|
|
2989
3003
|
title: view.title ?? existing.title,
|
|
2990
3004
|
submit: view.submit ?? existing.submit,
|
|
2991
3005
|
close: view.close ?? existing.close,
|
|
@@ -3000,7 +3014,7 @@ var handlers = {
|
|
|
3000
3014
|
const channel = ctx.store.channel(required(args, "channel_id"));
|
|
3001
3015
|
const threadTs = required(args, "thread_ts");
|
|
3002
3016
|
const t = ctx.store.assistantThread(channel.id, threadTs);
|
|
3003
|
-
t.status =
|
|
3017
|
+
t.status = str2(args, "status") ?? "";
|
|
3004
3018
|
ctx.store.onChangeEmit({
|
|
3005
3019
|
kind: "assistant.status",
|
|
3006
3020
|
channel: channel.id,
|
|
@@ -3027,7 +3041,7 @@ var handlers = {
|
|
|
3027
3041
|
const threadTs = required(args, "thread_ts");
|
|
3028
3042
|
const t = ctx.store.assistantThread(channel.id, threadTs);
|
|
3029
3043
|
t.prompts = Array.isArray(args.prompts) ? args.prompts : [];
|
|
3030
|
-
t.promptsTitle =
|
|
3044
|
+
t.promptsTitle = str2(args, "title");
|
|
3031
3045
|
ctx.store.onChangeEmit({
|
|
3032
3046
|
kind: "assistant.prompts",
|
|
3033
3047
|
channel: channel.id,
|
|
@@ -3044,7 +3058,7 @@ var handlers = {
|
|
|
3044
3058
|
}
|
|
3045
3059
|
};
|
|
3046
3060
|
function streamText(args) {
|
|
3047
|
-
const md =
|
|
3061
|
+
const md = str2(args, "markdown_text");
|
|
3048
3062
|
if (md)
|
|
3049
3063
|
return md;
|
|
3050
3064
|
const chunks = Array.isArray(args.chunks) ? args.chunks : [];
|
|
@@ -4106,9 +4120,6 @@ function denied(req, path) {
|
|
|
4106
4120
|
}
|
|
4107
4121
|
|
|
4108
4122
|
// src/frames.ts
|
|
4109
|
-
import { copyFileSync, mkdirSync as mkdirSync2, mkdtempSync, readFileSync as readFileSync3, rmSync, writeFileSync } from "fs";
|
|
4110
|
-
import { tmpdir } from "os";
|
|
4111
|
-
import { join, resolve } from "path";
|
|
4112
4123
|
var FRAME_KINDS = new Set([
|
|
4113
4124
|
"message.add",
|
|
4114
4125
|
"message.update",
|