mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
## Summary Mobile, CLI, and legacy desktop write millisecond timestamps for new star and mute edits. They compare legacy seconds in the same unit and retain the original timestamps of untouched entries during rollout. Mobile and CLI cap saved records at 500 entries and repair oversized records on a write. CLI no-op adds and removes leave oversized records alone. They keep the current edits and evict by age, preferring clears when timestamps tie. The CLI accepts a full flag record within the short-form NIP-44 envelope. Mobile builds each write from the writer's retained snapshot plus a durable journal of local edits. The journal drains in batches of at most 500 edits. It retries transient failures and older writer snapshots with bounded backoff and retires a pending edit after verifying that the writer already holds it or holds a strictly newer one. Retried writes preserve the original entry timestamps. An exact timestamp tie with a conflicting writer value advances by one millisecond so older clients accept the edit. An unreadable record pauses writes until a valid snapshot or app resume. On mobile upgrade, a verified writer snapshot lets the app recover newer local star and mute edits from the old cache. The legacy cache has no edit journal. Below 500 writer entries, the app also recovers absent active flags. At 500 or above it only recovers absent active flags dated after the snapshot's creation second. Absent legacy clears and clears newer than the writer's recorded clear are kept locally. They enter the publish queue when an older or equal enable appears. If the enable disappears before preflight, the clear returns to the local cache. A newer enable, an equal or newer clear, or a fresh tap retires the saved clear. A cached enable is queued when it is newer than the writer entry or ties a writer clear. A strictly newer cached enable advances the timestamp even when both copies are enabled. Recovered enables and promoted clears keep their original stamps in the journal. When the relay entry is absent or uses seconds, the write keeps a recovered seconds stamp so a later edit from an older phone can win. An existing millisecond entry uses millisecond comparison and serialization for the recovered edit. ## Validation - Mobile regressions exercise a later seconds-stamped edit, two-device eviction, a pending clear at the cap across manager replacement, transient read recovery, a losing duplicate acknowledgement, and a second toggle during confirmation, including at the cap. - CLI regressions cover seconds-stamped entries, the 500-entry limit, recovery from oversized records, and clear retention, including a repeated remove that leaves active flags intact. - Runtime validation uses scripted relays. - All 702 CLI tests passed. - Pre-push gates passed, including the full Flutter suite and analysis, repository Rust tests, and desktop Tauri checks. - Legacy desktop tests cover mixed-unit merges and back-to-back toggles. Mobile upgrade regressions cover newer offline entries, eviction, reconnect, and malformed-head recovery. --------- Signed-off-by: Wes <wesb@block.xyz> Co-authored-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz>
43 lines
1.5 KiB
Dart
43 lines
1.5 KiB
Dart
const maxSidebarFlags = 500;
|
|
|
|
/// Older clients used ten-digit Unix seconds. Leave earlier sentinel values
|
|
/// and millisecond timestamps unchanged, including already converted entries.
|
|
int sidebarFlagTimeMillis(int timestamp) =>
|
|
timestamp >= 1000000000 && timestamp < 10000000000
|
|
? timestamp * 1000
|
|
: timestamp;
|
|
|
|
/// A channel star or mute and the timestamp of its last change.
|
|
abstract interface class SidebarFlagEntry {
|
|
bool get enabled;
|
|
int get updatedAt;
|
|
}
|
|
|
|
/// Bound the record by age, keeping edits in this write and preferring to
|
|
/// evict a clear over an active flag of the same age. New clears must survive
|
|
/// older active flags so clients with stale caches can observe them.
|
|
void boundSidebarFlags<T extends SidebarFlagEntry>(
|
|
Map<String, T> channels,
|
|
Set<String> changed,
|
|
) {
|
|
final excess = channels.length - maxSidebarFlags;
|
|
if (excess <= 0) return;
|
|
|
|
final evictionOrder = channels.entries.toList()
|
|
..sort((a, b) {
|
|
final byEdit = (changed.contains(a.key) ? 1 : 0).compareTo(
|
|
changed.contains(b.key) ? 1 : 0,
|
|
);
|
|
if (byEdit != 0) return byEdit;
|
|
final byTime = sidebarFlagTimeMillis(
|
|
a.value.updatedAt,
|
|
).compareTo(sidebarFlagTimeMillis(b.value.updatedAt));
|
|
if (byTime != 0) return byTime;
|
|
if (a.value.enabled != b.value.enabled) return a.value.enabled ? 1 : -1;
|
|
return a.key.compareTo(b.key);
|
|
});
|
|
for (final entry in evictionOrder.take(excess)) {
|
|
channels.remove(entry.key);
|
|
}
|
|
}
|