mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 18:50:21 +00:00
Harden Telegram poll gating and schema consistency (#36547)
Merged via squash.
Prepared head SHA: f77824419e
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
committed by
GitHub
parent
f771ba8de9
commit
6dfd39c32f
89
src/poll-params.ts
Normal file
89
src/poll-params.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
export type PollCreationParamKind = "string" | "stringArray" | "number" | "boolean";
|
||||
|
||||
export type PollCreationParamDef = {
|
||||
kind: PollCreationParamKind;
|
||||
telegramOnly?: boolean;
|
||||
};
|
||||
|
||||
export const POLL_CREATION_PARAM_DEFS: Record<string, PollCreationParamDef> = {
|
||||
pollQuestion: { kind: "string" },
|
||||
pollOption: { kind: "stringArray" },
|
||||
pollDurationHours: { kind: "number" },
|
||||
pollMulti: { kind: "boolean" },
|
||||
pollDurationSeconds: { kind: "number", telegramOnly: true },
|
||||
pollAnonymous: { kind: "boolean", telegramOnly: true },
|
||||
pollPublic: { kind: "boolean", telegramOnly: true },
|
||||
};
|
||||
|
||||
export type PollCreationParamName = keyof typeof POLL_CREATION_PARAM_DEFS;
|
||||
|
||||
export const POLL_CREATION_PARAM_NAMES = Object.keys(POLL_CREATION_PARAM_DEFS);
|
||||
|
||||
function toSnakeCaseKey(key: string): string {
|
||||
return key
|
||||
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
|
||||
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
function readPollParamRaw(params: Record<string, unknown>, key: string): unknown {
|
||||
if (Object.hasOwn(params, key)) {
|
||||
return params[key];
|
||||
}
|
||||
const snakeKey = toSnakeCaseKey(key);
|
||||
if (snakeKey !== key && Object.hasOwn(params, snakeKey)) {
|
||||
return params[snakeKey];
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resolveTelegramPollVisibility(params: {
|
||||
pollAnonymous?: boolean;
|
||||
pollPublic?: boolean;
|
||||
}): boolean | undefined {
|
||||
if (params.pollAnonymous && params.pollPublic) {
|
||||
throw new Error("pollAnonymous and pollPublic are mutually exclusive");
|
||||
}
|
||||
return params.pollAnonymous ? true : params.pollPublic ? false : undefined;
|
||||
}
|
||||
|
||||
export function hasPollCreationParams(params: Record<string, unknown>): boolean {
|
||||
for (const key of POLL_CREATION_PARAM_NAMES) {
|
||||
const def = POLL_CREATION_PARAM_DEFS[key];
|
||||
const value = readPollParamRaw(params, key);
|
||||
if (def.kind === "string" && typeof value === "string" && value.trim().length > 0) {
|
||||
return true;
|
||||
}
|
||||
if (def.kind === "stringArray") {
|
||||
if (
|
||||
Array.isArray(value) &&
|
||||
value.some((entry) => typeof entry === "string" && entry.trim())
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (typeof value === "string" && value.trim().length > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (def.kind === "number") {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return true;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.length > 0 && Number.isFinite(Number(trimmed))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (def.kind === "boolean") {
|
||||
if (value === true) {
|
||||
return true;
|
||||
}
|
||||
if (typeof value === "string" && value.trim().toLowerCase() === "true") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user