@desplega.ai/slack-mock 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +113 -0
- package/dist/body.d.ts +6 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +94 -0
- package/dist/events.d.ts +36 -0
- package/dist/ids.d.ts +8 -0
- package/dist/index-thgwpw70.js +3693 -0
- package/dist/index-v7mbr1cx.js +48 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +17 -0
- package/dist/render/blocks.d.ts +7 -0
- package/dist/render/index.d.ts +25 -0
- package/dist/render/mrkdwn.d.ts +16 -0
- package/dist/screenshot.d.ts +13 -0
- package/dist/screenshot.js +9 -0
- package/dist/server.d.ts +240 -0
- package/dist/socket-mode.d.ts +73 -0
- package/dist/store.d.ts +150 -0
- package/dist/types.d.ts +211 -0
- package/dist/web-api.d.ts +26 -0
- package/package.json +60 -0
- package/src/body.ts +84 -0
- package/src/cli.ts +89 -0
- package/src/events.ts +281 -0
- package/src/ids.ts +29 -0
- package/src/index.ts +13 -0
- package/src/render/blocks.ts +392 -0
- package/src/render/index.ts +846 -0
- package/src/render/mrkdwn.ts +321 -0
- package/src/screenshot.ts +59 -0
- package/src/server.ts +1198 -0
- package/src/socket-mode.ts +261 -0
- package/src/store.ts +654 -0
- package/src/types.ts +159 -0
- package/src/web-api.ts +694 -0
package/src/store.ts
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { nextTs, nowUnix, slackId } from "./ids.ts";
|
|
4
|
+
import type {
|
|
5
|
+
ApiCall,
|
|
6
|
+
Change,
|
|
7
|
+
SlackChannel,
|
|
8
|
+
SlackFile,
|
|
9
|
+
SlackMessage,
|
|
10
|
+
SlackReaction,
|
|
11
|
+
SlackUser,
|
|
12
|
+
SlackView,
|
|
13
|
+
} from "./types.ts";
|
|
14
|
+
|
|
15
|
+
export class SlackApiError extends Error {
|
|
16
|
+
constructor(
|
|
17
|
+
public code: string,
|
|
18
|
+
public extra: Record<string, unknown> = {},
|
|
19
|
+
) {
|
|
20
|
+
super(code);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StoreOptions {
|
|
25
|
+
/** Append every change as one JSON line to this file and replay it on start. */
|
|
26
|
+
dataFile?: string;
|
|
27
|
+
teamId?: string;
|
|
28
|
+
teamName?: string;
|
|
29
|
+
teamDomain?: string;
|
|
30
|
+
appId?: string;
|
|
31
|
+
appName?: string;
|
|
32
|
+
botUserId?: string;
|
|
33
|
+
botId?: string;
|
|
34
|
+
botToken?: string;
|
|
35
|
+
appToken?: string;
|
|
36
|
+
userToken?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AddUserInput {
|
|
40
|
+
id?: string;
|
|
41
|
+
name: string;
|
|
42
|
+
real_name?: string;
|
|
43
|
+
email?: string;
|
|
44
|
+
is_admin?: boolean;
|
|
45
|
+
is_bot?: boolean;
|
|
46
|
+
tz?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface AddChannelInput {
|
|
50
|
+
id?: string;
|
|
51
|
+
name: string;
|
|
52
|
+
is_private?: boolean;
|
|
53
|
+
members?: string[];
|
|
54
|
+
creator?: string;
|
|
55
|
+
topic?: string;
|
|
56
|
+
purpose?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface HistoryQuery {
|
|
60
|
+
oldest?: string;
|
|
61
|
+
latest?: string;
|
|
62
|
+
inclusive?: boolean;
|
|
63
|
+
limit?: number;
|
|
64
|
+
cursor?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface Page<T> {
|
|
68
|
+
items: T[];
|
|
69
|
+
has_more: boolean;
|
|
70
|
+
next_cursor: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** In-memory Slack workspace with an optional append-only JSONL journal. */
|
|
74
|
+
export class Store {
|
|
75
|
+
readonly team: { id: string; name: string; domain: string };
|
|
76
|
+
readonly app: { id: string; name: string };
|
|
77
|
+
readonly bot: {
|
|
78
|
+
userId: string;
|
|
79
|
+
botId: string;
|
|
80
|
+
token: string;
|
|
81
|
+
appToken: string;
|
|
82
|
+
userToken?: string;
|
|
83
|
+
};
|
|
84
|
+
readonly users = new Map<string, SlackUser>();
|
|
85
|
+
readonly channels = new Map<string, SlackChannel>();
|
|
86
|
+
readonly messages = new Map<string, SlackMessage[]>();
|
|
87
|
+
readonly files = new Map<string, SlackFile>();
|
|
88
|
+
readonly views = new Map<string, SlackView>();
|
|
89
|
+
/** Ephemeral messages (chat.postEphemeral, command responses). Not part of history. */
|
|
90
|
+
readonly ephemerals: SlackMessage[] = [];
|
|
91
|
+
readonly apiCalls: ApiCall[] = [];
|
|
92
|
+
readonly assistantThreads = new Map<
|
|
93
|
+
string,
|
|
94
|
+
{ status?: string; title?: string; prompts?: unknown[]; promptsTitle?: string }
|
|
95
|
+
>();
|
|
96
|
+
private listeners = new Set<(change: Change) => void>();
|
|
97
|
+
private dataFile?: string;
|
|
98
|
+
private replaying = false;
|
|
99
|
+
|
|
100
|
+
constructor(opts: StoreOptions = {}) {
|
|
101
|
+
this.team = {
|
|
102
|
+
id: opts.teamId ?? "T0MOCK0000",
|
|
103
|
+
name: opts.teamName ?? "Mock Workspace",
|
|
104
|
+
domain: opts.teamDomain ?? "mock",
|
|
105
|
+
};
|
|
106
|
+
this.app = { id: opts.appId ?? "A0MOCK0000", name: opts.appName ?? "mock-bot" };
|
|
107
|
+
this.bot = {
|
|
108
|
+
userId: opts.botUserId ?? "U0BOT00000",
|
|
109
|
+
botId: opts.botId ?? "B0BOT00000",
|
|
110
|
+
token: opts.botToken ?? "xoxb-mock-bot-token",
|
|
111
|
+
appToken: opts.appToken ?? "xapp-mock-app-token",
|
|
112
|
+
userToken: opts.userToken,
|
|
113
|
+
};
|
|
114
|
+
this.dataFile = opts.dataFile;
|
|
115
|
+
if (this.dataFile && existsSync(this.dataFile)) this.replay(this.dataFile);
|
|
116
|
+
if (!this.users.has(this.bot.userId)) {
|
|
117
|
+
this.addUser({
|
|
118
|
+
id: this.bot.userId,
|
|
119
|
+
name: this.app.name,
|
|
120
|
+
real_name: this.app.name,
|
|
121
|
+
is_bot: true,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---------------------------------------------------------------- events
|
|
127
|
+
|
|
128
|
+
onChange(listener: (change: Change) => void): () => void {
|
|
129
|
+
this.listeners.add(listener);
|
|
130
|
+
return () => this.listeners.delete(listener);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Emit a change that is not produced by a store mutation (e.g. assistant status). */
|
|
134
|
+
onChangeEmit(change: Change): void {
|
|
135
|
+
this.emit(change);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private emit(change: Change): void {
|
|
139
|
+
if (this.dataFile && !this.replaying && change.kind !== "api.call") {
|
|
140
|
+
appendFileSync(
|
|
141
|
+
this.dataFile,
|
|
142
|
+
`${JSON.stringify({ at: new Date().toISOString(), ...stripBytes(change) })}\n`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
for (const l of this.listeners) l(change);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private replay(file: string): void {
|
|
149
|
+
this.replaying = true;
|
|
150
|
+
try {
|
|
151
|
+
let lineNo = 0;
|
|
152
|
+
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
153
|
+
lineNo += 1;
|
|
154
|
+
if (!line.trim()) continue;
|
|
155
|
+
try {
|
|
156
|
+
this.applyReplayed(JSON.parse(line) as Change);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
console.warn(
|
|
159
|
+
`[slack-mock] skipping corrupt journal line ${lineNo} in ${file}: ${e instanceof Error ? e.message : e}`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
} finally {
|
|
164
|
+
this.replaying = false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private applyReplayed(change: Change): void {
|
|
169
|
+
switch (change.kind) {
|
|
170
|
+
case "user.add":
|
|
171
|
+
this.users.set(change.user.id, change.user);
|
|
172
|
+
break;
|
|
173
|
+
case "channel.add":
|
|
174
|
+
case "channel.update":
|
|
175
|
+
this.channels.set(change.channel.id, change.channel);
|
|
176
|
+
break;
|
|
177
|
+
case "member.join": {
|
|
178
|
+
const c = this.channels.get(change.channel.id);
|
|
179
|
+
if (c && !c.members.includes(change.user)) c.members.push(change.user);
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case "message.add":
|
|
183
|
+
this.insertMessage(change.message);
|
|
184
|
+
break;
|
|
185
|
+
case "message.update":
|
|
186
|
+
case "reaction.add":
|
|
187
|
+
case "reaction.remove": {
|
|
188
|
+
const list = this.messages.get(change.message.channel);
|
|
189
|
+
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
190
|
+
if (list && i >= 0) list[i] = change.message;
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
case "message.delete": {
|
|
194
|
+
const list = this.messages.get(change.message.channel);
|
|
195
|
+
const i = list?.findIndex((m) => m.ts === change.message.ts) ?? -1;
|
|
196
|
+
if (list && i >= 0) list.splice(i, 1);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case "file.add":
|
|
200
|
+
this.files.set(change.file.id, change.file);
|
|
201
|
+
break;
|
|
202
|
+
default:
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ----------------------------------------------------------------- users
|
|
208
|
+
|
|
209
|
+
addUser(input: AddUserInput): SlackUser {
|
|
210
|
+
const id = input.id ?? slackId("U");
|
|
211
|
+
const realName = input.real_name ?? input.name;
|
|
212
|
+
const user: SlackUser = {
|
|
213
|
+
id,
|
|
214
|
+
team_id: this.team.id,
|
|
215
|
+
name: input.name,
|
|
216
|
+
real_name: realName,
|
|
217
|
+
deleted: false,
|
|
218
|
+
is_bot: input.is_bot ?? false,
|
|
219
|
+
is_admin: input.is_admin ?? false,
|
|
220
|
+
is_app_user: false,
|
|
221
|
+
tz: input.tz ?? "UTC",
|
|
222
|
+
profile: {
|
|
223
|
+
real_name: realName,
|
|
224
|
+
display_name: input.name,
|
|
225
|
+
real_name_normalized: realName,
|
|
226
|
+
display_name_normalized: input.name,
|
|
227
|
+
email: input.email,
|
|
228
|
+
team: this.team.id,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
this.users.set(id, user);
|
|
232
|
+
this.emit({ kind: "user.add", user });
|
|
233
|
+
return user;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
user(id: string): SlackUser {
|
|
237
|
+
const u = this.users.get(id);
|
|
238
|
+
if (!u) throw new SlackApiError("user_not_found");
|
|
239
|
+
return u;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
userByEmail(email: string): SlackUser | undefined {
|
|
243
|
+
for (const u of this.users.values()) if (u.profile.email === email) return u;
|
|
244
|
+
return undefined;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
isBotUser(userId: string | undefined): boolean {
|
|
248
|
+
return userId === this.bot.userId;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// -------------------------------------------------------------- channels
|
|
252
|
+
|
|
253
|
+
addChannel(input: AddChannelInput): SlackChannel {
|
|
254
|
+
const name = input.name.replace(/^#/, "");
|
|
255
|
+
for (const c of this.channels.values()) {
|
|
256
|
+
if (c.name === name && !c.is_im) throw new SlackApiError("name_taken");
|
|
257
|
+
}
|
|
258
|
+
const id = input.id ?? slackId(input.is_private ? "G" : "C");
|
|
259
|
+
const creator = input.creator ?? this.bot.userId;
|
|
260
|
+
const members = [...new Set([creator, ...(input.members ?? [])])];
|
|
261
|
+
const channel: SlackChannel = {
|
|
262
|
+
id,
|
|
263
|
+
name,
|
|
264
|
+
is_channel: !input.is_private,
|
|
265
|
+
is_group: !!input.is_private,
|
|
266
|
+
is_im: false,
|
|
267
|
+
is_mpim: false,
|
|
268
|
+
is_private: !!input.is_private,
|
|
269
|
+
is_archived: false,
|
|
270
|
+
is_general: name === "general",
|
|
271
|
+
created: nowUnix(),
|
|
272
|
+
creator,
|
|
273
|
+
topic: { value: input.topic ?? "", creator, last_set: 0 },
|
|
274
|
+
purpose: { value: input.purpose ?? "", creator, last_set: 0 },
|
|
275
|
+
members,
|
|
276
|
+
};
|
|
277
|
+
this.channels.set(id, channel);
|
|
278
|
+
this.messages.set(id, []);
|
|
279
|
+
this.emit({ kind: "channel.add", channel });
|
|
280
|
+
return channel;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Direct message channel between the bot and a user (created on demand). */
|
|
284
|
+
openDm(userId: string): SlackChannel {
|
|
285
|
+
this.user(userId);
|
|
286
|
+
for (const c of this.channels.values()) if (c.is_im && c.user === userId) return c;
|
|
287
|
+
const id = slackId("D");
|
|
288
|
+
const channel: SlackChannel = {
|
|
289
|
+
id,
|
|
290
|
+
name: id,
|
|
291
|
+
is_channel: false,
|
|
292
|
+
is_group: false,
|
|
293
|
+
is_im: true,
|
|
294
|
+
is_mpim: false,
|
|
295
|
+
is_private: true,
|
|
296
|
+
is_archived: false,
|
|
297
|
+
is_general: false,
|
|
298
|
+
created: nowUnix(),
|
|
299
|
+
creator: userId,
|
|
300
|
+
user: userId,
|
|
301
|
+
topic: { value: "", creator: "", last_set: 0 },
|
|
302
|
+
purpose: { value: "", creator: "", last_set: 0 },
|
|
303
|
+
members: [userId, this.bot.userId],
|
|
304
|
+
};
|
|
305
|
+
this.channels.set(id, channel);
|
|
306
|
+
this.messages.set(id, []);
|
|
307
|
+
this.emit({ kind: "channel.add", channel });
|
|
308
|
+
return channel;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
channel(id: string): SlackChannel {
|
|
312
|
+
const c = this.channels.get(id);
|
|
313
|
+
if (!c) throw new SlackApiError("channel_not_found");
|
|
314
|
+
return c;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
channelByName(name: string): SlackChannel | undefined {
|
|
318
|
+
const n = name.replace(/^#/, "");
|
|
319
|
+
for (const c of this.channels.values()) if (c.name === n) return c;
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
channelType(c: SlackChannel): "channel" | "group" | "im" | "mpim" {
|
|
324
|
+
if (c.is_im) return "im";
|
|
325
|
+
if (c.is_mpim) return "mpim";
|
|
326
|
+
if (c.is_private) return "group";
|
|
327
|
+
return "channel";
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
join(channelId: string, userId: string, inviter?: string): { alreadyMember: boolean } {
|
|
331
|
+
const channel = this.channel(channelId);
|
|
332
|
+
if (channel.is_archived) throw new SlackApiError("is_archived");
|
|
333
|
+
if (channel.members.includes(userId)) return { alreadyMember: true };
|
|
334
|
+
channel.members.push(userId);
|
|
335
|
+
this.emit({ kind: "member.join", channel, user: userId, inviter });
|
|
336
|
+
return { alreadyMember: false };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
archive(channelId: string): void {
|
|
340
|
+
const channel = this.channel(channelId);
|
|
341
|
+
if (channel.is_archived) throw new SlackApiError("already_archived");
|
|
342
|
+
if (channel.is_general) throw new SlackApiError("cant_archive_general");
|
|
343
|
+
channel.is_archived = true;
|
|
344
|
+
this.emit({ kind: "channel.update", channel });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// -------------------------------------------------------------- messages
|
|
348
|
+
|
|
349
|
+
private insertMessage(message: SlackMessage): void {
|
|
350
|
+
const list = this.messages.get(message.channel) ?? [];
|
|
351
|
+
this.messages.set(message.channel, list);
|
|
352
|
+
let i = list.length;
|
|
353
|
+
while (i > 0 && Number(list[i - 1]!.ts) > Number(message.ts)) i--;
|
|
354
|
+
list.splice(i, 0, message);
|
|
355
|
+
if (message.thread_ts && message.thread_ts !== message.ts) {
|
|
356
|
+
const parent = this.findMessage(message.channel, message.thread_ts);
|
|
357
|
+
if (parent) {
|
|
358
|
+
parent.reply_count = (parent.reply_count ?? 0) + 1;
|
|
359
|
+
const users = new Set(parent.reply_users ?? []);
|
|
360
|
+
if (message.user) users.add(message.user);
|
|
361
|
+
parent.reply_users = [...users];
|
|
362
|
+
parent.reply_users_count = users.size;
|
|
363
|
+
parent.latest_reply = message.ts;
|
|
364
|
+
if (parent.user) message.parent_user_id = parent.user;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
findMessage(channelId: string, ts: string): SlackMessage | undefined {
|
|
370
|
+
return this.messages.get(channelId)?.find((m) => m.ts === ts);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** A message by ts, including ephemeral ones (used by response_url handling). */
|
|
374
|
+
findAnyMessage(channelId: string, ts: string): SlackMessage | undefined {
|
|
375
|
+
return (
|
|
376
|
+
this.findMessage(channelId, ts) ??
|
|
377
|
+
this.ephemerals.find((m) => m.channel === channelId && m.ts === ts)
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Replace the content of an ephemeral message in place (no event, like Slack). */
|
|
382
|
+
updateEphemeral(channelId: string, ts: string, patch: Partial<SlackMessage>): SlackMessage {
|
|
383
|
+
const m = this.ephemerals.find((x) => x.channel === channelId && x.ts === ts);
|
|
384
|
+
if (!m) throw new SlackApiError("message_not_found");
|
|
385
|
+
Object.assign(m, patch);
|
|
386
|
+
return m;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
deleteEphemeral(channelId: string, ts: string): void {
|
|
390
|
+
const i = this.ephemerals.findIndex((x) => x.channel === channelId && x.ts === ts);
|
|
391
|
+
if (i < 0) throw new SlackApiError("message_not_found");
|
|
392
|
+
this.ephemerals.splice(i, 1);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
message(channelId: string, ts: string): SlackMessage {
|
|
396
|
+
const m = this.findMessage(channelId, ts);
|
|
397
|
+
if (!m) throw new SlackApiError("message_not_found");
|
|
398
|
+
return m;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
addMessage(input: Omit<SlackMessage, "type" | "team" | "ts"> & { ts?: string }): SlackMessage {
|
|
402
|
+
const channel = this.channel(input.channel);
|
|
403
|
+
if (channel.is_archived) throw new SlackApiError("is_archived");
|
|
404
|
+
if (input.thread_ts && !this.findMessage(input.channel, input.thread_ts)) {
|
|
405
|
+
throw new SlackApiError("thread_not_found");
|
|
406
|
+
}
|
|
407
|
+
const message: SlackMessage = {
|
|
408
|
+
type: "message",
|
|
409
|
+
team: this.team.id,
|
|
410
|
+
ts: input.ts ?? nextTs(),
|
|
411
|
+
...input,
|
|
412
|
+
};
|
|
413
|
+
if (message.ephemeral_user) this.ephemerals.push(message);
|
|
414
|
+
else this.insertMessage(message);
|
|
415
|
+
this.emit({ kind: "message.add", message });
|
|
416
|
+
return message;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
updateMessage(
|
|
420
|
+
channelId: string,
|
|
421
|
+
ts: string,
|
|
422
|
+
patch: Partial<SlackMessage>,
|
|
423
|
+
editor: string,
|
|
424
|
+
): SlackMessage {
|
|
425
|
+
const message = this.message(channelId, ts);
|
|
426
|
+
const previous = structuredClone(message);
|
|
427
|
+
Object.assign(message, patch, { edited: { user: editor, ts: nextTs() } });
|
|
428
|
+
this.emit({ kind: "message.update", message, previous });
|
|
429
|
+
return message;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
deleteMessage(channelId: string, ts: string): SlackMessage {
|
|
433
|
+
const list = this.messages.get(channelId);
|
|
434
|
+
const i = list?.findIndex((m) => m.ts === ts) ?? -1;
|
|
435
|
+
if (!list || i < 0) throw new SlackApiError("message_not_found");
|
|
436
|
+
const [message] = list.splice(i, 1);
|
|
437
|
+
if (message!.thread_ts && message!.thread_ts !== ts) {
|
|
438
|
+
const parent = this.findMessage(channelId, message!.thread_ts);
|
|
439
|
+
if (parent?.reply_count) parent.reply_count -= 1;
|
|
440
|
+
}
|
|
441
|
+
this.emit({ kind: "message.delete", message: message! });
|
|
442
|
+
return message!;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
addReaction(channelId: string, ts: string, name: string, userId: string): SlackMessage {
|
|
446
|
+
const message = this.message(channelId, ts);
|
|
447
|
+
message.reactions ??= [];
|
|
448
|
+
let reaction: SlackReaction | undefined = message.reactions.find((r) => r.name === name);
|
|
449
|
+
if (reaction?.users.includes(userId)) throw new SlackApiError("already_reacted");
|
|
450
|
+
if (!reaction) {
|
|
451
|
+
reaction = { name, users: [], count: 0 };
|
|
452
|
+
message.reactions.push(reaction);
|
|
453
|
+
}
|
|
454
|
+
reaction.users.push(userId);
|
|
455
|
+
reaction.count = reaction.users.length;
|
|
456
|
+
this.emit({ kind: "reaction.add", message, name, user: userId });
|
|
457
|
+
return message;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
removeReaction(channelId: string, ts: string, name: string, userId: string): SlackMessage {
|
|
461
|
+
const message = this.message(channelId, ts);
|
|
462
|
+
const reaction = message.reactions?.find((r) => r.name === name);
|
|
463
|
+
if (!reaction?.users.includes(userId)) throw new SlackApiError("no_reaction");
|
|
464
|
+
reaction.users = reaction.users.filter((u) => u !== userId);
|
|
465
|
+
reaction.count = reaction.users.length;
|
|
466
|
+
message.reactions = message.reactions!.filter((r) => r.count > 0);
|
|
467
|
+
if (message.reactions.length === 0) message.reactions = undefined;
|
|
468
|
+
this.emit({ kind: "reaction.remove", message, name, user: userId });
|
|
469
|
+
return message;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Top-level messages of a channel (thread replies excluded, like conversations.history). */
|
|
473
|
+
history(channelId: string, q: HistoryQuery = {}): Page<SlackMessage> {
|
|
474
|
+
this.channel(channelId);
|
|
475
|
+
const all = (this.messages.get(channelId) ?? []).filter(
|
|
476
|
+
(m) => !m.thread_ts || m.thread_ts === m.ts || m.subtype === "thread_broadcast",
|
|
477
|
+
);
|
|
478
|
+
return paginate(all, q);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/** Parent plus replies, oldest first (like conversations.replies). */
|
|
482
|
+
replies(channelId: string, threadTs: string, q: HistoryQuery = {}): Page<SlackMessage> {
|
|
483
|
+
const parent = this.message(channelId, threadTs);
|
|
484
|
+
const all = [
|
|
485
|
+
parent,
|
|
486
|
+
...(this.messages.get(channelId) ?? []).filter(
|
|
487
|
+
(m) => m.thread_ts === threadTs && m.ts !== threadTs,
|
|
488
|
+
),
|
|
489
|
+
];
|
|
490
|
+
return paginate(all, q);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// ----------------------------------------------------------------- files
|
|
494
|
+
|
|
495
|
+
addFile(input: {
|
|
496
|
+
id?: string;
|
|
497
|
+
name: string;
|
|
498
|
+
title?: string;
|
|
499
|
+
mimetype?: string;
|
|
500
|
+
user: string;
|
|
501
|
+
bytes: Uint8Array;
|
|
502
|
+
baseUrl: string;
|
|
503
|
+
}): SlackFile {
|
|
504
|
+
const id = input.id ?? slackId("F");
|
|
505
|
+
const ext = input.name.includes(".") ? input.name.split(".").pop()!.toLowerCase() : "";
|
|
506
|
+
const mimetype = input.mimetype ?? guessMime(ext);
|
|
507
|
+
const now = nowUnix();
|
|
508
|
+
const file: SlackFile = {
|
|
509
|
+
id,
|
|
510
|
+
created: now,
|
|
511
|
+
timestamp: now,
|
|
512
|
+
name: input.name,
|
|
513
|
+
title: input.title ?? input.name,
|
|
514
|
+
mimetype,
|
|
515
|
+
filetype: ext || "binary",
|
|
516
|
+
pretty_type: ext ? ext.toUpperCase() : "Binary",
|
|
517
|
+
user: input.user,
|
|
518
|
+
size: input.bytes.byteLength,
|
|
519
|
+
mode: "hosted",
|
|
520
|
+
is_external: false,
|
|
521
|
+
is_public: false,
|
|
522
|
+
url_private: `${input.baseUrl}/files-pri/${this.team.id}-${id}/${encodeURIComponent(input.name)}`,
|
|
523
|
+
url_private_download: `${input.baseUrl}/files-pri/${this.team.id}-${id}/download/${encodeURIComponent(input.name)}`,
|
|
524
|
+
permalink: `${input.baseUrl}/files/${input.user}/${id}/${encodeURIComponent(input.name)}`,
|
|
525
|
+
channels: [],
|
|
526
|
+
bytes: input.bytes,
|
|
527
|
+
};
|
|
528
|
+
if (mimetype.startsWith("image/")) {
|
|
529
|
+
const thumbs = file as unknown as Record<string, string>;
|
|
530
|
+
for (const size of [64, 80, 160, 360, 480]) thumbs[`thumb_${size}`] = file.url_private;
|
|
531
|
+
}
|
|
532
|
+
this.files.set(id, file);
|
|
533
|
+
this.emit({ kind: "file.add", file });
|
|
534
|
+
return file;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
file(id: string): SlackFile {
|
|
538
|
+
const f = this.files.get(id);
|
|
539
|
+
if (!f) throw new SlackApiError("file_not_found");
|
|
540
|
+
return f;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// ----------------------------------------------------------------- views
|
|
544
|
+
|
|
545
|
+
openView(
|
|
546
|
+
view: Omit<SlackView, "id" | "hash" | "root_view_id" | "team_id" | "app_id" | "bot_id"> & {
|
|
547
|
+
id?: string;
|
|
548
|
+
},
|
|
549
|
+
): SlackView {
|
|
550
|
+
const id = view.id ?? slackId("V");
|
|
551
|
+
const full: SlackView = {
|
|
552
|
+
...view,
|
|
553
|
+
id,
|
|
554
|
+
team_id: this.team.id,
|
|
555
|
+
app_id: this.app.id,
|
|
556
|
+
bot_id: this.bot.botId,
|
|
557
|
+
hash: `${nowUnix()}.${slackId("", 6).toLowerCase()}`,
|
|
558
|
+
root_view_id: id,
|
|
559
|
+
};
|
|
560
|
+
this.views.set(id, full);
|
|
561
|
+
this.emit({ kind: "view.open", view: full });
|
|
562
|
+
return full;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// ------------------------------------------------------------- api calls
|
|
566
|
+
|
|
567
|
+
recordApiCall(call: ApiCall): void {
|
|
568
|
+
this.apiCalls.push(call);
|
|
569
|
+
this.emit({ kind: "api.call", call });
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
assistantThread(channel: string, threadTs: string) {
|
|
573
|
+
const key = `${channel}:${threadTs}`;
|
|
574
|
+
let t = this.assistantThreads.get(key);
|
|
575
|
+
if (!t) {
|
|
576
|
+
t = {};
|
|
577
|
+
this.assistantThreads.set(key, t);
|
|
578
|
+
}
|
|
579
|
+
return t;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function paginate<T extends { ts: string }>(all: T[], q: HistoryQuery): Page<T> {
|
|
584
|
+
let items = all;
|
|
585
|
+
if (q.oldest)
|
|
586
|
+
items = items.filter((m) =>
|
|
587
|
+
q.inclusive ? Number(m.ts) >= Number(q.oldest) : Number(m.ts) > Number(q.oldest),
|
|
588
|
+
);
|
|
589
|
+
if (q.latest)
|
|
590
|
+
items = items.filter((m) =>
|
|
591
|
+
q.inclusive ? Number(m.ts) <= Number(q.latest) : Number(m.ts) < Number(q.latest),
|
|
592
|
+
);
|
|
593
|
+
const start = q.cursor
|
|
594
|
+
? Number.parseInt(
|
|
595
|
+
Buffer.from(q.cursor, "base64").toString("utf8").replace("offset:", ""),
|
|
596
|
+
10,
|
|
597
|
+
) || 0
|
|
598
|
+
: 0;
|
|
599
|
+
const limit = Math.max(1, Math.min(q.limit ?? 100, 1000));
|
|
600
|
+
const page = items.slice(start, start + limit);
|
|
601
|
+
const has_more = start + limit < items.length;
|
|
602
|
+
return {
|
|
603
|
+
items: page,
|
|
604
|
+
has_more,
|
|
605
|
+
next_cursor: has_more ? Buffer.from(`offset:${start + limit}`).toString("base64") : "",
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function guessMime(ext: string): string {
|
|
610
|
+
const map: Record<string, string> = {
|
|
611
|
+
png: "image/png",
|
|
612
|
+
jpg: "image/jpeg",
|
|
613
|
+
jpeg: "image/jpeg",
|
|
614
|
+
gif: "image/gif",
|
|
615
|
+
webp: "image/webp",
|
|
616
|
+
svg: "image/svg+xml",
|
|
617
|
+
txt: "text/plain",
|
|
618
|
+
md: "text/markdown",
|
|
619
|
+
log: "text/plain",
|
|
620
|
+
json: "application/json",
|
|
621
|
+
csv: "text/csv",
|
|
622
|
+
pdf: "application/pdf",
|
|
623
|
+
html: "text/html",
|
|
624
|
+
zip: "application/zip",
|
|
625
|
+
};
|
|
626
|
+
return map[ext] ?? "application/octet-stream";
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** Public (wire) form of a message: mock-only fields removed, file bytes dropped. */
|
|
630
|
+
export function wireMessage(m: SlackMessage): Record<string, unknown> {
|
|
631
|
+
const { ephemeral_user: _e, ...rest } = m;
|
|
632
|
+
if (rest.files) rest.files = rest.files.map(wireFile);
|
|
633
|
+
return rest;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
export function wireFile(f: SlackFile): SlackFile {
|
|
637
|
+
const { bytes: _b, ...rest } = f;
|
|
638
|
+
return rest;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
function stripBytes(change: Change): Change {
|
|
642
|
+
if (change.kind === "file.add") return { ...change, file: wireFile(change.file) };
|
|
643
|
+
if ("message" in change && change.message.files) {
|
|
644
|
+
return {
|
|
645
|
+
...change,
|
|
646
|
+
message: { ...change.message, files: change.message.files.map(wireFile) },
|
|
647
|
+
} as Change;
|
|
648
|
+
}
|
|
649
|
+
return change;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
export function ensureDir(file: string): void {
|
|
653
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
654
|
+
}
|