Make the presence roster the single source of truth for online status

Online status had three server-side candidate populations and two
client-side deciders, so the feed roster and the /messages indicators
could legitimately disagree.

PresenceRelayService built its online set from whichever topics happened
to be subscribed on a given tick: the roster candidates on /feed, only
the per-uid dot rows on /messages. Different populations meant a
different hysteresis baseline, so the same user could be online in one
place and offline in the other. On top of that, PresenceManager re-derived
online status client-side from a frozen data-presence-last-seen with a
strict timeout and no hysteresis, re-evaluated every 20s, so any element
whose relay frame was missed drifted grey after the timeout and stayed
there. AppChat carried a third renderer with its own PubSubClient that
only ever wrote online/offline, plus hand-built dot markup duplicating
_presence_dot.html.

The relay now collapses to one set on one topic. Each tick it reads the
online population in a single indexed query (online_candidates, capped by
the new PRESENCE_TRACK_LIMIT), applies hysteresis once, and publishes
{count, online, users} on public.presence.roster only when the uid set
changes. online is the authority for every avatar dot; users is the same
set trimmed to PRESENCE_ONLINE_LIMIT for the feed panel. The per-uid
public.presence.{uid} topics are gone, which also removes one
subscription per distinct author on a page.

is_online(user) is now stays_online(seconds_since(last_seen), False), so
the server-rendered initial state and the live set apply one formula.

PresenceManager makes one subscription and renders every
[data-presence-uid] element as membership of that set, with no clock and
no expiry timer; before the first frame the server-rendered state stands.
Relative "last seen" text is a <time data-dt data-dt-mode="ago"> handled
by the shared LocalTime. AppChat lost its presence code entirely, and the
new Avatar.badgeElement is the JS twin of _presence_dot.html, so dot
markup now lives in exactly two places.

Also fix the awards column ensure-block, which the awards tests exposed.
backfill_api_keys opened with an "if users not in db.tables" guard, but
on a brand-new database that is precisely the state at init_db time, so
the whole users ensure-block was skipped. The first signup then created
users with only the columns of that INSERT, leaving every ensured-but-
unwritten column absent from the server's reflected metadata for the rest
of the process lifetime. That is why the awards tab, the prominent award
banner and the avatar award badge were invisible on a fresh database. It
now calls get_table("users") unconditionally.

test_avatar_badge_on_feed_when_prominent asserted that any online user
carries an award badge while only awarding a user who was never active,
so it passed only by accident. It now makes the awarded user active and
asserts the badge on that user's roster entry.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-07 10:53:43 +02:00
co-authored by Claude Opus 5
parent cf5b7751e3
commit b777a5b9d0
26 changed files with 340 additions and 236 deletions
+23
View File
@@ -381,6 +381,29 @@ def test_feed_shows_online_now_section(app_server):
assert f'class="online-user" title="{name}"' in html
def test_online_now_avatar_uses_the_shared_presence_dot(app_server):
name = _unique("0ros")
s = requests.Session()
s.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
},
allow_redirects=True,
)
uid = _db_user(name)["uid"]
html = s.get(f"{BASE_URL}/feed").text
panel = html.split('data-online-users-list', 1)[1].split("</div>", 2)[0]
# the roster avatar is driven by the same subscribed dot as every other avatar,
# never a hardcoded green marker
assert f'data-presence-uid="{uid}"' in panel
assert "data-presence-last-seen" in panel
assert '<span class="presence-dot online" aria-hidden="true"></span>' not in panel
def test_feed_shows_poll_results_without_voting(app_server):
s, _ = _session_polls()
title = f"feedpoll-{int(time.time() * 1000)}"
+28
View File
@@ -180,3 +180,31 @@ def test_messages_conversation_list_avatar_has_presence_dot(app_server):
assert "conversation-item" in html
assert "presence-dot" in html
assert f'data-presence-uid="{other_uid}"' in html
def test_chat_presence_agrees_with_the_feed_online_roster(app_server):
sender, _ = _member()
# the roster is alphabetical and capped, so name the partner to sort first
other_name = _unique("0chat")
other = requests.Session()
other.post(
f"{BASE_URL}/auth/signup",
data={
"username": other_name,
"email": f"{other_name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
},
allow_redirects=True,
)
other_uid = _db_user(other_name)["uid"]
# the partner is active, so the feed roster - the single source of truth - lists them
roster = sender.get(f"{BASE_URL}/feed").text
assert f'class="online-user" title="{other_name}"' in roster
# ...and the chat header must say exactly the same thing
chat = sender.get(f"{BASE_URL}/messages?with_uid={other_uid}").text
header = chat.split('id="messages-presence"', 1)[1].split("</span>", 1)[0]
assert header.endswith("online"), header
assert f'data-presence-uid="{other_uid}"' in header