mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:04:32 +00:00
refactor(line): share source info parsing
This commit is contained in:
@@ -45,6 +45,17 @@ const { buildLineMessageContextMock, buildLinePostbackContextMock } = vi.hoisted
|
|||||||
vi.mock("./bot-message-context.js", () => ({
|
vi.mock("./bot-message-context.js", () => ({
|
||||||
buildLineMessageContext: (...args: unknown[]) => buildLineMessageContextMock(...args),
|
buildLineMessageContext: (...args: unknown[]) => buildLineMessageContextMock(...args),
|
||||||
buildLinePostbackContext: (...args: unknown[]) => buildLinePostbackContextMock(...args),
|
buildLinePostbackContext: (...args: unknown[]) => buildLinePostbackContextMock(...args),
|
||||||
|
getLineSourceInfo: (source: {
|
||||||
|
type?: string;
|
||||||
|
userId?: string;
|
||||||
|
groupId?: string;
|
||||||
|
roomId?: string;
|
||||||
|
}) => ({
|
||||||
|
userId: source.userId,
|
||||||
|
groupId: source.type === "group" ? source.groupId : undefined,
|
||||||
|
roomId: source.type === "room" ? source.roomId : undefined,
|
||||||
|
isGroup: source.type === "group" || source.type === "room",
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { readAllowFromStoreMock, upsertPairingRequestMock } = vi.hoisted(() => ({
|
const { readAllowFromStoreMock, upsertPairingRequestMock } = vi.hoisted(() => ({
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
} from "../pairing/pairing-store.js";
|
} from "../pairing/pairing-store.js";
|
||||||
import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js";
|
import { firstDefined, isSenderAllowed, normalizeAllowFromWithStore } from "./bot-access.js";
|
||||||
import {
|
import {
|
||||||
|
getLineSourceInfo,
|
||||||
buildLineMessageContext,
|
buildLineMessageContext,
|
||||||
buildLinePostbackContext,
|
buildLinePostbackContext,
|
||||||
type LineInboundContext,
|
type LineInboundContext,
|
||||||
@@ -40,28 +41,6 @@ export interface LineHandlerContext {
|
|||||||
processMessage: (ctx: LineInboundContext) => Promise<void>;
|
processMessage: (ctx: LineInboundContext) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type LineSourceInfo = {
|
|
||||||
userId?: string;
|
|
||||||
groupId?: string;
|
|
||||||
roomId?: string;
|
|
||||||
isGroup: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
function getSourceInfo(source: EventSource): LineSourceInfo {
|
|
||||||
const userId =
|
|
||||||
source.type === "user"
|
|
||||||
? source.userId
|
|
||||||
: source.type === "group"
|
|
||||||
? source.userId
|
|
||||||
: source.type === "room"
|
|
||||||
? source.userId
|
|
||||||
: undefined;
|
|
||||||
const groupId = source.type === "group" ? source.groupId : undefined;
|
|
||||||
const roomId = source.type === "room" ? source.roomId : undefined;
|
|
||||||
const isGroup = source.type === "group" || source.type === "room";
|
|
||||||
return { userId, groupId, roomId, isGroup };
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveLineGroupConfig(params: {
|
function resolveLineGroupConfig(params: {
|
||||||
config: ResolvedLineAccount["config"];
|
config: ResolvedLineAccount["config"];
|
||||||
groupId?: string;
|
groupId?: string;
|
||||||
@@ -129,7 +108,7 @@ async function shouldProcessLineEvent(
|
|||||||
context: LineHandlerContext,
|
context: LineHandlerContext,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const { cfg, account } = context;
|
const { cfg, account } = context;
|
||||||
const { userId, groupId, roomId, isGroup } = getSourceInfo(event.source);
|
const { userId, groupId, roomId, isGroup } = getLineSourceInfo(event.source);
|
||||||
const senderId = userId ?? "";
|
const senderId = userId ?? "";
|
||||||
|
|
||||||
const storeAllowFrom = await readChannelAllowFromStore("line").catch(() => []);
|
const storeAllowFrom = await readChannelAllowFromStore("line").catch(() => []);
|
||||||
|
|||||||
@@ -26,12 +26,14 @@ interface BuildLineMessageContextParams {
|
|||||||
account: ResolvedLineAccount;
|
account: ResolvedLineAccount;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSourceInfo(source: EventSource): {
|
export type LineSourceInfo = {
|
||||||
userId?: string;
|
userId?: string;
|
||||||
groupId?: string;
|
groupId?: string;
|
||||||
roomId?: string;
|
roomId?: string;
|
||||||
isGroup: boolean;
|
isGroup: boolean;
|
||||||
} {
|
};
|
||||||
|
|
||||||
|
export function getLineSourceInfo(source: EventSource): LineSourceInfo {
|
||||||
const userId =
|
const userId =
|
||||||
source.type === "user"
|
source.type === "user"
|
||||||
? source.userId
|
? source.userId
|
||||||
@@ -78,7 +80,7 @@ function resolveLineInboundRoute(params: {
|
|||||||
direction: "inbound",
|
direction: "inbound",
|
||||||
});
|
});
|
||||||
|
|
||||||
const { userId, groupId, roomId, isGroup } = getSourceInfo(params.source);
|
const { userId, groupId, roomId, isGroup } = getLineSourceInfo(params.source);
|
||||||
const peerId = buildPeerId(params.source);
|
const peerId = buildPeerId(params.source);
|
||||||
const route = resolveAgentRoute({
|
const route = resolveAgentRoute({
|
||||||
cfg: params.cfg,
|
cfg: params.cfg,
|
||||||
|
|||||||
Reference in New Issue
Block a user