Merge branch 'tulir/bot-reactions'
This commit is contained in:
@@ -40,6 +40,7 @@ from telethon.tl.types import (
|
|||||||
PeerUser,
|
PeerUser,
|
||||||
PhoneCallRequested,
|
PhoneCallRequested,
|
||||||
TypeUpdate,
|
TypeUpdate,
|
||||||
|
UpdateBotMessageReaction,
|
||||||
UpdateChannel,
|
UpdateChannel,
|
||||||
UpdateChannelUserTyping,
|
UpdateChannelUserTyping,
|
||||||
UpdateChatDefaultBannedRights,
|
UpdateChatDefaultBannedRights,
|
||||||
@@ -363,6 +364,8 @@ class AbstractUser(ABC):
|
|||||||
await self.update_phone_call(update)
|
await self.update_phone_call(update)
|
||||||
elif isinstance(update, UpdateMessageReactions):
|
elif isinstance(update, UpdateMessageReactions):
|
||||||
await self.update_reactions(update)
|
await self.update_reactions(update)
|
||||||
|
elif isinstance(update, UpdateBotMessageReaction):
|
||||||
|
await self.update_bot_reactions(update)
|
||||||
elif isinstance(update, (UpdateChatUserTyping, UpdateChannelUserTyping, UpdateUserTyping)):
|
elif isinstance(update, (UpdateChatUserTyping, UpdateChannelUserTyping, UpdateUserTyping)):
|
||||||
await self.update_typing(update)
|
await self.update_typing(update)
|
||||||
elif isinstance(update, UpdateUserStatus):
|
elif isinstance(update, UpdateUserStatus):
|
||||||
@@ -636,6 +639,12 @@ class AbstractUser(ABC):
|
|||||||
return
|
return
|
||||||
await portal.handle_telegram_reactions(self, TelegramID(update.msg_id), update.reactions)
|
await portal.handle_telegram_reactions(self, TelegramID(update.msg_id), update.reactions)
|
||||||
|
|
||||||
|
async def update_bot_reactions(self, update: UpdateBotMessageReaction) -> None:
|
||||||
|
portal = await po.Portal.get_by_entity(update.peer, tg_receiver=self.tgid)
|
||||||
|
if not portal or not portal.mxid or not portal.allow_bridging:
|
||||||
|
return
|
||||||
|
await portal.handle_telegram_bot_reactions(self, update)
|
||||||
|
|
||||||
async def update_phone_call(self, update: UpdatePhoneCall) -> None:
|
async def update_phone_call(self, update: UpdatePhoneCall) -> None:
|
||||||
self.log.debug("Phone call update %s", update)
|
self.log.debug("Phone call update %s", update)
|
||||||
if not isinstance(update.phone_call, PhoneCallRequested):
|
if not isinstance(update.phone_call, PhoneCallRequested):
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ from typing import (
|
|||||||
Callable,
|
Callable,
|
||||||
List,
|
List,
|
||||||
Literal,
|
Literal,
|
||||||
|
NamedTuple,
|
||||||
Union,
|
Union,
|
||||||
cast,
|
cast,
|
||||||
)
|
)
|
||||||
@@ -160,6 +161,7 @@ from telethon.tl.types import (
|
|||||||
TypeUser,
|
TypeUser,
|
||||||
TypeUserFull,
|
TypeUserFull,
|
||||||
TypeUserProfilePhoto,
|
TypeUserProfilePhoto,
|
||||||
|
UpdateBotMessageReaction,
|
||||||
UpdateChannelUserTyping,
|
UpdateChannelUserTyping,
|
||||||
UpdateChatUserTyping,
|
UpdateChatUserTyping,
|
||||||
UpdateMessageReactions,
|
UpdateMessageReactions,
|
||||||
@@ -270,6 +272,11 @@ class IgnoredMessageError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class WrappedReaction(NamedTuple):
|
||||||
|
reaction: ReactionEmoji | ReactionCustomEmoji
|
||||||
|
date: datetime | None
|
||||||
|
|
||||||
|
|
||||||
class Portal(DBPortal, BasePortal):
|
class Portal(DBPortal, BasePortal):
|
||||||
bot: "Bot"
|
bot: "Bot"
|
||||||
config: Config
|
config: Config
|
||||||
@@ -3251,12 +3258,40 @@ class Portal(DBPortal, BasePortal):
|
|||||||
recent_reactions = resp.reactions
|
recent_reactions = resp.reactions
|
||||||
|
|
||||||
async with self.reaction_lock(dbm.mxid):
|
async with self.reaction_lock(dbm.mxid):
|
||||||
await self._handle_telegram_reactions_locked(
|
await self._handle_telegram_user_reactions_locked(
|
||||||
source, dbm, recent_reactions, total_count, timestamp=timestamp
|
source, dbm, recent_reactions, total_count, timestamp=timestamp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def handle_telegram_bot_reactions(
|
||||||
|
self, source: au.AbstractUser, update: UpdateBotMessageReaction
|
||||||
|
) -> None:
|
||||||
|
tg_space = self.tgid if self.peer_type == "channel" else source.tgid
|
||||||
|
dbm = await DBMessage.get_one_by_tgid(TelegramID(update.msg_id), tg_space)
|
||||||
|
if dbm is None:
|
||||||
|
return
|
||||||
|
reactions: dict[TelegramID, list[WrappedReaction]] = {}
|
||||||
|
custom_emoji_ids: list[int] = []
|
||||||
|
if isinstance(update.actor, PeerUser):
|
||||||
|
user_id = TelegramID(update.actor.user_id)
|
||||||
|
elif isinstance(update.actor, PeerChannel):
|
||||||
|
user_id = TelegramID(update.actor.channel_id)
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
for reaction in update.new_reactions:
|
||||||
|
reactions.setdefault(user_id, []).append(WrappedReaction(reaction=reaction, date=None))
|
||||||
|
async with self.reaction_lock(dbm.mxid):
|
||||||
|
await self._handle_telegram_parsed_reactions_locked(
|
||||||
|
source,
|
||||||
|
dbm,
|
||||||
|
reactions,
|
||||||
|
custom_emoji_ids,
|
||||||
|
is_full=True,
|
||||||
|
only_user_id=user_id,
|
||||||
|
timestamp=update.date,
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _reactions_filter(lst: list[MessagePeerReaction], existing: DBReaction) -> bool:
|
def _reactions_filter(lst: list[WrappedReaction], existing: DBReaction) -> bool:
|
||||||
if not lst:
|
if not lst:
|
||||||
return False
|
return False
|
||||||
for wrapped_reaction in lst:
|
for wrapped_reaction in lst:
|
||||||
@@ -3279,7 +3314,7 @@ class Portal(DBPortal, BasePortal):
|
|||||||
return await source.get_max_reactions(is_premium)
|
return await source.get_max_reactions(is_premium)
|
||||||
return 3 if is_premium else 1
|
return 3 if is_premium else 1
|
||||||
|
|
||||||
async def _handle_telegram_reactions_locked(
|
async def _handle_telegram_user_reactions_locked(
|
||||||
self,
|
self,
|
||||||
source: au.AbstractUser,
|
source: au.AbstractUser,
|
||||||
msg: DBMessage,
|
msg: DBMessage,
|
||||||
@@ -3287,17 +3322,38 @@ class Portal(DBPortal, BasePortal):
|
|||||||
total_count: int,
|
total_count: int,
|
||||||
timestamp: datetime | None = None,
|
timestamp: datetime | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
reactions: dict[TelegramID, list[MessagePeerReaction]] = {}
|
reactions: dict[TelegramID, list[WrappedReaction]] = {}
|
||||||
custom_emoji_ids: list[int] = []
|
custom_emoji_ids: list[int] = []
|
||||||
for reaction in reaction_list:
|
for reaction in reaction_list:
|
||||||
if isinstance(reaction.peer_id, (PeerUser, PeerChannel)) and isinstance(
|
if isinstance(reaction.peer_id, (PeerUser, PeerChannel)) and isinstance(
|
||||||
reaction.reaction, (ReactionEmoji, ReactionCustomEmoji)
|
reaction.reaction, (ReactionEmoji, ReactionCustomEmoji)
|
||||||
):
|
):
|
||||||
sender_user_id = p.Puppet.get_id_from_peer(reaction.peer_id)
|
sender_user_id = p.Puppet.get_id_from_peer(reaction.peer_id)
|
||||||
reactions.setdefault(sender_user_id, []).append(reaction)
|
reactions.setdefault(sender_user_id, []).append(
|
||||||
|
WrappedReaction(reaction.reaction, reaction.date)
|
||||||
|
)
|
||||||
if isinstance(reaction.reaction, ReactionCustomEmoji):
|
if isinstance(reaction.reaction, ReactionCustomEmoji):
|
||||||
custom_emoji_ids.append(reaction.reaction.document_id)
|
custom_emoji_ids.append(reaction.reaction.document_id)
|
||||||
is_full = len(reaction_list) == total_count
|
is_full = len(reaction_list) == total_count
|
||||||
|
await self._handle_telegram_parsed_reactions_locked(
|
||||||
|
source,
|
||||||
|
msg,
|
||||||
|
reactions,
|
||||||
|
custom_emoji_ids,
|
||||||
|
is_full=is_full,
|
||||||
|
timestamp=timestamp,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _handle_telegram_parsed_reactions_locked(
|
||||||
|
self,
|
||||||
|
source: au.AbstractUser,
|
||||||
|
msg: DBMessage,
|
||||||
|
reactions: dict[TelegramID, list[WrappedReaction]],
|
||||||
|
custom_emoji_ids: list[int],
|
||||||
|
is_full: bool,
|
||||||
|
only_user_id: TelegramID | None = None,
|
||||||
|
timestamp: datetime | None = None,
|
||||||
|
) -> None:
|
||||||
custom_emojis = await util.transfer_custom_emojis_to_matrix(source, custom_emoji_ids)
|
custom_emojis = await util.transfer_custom_emojis_to_matrix(source, custom_emoji_ids)
|
||||||
|
|
||||||
existing_reactions = await DBReaction.get_all_by_message(msg.mxid, msg.mx_room)
|
existing_reactions = await DBReaction.get_all_by_message(msg.mxid, msg.mx_room)
|
||||||
@@ -3305,6 +3361,8 @@ class Portal(DBPortal, BasePortal):
|
|||||||
removed: list[DBReaction] = []
|
removed: list[DBReaction] = []
|
||||||
for existing_reaction in existing_reactions:
|
for existing_reaction in existing_reactions:
|
||||||
sender_id = existing_reaction.tg_sender
|
sender_id = existing_reaction.tg_sender
|
||||||
|
if only_user_id is not None and sender_id != only_user_id:
|
||||||
|
continue
|
||||||
new_reactions = reactions.get(sender_id)
|
new_reactions = reactions.get(sender_id)
|
||||||
if self._reactions_filter(new_reactions, existing_reaction):
|
if self._reactions_filter(new_reactions, existing_reaction):
|
||||||
if new_reactions is not None and len(new_reactions) == 0:
|
if new_reactions is not None and len(new_reactions) == 0:
|
||||||
|
|||||||
Reference in New Issue
Block a user