OpenWA
OpenWA copied to clipboard
refactor(engine): neutral MessageType instead of raw whatsapp-web.js type tokens (#265)
Second item of the #265 engine-pluggability decoupling: stop raw whatsapp-web.js MessageTypes tokens leaking past the engine adapter.
What changed
- Neutral
MessageTypeunion (text|image|video|audio|voice|document|sticker|location|contact|revoked|unknown) onIncomingMessage.type. message-mapper: newmapWwebjsMessageType(chat→text,ptt→voice,vcard/multi_vcard→contact, others passthrough/unknown) is the sole translation point. So incoming live +getChatHistory+ persisted rows + themessage.received/message.sentwebhooks are now neutral — consistent with outgoing (whichmessage.servicealready wrote as neutral).- Migration
NeutralizeMessageTypesbackfills existingmessages.typerows. This matters because the dashboard chat view reads persisted rows (GET /messages), and stats group bytype— without it, oldchat/ptt/vcardrows would mis-render and split the by-type stats. Collision-free, idempotent, portable (SQLite/Postgres). - Dashboard: removed the dead wwebjs
'ptt'case fromrenderMedia; 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):
- The dashboard reads persisted rows (
getMessages), not enginegetChatHistory→ hence the backfill migration (without it, old voice notes would regress to document links). - The webhook change affects both
message.receivedandmessage.sent(the sent webhook is dispatched frombuildIncomingMessageBase, 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
mapWwebjsMessageTypeboundary 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.