Files
buzz/mobile/lib/shared/channels
Wes f9b18c7f3a Cache recently opened mobile channel pages (#9809)
## What changed

Previously opened channels can show their newest saved page after a cold
launch, including offline. The same timeline provider combines the saved
page with live edits, deletions, and optimistic sends. When the relay
has loaded history, its timeline takes over and older-page navigation
becomes available. The chat labels saved messages until relay history
takes over. If a connected refresh fails, the chat offers Retry and
keeps the saved-message label visible during the retry.

- Keep verified relay events in a disposable, app-private SQLite cache
per community and signing identity on iOS and Android. Close SQLite
connections after each operation.
- Cap the main file at 45 MiB to leave room for a rollback journal
within the 100 MB per-community limit. Evict least-recently opened pages
and retry once when SQLite reaches the physical limit.
- Omit an oversized relay page as a whole so the cache cannot show a
message without an edit or deletion from the same response. Keep
readable pages available when only the recency write fails.
- Purge on leave, deletion, explicit channel access refusal, and
writer-confirmed loss of a known membership. Expired sessions leave the
cache intact. SQLite secure deletion clears removed payloads after
commit. A durable marker covers interrupted purges. Account switches and
removal retire old saves.
- Keep message-link lookups valid across the first connection. An access
refusal still discards a late result.
- After a confirmed leave or delete, record a durable cache purge before
refreshing the channel list. Disk cleanup runs in the background and the
refresh skips redundant writer confirmation for that channel.
- For joined, unarchived channels, remove stale saved rows on
channel-tagged deletions or edits the app receives while the
conversation is closed. Reuse deletions from the existing reconnect
catch-up.
- Give an already-connected relay one 250 ms head start before reading
the saved page. Offline opens start the read immediately.

## Screenshots and timing

[iPhone 17e simulator
captures](https://github.com/block/buzz/pull/9809#issuecomment-7050704433)
show the same seeded chat before the cache, with saved messages while
offline, and after the relay returns. In the debug capture run, the
baseline `9c0bcd623` showed no messages within 8 seconds. The updated
build `2cb28eda` showed the target saved message **752 ms** after the
tap.

For a first online open on the same simulator, the baseline and the
instrumented `2cb28eda` build took **449 ms** and **441 ms** median from
tap to target message. Each build had four measured opens after one
warm-up. The ranges were 437–481 ms and 414–447 ms.

In the earlier paired debug simulator experiment on `6e113d83`, the 250
ms preview delay reduced median first online open from **507 ms to 452
ms** across 15 measured opens per build. In the delay build, seeded
offline opens took **488–557 ms** across the two channel medians, and
reopens in the same app session took **174–245 ms**. All 20 offline
opens and reopens showed the target message.

## Verification

- The mobile pre-push gate and file-size check passed for `6650ebad`.
Regression tests cover oversized pages, session expiry versus channel
revocation, live mutations during preview, paging after relay history
loads, writer-confirmed membership loss, closed-chat edits and
deletions, the saved-message label across Retry and history takeover,
links held across first connect, and interrupted or overlapping purges.
- The host SQLite probe interrupted eviction, relay-key rotation, and a
synthetic table rewrite. All three reopened with a clean integrity
check. The largest observed database and journal footprint was 93.84 MB.
- Physical iPhone suspension, airplane-mode reopen, and profile-mode
timing are pending.

## Try it

1. While connected, open a populated channel and let its newest page
save.
2. Force-quit, turn on airplane mode, and reopen the same channel. The
saved page should appear.
3. Reconnect. The relay's newest page should take over without
duplicating messages.

A saved page can still show stale rows while the phone stays offline,
for a live edit missed while the app was stopped, for an archived or
unjoined public channel, or for a NIP-09 deletion without a channel tag.
Reconnect catch-up covers the deletions returned by its bounded unread
query. A direct online open refreshes the page. Offline thread replies
are separate follow-up work.

---------

Signed-off-by: Wes <wesb@block.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz>
Co-authored-by: Rocket <0c41329ade659e01801116549bc617351689892a382bc864a1186dc9191293a7@buzz.block.builderlab.xyz>
Co-authored-by: Mantis <530af7635ef7d0e1a5189d234338db88cb4ef1ede7119b4ebb4fb05c3eafe3b1@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-20 16:03:27 -07:00
..