Make bridgebot leave rooms when last member leaves

Most useful for leaving abandoned notice rooms
This commit is contained in:
Andrew Ferrazzutti 2021-07-30 01:50:32 -04:00
parent bc0ee93276
commit 39a691db35
2 changed files with 14 additions and 0 deletions

View File

@ -47,3 +47,8 @@ class User:
if not row: if not row:
return None return None
return cls(**row) 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)

View File

@ -22,6 +22,7 @@ from mautrix.types import (Event, EventType, MessageEvent, StateEvent, Encrypted
from mautrix.errors import MatrixError from mautrix.errors import MatrixError
from . import portal as po, puppet as pu, user as u from . import portal as po, puppet as pu, user as u
from .db import User as DBUser
if TYPE_CHECKING: if TYPE_CHECKING:
from .__main__ import MessagesBridge 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: async def handle_leave(self, room_id: RoomID, user_id: UserID, event_id: EventID) -> None:
portal = await po.Portal.get_by_mxid(room_id) portal = await po.Portal.get_by_mxid(room_id)
if not portal: 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 return
user = await u.User.get_by_mxid(user_id, create=False) user = await u.User.get_by_mxid(user_id, create=False)