now-playing: Navidrome "Listening to" service for Lotus Chat (cinny #491) (#68)
Lint / Shell (shellcheck) (push) Successful in 10s
Lint / JS (eslint) (push) Successful in 7s
Lint / No Hookshot webhook IDs in README (push) Successful in 6s
Lint / No secrets in webhook configs (push) Successful in 6s
Lint / Landing page is rendered (matrix (push) Successful in 7s
Lint / Python (ruff) (push) Successful in 11s
Lint / Python deps (pip-audit) (push) Successful in 36s
Lint / Secret scan (gitleaks) (push) Successful in 5s

This commit was merged in pull request #68.
This commit is contained in:
2026-11-16 23:51:07 -05:00
parent b3fec4af2a
commit 3a0a4e15a8
7 changed files with 767 additions and 3 deletions
+2 -2
View File
@@ -85,10 +85,10 @@ jobs:
mv ruff /usr/local/bin/ruff
- name: Check syntax errors
run: ruff check matrixbot/ --select E9,F63,F7,F82 --output-format=github
run: ruff check matrixbot/ now-playing/ --select E9,F63,F7,F82 --output-format=github
- name: Run full lint
run: ruff check matrixbot/ --output-format=github
run: ruff check matrixbot/ now-playing/ --output-format=github
python-audit:
name: Python deps (pip-audit)
+19 -1
View File
@@ -29,6 +29,10 @@ matrix/
├── discord-bridge/
│ ├── config.yaml # mautrix-discord config (DB password filled in on the server, never committed)
│ └── install.sh # One-time installer, run on the Proxmox node hosting LXC 109 + 151
├── now-playing/ # "Listening to" service for Lotus Chat (cinny #491), LXC 151 :8072
│ ├── lotus_now_playing.py
│ ├── test_lotus_now_playing.py
│ └── install.sh
├── deploy/ # Auto-deployment infrastructure
│ ├── lxc151-hookshot.sh # Deploy script for LXC 151 (matrix/hookshot/livekit)
│ ├── lxc106-cinny.sh # Deploy script for LXC 106 (cinny)
@@ -45,6 +49,7 @@ matrix/
├── livekit-graceful-restart.timer # Runs every 5 min
├── draupnir.service
├── mautrix-discord.service # Discord bridge (LXC 151)
├── lotus-now-playing.service # "Listening to" service (LXC 151)
└── cinny-upstream-check.cron # Installed to /etc/cron.d/ on LXC 106 — runs daily at noon
```
@@ -304,6 +309,7 @@ The token in `draupnir/production.yaml` in this repo is **intentionally redacted
| 7881 | LiveKit RTC TCP | 0.0.0.0 |
| 8070 | voice-limit-guard (fronts lk-jwt-service) | 0.0.0.0 |
| 8071 | lk-jwt-service (behind guard) | 127.0.0.1 (matrix #24; was 0.0.0.0) |
| 8072 | lotus-now-playing ("Listening to", cinny #491) | 0.0.0.0 |
| 8080 | synapse-admin (nginx) | 0.0.0.0 |
| 3478 | coturn STUN/TURN | 0.0.0.0 |
| 5349 | coturn TURNS/TLS | 0.0.0.0 |
@@ -381,7 +387,7 @@ Webhook URL format: `https://matrix.lotusguild.org/webhook/<uuid>`
- JS transformation functions use hookshot v2 API: `result = { version: "v2", plain, html, msgtype }`
- The `result` variable must be assigned without `var`/`let`/`const` (QuickJS IIFE sandbox)
- NPM proxies `https://matrix.lotusguild.org/webhook/*` → `http://10.10.10.29:9003`
- NPM proxies `/sfu/get` and `/get_token` → `http://10.10.10.29:8070` (lk-jwt-service). Both paths are in `/data/nginx/proxy_host/49.conf` on LXC 139 — **NPM will overwrite these if proxy host 49 is re-saved via the UI; re-add both location blocks after any NPM save**
- NPM proxies `/sfu/get`, `/get_token` and `/delegate_delayed_leave` → `http://10.10.10.29:8070` (voice-limit-guard → lk-jwt-service), and `/now-playing/` → `:8072`. Since 2026-11-12 all of proxy host 49's custom locations live in its **Advanced** config (NPM sqlite `proxy_host.advanced_config`), so a UI save keeps them. Change them there (or in both the DB and `/data/nginx/proxy_host/49.conf`), never only in the conf file.
- Proxmox sends Discord embed format: `data.embeds[0].{title,description,fields}` — NOT flat fields
- Transform functions are stored as Matrix room state (`uk.half-shot.matrix-hookshot.generic.hook`) and deployed via `hookshot/deploy.sh`
- Hook UUIDs are **not** in that state. Hookshot creates them and keeps a `{uuid: name}` map in the `@hookshot` bot's room account data (same event type). Deploying a new transform creates the hook. To look a UUID up on LXC 151, use the appservice token from `/etc/matrix-synapse/hookshot-registration.yaml`: `GET /_matrix/client/v3/user/@hookshot:matrix.lotusguild.org/rooms/<room>/account_data/uk.half-shot.matrix-hookshot.generic.hook?user_id=@hookshot:matrix.lotusguild.org`
@@ -474,6 +480,18 @@ while the bridge or Synapse was down are bridged when it starts again.
---
## Now Playing ("Listening to", cinny #491)
`now-playing/lotus_now_playing.py` (`lotus-now-playing.service`, LXC 151, `:8072`, NPM `/now-playing/`) gives Lotus Chat what people are playing, for a Discord/Spotify-style "Listening to" card.
- **Source:** Navidrome `getNowPlaying`, polled every 10 s as the non-admin Navidrome user `lotus-nowplaying` (direct to `http://10.10.10.28:4533`). Navidrome is the scrobbling master and Dropped Needle passes the real listener through, so every play shows up under the listener's own Navidrome username, which is their SSO username. Dropped Needle's own `droppedneedle` service user is ignored.
- **Who's asking:** Lotus Chat sends a Matrix OpenID token; the service checks it with Synapse (`/_matrix/federation/v1/openid/userinfo`). Only users of this homeserver can read anything.
- **Matrix user → SSO username:** `/etc/lotus-now-playing/users.json` first (server-only, not in this public repo; for accounts whose SSO link has no email), then the Synapse admin API: an email `<name>@lotusguild.org`, or the legacy plain-username external ID. Uses `MATRIX_TOKEN` from `/etc/matrix-deploy.env`.
- **Opt-in:** off by default; each person turns on "Share what I'm listening to" in Lotus Chat. Stored in `/var/lib/lotus-now-playing/state.json`.
- **API:** `GET`/`PUT /now-playing/v1/me` (`{share, linked, playing}`), `GET /now-playing/v1/users?ids=@a,@b` (up to 50; only opted-in, matched, playing users), and `GET /now-playing/v1/cover/<id>?size=`. The cover endpoint has no auth (for `<img>`) but only serves covers an opted-in user is playing right now.
- Titles, artists and tokens are never logged.
- **Install / update:** create `/etc/lotus-now-playing/env` (`NAVIDROME_USER`, `NAVIDROME_PASSWORD`) once, then `bash /opt/matrix-config/now-playing/install.sh`. Tests: `python3 -m unittest now-playing/test_lotus_now_playing.py`.
## Voice Bridge (nether-voicebridge, Lotus-patched)
Bridges Discord voice into a Lotus voice room. People on Discord appear **inside the Matrix call**
+5
View File
@@ -36,5 +36,10 @@
"login": "Login"
}
}
},
"nowPlaying": {
"matrix.lotusguild.org": {
"url": "https://matrix.lotusguild.org/now-playing"
}
}
}
+37
View File
@@ -0,0 +1,37 @@
#!/bin/bash
# Install or update the now-playing service (cinny #491) on LXC 151.
# Run with this repo's main branch pulled to /opt/matrix-config:
# bash /opt/matrix-config/now-playing/install.sh
# First install: put the Navidrome login in /etc/lotus-now-playing/env first
# (NAVIDROME_USER=lotus-nowplaying / NAVIDROME_PASSWORD=...), mode 640 root:lotus-now-playing.
# Optional /etc/lotus-now-playing/users.json maps Matrix IDs whose SSO link has no
# email to their SSO username, e.g. {"@bigmanjeff:matrix.lotusguild.org": "jeff"}.
# Neither file is kept in this (public) repo. Safe to re-run.
set -euo pipefail
REPO=/opt/matrix-config
CONF=/etc/lotus-now-playing
id lotus-now-playing >/dev/null 2>&1 ||
useradd --system --no-create-home --home-dir /var/lib/lotus-now-playing --shell /usr/sbin/nologin lotus-now-playing
install -d -m750 -o root -g lotus-now-playing "$CONF"
install -D -m644 "$REPO/now-playing/lotus_now_playing.py" /opt/lotus-now-playing/lotus_now_playing.py
if [ ! -f "$CONF/env" ]; then
echo "missing $CONF/env (NAVIDROME_USER / NAVIDROME_PASSWORD); create it and re-run" >&2
exit 1
fi
chown root:lotus-now-playing "$CONF/env"
chmod 640 "$CONF/env"
if [ -f "$CONF/users.json" ]; then
chown root:lotus-now-playing "$CONF/users.json"
chmod 640 "$CONF/users.json"
fi
install -m644 "$REPO/systemd/lotus-now-playing.service" /etc/systemd/system/lotus-now-playing.service
systemctl daemon-reload
systemctl enable lotus-now-playing >/dev/null
systemctl restart lotus-now-playing
sleep 2
systemctl --no-pager --lines=5 status lotus-now-playing
curl -fsS http://127.0.0.1:8072/healthz && echo " (healthy)"
+493
View File
@@ -0,0 +1,493 @@
#!/usr/bin/env python3
"""Now playing (Navidrome) -> Lotus Chat "Listening to" cards. [cinny #491]
Navidrome is the scrobbling master: every play, whether from Dropped Needle or
any Subsonic app, shows up in its `getNowPlaying` under the listener's own
username, which is their SSO (LLDAP) username. This service polls that list and
serves it to Lotus Chat for users who opted in, keyed by Matrix user ID.
- Who is asking: Lotus Chat sends a Matrix OpenID token (Authorization: Bearer).
It is checked against Synapse's /_matrix/federation/v1/openid/userinfo, so
only signed-in users of this homeserver can read anything.
- Matrix user -> SSO username: an override file first (accounts whose SSO link
carries no email), then the Synapse admin API (email <name>@<domain>, or a
legacy external ID that is the plain username).
- Opt-in: off by default, stored here in STATE_DIR/state.json.
- Cover art is proxied only for covers that an opted-in user is playing right
now, so the endpoint can't be used to browse the library.
Track titles, artists and tokens are never logged.
"""
import hashlib
import json
import logging
import os
import re
import secrets
import tempfile
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
log = logging.getLogger("lotus-now-playing")
PREFIX = "/now-playing/v1"
MAX_IDS = 50
SUBSONIC_CLIENT = "lotus-now-playing"
SUBSONIC_VERSION = "1.16.1"
# Navidrome drops a session a while after the last report, so an entry older
# than this (no position updates, not paused) is treated as stale.
STALE_MINUTES = 30
COVER_SIZES = (64, 128, 256, 512)
UUID_RE = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
# --- pure helpers (unit tested) ----------------------------------------------
def parse_now_playing(payload, ignore_users=()):
"""getNowPlaying JSON -> {username: entry}. Keeps the most recent session per user."""
resp = (payload or {}).get("subsonic-response") or {}
if resp.get("status") != "ok":
raise ValueError((resp.get("error") or {}).get("message") or "getNowPlaying failed")
entries = (resp.get("nowPlaying") or {}).get("entry") or []
if isinstance(entries, dict):
entries = [entries]
out = {}
for e in entries:
user = (e.get("username") or "").strip()
if not user or user in ignore_users:
continue
state = (e.get("state") or "playing").lower()
if state not in ("playing", "paused"):
continue
minutes = int(e.get("minutesAgo") or 0)
if state == "playing" and minutes > STALE_MINUTES and not e.get("positionMs"):
continue
entry = {
"title": e.get("title") or "",
"artist": e.get("displayArtist") or e.get("artist") or "",
"album": e.get("album") or "",
"durationS": int(e.get("duration") or 0),
"positionMs": int(e.get("positionMs") or 0),
"state": state,
"player": e.get("playerName") or "",
"coverArt": e.get("coverArt") or "",
"minutesAgo": minutes,
}
if not entry["title"]:
continue
prev = out.get(user)
if prev is None or minutes < prev["minutesAgo"]:
out[user] = entry
return out
def username_from_admin_user(user, email_domain):
"""Synapse admin v2 user JSON -> SSO username, or None."""
for ext in user.get("external_ids") or []:
provider = ext.get("auth_provider") or ""
ext_id = ext.get("external_id") or ""
# One legacy link stores the plain username instead of Authelia's opaque ID.
if (provider.startswith("http") and re.fullmatch(r"[a-z0-9._-]+", ext_id)
and not UUID_RE.fullmatch(ext_id)):
return ext_id
suffix = "@" + email_domain.lower()
for tp in user.get("threepids") or []:
addr = (tp.get("address") or "").strip().lower()
if tp.get("medium") == "email" and addr.endswith(suffix):
local = addr[: -len(suffix)]
if re.fullmatch(r"[a-z0-9._-]+", local):
return local
return None
def public_entry(entry, now_ms):
"""What Lotus Chat gets for one listener."""
out = {k: entry[k] for k in ("title", "artist", "album", "durationS", "positionMs", "state", "player")}
out["updatedAt"] = now_ms
out["cover"] = f"{PREFIX}/cover/{urllib.parse.quote(entry['coverArt'], safe='')}" if entry.get("coverArt") else None
return out
def parse_ids(raw, server_name):
ids = []
for part in (raw or "").split(","):
part = part.strip()
if part.startswith("@") and part.endswith(":" + server_name) and part not in ids:
ids.append(part)
return ids[:MAX_IDS]
# --- stateful pieces ---------------------------------------------------------
class OptIns:
"""{mxid: True} persisted atomically."""
def __init__(self, path):
self.path = path
self.lock = threading.Lock()
self.data = {}
try:
with open(path) as f:
loaded = json.load(f)
self.data = {k: True for k, v in loaded.items() if v is True}
except FileNotFoundError:
pass
except (OSError, ValueError) as exc:
log.error("could not read %s: %s", path, exc)
def get(self, mxid):
with self.lock:
return self.data.get(mxid, False)
def all(self):
with self.lock:
return list(self.data)
def set(self, mxid, share):
with self.lock:
if share:
self.data[mxid] = True
else:
self.data.pop(mxid, None)
snapshot = dict(self.data)
d = os.path.dirname(self.path) or "."
fd, tmp = tempfile.mkstemp(dir=d, prefix=".state-")
try:
with os.fdopen(fd, "w") as f:
json.dump(snapshot, f, indent=1, sort_keys=True)
os.replace(tmp, self.path)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise
class TTLCache:
def __init__(self, max_items=2000):
self.lock = threading.Lock()
self.items = {}
self.max_items = max_items
def get(self, key, now=None):
now = time.monotonic() if now is None else now
with self.lock:
hit = self.items.get(key)
if hit and hit[1] > now:
return hit[0], True
self.items.pop(key, None)
return None, False
def put(self, key, value, ttl, now=None):
now = time.monotonic() if now is None else now
with self.lock:
if len(self.items) >= self.max_items:
for k in [k for k, (_, exp) in self.items.items() if exp <= now] or list(self.items)[:100]:
self.items.pop(k, None)
self.items[key] = (value, now + ttl)
class Service:
def __init__(self, cfg, http_get=None):
self.cfg = cfg
self.http_get = http_get or _http_get
self.optins = OptIns(os.path.join(cfg["state_dir"], "state.json"))
self.tokens = TTLCache()
self.users = TTLCache()
self.lock = threading.Lock()
self.playing = {}
self.overrides = {}
self.overrides_mtime = None
self.load_overrides()
# Navidrome
def subsonic_url(self, method, **params):
salt = secrets.token_hex(8)
token = hashlib.md5((self.cfg["nd_password"] + salt).encode()).hexdigest() # noqa: S324 (Subsonic protocol)
q = {"u": self.cfg["nd_user"], "t": token, "s": salt, "v": SUBSONIC_VERSION,
"c": SUBSONIC_CLIENT, "f": "json", **params}
return f"{self.cfg['nd_url']}/rest/{method}?{urllib.parse.urlencode(q)}"
def poll_once(self):
status, body, _ = self.http_get(self.subsonic_url("getNowPlaying"), {}, 10)
if status != 200:
raise ValueError(f"getNowPlaying HTTP {status}")
parsed = parse_now_playing(json.loads(body), self.cfg["ignore_users"])
with self.lock:
self.playing = parsed
return len(parsed)
def poll_forever(self):
failures = 0
while True:
try:
n = self.poll_once()
if failures:
log.info("navidrome reachable again (%d sessions)", n)
failures = 0
except Exception as exc: # keep polling whatever happens
failures += 1
if failures in (1, 10) or failures % 100 == 0:
log.warning("poll failed (%d in a row): %s", failures, exc)
if failures >= 3:
with self.lock:
self.playing = {}
time.sleep(self.cfg["poll_s"])
# identity
def verify_token(self, token):
if not token or len(token) > 512:
return None
cached, hit = self.tokens.get(token)
if hit:
return cached
url = (f"{self.cfg['synapse']}/_matrix/federation/v1/openid/userinfo?"
+ urllib.parse.urlencode({"access_token": token}))
try:
status, body, _ = self.http_get(url, {}, 5)
except Exception as exc:
log.warning("openid check failed: %s", exc)
return None
mxid = None
if status == 200:
sub = (json.loads(body) or {}).get("sub") or ""
if sub.startswith("@") and sub.endswith(":" + self.cfg["server_name"]):
mxid = sub
# Tokens live about an hour; a rejected one is remembered briefly.
self.tokens.put(token, mxid, 600 if mxid else 30)
return mxid
def load_overrides(self):
path = self.cfg["users_file"]
try:
mtime = os.path.getmtime(path)
except OSError:
self.overrides = {}
return
if mtime == self.overrides_mtime:
return
try:
with open(path) as f:
data = json.load(f)
self.overrides = {k: v for k, v in data.items() if isinstance(v, str) and v}
self.overrides_mtime = mtime
except (OSError, ValueError) as exc:
log.error("could not read %s: %s", path, exc)
def resolve(self, mxid):
self.load_overrides()
if mxid in self.overrides:
return self.overrides[mxid]
cached, hit = self.users.get(mxid)
if hit:
return cached
name = None
url = f"{self.cfg['synapse']}/_synapse/admin/v2/users/{urllib.parse.quote(mxid)}"
try:
status, body, _ = self.http_get(url, {"Authorization": f"Bearer {self.cfg['matrix_token']}"}, 5)
if status == 200:
name = username_from_admin_user(json.loads(body), self.cfg["email_domain"])
elif status != 404:
return None # don't cache transient failures
except Exception as exc:
log.warning("user lookup failed: %s", exc)
return None
self.users.put(mxid, name, 3600 if name else 300)
return name
# views
def entry_for(self, mxid):
if not self.optins.get(mxid):
return None
name = self.resolve(mxid)
if not name:
return None
with self.lock:
return self.playing.get(name)
def users_view(self, ids):
now_ms = int(time.time() * 1000)
out = {}
for mxid in ids:
entry = self.entry_for(mxid)
if entry:
out[mxid] = public_entry(entry, now_ms)
return out
def me_view(self, mxid):
name = self.resolve(mxid)
share = self.optins.get(mxid)
entry = None
if name:
with self.lock:
entry = self.playing.get(name)
return {
"share": share,
"linked": bool(name),
"playing": public_entry(entry, int(time.time() * 1000)) if entry else None,
}
def cover_allowed(self, cover_id):
with self.lock:
playing = dict(self.playing)
wanted = {name for name, e in playing.items() if e.get("coverArt") == cover_id}
if not wanted:
return False
return any(self.resolve(m) in wanted for m in self.optins.all())
# --- HTTP --------------------------------------------------------------------
def _http_get(url, headers, timeout):
req = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
return resp.status, resp.read(), resp.headers.get("Content-Type", "")
except urllib.error.HTTPError as exc:
return exc.code, exc.read(), exc.headers.get("Content-Type", "")
class Handler(BaseHTTPRequestHandler):
server_version = "lotus-now-playing"
service: Service = None # set in main()
def log_message(self, fmt, *args): # no request logs: paths carry user IDs
pass
def _cors(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
self.send_header("Access-Control-Allow-Methods", "GET, PUT, OPTIONS")
self.send_header("Access-Control-Max-Age", "600")
def _json(self, status, obj):
body = json.dumps(obj).encode()
self.send_response(status)
self._cors()
self.send_header("Content-Type", "application/json")
self.send_header("Cache-Control", "no-store")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _caller(self):
auth = self.headers.get("Authorization") or ""
token = auth[7:].strip() if auth.lower().startswith("bearer ") else ""
return self.service.verify_token(token)
def do_OPTIONS(self):
self.send_response(204)
self._cors()
self.end_headers()
def do_GET(self):
url = urllib.parse.urlsplit(self.path)
if url.path == "/healthz":
return self._json(200, {"ok": True})
if url.path.startswith(PREFIX + "/cover/"):
return self._cover(urllib.parse.unquote(url.path[len(PREFIX) + 7:]), url.query)
mxid = self._caller()
if not mxid:
return self._json(401, {"errcode": "M_UNKNOWN_TOKEN", "error": "Matrix OpenID token required"})
if url.path == PREFIX + "/me":
return self._json(200, self.service.me_view(mxid))
if url.path == PREFIX + "/users":
raw = urllib.parse.parse_qs(url.query).get("ids", [""])[0]
ids = parse_ids(raw, self.service.cfg["server_name"])
return self._json(200, {"users": self.service.users_view(ids)})
return self._json(404, {"errcode": "M_NOT_FOUND"})
def do_PUT(self):
url = urllib.parse.urlsplit(self.path)
if url.path != PREFIX + "/me":
return self._json(404, {"errcode": "M_NOT_FOUND"})
mxid = self._caller()
if not mxid:
return self._json(401, {"errcode": "M_UNKNOWN_TOKEN", "error": "Matrix OpenID token required"})
try:
length = int(self.headers.get("Content-Length") or 0)
body = json.loads(self.rfile.read(min(length, 4096)) or b"{}")
share = body["share"]
if not isinstance(share, bool):
raise ValueError
except (ValueError, KeyError, TypeError):
return self._json(400, {"errcode": "M_BAD_JSON", "error": "expected {\"share\": true|false}"})
try:
self.service.optins.set(mxid, share)
except OSError as exc:
log.error("could not save opt-in: %s", exc)
return self._json(500, {"errcode": "M_UNKNOWN", "error": "could not save"})
log.info("sharing %s (now %d users)", "on" if share else "off", len(self.service.optins.all()))
return self._json(200, self.service.me_view(mxid))
def _cover(self, cover_id, query):
if not cover_id or len(cover_id) > 128 or not self.service.cover_allowed(cover_id):
return self._json(404, {"errcode": "M_NOT_FOUND"})
try:
size = int(urllib.parse.parse_qs(query).get("size", ["256"])[0])
except ValueError:
size = 256
size = min(COVER_SIZES, key=lambda s: abs(s - size))
try:
status, body, ctype = self.service.http_get(
self.service.subsonic_url("getCoverArt", id=cover_id, size=size), {}, 10)
except Exception:
status, body, ctype = 502, b"", ""
if status != 200 or not ctype.startswith("image/"):
return self._json(502, {"errcode": "M_UNKNOWN", "error": "cover unavailable"})
self.send_response(200)
self._cors()
self.send_header("Content-Type", ctype)
self.send_header("Cache-Control", "public, max-age=300")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def load_config(env=os.environ):
def need(name):
value = env.get(name, "")
if not value:
raise SystemExit(f"missing {name}")
return value
return {
"bind": env.get("BIND_HOST", "0.0.0.0"),
"port": int(env.get("BIND_PORT", "8072")),
"server_name": env.get("SERVER_NAME", "matrix.lotusguild.org"),
"email_domain": env.get("EMAIL_DOMAIN", "lotusguild.org"),
"synapse": env.get("SYNAPSE_API", "http://127.0.0.1:8008").rstrip("/"),
"matrix_token": need("MATRIX_TOKEN"),
"nd_url": need("NAVIDROME_URL").rstrip("/"),
"nd_user": need("NAVIDROME_USER"),
"nd_password": need("NAVIDROME_PASSWORD"),
"ignore_users": tuple(u for u in env.get("IGNORE_USERS", "droppedneedle").split(",") if u),
"state_dir": env.get("STATE_DIR", "/var/lib/lotus-now-playing"),
"users_file": env.get("USERS_FILE", "/etc/lotus-now-playing/users.json"),
"poll_s": max(5, int(env.get("POLL_SECONDS", "10"))),
}
def main():
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
cfg = load_config()
service = Service(cfg)
try:
log.info("navidrome ok, %d sessions", service.poll_once())
except Exception as exc:
log.warning("first poll failed: %s", exc)
threading.Thread(target=service.poll_forever, daemon=True).start()
Handler.service = service
server = ThreadingHTTPServer((cfg["bind"], cfg["port"]), Handler)
server.daemon_threads = True
log.info("listening on %s:%d (%d users sharing)", cfg["bind"], cfg["port"], len(service.optins.all()))
server.serve_forever()
if __name__ == "__main__":
main()
+173
View File
@@ -0,0 +1,173 @@
"""Tests for lotus_now_playing.py. Run: python3 -m unittest now-playing/test_lotus_now_playing.py"""
import json
import os
import sys
import tempfile
import unittest
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import lotus_now_playing as np # noqa: E402
def nd(entries, status="ok"):
return {"subsonic-response": {"status": status, "nowPlaying": {"entry": entries}}}
def entry(user, title="Song", minutes=0, state="playing", cover="al-1", **kw):
e = {"username": user, "title": title, "artist": "Band", "album": "LP", "duration": 200,
"minutesAgo": minutes, "state": state, "coverArt": cover, "playerName": "DN", "positionMs": 1000}
e.update(kw)
return e
class ParseTests(unittest.TestCase):
def test_basic_and_ignored_users(self):
out = np.parse_now_playing(nd([entry("jared"), entry("droppedneedle")]), ("droppedneedle",))
self.assertEqual(list(out), ["jared"])
self.assertEqual(out["jared"]["artist"], "Band")
self.assertEqual(out["jared"]["durationS"], 200)
def test_prefers_display_artist(self):
out = np.parse_now_playing(nd([entry("a", displayArtist="Band feat. X")]))
self.assertEqual(out["a"]["artist"], "Band feat. X")
def test_newest_session_wins(self):
out = np.parse_now_playing(nd([entry("a", "Old", minutes=5), entry("a", "New", minutes=0)]))
self.assertEqual(out["a"]["title"], "New")
def test_drops_stopped_untitled_and_stale(self):
out = np.parse_now_playing(nd([
entry("a", state="stopped"),
entry("b", title=""),
entry("c", minutes=90, positionMs=0),
entry("d", state="paused"),
]))
self.assertEqual(list(out), ["d"])
def test_single_entry_object_and_empty(self):
self.assertEqual(list(np.parse_now_playing(nd(entry("a")))), ["a"])
self.assertEqual(np.parse_now_playing({"subsonic-response": {"status": "ok"}}), {})
def test_error_raises(self):
with self.assertRaises(ValueError):
np.parse_now_playing({"subsonic-response": {"status": "failed", "error": {"message": "nope"}}})
class MappingTests(unittest.TestCase):
def test_email_on_domain(self):
user = {"threepids": [{"medium": "email", "address": "Tyler@LotusGuild.org"}]}
self.assertEqual(np.username_from_admin_user(user, "lotusguild.org"), "tyler")
def test_other_domain_ignored(self):
user = {"threepids": [{"medium": "email", "address": "x@gmail.com"}]}
self.assertIsNone(np.username_from_admin_user(user, "lotusguild.org"))
def test_legacy_external_id(self):
user = {"external_ids": [{"auth_provider": "https://auth.lotusguild.org", "external_id": "cole"}]}
self.assertEqual(np.username_from_admin_user(user, "lotusguild.org"), "cole")
def test_opaque_uuid_is_not_a_username(self):
user = {"external_ids": [
{"auth_provider": "oidc-authelia", "external_id": "b546660f-371c-45de-812c-bbae0e615148"},
{"auth_provider": "https://auth.lotusguild.org", "external_id": "b546660f-371c-45de-812c-bbae0e615148"},
]}
self.assertIsNone(np.username_from_admin_user(user, "lotusguild.org"))
def test_parse_ids(self):
ids = np.parse_ids("@a:hs, @b:other,@a:hs,junk,@c:hs", "hs")
self.assertEqual(ids, ["@a:hs", "@c:hs"])
self.assertEqual(len(np.parse_ids(",".join(f"@u{i}:hs" for i in range(80)), "hs")), np.MAX_IDS)
class FakeHttp:
def __init__(self):
self.calls = []
self.now_playing = nd([entry("jared", cover="al-9"), entry("jeff", cover="al-7")])
self.admin = {
"@jared:hs": {"threepids": [{"medium": "email", "address": "jared@lotusguild.org"}]},
"@nomail:hs": {"threepids": []},
}
self.openid = {"good": "@jared:hs", "foreign": "@x:elsewhere"}
def __call__(self, url, headers, timeout):
self.calls.append(url)
if "/rest/getNowPlaying" in url:
return 200, json.dumps(self.now_playing).encode(), "application/json"
if "/openid/userinfo" in url:
tok = url.split("access_token=")[1]
if tok in self.openid:
return 200, json.dumps({"sub": self.openid[tok]}).encode(), "application/json"
return 401, b"{}", "application/json"
if "/_synapse/admin/v2/users/" in url:
mxid = url.rsplit("/", 1)[1].replace("%40", "@").replace("%3A", ":")
if mxid in self.admin:
return 200, json.dumps(self.admin[mxid]).encode(), "application/json"
return 404, b"{}", "application/json"
raise AssertionError(url)
class ServiceTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
users = os.path.join(self.tmp.name, "users.json")
with open(users, "w") as f:
json.dump({"@bigmanjeff:hs": "jeff"}, f)
self.http = FakeHttp()
self.svc = np.Service({
"server_name": "hs", "email_domain": "lotusguild.org", "synapse": "http://syn",
"matrix_token": "admin", "nd_url": "http://nd", "nd_user": "svc", "nd_password": "pw",
"ignore_users": ("droppedneedle",), "state_dir": self.tmp.name, "users_file": users, "poll_s": 10,
}, http_get=self.http)
self.svc.poll_once()
def tearDown(self):
self.tmp.cleanup()
def test_token_check_and_cache(self):
self.assertEqual(self.svc.verify_token("good"), "@jared:hs")
self.assertEqual(self.svc.verify_token("good"), "@jared:hs")
self.assertEqual(sum("/openid/" in c for c in self.http.calls), 1)
self.assertIsNone(self.svc.verify_token("bad"))
self.assertIsNone(self.svc.verify_token("foreign"))
self.assertIsNone(self.svc.verify_token(""))
def test_opt_in_required(self):
self.assertEqual(self.svc.users_view(["@jared:hs", "@bigmanjeff:hs"]), {})
self.svc.optins.set("@jared:hs", True)
view = self.svc.users_view(["@jared:hs", "@bigmanjeff:hs"])
self.assertEqual(list(view), ["@jared:hs"])
self.assertEqual(view["@jared:hs"]["cover"], "/now-playing/v1/cover/al-9")
def test_override_mapping(self):
self.svc.optins.set("@bigmanjeff:hs", True)
self.assertIn("@bigmanjeff:hs", self.svc.users_view(["@bigmanjeff:hs"]))
def test_opt_in_persists_and_opt_out(self):
self.svc.optins.set("@jared:hs", True)
reloaded = np.OptIns(os.path.join(self.tmp.name, "state.json"))
self.assertTrue(reloaded.get("@jared:hs"))
self.svc.optins.set("@jared:hs", False)
self.assertEqual(self.svc.users_view(["@jared:hs"]), {})
def test_me_view(self):
me = self.svc.me_view("@jared:hs")
self.assertEqual((me["share"], me["linked"]), (False, True))
self.assertEqual(me["playing"]["title"], "Song")
self.assertEqual(self.svc.me_view("@nomail:hs"), {"share": False, "linked": False, "playing": None})
def test_cover_allow_list(self):
self.assertFalse(self.svc.cover_allowed("al-9")) # nobody opted in yet
self.svc.optins.set("@jared:hs", True)
self.assertTrue(self.svc.cover_allowed("al-9"))
self.assertFalse(self.svc.cover_allowed("al-7")) # jeff hasn't opted in
self.assertFalse(self.svc.cover_allowed("al-unknown"))
def test_subsonic_url_never_contains_password(self):
url = self.svc.subsonic_url("getNowPlaying")
self.assertNotIn("pw", url.split("?")[1].split("&t=")[0])
self.assertIn("u=svc", url)
self.assertNotIn("p=", url)
if __name__ == "__main__":
unittest.main()
+38
View File
@@ -0,0 +1,38 @@
[Unit]
Description=Now playing (Navidrome) for Lotus Chat "Listening to" cards
After=network-online.target matrix-synapse.service
Wants=network-online.target
[Service]
Type=simple
User=lotus-now-playing
Group=lotus-now-playing
# MATRIX_TOKEN (Synapse admin API, user lookups only), shared with voice-limit-guard.
EnvironmentFile=/etc/matrix-deploy.env
# NAVIDROME_USER / NAVIDROME_PASSWORD for the read-only lotus-nowplaying account.
EnvironmentFile=/etc/lotus-now-playing/env
Environment=NAVIDROME_URL=http://10.10.10.28:4533
Environment=SERVER_NAME=matrix.lotusguild.org
Environment=BIND_HOST=0.0.0.0
Environment=BIND_PORT=8072
Environment=STATE_DIR=/var/lib/lotus-now-playing
Environment=USERS_FILE=/etc/lotus-now-playing/users.json
StateDirectory=lotus-now-playing
StateDirectoryMode=0750
ExecStart=/usr/bin/python3 /opt/lotus-now-playing/lotus_now_playing.py
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=lotus-now-playing
MemoryMax=96M
CPUQuota=20%
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
[Install]
WantedBy=multi-user.target