Skip to content

Commit

Permalink
Automatically join new room on upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Nov 21, 2019
1 parent c190b75 commit 7a06df5
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion maubot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from mautrix.errors import MatrixInvalidToken, MatrixRequestError
from mautrix.types import (UserID, SyncToken, FilterID, ContentURI, StrippedStateEvent, Membership,
EventType, Filter, RoomFilter, RoomEventFilter)
StateEvent, EventType, Filter, RoomFilter, RoomEventFilter)
from mautrix.client import InternalEventType

from .lib.store_proxy import ClientStoreProxy
Expand Down Expand Up @@ -65,6 +65,7 @@ def __init__(self, db_instance: DBClient) -> None:
self.client.ignore_first_sync = True
if self.autojoin:
self.client.add_event_handler(EventType.ROOM_MEMBER, self._handle_invite)
self.client.add_event_handler(EventType.ROOM_TOMBSTONE, self._handle_tombstone)
self.client.add_event_handler(InternalEventType.SYNC_ERRORED, self._set_sync_ok(False))
self.client.add_event_handler(InternalEventType.SYNC_SUCCESSFUL, self._set_sync_ok(True))

Expand Down Expand Up @@ -189,6 +190,13 @@ def get(cls, user_id: UserID, db_instance: Optional[DBClient] = None) -> Optiona
def all(cls) -> Iterable['Client']:
return (cls.get(user.id, user) for user in DBClient.all())

async def _handle_tombstone(self, evt: StateEvent) -> None:
if not evt.content.replacement_room:
self.log.info(f"{evt.room_id} tombstoned with no replacement, ignoring")
return
_, server = self.client.parse_user_id(evt.sender)
await self.client.join_room(evt.content.replacement_room, servers=[server])

async def _handle_invite(self, evt: StrippedStateEvent) -> None:
if evt.state_key == self.id and evt.content.membership == Membership.INVITE:
await self.client.join_room(evt.room_id)
Expand Down

0 comments on commit 7a06df5

Please sign in to comment.