@crvouga/mockingbird-service-slack 3.7.0 → 3.8.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.
@@ -3821,7 +3821,7 @@ var stateAdminRoutes = (routes) => {
3821
3821
  };
3822
3822
 
3823
3823
  // ../core/dist/version.js
3824
- var PACKAGE_VERSION = true ? "3.7.0" : UNRELEASED_VERSION;
3824
+ var PACKAGE_VERSION = true ? "3.8.0" : UNRELEASED_VERSION;
3825
3825
 
3826
3826
  // ../core/dist/webhooks.js
3827
3827
  var json5 = (status, body) => new Response(JSON.stringify(body), {
@@ -11341,6 +11341,116 @@ var ProviderExpansion = class extends ProviderAPI {
11341
11341
  // src/index.ts
11342
11342
  import { Hono as Hono3 } from "hono";
11343
11343
 
11344
+ // src/blocks.ts
11345
+ var isRecord5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
11346
+ var BLOCK_TYPES = /* @__PURE__ */ new Set([
11347
+ "actions",
11348
+ "alert",
11349
+ "call",
11350
+ "card",
11351
+ "carousel",
11352
+ "context",
11353
+ "context_actions",
11354
+ "divider",
11355
+ "file",
11356
+ "header",
11357
+ "image",
11358
+ "input",
11359
+ "markdown",
11360
+ "plan",
11361
+ "rich_text",
11362
+ "section",
11363
+ "table",
11364
+ "task_card",
11365
+ "video"
11366
+ ]);
11367
+ var MAX_BLOCKS = 50;
11368
+ var checkText = (value, path, max, issues, plainOnly = false) => {
11369
+ if (!isRecord5(value)) {
11370
+ issues.push(`must be a text object [json-pointer:${path}]`);
11371
+ return;
11372
+ }
11373
+ if (value.type !== "plain_text" && !(value.type === "mrkdwn" && !plainOnly)) {
11374
+ issues.push(
11375
+ `must be of type ${plainOnly ? "plain_text" : "plain_text or mrkdwn"} [json-pointer:${path}/type]`
11376
+ );
11377
+ }
11378
+ if (typeof value.text !== "string" || value.text.length === 0) {
11379
+ issues.push(`must be a non-empty string [json-pointer:${path}/text]`);
11380
+ } else if (value.text.length > max) {
11381
+ issues.push(`must be less than ${max + 1} characters [json-pointer:${path}/text]`);
11382
+ }
11383
+ };
11384
+ var checkList = (value, path, max, issues, each) => {
11385
+ if (!Array.isArray(value)) {
11386
+ issues.push(`must be an array [json-pointer:${path}]`);
11387
+ return;
11388
+ }
11389
+ if (value.length > max)
11390
+ issues.push(`must have fewer than ${max + 1} items [json-pointer:${path}]`);
11391
+ if (!each) return;
11392
+ for (const [index, item] of value.entries()) each(item, `${path}/${index}`);
11393
+ };
11394
+ var checkBlock = (block, path, issues) => {
11395
+ if (!isRecord5(block) || typeof block.type !== "string") {
11396
+ issues.push(`must be an object with a type [json-pointer:${path}]`);
11397
+ return;
11398
+ }
11399
+ if (!BLOCK_TYPES.has(block.type)) {
11400
+ issues.push(`unsupported type: ${block.type} [json-pointer:${path}/type]`);
11401
+ return;
11402
+ }
11403
+ if (block.block_id !== void 0) {
11404
+ if (typeof block.block_id !== "string" || block.block_id.length > 255) {
11405
+ issues.push(`must be a string of at most 255 characters [json-pointer:${path}/block_id]`);
11406
+ }
11407
+ }
11408
+ switch (block.type) {
11409
+ case "section":
11410
+ if (block.text !== void 0) checkText(block.text, `${path}/text`, 3e3, issues);
11411
+ if (block.fields !== void 0) {
11412
+ checkList(
11413
+ block.fields,
11414
+ `${path}/fields`,
11415
+ 10,
11416
+ issues,
11417
+ (field2, fieldPath) => checkText(field2, fieldPath, 2e3, issues)
11418
+ );
11419
+ }
11420
+ break;
11421
+ case "header":
11422
+ if (block.text !== void 0) checkText(block.text, `${path}/text`, 150, issues, true);
11423
+ break;
11424
+ case "context":
11425
+ if (block.elements !== void 0) checkList(block.elements, `${path}/elements`, 10, issues);
11426
+ break;
11427
+ case "actions":
11428
+ if (block.elements !== void 0) checkList(block.elements, `${path}/elements`, 25, issues);
11429
+ break;
11430
+ }
11431
+ };
11432
+ var blockIssues = (blocks, path = "/blocks") => {
11433
+ const issues = [];
11434
+ checkList(
11435
+ blocks,
11436
+ path,
11437
+ MAX_BLOCKS,
11438
+ issues,
11439
+ (block, blockPath) => checkBlock(block, blockPath, issues)
11440
+ );
11441
+ return issues;
11442
+ };
11443
+ var attachmentBlockIssues = (attachments) => {
11444
+ if (!Array.isArray(attachments)) return [];
11445
+ const issues = [];
11446
+ attachments.forEach((attachment, index) => {
11447
+ if (isRecord5(attachment) && attachment.blocks !== void 0 && attachment.blocks !== null) {
11448
+ issues.push(...blockIssues(attachment.blocks, `/attachments/${index}/blocks`));
11449
+ }
11450
+ });
11451
+ return issues;
11452
+ };
11453
+
11344
11454
  // src/expanded-state.ts
11345
11455
  function syncToExpanded(state, expansion) {
11346
11456
  const ss = getSlackStore(expansion.store);
@@ -11893,8 +12003,8 @@ var SLACK_PRESETS = {
11893
12003
  };
11894
12004
  var json6 = (status, body) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
11895
12005
  var adminError4 = (status, message) => json6(status, { error: { type: "mockingbird_admin", message } });
11896
- var isRecord5 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
11897
- var list = (body, key) => Array.isArray(body) ? body : isRecord5(body) && Array.isArray(body[key]) ? body[key] : void 0;
12006
+ var isRecord6 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
12007
+ var list = (body, key) => Array.isArray(body) ? body : isRecord6(body) && Array.isArray(body[key]) ? body[key] : void 0;
11898
12008
  var hookSuffix = (value) => {
11899
12009
  let path = value;
11900
12010
  try {
@@ -11931,8 +12041,8 @@ var adminRoutes = (runtime) => ({
11931
12041
  hooks: runtime.instance(namespace).state.hooks.list({ order: "oldest" }).map((row) => ({ ...row.value, url: `/services/${row.value.path}` }))
11932
12042
  }),
11933
12043
  "POST /hooks": ({ body, namespace }) => {
11934
- const entries = list(body, "hooks") ?? (isRecord5(body) ? [body] : void 0);
11935
- if (!entries || entries.some((e) => !isRecord5(e) || typeof e.path !== "string")) {
12044
+ const entries = list(body, "hooks") ?? (isRecord6(body) ? [body] : void 0);
12045
+ if (!entries || entries.some((e) => !isRecord6(e) || typeof e.path !== "string")) {
11936
12046
  return adminError4(400, 'expected {"path": "T\u2026/B\u2026/X\u2026", "channel"?: "C\u2026"} or {"hooks": [...]}');
11937
12047
  }
11938
12048
  const api = runtime.instance(namespace);
@@ -11959,8 +12069,8 @@ var adminRoutes = (runtime) => ({
11959
12069
  channels: runtime.instance(namespace).state.channels.list({ order: "oldest" }).map((r) => r.value)
11960
12070
  }),
11961
12071
  "POST /channels": ({ body, namespace }) => {
11962
- const entries = list(body, "channels") ?? (isRecord5(body) ? [body] : void 0);
11963
- if (!entries || entries.some((e) => !isRecord5(e) || typeof e.name !== "string")) {
12072
+ const entries = list(body, "channels") ?? (isRecord6(body) ? [body] : void 0);
12073
+ if (!entries || entries.some((e) => !isRecord6(e) || typeof e.name !== "string")) {
11964
12074
  return adminError4(
11965
12075
  400,
11966
12076
  'expected {"id"?, "name", "is_private"?, "is_archived"?, "is_member"?}'
@@ -11986,8 +12096,8 @@ var adminRoutes = (runtime) => ({
11986
12096
  users: runtime.instance(namespace).state.users.list({ order: "oldest" }).map((r) => r.value)
11987
12097
  }),
11988
12098
  "POST /users": ({ body, namespace }) => {
11989
- const entries = list(body, "users") ?? (isRecord5(body) ? [body] : void 0);
11990
- if (!entries || entries.some((e) => !isRecord5(e) || typeof e.id !== "string")) {
12099
+ const entries = list(body, "users") ?? (isRecord6(body) ? [body] : void 0);
12100
+ if (!entries || entries.some((e) => !isRecord6(e) || typeof e.id !== "string")) {
11991
12101
  return adminError4(400, 'expected {"id": "U\u2026", "name"?, "real_name"?, "email"?}');
11992
12102
  }
11993
12103
  const api = runtime.instance(namespace);
@@ -12008,8 +12118,8 @@ var adminRoutes = (runtime) => ({
12008
12118
  return json6(201, { users: saved });
12009
12119
  },
12010
12120
  "POST /files": ({ body, namespace }) => {
12011
- const entries = list(body, "files") ?? (isRecord5(body) ? [body] : void 0);
12012
- if (!entries || entries.some((e) => !isRecord5(e) || typeof e.id !== "string")) {
12121
+ const entries = list(body, "files") ?? (isRecord6(body) ? [body] : void 0);
12122
+ if (!entries || entries.some((e) => !isRecord6(e) || typeof e.id !== "string")) {
12013
12123
  return adminError4(400, 'expected {"id": "F\u2026", "name"?, "mimetype"?, "size"?}');
12014
12124
  }
12015
12125
  const api = runtime.instance(namespace);
@@ -12035,7 +12145,7 @@ var adminRoutes = (runtime) => ({
12035
12145
  }),
12036
12146
  "GET /settings": ({ namespace }) => json6(200, runtime.instance(namespace).state.current()),
12037
12147
  "PUT /settings": ({ body, namespace }) => {
12038
- if (!isRecord5(body)) return adminError4(400, "expected a JSON object");
12148
+ if (!isRecord6(body)) return adminError4(400, "expected a JSON object");
12039
12149
  const patch = {};
12040
12150
  for (const key of [
12041
12151
  "teamId",
@@ -12085,11 +12195,11 @@ var adminRoutes = (runtime) => ({
12085
12195
  }))
12086
12196
  }),
12087
12197
  "POST /socket/disconnect": ({ body, namespace }) => {
12088
- const reason = isRecord5(body) && typeof body.reason === "string" ? body.reason : "warning";
12198
+ const reason = isRecord6(body) && typeof body.reason === "string" ? body.reason : "warning";
12089
12199
  if (reason !== "warning" && reason !== "refresh_requested" && reason !== "link_disabled") {
12090
12200
  return adminError4(400, "reason must be warning, refresh_requested, or link_disabled");
12091
12201
  }
12092
- const code = isRecord5(body) && typeof body.code === "number" ? body.code : 1e3;
12202
+ const code = isRecord6(body) && typeof body.code === "number" ? body.code : 1e3;
12093
12203
  return json6(200, { closed: disconnectSockets(namespace, reason, code) });
12094
12204
  }
12095
12205
  });
@@ -12129,7 +12239,7 @@ var slackCredential = (request, adminPrefix = ADMIN_PREFIX) => {
12129
12239
  const path = new URL(request.url).pathname;
12130
12240
  return WEBHOOK_PATH.exec(matchNamespacePath(path, adminPrefix)?.[2] ?? path)?.[1];
12131
12241
  };
12132
- var isRecord6 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
12242
+ var isRecord7 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
12133
12243
  var text = (status, body, headers = {}) => new Response(body, {
12134
12244
  status,
12135
12245
  headers: { "content-type": "text/plain; charset=utf-8", ...headers }
@@ -12152,7 +12262,7 @@ var readArgs = (context) => {
12152
12262
  const contentType = context.request.headers.get("content-type") ?? "";
12153
12263
  let json7 = false;
12154
12264
  if (body.kind === "json") {
12155
- if (!isRecord6(body.value)) throw new SlackError("invalid_json");
12265
+ if (!isRecord7(body.value)) throw new SlackError("invalid_json");
12156
12266
  Object.assign(args, body.value);
12157
12267
  json7 = true;
12158
12268
  } else if (body.kind === "form") {
@@ -12181,14 +12291,21 @@ var flag2 = (value) => {
12181
12291
  if (value === "false") return false;
12182
12292
  return null;
12183
12293
  };
12294
+ var invalidBlocks = (issues) => new SlackError("invalid_blocks", {
12295
+ response_metadata: { messages: issues.map((issue) => `[ERROR] ${issue}`) }
12296
+ });
12184
12297
  var checkBlocks = (blocks) => {
12185
12298
  if (blocks === void 0 || blocks === null || blocks === "") return null;
12186
- if (!Array.isArray(blocks) || blocks.length > 50) throw new SlackError("invalid_blocks");
12187
- for (const block of blocks) {
12188
- if (!isRecord6(block) || typeof block.type !== "string") throw new SlackError("invalid_blocks");
12189
- }
12299
+ const issues = blockIssues(blocks);
12300
+ if (issues.length > 0) throw invalidBlocks(issues);
12190
12301
  return blocks;
12191
12302
  };
12303
+ var checkAttachments = (attachments) => {
12304
+ if (!Array.isArray(attachments)) return null;
12305
+ const issues = attachmentBlockIssues(attachments);
12306
+ if (issues.length > 0) throw invalidBlocks(issues);
12307
+ return attachments;
12308
+ };
12192
12309
  var SlackAPI = class {
12193
12310
  app;
12194
12311
  sqlite;
@@ -12434,14 +12551,15 @@ var SlackAPI = class {
12434
12551
  return text(400, "invalid_payload");
12435
12552
  }
12436
12553
  } else return text(400, "invalid_payload");
12437
- if (!isRecord6(payload)) return text(400, "invalid_payload");
12554
+ if (!isRecord7(payload)) return text(400, "invalid_payload");
12438
12555
  let blocks;
12556
+ let attachments;
12439
12557
  try {
12440
12558
  blocks = checkBlocks(payload.blocks);
12559
+ attachments = checkAttachments(payload.attachments);
12441
12560
  } catch {
12442
12561
  return text(400, "invalid_blocks");
12443
12562
  }
12444
- const attachments = Array.isArray(payload.attachments) ? payload.attachments : null;
12445
12563
  if (attachments && attachments.length > 100) return text(400, "too_many_attachments");
12446
12564
  const messageText = typeof payload.text === "string" ? payload.text : null;
12447
12565
  if (!messageText && !blocks && !attachments) return text(400, "no_text");
@@ -12471,7 +12589,7 @@ var SlackAPI = class {
12471
12589
  const channel = this.channel(args.channel, context);
12472
12590
  if (channel.is_archived) throw new SlackError("is_archived");
12473
12591
  const blocks = checkBlocks(args.blocks);
12474
- const attachments = Array.isArray(args.attachments) ? args.attachments : null;
12592
+ const attachments = checkAttachments(args.attachments);
12475
12593
  const messageText = typeof args.text === "string" ? args.text : null;
12476
12594
  if (!messageText && !blocks && !attachments) throw new SlackError("no_text");
12477
12595
  if (messageText && messageText.length > 4e4) throw new SlackError("msg_too_long");
@@ -12508,6 +12626,7 @@ var SlackAPI = class {
12508
12626
  if (!message) throw new SlackError("message_not_found");
12509
12627
  if (message.source !== "api") throw new SlackError("cant_update_message");
12510
12628
  const blocks = checkBlocks(args.blocks);
12629
+ checkAttachments(args.attachments);
12511
12630
  const messageText = typeof args.text === "string" ? args.text : null;
12512
12631
  if (!messageText && !blocks && args.blocks === void 0) throw new SlackError("no_text");
12513
12632
  const next = {
@@ -12535,6 +12654,7 @@ var SlackAPI = class {
12535
12654
  const user = userId ? this.state.users.get(userId) : void 0;
12536
12655
  if (!user) throw new SlackError("user_not_found");
12537
12656
  const blocks = checkBlocks(args.blocks);
12657
+ checkAttachments(args.attachments);
12538
12658
  const messageText = typeof args.text === "string" ? args.text : null;
12539
12659
  if (!messageText && !blocks) throw new SlackError("no_text");
12540
12660
  const message = this.record({
@@ -12760,7 +12880,7 @@ var SlackAPI = class {
12760
12880
  openView(args) {
12761
12881
  if (!str(args.trigger_id)) throw new SlackError("invalid_arguments");
12762
12882
  const view = args.view;
12763
- if (!isRecord6(view) || view.type !== "modal" && view.type !== "home") {
12883
+ if (!isRecord7(view) || view.type !== "modal" && view.type !== "home") {
12764
12884
  throw new SlackError("invalid_arguments", {
12765
12885
  response_metadata: { messages: ["[ERROR] view.type must be modal"] }
12766
12886
  });
@@ -12818,4 +12938,4 @@ export {
12818
12938
  * Copyright (c) 2021 - present, Yusuke Wada and Hono contributors
12819
12939
  * MIT license: see THIRD_PARTY_NOTICES.md in the repository and npm packages.
12820
12940
  */
12821
- //# sourceMappingURL=chunk-WBNF7Q2J.js.map
12941
+ //# sourceMappingURL=chunk-LQUZASGQ.js.map