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

View Source

Decode app-state syncd mutations/patches into SyncActionData mutations and the next LTHash collection state. Ported from Baileys decodeSyncdMutations / decodeSyncdPatch (chat-utils.ts).

Pure given a key lookup: get_key.(key_id_base64) → the expanded Amarula.Protocol.AppState.Keys (or nil if the app-state-sync key isn't available yet — then the record is skipped and the caller may park the collection until APP_STATE_SYNC_KEY_SHARE arrives).

Collection state is %{version, hash, index_value_map} where index_value_map maps base64(index_mac) => value_mac (so a later REMOVE can subtract the right value mac from the LTHash).

Summary

Functions

Decode records (a list of SyncdRecord / SyncdMutation) against state, returning {:ok, mutations, new_state}. get_key resolves a record's app-state-sync key. With validate_macs: true (default), the value MAC and index MAC are checked; a record that fails MAC, can't decrypt, or has a missing key is skipped.

A fresh, empty collection state.

Types

mutation()

@type mutation() :: %{
  operation: :set | :remove,
  action: struct(),
  index: [String.t()]
}

state()

@type state() :: %{version: non_neg_integer(), hash: binary(), index_value_map: map()}

Functions

decode_mutations(records, state, get_key, opts \\ [])

@spec decode_mutations([struct()], state(), (String.t() -> map() | nil), keyword()) ::
  {:ok, [mutation()], state()}

Decode records (a list of SyncdRecord / SyncdMutation) against state, returning {:ok, mutations, new_state}. get_key resolves a record's app-state-sync key. With validate_macs: true (default), the value MAC and index MAC are checked; a record that fails MAC, can't decrypt, or has a missing key is skipped.

new_state()

@spec new_state() :: state()

A fresh, empty collection state.