Amarula.Protocol.Presence (amarula v0.1.0)

View Source

Build presence / chat-state stanzas, ported from Baileys sendPresenceUpdate and presenceSubscribe (src/Socket/chats.ts).

Pure stanza construction; the Connection supplies me and writes the node. Two kinds:

  • global presenceavailable / unavailable: <presence name= type=>.
  • chat statecomposing / recording / paused toward a jid: <chatstate from= to=><composing|paused/></chatstate>. recording is a composing tag with media: "audio".

Summary

Types

A parsed inbound presence/chat-state update (Baileys presence.update)

Functions

A chat-state (typing) stanza toward to_jid. from is me.lid when to_jid is a lid jid, else me.id.

Parse an inbound <presence> or <chatstate> node into an update/0 (Baileys handlePresenceUpdate). Returns {:error, :invalid} for a malformed node. from/participant are returned as wire jid strings.

Global online/offline presence. me is the auth creds me map (needs :name; Baileys skips the update without one). Returns {:ok, node} or {:error, :no_name}.

A <presence type=subscribe> stanza for to_jid (tcToken omitted).

Types

chatstate()

@type chatstate() :: :composing | :recording | :paused

presence()

@type presence() :: :available | :unavailable

update()

@type update() :: %{
  jid: String.t(),
  participant: String.t(),
  presence: presence() | :composing | :recording,
  last_seen: integer() | nil
}

A parsed inbound presence/chat-state update (Baileys presence.update):

  • :jid — the chat the update is for (attrs.from)
  • :participant — who within the chat (group member, or the contact itself)
  • :presence:available / :unavailable (a <presence>) or :composing / :recording / :paused:available (a <chatstate>)
  • :last_seen — unix seconds from a <presence last=> (nil otherwise)

Functions

chatstate(type, to_jid, me)

@spec chatstate(chatstate(), String.t(), map()) :: Amarula.Protocol.Binary.Node.t()

A chat-state (typing) stanza toward to_jid. from is me.lid when to_jid is a lid jid, else me.id.

parse_update(node)

@spec parse_update(Amarula.Protocol.Binary.Node.t()) ::
  {:ok, update()} | {:error, :invalid}

Parse an inbound <presence> or <chatstate> node into an update/0 (Baileys handlePresenceUpdate). Returns {:error, :invalid} for a malformed node. from/participant are returned as wire jid strings.

presence(type, arg2)

@spec presence(presence(), map()) ::
  {:ok, Amarula.Protocol.Binary.Node.t()} | {:error, :no_name}

Global online/offline presence. me is the auth creds me map (needs :name; Baileys skips the update without one). Returns {:ok, node} or {:error, :no_name}.

subscribe(to_jid, id)

@spec subscribe(String.t(), String.t()) :: Amarula.Protocol.Binary.Node.t()

A <presence type=subscribe> stanza for to_jid (tcToken omitted).