python icon indicating copy to clipboard operation
python copied to clipboard

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

Open bramenn opened this issue 3 years ago • 1 comments

/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

bramenn avatar Dec 28 '22 18:12 bramenn

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)

bramenn avatar Jan 04 '23 22:01 bramenn