Update ChatAction to handle new pin updates

This commit is contained in:
Lonami Exo 2020-10-31 11:21:38 +01:00
parent d83c154f8d
commit 9e3cb8180b

View File

@ -31,17 +31,15 @@ class ChatAction(EventBuilder):
""" """
@classmethod @classmethod
def build(cls, update, others=None, self_id=None): def build(cls, update, others=None, self_id=None):
raise RuntimeError('FIXME: handle new pinned updates') if isinstance(update, types.UpdatePinnedChannelMessages):
if isinstance(update, types.UpdatePinnedChannelMessages) and update.id == 0:
# Telegram does not always send
# UpdateChannelPinnedMessage for new pins
# but always for unpin, with update.id = 0
return cls.Event(types.PeerChannel(update.channel_id), return cls.Event(types.PeerChannel(update.channel_id),
unpin=True) pin_ids=update.messages,
pin=update.pinned)
elif isinstance(update, types.UpdatePinnedMessages) and update.id == 0: elif isinstance(update, types.UpdatePinnedMessages):
return cls.Event(types.PeerChat(update.chat_id), return cls.Event(update.peer,
unpin=True) pin_ids=update.messages,
pin=update.pinned)
elif isinstance(update, types.UpdateChatParticipantAdd): elif isinstance(update, types.UpdateChatParticipantAdd):
return cls.Event(types.PeerChat(update.chat_id), return cls.Event(types.PeerChat(update.chat_id),
@ -109,12 +107,8 @@ class ChatAction(EventBuilder):
return cls.Event(msg, return cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=True) new_photo=True)
elif isinstance(action, types.MessageActionPinMessage) and msg.reply_to: # Handled by specific updates
# Seems to not be reliable on unpins, but when pinning # elif isinstance(action, types.MessageActionPinMessage) and msg.reply_to:
# we prefer this because we know who caused it.
return cls.Event(msg,
users=msg.from_id,
new_pin=msg.reply_to.reply_to_msg_id)
class Event(EventCommon): class Event(EventCommon):
""" """
@ -154,19 +148,22 @@ class ChatAction(EventBuilder):
unpin (`bool`): unpin (`bool`):
`True` if the existing pin gets unpinned. `True` if the existing pin gets unpinned.
""" """
def __init__(self, where, new_pin=None, new_photo=None, def __init__(self, where, new_photo=None,
added_by=None, kicked_by=None, created=None, added_by=None, kicked_by=None, created=None,
users=None, new_title=None, unpin=None): users=None, new_title=None, pin_ids=None, pin=None):
if isinstance(where, types.MessageService): if isinstance(where, types.MessageService):
self.action_message = where self.action_message = where
where = where.peer_id where = where.peer_id
else: else:
self.action_message = None self.action_message = None
super().__init__(chat_peer=where, msg_id=new_pin) # TODO needs some testing (can there be more than one id, and do they follow pin order?)
# same in get_pinned_message
super().__init__(chat_peer=where, msg_id=pin_ids[0] if pin_ids else None)
self.new_pin = isinstance(new_pin, int) self.new_pin = pin_ids is not None
self._pinned_message = new_pin self._pin_ids = pin_ids
self._pinned_messages = None
self.new_photo = new_photo is not None self.new_photo = new_photo is not None
self.photo = \ self.photo = \
@ -203,7 +200,7 @@ class ChatAction(EventBuilder):
self._users = None self._users = None
self._input_users = None self._input_users = None
self.new_title = new_title self.new_title = new_title
self.unpin = unpin self.unpin = not pin
def _set_client(self, client): def _set_client(self, client):
super()._set_client(client) super()._set_client(client)
@ -257,16 +254,26 @@ class ChatAction(EventBuilder):
If ``new_pin`` is `True`, this returns the `Message If ``new_pin`` is `True`, this returns the `Message
<telethon.tl.custom.message.Message>` object that was pinned. <telethon.tl.custom.message.Message>` object that was pinned.
""" """
if self._pinned_message == 0: if self._pinned_messages is None:
return None await self.get_pinned_messages()
if isinstance(self._pinned_message, int)\ if self._pinned_messages:
and await self.get_input_chat(): return self._pinned_messages[0]
self._pinned_message = await self._client.get_messages(
self._input_chat, ids=self._pinned_message)
if isinstance(self._pinned_message, types.Message): async def get_pinned_messages(self):
return self._pinned_message """
If ``new_pin`` is `True`, this returns a `list` of `Message
<telethon.tl.custom.message.Message>` objects that were pinned.
"""
if not self._pin_ids:
return self._pin_ids # either None or empty list
chat = await self.get_input_chat()
if chat:
self._pinned_messages = await self._client.get_messages(
self._input_chat, ids=self._pin_ids)
return self._pinned_messages
@property @property
def added_by(self): def added_by(self):