Fix ChatAction not handling all pin events (#715)

This commit is contained in:
Jeff 2018-03-21 17:01:14 +08:00 committed by Lonami
parent f2407409b3
commit 95f368201e

View File

@ -609,11 +609,12 @@ class ChatAction(_EventBuilder):
Represents an action in a chat (such as user joined, left, or new pin). Represents an action in a chat (such as user joined, left, or new pin).
""" """
def build(self, update): def build(self, update):
if isinstance(update, types.UpdateChannelPinnedMessage): if isinstance(update, types.UpdateChannelPinnedMessage) and update.id == 0:
# Telegram sends UpdateChannelPinnedMessage and then # Telegram does not always send
# UpdateNewChannelMessage with MessageActionPinMessage. # UpdateChannelPinnedMessage for new pins
# but always for unpin, with update.id = 0
event = ChatAction.Event(types.PeerChannel(update.channel_id), event = ChatAction.Event(types.PeerChannel(update.channel_id),
new_pin=update.id) unpin=True)
elif isinstance(update, types.UpdateChatParticipantAdd): elif isinstance(update, types.UpdateChatParticipantAdd):
event = ChatAction.Event(types.PeerChat(update.chat_id), event = ChatAction.Event(types.PeerChat(update.chat_id),
@ -664,6 +665,11 @@ class ChatAction(_EventBuilder):
event = ChatAction.Event(msg, event = ChatAction.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=True) new_photo=True)
elif isinstance(action, types.MessageActionPinMessage):
# Telegram always sends this service message for new pins
event = ChatAction.Event(msg,
users=msg.from_id,
new_pin=msg.reply_to_msg_id)
else: else:
return return
else: else:
@ -678,7 +684,7 @@ class ChatAction(_EventBuilder):
Members: Members:
new_pin (:obj:`bool`): new_pin (:obj:`bool`):
``True`` if the pin has changed (new pin or removed). ``True`` if there is a new pin.
new_photo (:obj:`bool`): new_photo (:obj:`bool`):
``True`` if there's a new chat photo (or it was removed). ``True`` if there's a new chat photo (or it was removed).
@ -704,10 +710,13 @@ class ChatAction(_EventBuilder):
new_title (:obj:`bool`, optional): new_title (:obj:`bool`, optional):
The new title string for the chat, if applicable. The new title string for the chat, if applicable.
unpin (:obj:`bool`):
``True`` if the existing pin gets unpinned.
""" """
def __init__(self, where, new_pin=None, new_photo=None, def __init__(self, where, new_pin=None, new_photo=None,
added_by=None, kicked_by=None, created=None, added_by=None, kicked_by=None, created=None,
users=None, new_title=None): users=None, new_title=None, unpin=None):
if isinstance(where, types.MessageService): if isinstance(where, types.MessageService):
self.action_message = where self.action_message = where
where = where.to_id where = where.to_id
@ -726,7 +735,7 @@ class ChatAction(_EventBuilder):
self._added_by = None self._added_by = None
self._kicked_by = None self._kicked_by = None
self.user_added, self.user_joined, self.user_left,\ self.user_added, self.user_joined, self.user_left,\
self.user_kicked = (False, False, False, False) self.user_kicked, self.unpin = (False, False, False, False, False)
if added_by is True: if added_by is True:
self.user_joined = True self.user_joined = True
@ -745,6 +754,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
def respond(self, *args, **kwargs): def respond(self, *args, **kwargs):
""" """