diff --git a/my_project_name/callbacks.py b/my_project_name/callbacks.py index 08bbbc1..8019c1e 100644 --- a/my_project_name/callbacks.py +++ b/my_project_name/callbacks.py @@ -104,6 +104,19 @@ class Callbacks: # Successfully joined room logger.info(f"Joined {room.room_id}") + async def invite_event_filtered_callback( + self, room: MatrixRoom, event: InviteMemberEvent + ) -> None: + """ + Since the InviteMemberEvent is fired for every m.room.member state received + in a sync response's `rooms.invite` section, we will receive some that are + not actually our own invite event (such as the inviter's membership). + This makes sure we only call `callbacks.invite` with our own invite events. + """ + if event.state_key == self.client.user_id: + # This is our own membership (invite) event + await self.invite(room, event) + async def _reaction( self, room: MatrixRoom, event: UnknownEvent, reacted_to_id: str ) -> None: diff --git a/my_project_name/main.py b/my_project_name/main.py index 9a83088..9554ffd 100644 --- a/my_project_name/main.py +++ b/my_project_name/main.py @@ -11,7 +11,6 @@ from nio import ( InviteMemberEvent, LocalProtocolError, LoginError, - MatrixRoom, MegolmEvent, RoomMessageText, UnknownEvent, @@ -64,21 +63,9 @@ async def main(): # Set up event callbacks callbacks = Callbacks(client, store, config) client.add_event_callback(callbacks.message, (RoomMessageText,)) - - def invite_event_filtered_callback( - room: MatrixRoom, event: InviteMemberEvent - ) -> None: - """ - Since the InviteMemberEvent is fired for every m.room.member state received - in a sync response's `rooms.invite` section, we will receive some that are - not actually our own invite event (such as the inviter's membership). - This makes sure we only call `callbacks.invite` with our own invite events. - """ - if event.state_key == client.user_id: - # This is our own membership (invite) event - callbacks.invite(room, event) - - client.add_event_callback(invite_event_filtered_callback, (InviteMemberEvent,)) + client.add_event_callback( + callbacks.invite_event_filtered_callback, (InviteMemberEvent,) + ) client.add_event_callback(callbacks.decryption_failure, (MegolmEvent,)) client.add_event_callback(callbacks.unknown, (UnknownEvent,))