diff --git a/services/communication/atlasbot-deployment.yaml b/services/communication/atlasbot-deployment.yaml index 9778005..fbb9b3d 100644 --- a/services/communication/atlasbot-deployment.yaml +++ b/services/communication/atlasbot-deployment.yaml @@ -33,9 +33,7 @@ spec: - name: AUTH_BASE value: http://matrix-authentication-service:8080 - name: BOT_USER - value: atlas - - name: BOT_MENTIONS - value: atlas + value: atlasbot - name: BOT_PASS valueFrom: secretKeyRef: diff --git a/services/communication/reset-othrys-room-job.yaml b/services/communication/reset-othrys-room-job.yaml index ddcc0a7..e282b44 100644 --- a/services/communication/reset-othrys-room-job.yaml +++ b/services/communication/reset-othrys-room-job.yaml @@ -2,7 +2,7 @@ apiVersion: batch/v1 kind: Job metadata: - name: othrys-room-reset-1 + name: othrys-room-reset-2 namespace: comms spec: backoffLimit: 0 @@ -33,12 +33,7 @@ spec: name: atlasbot-credentials-runtime key: seeder-password - name: BOT_USER - value: atlas - - name: BOT_PASS - valueFrom: - secretKeyRef: - name: atlasbot-credentials-runtime - key: bot-password + value: atlasbot command: - /bin/sh - -c @@ -57,7 +52,6 @@ spec: SEEDER_USER = os.environ["SEEDER_USER"] SEEDER_PASS = os.environ["SEEDER_PASS"] BOT_USER = os.environ["BOT_USER"] - BOT_PASS = os.environ["BOT_PASS"] POWER_LEVELS = { "ban": 50, @@ -102,15 +96,6 @@ spec: r.raise_for_status() return r.json()["room_id"] - def ensure_user(token, localpart, password, admin): - user_id = f"@{localpart}:{SERVER_NAME}" - url = f"{BASE}/_synapse/admin/v2/users/{urllib.parse.quote(user_id)}" - payload = {"password": password, "admin": admin, "deactivated": False} - r = requests.put(url, headers=auth(token), json=payload) - if r.status_code not in (200, 201): - raise SystemExit(f"ensure user {user_id} failed: {r.status_code} {r.text}") - return user_id - def create_room(token): r = requests.post(f"{BASE}/_matrix/client/v3/createRoom", headers=auth(token), json={ "preset": "public_chat", @@ -152,34 +137,32 @@ spec: ) r.raise_for_status() - def admin_join(token, room_id, user_id): + def list_joined_members(token, room_id): + r = requests.get( + f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/members?membership=join", + headers=auth(token), + ) + r.raise_for_status() + members = [] + for ev in r.json().get("chunk", []): + if ev.get("type") != "m.room.member": + continue + uid = ev.get("state_key") + if not isinstance(uid, str) or not uid.startswith("@"): + continue + members.append(uid) + return members + + def invite_user(token, room_id, user_id): r = requests.post( - f"{BASE}/_synapse/admin/v1/join/{urllib.parse.quote(room_id)}", + f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/invite", headers=auth(token), json={"user_id": user_id}, ) + if r.status_code in (200, 202): + return r.raise_for_status() - def join_all_locals(token, room_id): - users = [] - from_token = None - while True: - url = f"{BASE}/_synapse/admin/v2/users?local=true&deactivated=false&limit=100" - if from_token: - url += f"&from={from_token}" - res = requests.get(url, headers=auth(token)) - res.raise_for_status() - data = res.json() - for u in data.get("users", []): - if u.get("is_guest"): - continue - users.append(u["name"]) - from_token = data.get("next_token") - if not from_token: - break - for uid in users: - admin_join(token, room_id, uid) - def send_message(token, room_id, body): r = requests.post( f"{BASE}/_matrix/client/v3/rooms/{urllib.parse.quote(room_id)}/send/m.room.message", @@ -195,8 +178,6 @@ spec: if not old_room_id: raise SystemExit(f"alias {ROOM_ALIAS} not found; refusing to proceed") - bot_user_id = ensure_user(token, BOT_USER, BOT_PASS, admin=False) - new_room_id = create_room(token) # Configure the new room. @@ -212,9 +193,16 @@ spec: set_directory_visibility(token, new_room_id, "public") - # Join the bot and all local non-guest users. - admin_join(token, new_room_id, bot_user_id) - join_all_locals(token, new_room_id) + # Invite the bot and all joined members of the old room. + bot_user_id = f"@{BOT_USER}:{SERVER_NAME}" + invite_user(token, new_room_id, bot_user_id) + for uid in list_joined_members(token, old_room_id): + if uid == f"@{SEEDER_USER}:{SERVER_NAME}": + continue + localpart = uid.split(":", 1)[0].lstrip("@") + if localpart.isdigit(): + continue + invite_user(token, new_room_id, uid) # Pin the guest invite message in the new room. event_id = send_message(token, new_room_id, PIN_MESSAGE) @@ -225,6 +213,7 @@ spec: put_state(token, old_room_id, "m.room.join_rules", {"join_rule": "invite"}) put_state(token, old_room_id, "m.room.guest_access", {"guest_access": "forbidden"}) put_state(token, old_room_id, "m.room.tombstone", {"body": "Othrys has been reset. Please join the new room.", "replacement_room": new_room_id}) + send_message(token, old_room_id, "Othrys was reset. Join the new room at https://live.bstein.dev/#/room/#othrys:live.bstein.dev?action=join") print(f"old_room_id={old_room_id}") print(f"new_room_id={new_room_id}") diff --git a/services/communication/seed-othrys-room.yaml b/services/communication/seed-othrys-room.yaml index a54f0de..a80b388 100644 --- a/services/communication/seed-othrys-room.yaml +++ b/services/communication/seed-othrys-room.yaml @@ -30,7 +30,7 @@ spec: name: atlasbot-credentials-runtime key: seeder-password - name: BOT_USER - value: atlas + value: atlasbot - name: BOT_PASS valueFrom: secretKeyRef: