communication: make pin job mutable

This commit is contained in:
Brad Stein 2025-12-31 15:23:17 -03:00
parent d8d741bbd9
commit 6203faae3f

View File

@ -1,11 +1,16 @@
# services/communication/pin-othrys-job.yaml # services/communication/pin-othrys-job.yaml
apiVersion: batch/v1 apiVersion: batch/v1
kind: Job kind: CronJob
metadata: metadata:
name: pin-othrys-invite name: pin-othrys-invite
namespace: communication namespace: communication
spec: spec:
ttlSecondsAfterFinished: 3600 schedule: "*/30 * * * *"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template: template:
spec: spec:
restartPolicy: OnFailure restartPolicy: OnFailure
@ -29,9 +34,17 @@ spec:
set -euo pipefail set -euo pipefail
pip install --no-cache-dir requests >/dev/null pip install --no-cache-dir requests >/dev/null
python - <<'PY' python - <<'PY'
import requests, urllib.parse, os import os, requests, urllib.parse
BASE = os.environ["SYNAPSE_BASE"] BASE = os.environ["SYNAPSE_BASE"]
ROOM_ALIAS = "#othrys:live.bstein.dev"
MESSAGE = (
"Invite guests: share https://live.bstein.dev/#/room/#othrys:live.bstein.dev?action=join "
"and choose 'Continue' -> 'Join as guest'."
)
def auth(token): return {"Authorization": f"Bearer {token}"}
def login(user, password): def login(user, password):
r = requests.post(f"{BASE}/_matrix/client/v3/login", json={ r = requests.post(f"{BASE}/_matrix/client/v3/login", json={
"type": "m.login.password", "type": "m.login.password",
@ -43,26 +56,54 @@ spec:
def resolve(alias, token): def resolve(alias, token):
enc = urllib.parse.quote(alias) enc = urllib.parse.quote(alias)
r = requests.get(f"{BASE}/_matrix/client/v3/directory/room/{enc}", headers={"Authorization": f"Bearer {token}"}) r = requests.get(f"{BASE}/_matrix/client/v3/directory/room/{enc}", headers=auth(token))
r.raise_for_status() r.raise_for_status()
return r.json()["room_id"] return r.json()["room_id"]
def get_pinned(room_id, token):
r = requests.get(
f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/state/m.room.pinned_events",
headers=auth(token),
)
if r.status_code == 404:
return []
r.raise_for_status()
return r.json().get("pinned", [])
def get_event(room_id, event_id, token):
r = requests.get(
f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/event/{urllib.parse.quote(event_id)}",
headers=auth(token),
)
if r.status_code == 404:
return None
r.raise_for_status()
return r.json()
def send(room_id, token, body): def send(room_id, token, body):
r = requests.post(f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/send/m.room.message", r = requests.post(
headers={"Authorization": f"Bearer {token}"}, f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/send/m.room.message",
json={"msgtype": "m.text", "body": body}) headers=auth(token),
json={"msgtype": "m.text", "body": body},
)
r.raise_for_status() r.raise_for_status()
return r.json()["event_id"] return r.json()["event_id"]
def pin(room_id, token, event_id): def pin(room_id, token, event_id):
r = requests.put(f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/state/m.room.pinned_events", r = requests.put(
headers={"Authorization": f"Bearer {token}"}, f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/state/m.room.pinned_events",
json={"pinned": [event_id]}) headers=auth(token),
json={"pinned": [event_id]},
)
r.raise_for_status() r.raise_for_status()
token = login(os.environ["SEEDER_USER"], os.environ["SEEDER_PASS"]) token = login(os.environ["SEEDER_USER"], os.environ["SEEDER_PASS"])
room_id = resolve("#othrys:live.bstein.dev", token) room_id = resolve(ROOM_ALIAS, token)
msg = "Invite guests: share https://live.bstein.dev/#/room/#othrys:live.bstein.dev?action=join and choose 'Continue' -> 'Join as guest'." for event_id in get_pinned(room_id, token):
eid = send(room_id, token, msg) ev = get_event(room_id, event_id, token)
if ev and ev.get("content", {}).get("body") == MESSAGE:
raise SystemExit(0)
eid = send(room_id, token, MESSAGE)
pin(room_id, token, eid) pin(room_id, token, eid)
PY PY