From 39a691db35a0036f8d544e4151383eae2191dda1 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Fri, 30 Jul 2021 01:50:32 -0400 Subject: [PATCH] Make bridgebot leave rooms when last member leaves Most useful for leaving abandoned notice rooms --- matrix_puppeteer_line/db/user.py | 5 +++++ matrix_puppeteer_line/matrix.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/matrix_puppeteer_line/db/user.py b/matrix_puppeteer_line/db/user.py index f71588b..63c3af1 100644 --- a/matrix_puppeteer_line/db/user.py +++ b/matrix_puppeteer_line/db/user.py @@ -47,3 +47,8 @@ class User: if not row: return None return cls(**row) + + @classmethod + async def discard_notice_room(cls, notice_room: RoomID) -> None: + await cls.db.execute('DELETE FROM "user" WHERE notice_room=$1', + notice_room) diff --git a/matrix_puppeteer_line/matrix.py b/matrix_puppeteer_line/matrix.py index b135140..2d6806d 100644 --- a/matrix_puppeteer_line/matrix.py +++ b/matrix_puppeteer_line/matrix.py @@ -22,6 +22,7 @@ from mautrix.types import (Event, EventType, MessageEvent, StateEvent, Encrypted from mautrix.errors import MatrixError from . import portal as po, puppet as pu, user as u +from .db import User as DBUser if TYPE_CHECKING: from .__main__ import MessagesBridge @@ -151,6 +152,14 @@ class MatrixHandler(BaseMatrixHandler): async def handle_leave(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None: portal = await po.Portal.get_by_mxid(room_id) if not portal: + intent = self.bridge.az.intent + try: + if len(await intent.get_room_members(room_id)) == 1: + self.log.info(f"Bridge bot leaving empty room") + await intent.leave_room(room_id) + await DBUser.discard_notice_room(room_id) + except MatrixError: + self.log.exception(f"Failed to get member list of {room_id}") return user = await u.User.get_by_mxid(user_id, create=False)