From 5f04729ce8c981fe03b0652349c46d1fcfd2cd2f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 13 Mar 2023 13:45:32 +0200 Subject: [PATCH] Preserve reaction timestamps if possible --- mautrix_telegram/portal.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 1d5877c5..35706ecf 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -3221,17 +3221,18 @@ class Portal(DBPortal, BasePortal): ) @staticmethod - def _reactions_filter(lst: list[TypeReaction], existing: DBReaction) -> bool: + def _reactions_filter(lst: list[MessagePeerReaction], existing: DBReaction) -> bool: if not lst: return False - for reaction in lst: + for wrapped_reaction in lst: + reaction = wrapped_reaction.reaction if isinstance(reaction, ReactionCustomEmoji) and existing.reaction == str( reaction.document_id ): - lst.remove(reaction) + lst.remove(wrapped_reaction) return True elif isinstance(reaction, ReactionEmoji) and existing.reaction == reaction.emoticon: - lst.remove(reaction) + lst.remove(wrapped_reaction) return True return False @@ -3251,15 +3252,14 @@ class Portal(DBPortal, BasePortal): total_count: int, timestamp: datetime | None = None, ) -> None: - reactions: dict[TelegramID, list[TypeReaction]] = {} + reactions: dict[TelegramID, list[MessagePeerReaction]] = {} custom_emoji_ids: list[int] = [] for reaction in reaction_list: if isinstance(reaction.peer_id, (PeerUser, PeerChannel)) and isinstance( reaction.reaction, (ReactionEmoji, ReactionCustomEmoji) ): - reactions.setdefault(p.Puppet.get_id_from_peer(reaction.peer_id), []).append( - reaction.reaction - ) + sender_user_id = p.Puppet.get_id_from_peer(reaction.peer_id) + reactions.setdefault(sender_user_id, []).append(reaction) if isinstance(reaction.reaction, ReactionCustomEmoji): custom_emoji_ids.append(reaction.reaction.document_id) is_full = len(reaction_list) == total_count @@ -3284,7 +3284,8 @@ class Portal(DBPortal, BasePortal): new_reaction: TypeReaction for sender, new_reactions in reactions.items(): - for new_reaction in new_reactions: + for new_wrapped_reaction in new_reactions: + new_reaction = new_wrapped_reaction.reaction if isinstance(new_reaction, ReactionEmoji): emoji_id = new_reaction.emoticon matrix_reaction = variation_selector.add(new_reaction.emoticon) @@ -3301,7 +3302,10 @@ class Portal(DBPortal, BasePortal): self.log.debug(f"Bridging reaction {emoji_id} by {sender} to {msg.tgid}") puppet: p.Puppet = await p.Puppet.get_by_tgid(sender) mxid = await puppet.intent_for(self).react( - msg.mx_room, msg.mxid, matrix_reaction, timestamp=timestamp + msg.mx_room, + msg.mxid, + matrix_reaction, + timestamp=new_wrapped_reaction.date or timestamp, ) await DBReaction( mxid=mxid,