mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 17:54:32 +00:00
refactor(signal): share reaction send helper
This commit is contained in:
@@ -22,6 +22,13 @@ export type SignalReactionResult = {
|
|||||||
timestamp?: number;
|
timestamp?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type SignalReactionErrorMessages = {
|
||||||
|
missingRecipient: string;
|
||||||
|
invalidTargetTimestamp: string;
|
||||||
|
missingEmoji: string;
|
||||||
|
missingTargetAuthor: string;
|
||||||
|
};
|
||||||
|
|
||||||
function normalizeSignalId(raw: string): string {
|
function normalizeSignalId(raw: string): string {
|
||||||
const trimmed = raw.trim();
|
const trimmed = raw.trim();
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
@@ -60,6 +67,69 @@ function resolveTargetAuthorParams(params: {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function sendReactionSignalCore(params: {
|
||||||
|
recipient: string;
|
||||||
|
targetTimestamp: number;
|
||||||
|
emoji: string;
|
||||||
|
remove: boolean;
|
||||||
|
opts: SignalReactionOpts;
|
||||||
|
errors: SignalReactionErrorMessages;
|
||||||
|
}): Promise<SignalReactionResult> {
|
||||||
|
const accountInfo = resolveSignalAccount({
|
||||||
|
cfg: loadConfig(),
|
||||||
|
accountId: params.opts.accountId,
|
||||||
|
});
|
||||||
|
const { baseUrl, account } = resolveSignalRpcContext(params.opts, accountInfo);
|
||||||
|
|
||||||
|
const normalizedRecipient = normalizeSignalUuid(params.recipient);
|
||||||
|
const groupId = params.opts.groupId?.trim();
|
||||||
|
if (!normalizedRecipient && !groupId) {
|
||||||
|
throw new Error(params.errors.missingRecipient);
|
||||||
|
}
|
||||||
|
if (!Number.isFinite(params.targetTimestamp) || params.targetTimestamp <= 0) {
|
||||||
|
throw new Error(params.errors.invalidTargetTimestamp);
|
||||||
|
}
|
||||||
|
const normalizedEmoji = params.emoji?.trim();
|
||||||
|
if (!normalizedEmoji) {
|
||||||
|
throw new Error(params.errors.missingEmoji);
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetAuthorParams = resolveTargetAuthorParams({
|
||||||
|
targetAuthor: params.opts.targetAuthor,
|
||||||
|
targetAuthorUuid: params.opts.targetAuthorUuid,
|
||||||
|
fallback: normalizedRecipient,
|
||||||
|
});
|
||||||
|
if (groupId && !targetAuthorParams.targetAuthor) {
|
||||||
|
throw new Error(params.errors.missingTargetAuthor);
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestParams: Record<string, unknown> = {
|
||||||
|
emoji: normalizedEmoji,
|
||||||
|
targetTimestamp: params.targetTimestamp,
|
||||||
|
...(params.remove ? { remove: true } : {}),
|
||||||
|
...targetAuthorParams,
|
||||||
|
};
|
||||||
|
if (normalizedRecipient) {
|
||||||
|
requestParams.recipients = [normalizedRecipient];
|
||||||
|
}
|
||||||
|
if (groupId) {
|
||||||
|
requestParams.groupIds = [groupId];
|
||||||
|
}
|
||||||
|
if (account) {
|
||||||
|
requestParams.account = account;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await signalRpcRequest<{ timestamp?: number }>("sendReaction", requestParams, {
|
||||||
|
baseUrl,
|
||||||
|
timeoutMs: params.opts.timeoutMs,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
timestamp: result?.timestamp,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a Signal reaction to a message
|
* Send a Signal reaction to a message
|
||||||
* @param recipient - UUID or E.164 phone number of the message author
|
* @param recipient - UUID or E.164 phone number of the message author
|
||||||
@@ -73,57 +143,19 @@ export async function sendReactionSignal(
|
|||||||
emoji: string,
|
emoji: string,
|
||||||
opts: SignalReactionOpts = {},
|
opts: SignalReactionOpts = {},
|
||||||
): Promise<SignalReactionResult> {
|
): Promise<SignalReactionResult> {
|
||||||
const accountInfo = resolveSignalAccount({
|
return await sendReactionSignalCore({
|
||||||
cfg: loadConfig(),
|
recipient,
|
||||||
accountId: opts.accountId,
|
|
||||||
});
|
|
||||||
const { baseUrl, account } = resolveSignalRpcContext(opts, accountInfo);
|
|
||||||
|
|
||||||
const normalizedRecipient = normalizeSignalUuid(recipient);
|
|
||||||
const groupId = opts.groupId?.trim();
|
|
||||||
if (!normalizedRecipient && !groupId) {
|
|
||||||
throw new Error("Recipient or groupId is required for Signal reaction");
|
|
||||||
}
|
|
||||||
if (!Number.isFinite(targetTimestamp) || targetTimestamp <= 0) {
|
|
||||||
throw new Error("Valid targetTimestamp is required for Signal reaction");
|
|
||||||
}
|
|
||||||
if (!emoji?.trim()) {
|
|
||||||
throw new Error("Emoji is required for Signal reaction");
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetAuthorParams = resolveTargetAuthorParams({
|
|
||||||
targetAuthor: opts.targetAuthor,
|
|
||||||
targetAuthorUuid: opts.targetAuthorUuid,
|
|
||||||
fallback: normalizedRecipient,
|
|
||||||
});
|
|
||||||
if (groupId && !targetAuthorParams.targetAuthor) {
|
|
||||||
throw new Error("targetAuthor is required for group reactions");
|
|
||||||
}
|
|
||||||
|
|
||||||
const params: Record<string, unknown> = {
|
|
||||||
emoji: emoji.trim(),
|
|
||||||
targetTimestamp,
|
targetTimestamp,
|
||||||
...targetAuthorParams,
|
emoji,
|
||||||
};
|
remove: false,
|
||||||
if (normalizedRecipient) {
|
opts,
|
||||||
params.recipients = [normalizedRecipient];
|
errors: {
|
||||||
}
|
missingRecipient: "Recipient or groupId is required for Signal reaction",
|
||||||
if (groupId) {
|
invalidTargetTimestamp: "Valid targetTimestamp is required for Signal reaction",
|
||||||
params.groupIds = [groupId];
|
missingEmoji: "Emoji is required for Signal reaction",
|
||||||
}
|
missingTargetAuthor: "targetAuthor is required for group reactions",
|
||||||
if (account) {
|
},
|
||||||
params.account = account;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await signalRpcRequest<{ timestamp?: number }>("sendReaction", params, {
|
|
||||||
baseUrl,
|
|
||||||
timeoutMs: opts.timeoutMs,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
ok: true,
|
|
||||||
timestamp: result?.timestamp,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,56 +171,17 @@ export async function removeReactionSignal(
|
|||||||
emoji: string,
|
emoji: string,
|
||||||
opts: SignalReactionOpts = {},
|
opts: SignalReactionOpts = {},
|
||||||
): Promise<SignalReactionResult> {
|
): Promise<SignalReactionResult> {
|
||||||
const accountInfo = resolveSignalAccount({
|
return await sendReactionSignalCore({
|
||||||
cfg: loadConfig(),
|
recipient,
|
||||||
accountId: opts.accountId,
|
|
||||||
});
|
|
||||||
const { baseUrl, account } = resolveSignalRpcContext(opts, accountInfo);
|
|
||||||
|
|
||||||
const normalizedRecipient = normalizeSignalUuid(recipient);
|
|
||||||
const groupId = opts.groupId?.trim();
|
|
||||||
if (!normalizedRecipient && !groupId) {
|
|
||||||
throw new Error("Recipient or groupId is required for Signal reaction removal");
|
|
||||||
}
|
|
||||||
if (!Number.isFinite(targetTimestamp) || targetTimestamp <= 0) {
|
|
||||||
throw new Error("Valid targetTimestamp is required for Signal reaction removal");
|
|
||||||
}
|
|
||||||
if (!emoji?.trim()) {
|
|
||||||
throw new Error("Emoji is required for Signal reaction removal");
|
|
||||||
}
|
|
||||||
|
|
||||||
const targetAuthorParams = resolveTargetAuthorParams({
|
|
||||||
targetAuthor: opts.targetAuthor,
|
|
||||||
targetAuthorUuid: opts.targetAuthorUuid,
|
|
||||||
fallback: normalizedRecipient,
|
|
||||||
});
|
|
||||||
if (groupId && !targetAuthorParams.targetAuthor) {
|
|
||||||
throw new Error("targetAuthor is required for group reaction removal");
|
|
||||||
}
|
|
||||||
|
|
||||||
const params: Record<string, unknown> = {
|
|
||||||
emoji: emoji.trim(),
|
|
||||||
targetTimestamp,
|
targetTimestamp,
|
||||||
|
emoji,
|
||||||
remove: true,
|
remove: true,
|
||||||
...targetAuthorParams,
|
opts,
|
||||||
};
|
errors: {
|
||||||
if (normalizedRecipient) {
|
missingRecipient: "Recipient or groupId is required for Signal reaction removal",
|
||||||
params.recipients = [normalizedRecipient];
|
invalidTargetTimestamp: "Valid targetTimestamp is required for Signal reaction removal",
|
||||||
}
|
missingEmoji: "Emoji is required for Signal reaction removal",
|
||||||
if (groupId) {
|
missingTargetAuthor: "targetAuthor is required for group reaction removal",
|
||||||
params.groupIds = [groupId];
|
},
|
||||||
}
|
|
||||||
if (account) {
|
|
||||||
params.account = account;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await signalRpcRequest<{ timestamp?: number }>("sendReaction", params, {
|
|
||||||
baseUrl,
|
|
||||||
timeoutMs: opts.timeoutMs,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
ok: true,
|
|
||||||
timestamp: result?.timestamp,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user