File size: 3,402 Bytes
d00c949
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
{
  "id": "940c2663-42d7-474f-b201-bec963d26840",
  "title": "themeAI",
  "content": "\"use strict\";\n\nmodule.exports = function (defaultFuncs, api, ctx) {\n  const { parseAndCheckLogin, CustomError } = require(\"../utils\");\n\n  function normId(v) {\n    if (v == null) return v;\n    const s = String(v).trim();\n    if (/^\\d+$/.test(s)) return s;\n    if (s[0] === \"#\") return s.slice(1).toLowerCase();\n    if (/^[0-9a-fA-F]{6,8}$/.test(s)) return s.toLowerCase();\n    return s;\n  }\n\n  const post = (url, form) =>\n    defaultFuncs.post(url, ctx.jar, form).then(res => parseAndCheckLogin(ctx, defaultFuncs)(res));\n\n  async function setTheme(themeOrColor, threadID) {\n    const form = {\n      dpr: 1,\n      queries: JSON.stringify({\n        o0: {\n          doc_id: \"1727493033983591\",\n          query_params: {\n            data: {\n              actor_id: ctx.i_userID || ctx.userID,\n              client_mutation_id: \"0\",\n              source: \"SETTINGS\",\n              theme_id: normId(themeOrColor),\n              thread_id: threadID\n            }\n          }\n        }\n      })\n    };\n    const r = await post(\"https://www.facebook.com/api/graphqlbatch/\", form);\n    const tail = r && r[r.length - 1];\n    if (tail && tail.error_results > 0) throw new CustomError(r[0]?.o0?.errors || r);\n    return { success: true, themeId: normId(themeOrColor) };\n  }\n\n  async function genAI(prompt) {\n    const form = {\n      av: ctx.userID,\n      __user: ctx.userID,\n      fb_api_caller_class: \"RelayModern\",\n      fb_api_req_friendly_name: \"useGenerateAIThemeMutation\",\n      variables: JSON.stringify({\n        input: {\n          actor_id: ctx.userID,\n          client_mutation_id: \"1\",\n          bypass_cache: true,\n          caller: \"MESSENGER\",\n          num_themes: 1,\n          prompt\n        }\n      }),\n      doc_id: \"23873748445608673\"\n    };\n    const data = await post(\"https://www.facebook.com/api/graphql/\", form);\n    const t = data?.data?.xfb_generate_ai_themes_from_prompt?.themes?.[0];\n    if (!t?.id) throw new Error(`Không lấy được theme từ response.\\nRaw=${JSON.stringify(data)}`);\n    const dark = Array.isArray(t.alternative_themes) && t.alternative_themes.find(x => x?.app_color_mode === \"DARK\");\n    return {\n      themeId: t.id,\n      themeName: t.accessibility_label || t.name || t.display_name || \"\",\n      lightUrl: t?.background_asset?.image?.uri || null,\n      darkUrl: dark?.background_asset?.image?.uri || null\n    };\n  }\n\n  return function theme(threadID, options = {}, callback) {\n    const run = async () => {\n      if (!threadID) throw new Error(\"Thiếu threadID\");\n      const { themeId, aiPrompt, dryRun } = options;\n\n      if (themeId && !aiPrompt) return setTheme(themeId, threadID);\n\n      if (aiPrompt) {\n        const info = await genAI(aiPrompt);\n        if (dryRun) return { preview: true, ...info };\n        const applied = await setTheme(info.themeId, threadID);\n        return { success: true, ...info, apply: applied };\n      }\n\n      throw new Error(\"Thiếu options.aiPrompt hoặc options.themeId\");\n    };\n\n    if (typeof callback === \"function\") run().then(d => callback(null, d)).catch(e => callback(e));\n    else return run();\n  };\n};",
  "language": "javascript",
  "createdAt": 1755759306429,
  "updatedAt": 1755759306429
}