refactor(slack): centralize target parsing

This commit is contained in:
Peter Steinberger
2026-01-18 00:15:02 +00:00
parent a5aa48beea
commit 4d590f9254
8 changed files with 190 additions and 83 deletions

View File

@@ -7,6 +7,7 @@ import { loadWebMedia } from "../web/media.js";
import type { SlackTokenSource } from "./accounts.js";
import { resolveSlackAccount } from "./accounts.js";
import { markdownToSlackMrkdwnChunks } from "./format.js";
import { parseSlackTarget } from "./targets.js";
import { resolveSlackBotToken } from "./token.js";
const SLACK_TEXT_LIMIT = 4000;
@@ -57,38 +58,11 @@ function resolveToken(params: {
}
function parseRecipient(raw: string): SlackRecipient {
const trimmed = raw.trim();
if (!trimmed) {
const target = parseSlackTarget(raw);
if (!target) {
throw new Error("Recipient is required for Slack sends");
}
const mentionMatch = trimmed.match(/^<@([A-Z0-9]+)>$/i);
if (mentionMatch) {
return { kind: "user", id: mentionMatch[1] };
}
if (trimmed.startsWith("user:")) {
return { kind: "user", id: trimmed.slice("user:".length) };
}
if (trimmed.startsWith("channel:")) {
return { kind: "channel", id: trimmed.slice("channel:".length) };
}
if (trimmed.startsWith("slack:")) {
return { kind: "user", id: trimmed.slice("slack:".length) };
}
if (trimmed.startsWith("@")) {
const candidate = trimmed.slice(1);
if (!/^[A-Z0-9]+$/i.test(candidate)) {
throw new Error("Slack DMs require a user id (use user:<id> or <@id>)");
}
return { kind: "user", id: candidate };
}
if (trimmed.startsWith("#")) {
const candidate = trimmed.slice(1);
if (!/^[A-Z0-9]+$/i.test(candidate)) {
throw new Error("Slack channels require a channel id (use channel:<id>)");
}
return { kind: "channel", id: candidate };
}
return { kind: "channel", id: trimmed };
return { kind: target.kind, id: target.id };
}
async function resolveChannelId(

59
src/slack/targets.test.ts Normal file
View File

@@ -0,0 +1,59 @@
import { describe, expect, it } from "vitest";
import { normalizeSlackMessagingTarget } from "../channels/plugins/normalize-target.js";
import { parseSlackTarget, resolveSlackChannelId } from "./targets.js";
describe("parseSlackTarget", () => {
it("parses user mentions and prefixes", () => {
expect(parseSlackTarget("<@U123>")).toMatchObject({
kind: "user",
id: "U123",
normalized: "user:u123",
});
expect(parseSlackTarget("user:U456")).toMatchObject({
kind: "user",
id: "U456",
normalized: "user:u456",
});
expect(parseSlackTarget("slack:U789")).toMatchObject({
kind: "user",
id: "U789",
normalized: "user:u789",
});
});
it("parses channel targets", () => {
expect(parseSlackTarget("channel:C123")).toMatchObject({
kind: "channel",
id: "C123",
normalized: "channel:c123",
});
expect(parseSlackTarget("#C999")).toMatchObject({
kind: "channel",
id: "C999",
normalized: "channel:c999",
});
});
it("rejects invalid @ and # targets", () => {
expect(() => parseSlackTarget("@bob-1")).toThrow(/Slack DMs require a user id/);
expect(() => parseSlackTarget("#general-1")).toThrow(/Slack channels require a channel id/);
});
});
describe("resolveSlackChannelId", () => {
it("strips channel: prefix and accepts raw ids", () => {
expect(resolveSlackChannelId("channel:C123")).toBe("C123");
expect(resolveSlackChannelId("C123")).toBe("C123");
});
it("rejects user targets", () => {
expect(() => resolveSlackChannelId("user:U123")).toThrow(/channel id is required/i);
});
});
describe("normalizeSlackMessagingTarget", () => {
it("defaults raw ids to channels", () => {
expect(normalizeSlackMessagingTarget("C123")).toBe("channel:c123");
});
});

78
src/slack/targets.ts Normal file
View File

@@ -0,0 +1,78 @@
export type SlackTargetKind = "user" | "channel";
export type SlackTarget = {
kind: SlackTargetKind;
id: string;
raw: string;
normalized: string;
};
type SlackTargetParseOptions = {
defaultKind?: SlackTargetKind;
};
function normalizeTargetId(kind: SlackTargetKind, id: string) {
return `${kind}:${id}`.toLowerCase();
}
function buildTarget(kind: SlackTargetKind, id: string, raw: string): SlackTarget {
return {
kind,
id,
raw,
normalized: normalizeTargetId(kind, id),
};
}
export function parseSlackTarget(
raw: string,
options: SlackTargetParseOptions = {},
): SlackTarget | undefined {
const trimmed = raw.trim();
if (!trimmed) return undefined;
const mentionMatch = trimmed.match(/^<@([A-Z0-9]+)>$/i);
if (mentionMatch) {
return buildTarget("user", mentionMatch[1], trimmed);
}
if (trimmed.startsWith("user:")) {
const id = trimmed.slice("user:".length).trim();
return id ? buildTarget("user", id, trimmed) : undefined;
}
if (trimmed.startsWith("channel:")) {
const id = trimmed.slice("channel:".length).trim();
return id ? buildTarget("channel", id, trimmed) : undefined;
}
if (trimmed.startsWith("slack:")) {
const id = trimmed.slice("slack:".length).trim();
return id ? buildTarget("user", id, trimmed) : undefined;
}
if (trimmed.startsWith("@")) {
const candidate = trimmed.slice(1).trim();
if (!/^[A-Z0-9]+$/i.test(candidate)) {
throw new Error("Slack DMs require a user id (use user:<id> or <@id>)");
}
return buildTarget("user", candidate, trimmed);
}
if (trimmed.startsWith("#")) {
const candidate = trimmed.slice(1).trim();
if (!/^[A-Z0-9]+$/i.test(candidate)) {
throw new Error("Slack channels require a channel id (use channel:<id>)");
}
return buildTarget("channel", candidate, trimmed);
}
if (options.defaultKind) {
return buildTarget(options.defaultKind, trimmed, trimmed);
}
return buildTarget("channel", trimmed, trimmed);
}
export function resolveSlackChannelId(raw: string): string {
const target = parseSlackTarget(raw, { defaultKind: "channel" });
if (!target) {
throw new Error("Slack channel id is required.");
}
if (target.kind !== "channel") {
throw new Error("Slack channel id is required (use channel:<id>).");
}
return target.id;
}