Skip to content

fix: dedup duplicate publish dispatches and wait for Toutiao image upload#209

Open
xiaopanddxiong wants to merge 4 commits intoleaperone:mainfrom
xiaopanddxiong:fix/dedup-publish-and-image-upload-wait
Open

fix: dedup duplicate publish dispatches and wait for Toutiao image upload#209
xiaopanddxiong wants to merge 4 commits intoleaperone:mainfrom
xiaopanddxiong:fix/dedup-publish-and-image-upload-wait

Conversation

@xiaopanddxiong
Copy link
Copy Markdown

What & Why

While integrating MultiPost via the REST API for an automated publishing pipeline, I hit two reliability issues. Both are fixed here.

1. Same content gets published 2-3 times per task

Symptom: One REST API task triggers Chrome to instantly open 2-3 publish tabs, and the same content gets posted 2-3 times.

Root cause: When the extension receives NEW_TASK from ping, it opens multipost.app/on-task?taskId=.... That page then dispatches chrome.runtime.sendMessage({ action: "MULTIPOST_EXTENSION_PUBLISH", data }) — but it dispatches it multiple times (likely React StrictMode useEffect double-invocation, or other internal logic). The background handler had no dedup, so each dispatch ran the full publish flow.

I confirmed this by inspecting Service Worker network — only one ping returned NEW_TASK, but chrome.windows.create({ url: "tabs/publish.html" }) was being called 3 times.

Fix (src/background/index.ts):

  • Add a 60-second sliding-window dedup keyed by content fingerprint (platforms + image count + first 256 chars of content/title) on the MULTIPOST_EXTENSION_PUBLISH handler.
  • Duplicates within the window are dropped with a log line.

Also added a defensive in-memory dedup on NEW_TASK task URLs in src/background/services/api.ts (LRU-capped at 500 entries) to guard against ping returning the same task multiple times.

2. Toutiao dynamic post: image is missing when uploading large images

Symptom: For images larger than ~3-5 MB, the post goes live with no image attached.

Root cause: src/sync/dynamic/toutiao.ts had a hard-coded setTimeout(5000) between dispatching the file input and clicking the upload-confirm button. For large images, 5 seconds isn't enough — the confirm button is still disabled, the click is a no-op, and the post is published without the image.

Fix:

  • Replace the fixed 5s wait with a polling loop (max 60s) that waits for button[data-e2e="imageUploadConfirm-btn"] to become non-disabled before clicking.
  • After clicking confirm, wait (max 30s) for the modal to disappear and the inserted thumbnail to appear in the editor before proceeding.

How tested

  • Triggered a publish task via the REST API with a 5.4 MB PNG.
  • Before fix: 3 publish tabs spawned, post sent 3 times, image sometimes missing.
  • After fix: exactly 1 publish tab, 1 post on Toutiao with the image attached. Service Worker console shows [MultiPost] skip duplicate PUBLISH request for the suppressed dispatches.

Files changed

  • src/background/index.ts — content-fingerprint dedup for MULTIPOST_EXTENSION_PUBLISH
  • src/background/services/api.ts — task URL dedup for NEW_TASK
  • src/sync/dynamic/toutiao.ts — poll for upload completion instead of fixed 5s wait

No new dependencies, no behavioral change for the happy path.

1) src/background/index.ts: 对 MULTIPOST_EXTENSION_PUBLISH 增加指纹去重
   (60s 窗口, key=平台+图片数+内容前256字符), 解决 multipost.app
   on-task 网页对扩展重复 dispatch 同一发布请求导致同条内容被发布
   多次的问题.

2) src/background/services/api.ts: 对 NEW_TASK 的 task URL 增加内存
   级去重 (LRU 上限 500), 兜底 ping 多次返回同一 taskId 的情况.

3) src/sync/dynamic/toutiao.ts: 将固定 5s 等待替换为轮询确认按钮
   可点击 (最长 60s) + 等待弹窗关闭与缩略图出现 (最长 30s),
   解决大图(>5MB) 上传未完成就触发发布按钮导致图片丢失的问题.
@leaperone-bot
Copy link
Copy Markdown

leaperone-bot commented May 3, 2026

🤖 Leo

Code Review

⏳ 等待审查...

最后更新: 2026-05-07 15:56 · beb348c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants