Skip to content

/sync will return the most recent messages for a room as soon as you /join it. #128

Description

@bramenn

/sync will return the most recent messages for a room as soon as you /join it.
We do NOT want to process those events in that particular room because they may have already been processed (if you toggle the bot in/out of the room).

https://github.com/mautrix/go/blob/master/sync.go#L259-L282

Activity

  1. bramenn commented on Jan 4, 2023

    @bramenn
    ContributorAuthor

    The best thing is that in your bot, overwrite the handle_sync method as follows:

        def handle_sync(self, data: JSON) -> list[asyncio.Task]:
    
            # This is a way to remove duplicate events from the sync.
            for room_id, room_data in data.get("rooms", {}).get("join", {}).items():
                for i in range(len(room_data.get("timeline", {}).get("events", [])) - 1, -1, -1):
                    evt = room_data.get("timeline", {}).get("events", [])[i]
                    if (
                        evt.get("type", "") == "m.room.member"
                        and evt.get("state_key", "") == self.mxid
                    ):
                        if evt.get("content", {}).get("membership") == "join":
                            self.LAST_JOIN_EVENT[room_id] = evt.get("origin_server_ts")
    
                    if (
                        self.LAST_JOIN_EVENT.get(room_id)
                        and evt.get("origin_server_ts") < self.LAST_JOIN_EVENT[room_id]
                    ):
                        del room_data.get("timeline", {}).get("events", [])[i]
                        continue
    
            return super().handle_sync(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions