Skip to main content

Module events

Module events 

Source
Expand description

Event storage — save_event for the flat event architecture.

Structs§

ReplyContext
Context data for a replied-to message.

Enums§

UnreadMark
What compute_unread_anchor decided a chat’s read marker should become to surface its newest contact message as unread. Computed from the full DB history (RAM may hold only a preview message for an unopened community).

Functions§

compute_unread_anchor
Decide how to mark chat_identifier unread. Anchors on the newest message strictly before the newest contact message’s second — the count query compares whole seconds with a strict >, so a same-second anchor would leave the target on the boundary and it would read as caught-up.
delete_event
Delete an event from the events table by ID.
event_author
The stored author (npub) of an event, or None if the row (or DB) is absent. Lets the out-of-window moderation-hide path authorize against a paged-out message’s real author.
event_delete_context
The owning chat identifier, mine flag, and stored author (npub) of an event, or None if the row (or DB) is absent. Lets delete-affordance resolution give paged-out rows the same verdict as resident ones — residency is a cache detail, not a verdict.
event_exists
Check if an event exists in the database.
flush_message_batch
Drain a sync loop’s pending message batch into one transaction. The shared flush for the segment-flush pattern: bulk loops COLLECT message saves and call this at delete barriers + loop end (a batched save committing AFTER a delete it originally preceded would resurrect the deleted row — flushing first preserves wire order). Session-guarded HERE so every bulk path gets the same swap-safety: on a stale session the batch is DROPPED, never written into the next account’s DB (the caller’s loop is about to bail anyway).
get_all_chats_last_messages
Get the last message for ALL chats in a single batch query. Optimized for app startup (chat list sidebar).
get_chat_message_count
Get message count for a chat.
get_event_wrap_context
The stored context a pin proof needs to recover a message’s wrap: its wrapper_event_id and the rumor’s stored tags (for the epoch binding).
get_events
Get events for a chat with pagination, optionally filtered by kind. Message/edit content is decrypted via maybe_decrypt.
get_message_views
Get message events with reactions, edits, and attachments composed.
get_messages_around
Anchored (random-access) message window: load before messages up to and including the anchor, plus after messages strictly newer than it. O(window) regardless of how deep the anchor sits in the chat — unlike the offset pager, which is O(depth) to reach a far-back message.
get_pivx_payments_for_chat
Get PIVX payment events for a chat.
get_related_events
Get events that reference specific message IDs (reactions, edits).
get_reply_contexts
Fetch reply context for a list of message IDs.
get_system_events_for_chat
Get system events (member joined/left) for a chat.
is_own_event
Whether event_id is one of our own messages (mine = 1). A reply to our own message is an implicit ping, so notifications treat it like a direct @mention (breaks through a muted channel). Missing row → not ours → false.
message_exists_in_db
Check if a message/event exists in the database. Returns false if DB unavailable.
populate_reply_context
Populate reply context for a single message. Used for real-time messages that don’t go through get_message_views.
populate_reply_contexts
Populate reply context for a PAGE of messages in one query — the sync/back-page counterpart to populate_reply_context. Every path that emits a message straight to the UI must resolve the quote first: the frontend renders the emitted payload, and a reply whose parent lies outside the rendered window has no other source for it (a parent inside the window is resolved from memory instead). Messages whose parent isn’t persisted yet are left untouched.
save_chat_messages
Batch save messages for a chat — one transaction for the whole slice.
save_edit_event
Save a message edit as a kind=16 event referencing the original message.
save_event
save_message
Save a single message to the database.
save_messages_batch
Save many messages for one chat in a SINGLE transaction — the bulk-sync persist path (community backfill pages, negentropy catch-up). One commit amortizes the per-transaction WAL overhead across the whole page instead of paying it per message.
save_messages_batch_multi
Multi-chat variant for the DM sync stream: gift-wrapped messages span many contacts, and splitting per chat would give back most of the batching win. Each message may carry its gift-wrap ledger entry, committed in the SAME transaction right after its row (see BatchRow::wrapper). Groups keep their slice order; everything lands in ONE transaction.
save_pivx_payment_event
Save a PIVX payment event, resolving chat_id from conversation identifier.
save_reaction_event
Save a reaction as a kind=7 event referencing the message.
save_system_event_at
Like save_system_event_by_id but stamps created_at from the event’s own authenticated timestamp (clamped to not exceed local now, since the inner author sets it) so a HISTORICALLY-synced presence (join/leave) sorts at the time it happened, not at ingest-time now. received_at stays local now.
save_system_event_by_id
Save a system event (member joined/left/removed) with dedup. Returns true if inserted, false if duplicate.
unread_count_for_chat
Unread count for a SINGLE chat, same semantics as unread_counts. The RAM cache calls this to reconcile one chat (open / delete / retreat) without recomputing every chat’s count.
unread_counts
Per-chat unread count, computed straight from the DB so it’s correct even when only the last message is in RAM (the boot state). Mirrors the in-memory walk-back exactly: unread = non-mine messages newer than the most recent “anchor” (our own message OR the last_read marker, whichever is latest). A never-read chat (empty last_read, no own message) counts all its non-mine messages. Returns chat_identifier → count; chats with 0 unread are omitted. Muted/blocked filtering is left to the caller (it lives in RAM state, cheaply).
update_wrapper_event_id
Update the wrapper event ID for an existing event. Returns true if updated, false if event already had a wrapper_id.
wrapper_event_exists
Check if a wrapper (giftwrap) event ID exists. Returns false if DB unavailable.