import { callQiaoqiaoAgentApi } from "../tools-common/qiaoqiao-api.js"; import type { ResolvedQiaoqiaoAccount } from "../types.js"; import type { QiaoqiaoPostParams } from "./schemas.js"; export async function runPostAction( account: ResolvedQiaoqiaoAccount, params: QiaoqiaoPostParams, ): Promise { const action = params.action ?? "create"; const accountConfig = account.config as { apiBase?: string }; const apiBase = accountConfig.apiBase; if (action === "batch_details") { const postIds = (params.postIds ?? []).map((postId) => String(postId).trim()).filter(Boolean); if (postIds.length === 0) { throw new Error("postIds is required for batch_details"); } const posts = await callQiaoqiaoAgentApi>({ method: "POST", path: "/posts/batch-details", appId: account.appId!, appSecret: account.appSecret!, apiBase, body: { postIds }, }); return { ok: true, action, total: Object.keys(posts).length, posts, }; } if (action === "delete") { const postId = String(params.post_id || "").trim(); if (!postId) { throw new Error("post_id is required for delete"); } const data = await callQiaoqiaoAgentApi({ method: "DELETE", path: `/posts/${encodeURIComponent(postId)}`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, post_id: postId, data, }; } if (action === "like") { const postId = String(params.post_id || "").trim(); if (!postId) { throw new Error("post_id is required for like"); } const data = await callQiaoqiaoAgentApi<{ liked?: boolean; likeCount?: number; postId?: string; }>({ method: "POST", path: `/posts/${encodeURIComponent(postId)}/like`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, post_id: postId, liked: Boolean(data.liked), likeCount: Number(data.likeCount ?? 0), data, }; } if (action === "unlike") { const postId = String(params.post_id || "").trim(); if (!postId) { throw new Error("post_id is required for unlike"); } const data = await callQiaoqiaoAgentApi<{ liked?: boolean; alreadyUnliked?: boolean; likeCount?: number; postId?: string; }>({ method: "DELETE", path: `/posts/${encodeURIComponent(postId)}/like`, appId: account.appId!, appSecret: account.appSecret!, apiBase, }); return { ok: true, action, post_id: postId, liked: Boolean(data.liked), alreadyUnliked: Boolean(data.alreadyUnliked), likeCount: Number(data.likeCount ?? 0), data, }; } const content = (params.content ?? "").trim(); if (!content) { throw new Error("content is required for create"); } const post = await callQiaoqiaoAgentApi({ method: "POST", path: "/posts", appId: account.appId!, appSecret: account.appSecret!, apiBase, body: { content, images: params.images ?? [], }, }); return { ok: true, action, post, }; }