Amarula.Protocol.AppState.Sync (amarula v0.1.0)

View Source

App-state sync orchestration helpers, ported from Baileys resyncAppState (src/Socket/chats.ts). Builds the patch-request IQ, extracts the per-collection patches from the reply, and decodes them through AppState.Patch + AppState.SyncAction into consumer changes — given a key lookup and the prior collection state.

The wire round-trip + storage live in Connection; this module is the pure glue between them and the decode stack.

Summary

Functions

The collection names we sync.

Decode a collection's patches against state with get_key, returning {:ok, changes, new_state} where changes are SyncAction.decode/1 results ({:chat, _} / {:contact, _} / …).

Pull the <collection> results out of a sync IQ reply. Returns a list of %{name, patches: [%SyncdPatch{}], has_more: bool}. (Snapshots delivered as an external blob reference are noted but not downloaded here.)

Build the patch-request IQ for collections, each a {name, version, snapshot?} tuple

Functions

collections()

@spec collections() :: [String.t()]

The collection names we sync.

decode_collection(patches, state, get_key)

@spec decode_collection(
  [Amarula.Protocol.Proto.SyncdPatch.t()],
  Amarula.Protocol.AppState.Patch.state(),
  (String.t() -> map() | nil)
) ::
  {:ok, [Amarula.Protocol.AppState.SyncAction.result()],
   Amarula.Protocol.AppState.Patch.state()}

Decode a collection's patches against state with get_key, returning {:ok, changes, new_state} where changes are SyncAction.decode/1 results ({:chat, _} / {:contact, _} / …).

extract_collections(reply)

@spec extract_collections(Amarula.Protocol.Binary.Node.t()) :: [map()]

Pull the <collection> results out of a sync IQ reply. Returns a list of %{name, patches: [%SyncdPatch{}], has_more: bool}. (Snapshots delivered as an external blob reference are noted but not downloaded here.)

request_iq(collections)

@spec request_iq([{String.t(), non_neg_integer(), boolean()}]) ::
  Amarula.Protocol.Binary.Node.t()

Build the patch-request IQ for collections, each a {name, version, snapshot?} tuple:

<iq xmlns="w:sync:app:state" type="set"><sync>
  <collection name= version= return_snapshot=/></sync></iq>