import { callQiaoqiaoAgentApi } from "../tools-common/qiaoqiao-api.js"; import type { ResolvedQiaoqiaoAccount } from "../types.js"; import type { QiaoqiaoCommentParams } from "./schemas.js"; type CommentListResponse = { comments?: unknown[]; commentCount?: number; commentsClosed?: boolean; }; export async function runCommentAction( account: ResolvedQiaoqiaoAccount, params: QiaoqiaoCommentParams, ): Promise { const action = params.action ?? "create"; const accountConfig = account.config as { apiBase?: string }; const apiBase = accountConfig.apiBase; if (action === "delete") { const commentId = String(params.comment_id || "").trim(); if (!commentId) { throw new Error("comment_id is required for delete"); } const data = await callQiaoqiaoAgentApi({ method: "DELETE", path: `/posts/comments/${encodeURIComponent(commentId)}`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, comment_id: commentId, data, }; } const postId = String(params.post_id || "").trim(); if (!postId) { throw new Error(`post_id is required for ${action}`); } if (action === "list") { const limit = Math.min(Math.max(params.limit ?? 20, 1), 50); const offset = Math.max(params.offset ?? 0, 0); const query = new URLSearchParams({ limit: String(limit), offset: String(offset), }); const data = await callQiaoqiaoAgentApi({ method: "GET", path: `/posts/${encodeURIComponent(postId)}/comments?${query.toString()}`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, post_id: postId, total: Number(data.commentCount ?? (Array.isArray(data.comments) ? data.comments.length : 0)), commentsClosed: Boolean(data.commentsClosed), comments: data.comments ?? [], }; } if (action === "interactions") { const interactions = await callQiaoqiaoAgentApi({ method: "GET", path: `/posts/${encodeURIComponent(postId)}/interactions`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, post_id: postId, interactions, }; } const content = String(params.content || "").trim(); if (!content) { throw new Error("content is required for create"); } const comment = await callQiaoqiaoAgentApi({ method: "POST", path: `/posts/${encodeURIComponent(postId)}/comments`, appId: account.appId!, appSecret: account.appSecret!, apiBase, body: { content, ...(params.parentId?.trim() ? { parentId: params.parentId.trim() } : {}), }, }); return { ok: true, action, post_id: postId, comment, }; }