Skip to main content

Module attachments

Module attachments 

Source
Expand description

Attachments table operations.

Attachments are normalized into one row per attachment (see migration 74), keyed to their event (ON DELETE CASCADE) and indexed by content hash. This module is the single source of truth for attachment persistence; the legacy ["attachments", …] tag in events.tags is left in place on pre-migration events as an untouched safety net but is never read.

With Local Encryption on, the fields that open or describe a file (key, nonce, name, url, fallback_urls, img_meta) are sealed at rest; an empty one stays empty so <> '' still means “unset”. hash stays readable: dedup and file sharing look rows up by it.

Structs§

ReusableUpload
A prior upload of this exact plaintext, reusable by reference: the blob is already on a Blossom server and key/nonce decrypt it.

Functions§

backfill_downloaded_by_hash
Mark every OTHER attachment sharing this content hash as downloaded to the same path — the download-sharing dedup, now an indexed WHERE hash = ? instead of a LIKE '%hash%' table scan. Returns the affected event ids so the caller can reconcile in-memory STATE.
clear_attachment_download
Mark an attachment not-downloaded (its file went missing). Clears the path.
clear_kept
Attachments of hash whose only copy was the one the store kept are no longer downloaded.
downloaded_attachment_paths
A downloaded attachment’s on-disk path, for the integrity sweep. Returns (event_id, hash, path) for every attachment claiming downloaded=1; an empty path is a copy kept in the media store. An indexed read, no per-event JSON parse.
downloaded_paths_by_hash
Paths where these bytes already landed: every downloaded=1 row for the hash, wherever the file was saved (collision suffixes included — the name on the message says nothing about the name on disk). Rows are CLAIMS: the caller must hash-verify a path before treating it as the content.
downloads_on_disk
Downloads still shown from a file of their own: (hash, path, extension), one row per pair.
events_with_attachment_hash
The message carrying an attachment with this content hash, newest first.
find_by_webxdc_topic_in
The Mini App message carrying topic in one chat: (event id, attachment). Scoped to the chat because a topic is only a session id there; anyone can paste the same topic onto an app sent from elsewhere.
find_reusable_by_hash
The smart-forward ledger read: has this exact plaintext (by SHA-256) ever crossed this account with a decryptable blob behind it?
forget_path
Rows of hash stop showing path, which holds other bytes. Returns the events touched.
get_attachments_for_event
Attachments for a single event, ordered by att_index.
get_attachments_for_events
Attachments for a set of events, event_id → Vec<Attachment> ordered by att_index. Batched (one IN (…) query) for a message window, mirroring the reactions/edits loaders.
hash_referenced_elsewhere
Whether any OTHER event’s attachment shares this plaintext hash — the local file half of the same refcount: the decrypted file on disk serves every message that names its hash, windowed out of memory or not.
insert_attachment_rows
Upsert a message’s attachment rows onto the given connection or transaction, so save_message can commit them ATOMICALLY with the event row (an event + no attachments would render as a broken file message with no fallback). Upserts on (event_id, att_index). The mutable local state is handled so a re-save never regresses a completed download: downloaded and verified are MONOTONIC (MAX(existing, incoming)), and hash/path only take the incoming values when the incoming carries a completed download (downloaded=1) — the nonce→content-hash rewrite the download path performs. A proven blob (verified=1) also takes the incoming hash. So a relay re-delivery (downloaded=0) preserves the downloaded file, its content-hash key, and its path; a completed download persists all three in one pass. Explicit un-download goes through clear_attachment_download, never here. Sealed with the caller’s sealer, inside its at-rest ticket.
keep_at_store
Rows of hash at path show the store’s copy, and the file stays: it holds other bytes now. Returns the events touched.
kept_names
Each kept file with the name and extension one of its messages gave it.
let_go
A file this account no longer shows, under every name rows give it. Returns the events touched.
mark_downloaded
Mark row id downloaded while it still names path; false if it changed meanwhile.
move_to_store
A file is leaving, under every name rows give it: rows of hash show the store’s copy, and any claiming other bytes there are no longer downloaded. Returns the events touched.
names_path
Whether anything this account keeps still opens path.
out_of_store
Rows shown from the store’s copy of hash show the plain file at path instead. Returns the events touched.
rewrite_downloaded_paths
Repoint downloaded-file paths from old download directories to a new one (Android download-dir migration). For each downloaded attachment whose path starts with an old prefix: move it to new_dir/<filename> if that exists, else mark it not-downloaded. Returns the affected event ids.
set_attachment_downloaded
Flip one attachment’s downloaded state — a single-row UPDATE keyed by (event_id, content hash), replacing the old read-modify-write of the whole tags blob.
set_attachment_size
Record the size of an attachment that arrived without one.
unmarked_downloads
Rows that name a file without marking it downloaded: (row id, hash, path, size).
urls_unreferenced_elsewhere
Of urls, the ones NO other event’s attachment still points at — the only ones a delete may scrub from Blossom. One blob can back many messages (smart-forward), and the shared copy must outlive every send but the last. DB-backed on purpose: the in-memory STATE windows to recent messages, so a reference in an older message is invisible there. Sealed URLs can’t be matched in SQL, so every other row’s URL is opened and compared here.
verify_local_copy
The verified on-disk copy of a claimed attachment, if any: the store’s (an empty path), else the ledger’s recorded paths for the hash plus the download dir under the wire name, size-gated cheaply (wire size is ciphertext length, 16 AES-GCM bytes over the plaintext), then content-hashed on the blocking pool. The claim is never trusted without the hash passing — same bar as a download.
verify_message_attachments
Run verify_local_copy over a message’s undownloaded attachments, converting verified claims in place. No-op for messages whose files are unknown (indexed probe misses) or already resident.