Skip InviteMemberEvents that are not our invites (#30)

Co-authored-by: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com>
This commit is contained in:
reivilibre 2021-10-14 14:53:52 +01:00 committed by GitHub
parent c7752db54c
commit 54c2b88d60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@ from nio import (
InviteMemberEvent,
LocalProtocolError,
LoginError,
MatrixRoom,
MegolmEvent,
RoomMessageText,
UnknownEvent,
@ -63,7 +64,21 @@ async def main():
# Set up event callbacks
callbacks = Callbacks(client, store, config)
client.add_event_callback(callbacks.message, (RoomMessageText,))
client.add_event_callback(callbacks.invite, (InviteMemberEvent,))
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.decryption_failure, (MegolmEvent,))
client.add_event_callback(callbacks.unknown, (UnknownEvent,))