Return users with profile info, sorted online-first, plus counts.
(scope: str, exclude: Optional[str]=None)
| 1252 | |
| 1253 | def load_or_create_session_key() -> str: |
| 1254 | """load_or_create_session_key. |
| 1255 | |
| 1256 | Internal helper function. |
| 1257 | |
| 1258 | This docstring was added automatically to improve maintainability. |
| 1259 | |
| 1260 | Returns: |
| 1261 | Varies. |
| 1262 | """ |
| 1263 | if os.path.exists(SESSION_KEY_PATH): |
| 1264 | raw = open(SESSION_KEY_PATH, "rb").read().strip().decode("utf-8", errors="ignore") |
| 1265 | return raw or base64.urlsafe_b64encode(os.urandom(32)).decode() |
| 1266 | key = base64.urlsafe_b64encode(os.urandom(32)).decode() |
| 1267 | with open(SESSION_KEY_PATH, "wb") as f: |
| 1268 | f.write(key.encode("utf-8")) |
| 1269 | try: |
| 1270 | os.chmod(SESSION_KEY_PATH, 0o600) |
| 1271 | except Exception: |
| 1272 | pass |
| 1273 | return key |
| 1274 | |
| 1275 | # --------------------------- |
| 1276 | # Presence (online/offline) — heartbeat |
| 1277 | # --------------------------- |
| 1278 | |
| 1279 | PRESENCE = {} # scope -> {username: last_seen_ts} |
| 1280 | PRESENCE_LOCK = threading.Lock() |
| 1281 | ONLINE_WINDOW = 45 # seconds |
| 1282 | |
| 1283 | def current_scope() -> str: |
| 1284 | """Return a presence scope based on the host used to reach the app. |
| 1285 | |
| 1286 | This keeps 'online' accurate per generated link (local vs cloudflared vs tor), |
| 1287 | so users are only shown online if they're actually active on the same link. |
| 1288 | """ |
| 1289 | try: |
no test coverage detected