Skip to main content

Module editing_session

Module editing_session 

Source
Expand description

Concurrent-editor tracking.

Pairs with the editor lock: the lock prevents conflicting writes; the editing-session model surfaces a banner so people see each other before they collide. Every time the editor JS pings the lock heartbeat (POST /cms-admin/pages/{id}/lock-heartbeat), we also touch a row in cms_editing_session so a separate JSON endpoint can list the other active viewers/editors.

§Liveness

A session is “active” when last_seen_at is within the last ACTIVE_WINDOW_SECS seconds. Older rows are garbage-collected lazily on heartbeat. The GC threshold is 1 hour — long enough to survive transient network blips, short enough that a closed-tab session falls off in reasonable time.

§“Viewing” vs “Editing”

The is_editing boolean is a hint, not a hard guarantee. The editor JS sets it true once the form is touched (any input event) and resets it on every page load. Useful UX signal — “alice is editing” reads more urgent than “alice is viewing”.

Structs§

EditingSession

Constants§

ACTIVE_WINDOW_SECS
How long a session row counts as “active”. Stale rows past this window are filtered out of the JSON response and reaped on the next heartbeat.
GC_THRESHOLD_SECS
Rows older than this are garbage-collected on heartbeat — long past the active window so transient outages don’t lose sessions the editor would otherwise rejoin.

Functions§

gc_stale
Garbage-collect rows older than GC_THRESHOLD_SECS. Called from the heartbeat handler so the table doesn’t grow unbounded — we don’t need a cron.
others_on_page
Other active sessions on this page (viewer’s own session filtered out). Returns (user_id, last_seen_at, is_editing) tuples sorted by last_seen_at desc.
touch
UPSERT the heartbeat row for (page_id, user_id) and bump last_seen_at to now. Application-enforced uniqueness — we don’t have a SQL UNIQUE constraint because not every dialect agrees on composite uniqueness syntax. The race window is small enough that the worst-case duplicate row is fine; the GC drops them.