OpenWA icon indicating copy to clipboard operation
OpenWA copied to clipboard

refactor(engine): neutral MessageType instead of raw whatsapp-web.js type tokens (#265)

Open rmyndharis opened this issue 5 months ago • 0 comments

Second item of the #265 engine-pluggability decoupling: stop raw whatsapp-web.js MessageTypes tokens leaking past the engine adapter.

What changed

  • Neutral MessageType union (text|image|video|audio|voice|document|sticker|location|contact|revoked|unknown) on IncomingMessage.type.
  • message-mapper: new mapWwebjsMessageType (chat→text, ptt→voice, vcard/multi_vcard→contact, others passthrough/unknown) is the sole translation point. So incoming live + getChatHistory + persisted rows + the message.received/message.sent webhooks are now neutral — consistent with outgoing (which message.service already wrote as neutral).
  • Migration NeutralizeMessageTypes backfills existing messages.type rows. This matters because the dashboard chat view reads persisted rows (GET /messages), and stats group by type — without it, old chat/ptt/vcard rows would mis-render and split the by-type stats. Collision-free, idempotent, portable (SQLite/Postgres).
  • Dashboard: removed the dead wwebjs 'ptt' case from renderMedia; the latent chat-vs-text bug (incoming text mis-styled as media + shown as [chat] in reply previews) is fixed.
  • Docs + CHANGELOG updated.

Decisions (confirmed)

Neutralize type everywhere incl. the webhook — a contract change toward consistency. The adversarial review corrected my initial assumption twice (both load-bearing, both fixed here):

  1. The dashboard reads persisted rows (getMessages), not engine getChatHistory → hence the backfill migration (without it, old voice notes would regress to document links).
  2. The webhook change affects both message.received and message.sent (the sent webhook is dispatched from buildIncomingMessageBase, not the neutral DB write).

Webhook contract change for integrators: incoming/history type is now neutral — chat→text, ptt→voice, vcard→contact.

Verification

  • Backend build ✅ · 449 tests ✅ (added a mapWwebjsMessageType boundary test) · lint ✅ · dashboard build + lint ✅.
  • Migration SQL verified against an in-memory SQLite DB (legacy tokens mapped, neutral/passthrough untouched).
  • Adversarial review (3 dimensions, independently verified): all required changes (backfill migration, docs, dual-direction webhook note) incorporated.

Part of #265.

rmyndharis avatar Jun 16 '26 17:06 rmyndharis