Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/eve/src/public/channels/slack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export {

export { defaultSlackAuth } from "#public/channels/slack/defaults.js";

export {
slackUserGroupMentions,
withoutSlackUserGroupMention,
type SlackUserGroupMention,
} from "#public/channels/slack/user-groups.js";

export {
describeActionRequest,
describeActionRequests,
Expand Down
22 changes: 22 additions & 0 deletions packages/eve/src/public/channels/slack/user-groups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";

import {
slackUserGroupMentions,
withoutSlackUserGroupMention,
} from "#public/channels/slack/user-groups.js";

describe("slackUserGroupMentions", () => {
it("returns unique opaque group ids in mention order", () => {
expect(
slackUserGroupMentions("Ask <!subteam^S123|preview> then <!subteam^S456>. <!subteam^S123>"),
).toEqual([{ id: "S123" }, { id: "S456" }]);
});
});

describe("withoutSlackUserGroupMention", () => {
it("removes only the selected group mention", () => {
expect(
withoutSlackUserGroupMention("<!subteam^S123|preview> check <!subteam^S456>", "S123"),
).toBe("check <!subteam^S456>");
});
});
34 changes: 34 additions & 0 deletions packages/eve/src/public/channels/slack/user-groups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const USER_GROUP_MENTION = /<!subteam\^([A-Z0-9]+)(?:\|[^>]+)?>/gu;

/** One Slack user-group mention found in message text. */
export interface SlackUserGroupMention {
readonly id: string;
}

/**
* Returns unique Slack user-group ids mentioned in text in first-mention
* order. The parser deliberately retains Slack's opaque id rather than a
* mutable display handle so channel-owned registries can verify ownership.
*/
export function slackUserGroupMentions(text: string): readonly SlackUserGroupMention[] {
const seen = new Set<string>();
const mentions: SlackUserGroupMention[] = [];
for (const match of text.matchAll(USER_GROUP_MENTION)) {
const id = match[1];
if (id === undefined || seen.has(id)) continue;
seen.add(id);
mentions.push({ id });
}
return mentions;
}

/**
* Removes one recognized user-group mention using the same whitespace
* normalization callers use for an empty Slack app mention.
*/
export function withoutSlackUserGroupMention(text: string, userGroupId: string): string {
return text
.replace(USER_GROUP_MENTION, (mention, id: string) => (id === userGroupId ? "" : mention))
.replace(/\s+/gu, " ")
.trim();
}
Loading