OpenWA icon indicating copy to clipboard operation
OpenWA copied to clipboard

refactor(engine): move JID construction into the engine (#265)

Open rmyndharis opened this issue 5 months ago • 0 comments

Part of #265 (engine-pluggability decoupling). Item 3 of the roadmap: get the whatsapp-web.js JID scheme out of engine-neutral code so a different engine (e.g. Baileys) can supply its own.

What leaked

Two sites hard-coded wwebjs's JID format:

  1. Check-number endpoint — the controller hand-built ${number}@c.us.
  2. session.service — the status/story skip matched the status@broadcast pseudo-JID directly.

Changes

  • IWhatsAppEngine.getNumberId(number) — new contract returning the engine's canonical chat id (or null). The wwebjs adapter resolves it via client.getNumberId(...)?._serialized, and checkNumberExists now delegates to it (DRY; identical ensureReady → HTTP 409 contract).
  • contact.controller returns the engine-resolved whatsappId instead of a hand-built …@c.us.
  • Neutral isStatusBroadcast flag — set by the message-mapper (same predicate as the old literal check); session.service branches on the flag. The status@broadcast literal is now confined to the adapter. getChatHistory recomputes the chatId-derived flags (isGroup, isStatusBroadcast) after its chatId override, mirroring the existing pattern.
  • Docs — fixed the stale check-number response shape in docs/06 and docs/07 ({ number, exists, whatsappId }, no envelope, correct check/:number path).

⚠️ Behavior note

whatsappId is now the engine-resolved canonical id, not an echo of the input. It may be normalized and differ from ${input}@c.us (e.g. a @lid identifier). exists is unchanged (still "engine returned a non-null id"). Documented in CHANGELOG + the endpoint docs.

Live-verified against a real linked account:

  • check/6281270008896 → {exists:true, whatsappId:"262813461250071@lid"} — proves the canonical id is returned (and is more correct than the old @c.us guess for @lid contacts).
  • check/19999999999 → {exists:false, whatsappId:null}.

Review

Adversarial multi-agent review (getNumberId-contract: holds; status-flag-equivalence: equivalence exact). It caught a real CI blocker — a 121-char lint violation in the status spec — now fixed. The doc/CHANGELOG drift it flagged is addressed here too.

Verification

  • npm run build ✓
  • npm run lint ✓
  • 439/439 tests pass ✓

Deferred

dashboard/.../MessageTester.tsx still builds a @c.us JID — needs a dashboard checkNumber call + i18n error path. Tracked under #265.

rmyndharis avatar Jun 16 '26 18:06 rmyndharis