BaileysEx.Feature.AppState (baileys_ex v0.1.0-alpha.7)

Copy Markdown View Source

App state sync (Syncd) — runtime orchestration for cross-device state synchronization.

Provides patch construction, outbound push (app_patch/3), full resync (resync_app_state/4), and the high-level chat_modify/4 entry point. Delegates encoding/decoding to Syncd.Codec and action mapping to Syncd.ActionMapper.

Ports appPatch, resyncAppState, chatModify from Baileys chats.ts:465-902.

Summary

Functions

Send an outbound app state patch to the server.

Construct the app-state patch structure without sending it.

High-level chat modification — builds a patch and sends it.

Build a Baileys-style app-state patch for the given action and pass it to the provided transport callback when one is supplied.

Full resync of app state collections from the server.

Types

operation()

@type operation() :: :set | :remove

patch()

@type patch() :: %{
  sync_action: map(),
  index: [String.t()],
  type: patch_type(),
  api_version: pos_integer(),
  operation: operation()
}

patch_type()

@type patch_type() ::
  :critical_block
  | :critical_unblock_low
  | :regular_high
  | :regular_low
  | :regular

Functions

app_patch(queryable, store, patch_create, opts \\ [])

@spec app_patch(
  term(),
  GenServer.server() | BaileysEx.Connection.Store.Ref.t(),
  patch(),
  keyword()
) :: :ok | {:error, term()}

Send an outbound app state patch to the server.

Acquires the current sync version, encodes the patch, sends it via IQ, and persists the updated state.

Ports appPatch from chats.ts:779-853.

Parameters

  • queryable — function or {module, pid} for sending IQ queries
  • store — Store server or Ref
  • patch_create%{type:, index:, sync_action:, api_version:, operation:}
  • opts — options:
    • :emit_own_events — boolean (default true)
    • :event_emitter — pid
    • :me — current user contact info

build_patch(action, jid, value, opts \\ [])

@spec build_patch(atom(), String.t(), term(), keyword()) :: patch()

Construct the app-state patch structure without sending it.

chat_modify(queryable, store, action, jid, value, opts \\ [])

@spec chat_modify(
  term(),
  GenServer.server() | BaileysEx.Connection.Store.Ref.t(),
  atom(),
  String.t(),
  term(),
  keyword()
) :: :ok | {:error, term()}

High-level chat modification — builds a patch and sends it.

Ports chatModify from chats.ts:899-902.

push_patch(conn, action, jid, value, opts \\ [])

@spec push_patch(term(), atom(), String.t(), term(), keyword()) ::
  {:ok, patch()} | {:error, term()}

Build a Baileys-style app-state patch for the given action and pass it to the provided transport callback when one is supplied.

resync_app_state(queryable, store, collections, opts \\ [])

@spec resync_app_state(term(), GenServer.server(), [patch_type()], keyword()) ::
  :ok | {:error, term()}

Full resync of app state collections from the server.

Fetches snapshots and patches for each collection, decodes them, persists the updated LTHash state, and emits sync action events.

Ports resyncAppState from chats.ts:479-632.

Parameters

  • queryable — function or {module, pid} for sending IQ queries
  • store — Store server or Ref for key/version persistence
  • collections — list of collection names to sync
  • opts — options:
    • :is_initial_sync — boolean (default false)
    • :event_emitter — pid to receive events
    • :me — current user contact info
    • :validate_snapshot_macs — boolean (default false)
    • :validate_patch_macs — boolean (default false)