Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bramenn opened this issue Dec 28, 2022 · 1 comment

Comments

@bramenn
Copy link
Contributor

bramenn commented Dec 28, 2022

/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
Copy link
Contributor Author

bramenn commented Jan 4, 2023

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
Labels
None yet
Development

No branches or pull requests

1 participant