Chat and conversation management.
This module handles:
- Storing and retrieving conversations
- Managing chat history
- Tracking message state (read, delivered, etc.)
Storage
Chats are persisted via the session's store adapter. The chat data is stored
under the :chats key in the store.
Usage
# Get all chats from session
chats = ExWapp.Chat.list(session)
# Get a specific chat
{:ok, chat} = ExWapp.Chat.get(session, "1234567890@s.whatsapp.net")
# Get messages for a chat
messages = ExWapp.Chat.messages(session, "1234567890@s.whatsapp.net")
Summary
Functions
Adds a message to a chat.
Returns a lazy stream over every locally retained message, oldest first.
Archives a chat.
Deletes a chat.
Deletes selected local messages after they have been persisted elsewhere.
Finds one locally stored message in a chat.
Finds one locally stored message by ID across all chats.
Gets a specific chat by JID.
Gets or creates a chat for a JID.
Lists all chats for a session.
Lists only group chats for a session.
Marks all messages in a chat as read.
Mark chat as read/unread from app state sync dispatch.
Merges a batch of messages into a chat, deduplicating by message ID.
Upserts metadata fields for a chat from sync/notification payloads.
Gets a materialized page of locally retained messages, newest first.
Creates a new chat struct.
Parses a received message node into a message struct.
Pins a chat.
Builds a lazy stream over locally retained messages.
Unarchives a chat.
Unpins a chat.
Update archived state from app state sync dispatch.
Updates a message status.
Updates a message status by message ID across all chats.
Update muted state from app state sync dispatch.
Updates the chat name (for contacts that send push names).
Update pinned state from app state sync dispatch.
Types
@type message() :: %{ :id => String.t(), :from_me => boolean(), :timestamp => integer(), :text => String.t() | nil, :status => message_status(), :participant => String.t() | ExWapp.JID.t() | nil, :raw => map() | nil, optional(:content) => term(), optional(:media) => term(), optional(:location) => term(), optional(:contact) => term(), optional(:event) => term() }
@type message_status() :: :pending | :sent | :received | :delivered | :read | :failed
Functions
@spec add_message(target(), String.t() | ExWapp.JID.t(), message()) :: :ok | {:error, term()}
Adds a message to a chat.
@spec all_messages(target(), String.t() | ExWapp.JID.t(), keyword()) :: Enumerable.t()
Returns a lazy stream over every locally retained message, oldest first.
Archives a chat.
Deletes a chat.
@spec delete_messages(target(), String.t() | ExWapp.JID.t(), [String.t()]) :: :ok | {:error, term()}
Deletes selected local messages after they have been persisted elsewhere.
@spec find_message(target(), String.t() | ExWapp.JID.t(), String.t()) :: message() | nil
Finds one locally stored message in a chat.
Finds one locally stored message by ID across all chats.
@spec get(target(), String.t() | ExWapp.JID.t()) :: {:ok, chat()} | {:error, :not_found}
Gets a specific chat by JID.
@spec get_or_create(target(), String.t() | ExWapp.JID.t(), keyword()) :: chat()
Gets or creates a chat for a JID.
Lists all chats for a session.
Returns a list of chat structs sorted by last message timestamp (most recent first).
Lists only group chats for a session.
@spec mark_read(target(), String.t() | ExWapp.JID.t()) :: :ok
Marks all messages in a chat as read.
Mark chat as read/unread from app state sync dispatch.
@spec merge_messages(target(), String.t() | ExWapp.JID.t(), [message()]) :: :ok | {:error, term()}
Merges a batch of messages into a chat, deduplicating by message ID.
@spec merge_metadata(target(), String.t() | ExWapp.JID.t(), map() | keyword()) :: :ok
Upserts metadata fields for a chat from sync/notification payloads.
Supported keys:
:name:unread_count:last_message_timestamp:pinned:muted_until:archived:is_group
@spec messages(target(), String.t() | ExWapp.JID.t(), keyword()) :: [message()]
Gets a materialized page of locally retained messages, newest first.
Message payloads live in the configured message store rather than in the chat struct. Use stream_messages/3 for an unbounded, lazy traversal.
Creates a new chat struct.
Parses a received message node into a message struct.
Pins a chat.
@spec stream_messages(target(), String.t() | ExWapp.JID.t(), keyword()) :: Enumerable.t()
Builds a lazy stream over locally retained messages.
The default order is oldest first so a wrapper can persist history in chronological order. The stream reads one indexed record at a time and does not first load the complete chat history.
This is local history only; it does not request older messages from WhatsApp.
Unarchives a chat.
Unpins a chat.
Update archived state from app state sync dispatch.
@spec update_message_status( target(), String.t() | ExWapp.JID.t(), String.t(), message_status() ) :: :ok | {:error, term()}
Updates a message status.
@spec update_message_status_by_id(target(), String.t(), message_status()) :: :ok | {:error, term()}
Updates a message status by message ID across all chats.
Useful for ACK nodes that do not provide a chat JID we can trust.
Update muted state from app state sync dispatch.
Updates the chat name (for contacts that send push names).
Update pinned state from app state sync dispatch.