mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:51:23 +00:00
refactor(channels): share target parsing helpers
This commit is contained in:
@@ -43,6 +43,30 @@ export function ensureTargetId(params: {
|
|||||||
return params.candidate;
|
return params.candidate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function parseTargetMention(params: {
|
||||||
|
raw: string;
|
||||||
|
mentionPattern: RegExp;
|
||||||
|
kind: MessagingTargetKind;
|
||||||
|
}): MessagingTarget | undefined {
|
||||||
|
const match = params.raw.match(params.mentionPattern);
|
||||||
|
if (!match?.[1]) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return buildMessagingTarget(params.kind, match[1], params.raw);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseTargetPrefix(params: {
|
||||||
|
raw: string;
|
||||||
|
prefix: string;
|
||||||
|
kind: MessagingTargetKind;
|
||||||
|
}): MessagingTarget | undefined {
|
||||||
|
if (!params.raw.startsWith(params.prefix)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const id = params.raw.slice(params.prefix.length).trim();
|
||||||
|
return id ? buildMessagingTarget(params.kind, id, params.raw) : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export function requireTargetKind(params: {
|
export function requireTargetKind(params: {
|
||||||
platform: string;
|
platform: string;
|
||||||
target: MessagingTarget | undefined;
|
target: MessagingTarget | undefined;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import type { DirectoryConfigParams } from "../channels/plugins/directory-config
|
|||||||
import {
|
import {
|
||||||
buildMessagingTarget,
|
buildMessagingTarget,
|
||||||
ensureTargetId,
|
ensureTargetId,
|
||||||
|
parseTargetMention,
|
||||||
|
parseTargetPrefix,
|
||||||
requireTargetKind,
|
requireTargetKind,
|
||||||
type MessagingTarget,
|
type MessagingTarget,
|
||||||
type MessagingTargetKind,
|
type MessagingTargetKind,
|
||||||
@@ -23,21 +25,32 @@ export function parseDiscordTarget(
|
|||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const mentionMatch = trimmed.match(/^<@!?(\d+)>$/);
|
const mentionTarget = parseTargetMention({
|
||||||
if (mentionMatch) {
|
raw: trimmed,
|
||||||
return buildMessagingTarget("user", mentionMatch[1], trimmed);
|
mentionPattern: /^<@!?(\d+)>$/,
|
||||||
|
kind: "user",
|
||||||
|
});
|
||||||
|
if (mentionTarget) {
|
||||||
|
return mentionTarget;
|
||||||
}
|
}
|
||||||
if (trimmed.startsWith("user:")) {
|
const prefixedTarget =
|
||||||
const id = trimmed.slice("user:".length).trim();
|
parseTargetPrefix({
|
||||||
return id ? buildMessagingTarget("user", id, trimmed) : undefined;
|
raw: trimmed,
|
||||||
}
|
prefix: "user:",
|
||||||
if (trimmed.startsWith("channel:")) {
|
kind: "user",
|
||||||
const id = trimmed.slice("channel:".length).trim();
|
}) ??
|
||||||
return id ? buildMessagingTarget("channel", id, trimmed) : undefined;
|
parseTargetPrefix({
|
||||||
}
|
raw: trimmed,
|
||||||
if (trimmed.startsWith("discord:")) {
|
prefix: "channel:",
|
||||||
const id = trimmed.slice("discord:".length).trim();
|
kind: "channel",
|
||||||
return id ? buildMessagingTarget("user", id, trimmed) : undefined;
|
}) ??
|
||||||
|
parseTargetPrefix({
|
||||||
|
raw: trimmed,
|
||||||
|
prefix: "discord:",
|
||||||
|
kind: "user",
|
||||||
|
});
|
||||||
|
if (prefixedTarget) {
|
||||||
|
return prefixedTarget;
|
||||||
}
|
}
|
||||||
if (trimmed.startsWith("@")) {
|
if (trimmed.startsWith("@")) {
|
||||||
const candidate = trimmed.slice(1).trim();
|
const candidate = trimmed.slice(1).trim();
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
buildMessagingTarget,
|
buildMessagingTarget,
|
||||||
ensureTargetId,
|
ensureTargetId,
|
||||||
|
parseTargetMention,
|
||||||
|
parseTargetPrefix,
|
||||||
requireTargetKind,
|
requireTargetKind,
|
||||||
type MessagingTarget,
|
type MessagingTarget,
|
||||||
type MessagingTargetKind,
|
type MessagingTargetKind,
|
||||||
@@ -21,21 +23,32 @@ export function parseSlackTarget(
|
|||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const mentionMatch = trimmed.match(/^<@([A-Z0-9]+)>$/i);
|
const mentionTarget = parseTargetMention({
|
||||||
if (mentionMatch) {
|
raw: trimmed,
|
||||||
return buildMessagingTarget("user", mentionMatch[1], trimmed);
|
mentionPattern: /^<@([A-Z0-9]+)>$/i,
|
||||||
|
kind: "user",
|
||||||
|
});
|
||||||
|
if (mentionTarget) {
|
||||||
|
return mentionTarget;
|
||||||
}
|
}
|
||||||
if (trimmed.startsWith("user:")) {
|
const prefixedTarget =
|
||||||
const id = trimmed.slice("user:".length).trim();
|
parseTargetPrefix({
|
||||||
return id ? buildMessagingTarget("user", id, trimmed) : undefined;
|
raw: trimmed,
|
||||||
}
|
prefix: "user:",
|
||||||
if (trimmed.startsWith("channel:")) {
|
kind: "user",
|
||||||
const id = trimmed.slice("channel:".length).trim();
|
}) ??
|
||||||
return id ? buildMessagingTarget("channel", id, trimmed) : undefined;
|
parseTargetPrefix({
|
||||||
}
|
raw: trimmed,
|
||||||
if (trimmed.startsWith("slack:")) {
|
prefix: "channel:",
|
||||||
const id = trimmed.slice("slack:".length).trim();
|
kind: "channel",
|
||||||
return id ? buildMessagingTarget("user", id, trimmed) : undefined;
|
}) ??
|
||||||
|
parseTargetPrefix({
|
||||||
|
raw: trimmed,
|
||||||
|
prefix: "slack:",
|
||||||
|
kind: "user",
|
||||||
|
});
|
||||||
|
if (prefixedTarget) {
|
||||||
|
return prefixedTarget;
|
||||||
}
|
}
|
||||||
if (trimmed.startsWith("@")) {
|
if (trimmed.startsWith("@")) {
|
||||||
const candidate = trimmed.slice(1).trim();
|
const candidate = trimmed.slice(1).trim();
|
||||||
|
|||||||
Reference in New Issue
Block a user