From ffe826b35ff3fff12bbe2e16b3a7c7fb4a35b960 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 9 Feb 2018 12:42:04 +0100 Subject: [PATCH] Add a new UserUpdate Event --- telethon/events/__init__.py | 151 ++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/telethon/events/__init__.py b/telethon/events/__init__.py index 31cf1f07..22665cf1 100644 --- a/telethon/events/__init__.py +++ b/telethon/events/__init__.py @@ -1,4 +1,5 @@ import abc +import datetime import itertools from .. import utils @@ -602,3 +603,153 @@ class ChatAction(_EventBuilder): self._users = [] return self._users + + +class UserUpdate(_EventBuilder): + """ + Represents an user update (gone online, offline, joined Telegram). + """ + + def build(self, update): + if isinstance(update, types.UpdateUserStatus): + event = UserUpdate.Event(update.user_id, + status=update.status) + else: + return + + return event + + def resolve(self, client): + pass + + class Event(_EventCommon): + """ + Represents the event of an user status update (last seen, joined). + + Members: + online (:obj:`bool`, optional): + ``True`` if the user is currently online, ``False`` otherwise. + Might be ``None`` if this information is not present. + + last_seen (:obj:`datetime`, optional): + Exact date when the user was last seen if known. + + until (:obj:`datetime`, optional): + Until when will the user remain online. + + within_months (:obj:`bool`): + ``True`` if the user was seen within 30 days. + + within_weeks (:obj:`bool`): + ``True`` if the user was seen within 7 days. + + recently (:obj:`bool`): + ``True`` if the user was seen within a day. + + action (:obj:`SendMessageAction`, optional): + The "typing" action if any the user is performing if any. + + cancel (:obj:`bool`): + ``True`` if the action was cancelling other actions. + + typing (:obj:`bool`): + ``True`` if the action is typing a message. + + recording (:obj:`bool`): + ``True`` if the action is recording something. + + uploading (:obj:`bool`): + ``True`` if the action is uploading something. + + playing (:obj:`bool`): + ``True`` if the action is playing a game. + + audio (:obj:`bool`): + ``True`` if what's being recorded/uploaded is an audio. + + round (:obj:`bool`): + ``True`` if what's being recorded/uploaded is a round video. + + video (:obj:`bool`): + ``True`` if what's being recorded/uploaded is an video. + + document (:obj:`bool`): + ``True`` if what's being uploaded is document. + + geo (:obj:`bool`): + ``True`` if what's being uploaded is a geo. + + photo (:obj:`bool`): + ``True`` if what's being uploaded is a photo. + + contact (:obj:`bool`): + ``True`` if what's being uploaded (selected) is a contact. + """ + def __init__(self, user_id, status=None, typing=None): + super().__init__(types.PeerUser(user_id)) + + self.online = None if status is None else \ + isinstance(status, types.UserStatusOnline) + + self.last_seen = status.was_online if \ + isinstance(status, types.UserStatusOffline) else None + + self.until = status.expires if \ + isinstance(status, types.UserStatusOnline) else None + + if self.last_seen: + diff = datetime.datetime.now() - self.last_seen + if diff < datetime.timedelta(days=30): + self.within_months = True + if diff < datetime.timedelta(days=7): + self.within_weeks = True + if diff < datetime.timedelta(days=1): + self.recently = True + else: + self.within_months = self.within_weeks = self.recently = False + if isinstance(status, (types.UserStatusOnline, + types.UserStatusRecently)): + self.within_months = self.within_weeks = True + self.recently = True + elif isinstance(status, types.UserStatusLastWeek): + self.within_months = self.within_weeks = True + elif isinstance(status, types.UserStatusLastMonth): + self.within_months = True + + self.action = typing + if typing: + self.cancel = self.typing = self.recording = self.uploading = \ + self.playing = False + self.audio = self.round = self.video = self.document = \ + self.geo = self.photo = self.contact = False + + if isinstance(typing, types.SendMessageCancelAction): + self.cancel = True + elif isinstance(typing, types.SendMessageTypingAction): + self.typing = True + elif isinstance(typing, types.SendMessageGamePlayAction): + self.playing = True + elif isinstance(typing, types.SendMessageGeoLocationAction): + self.geo = True + elif isinstance(typing, types.SendMessageRecordAudioAction): + self.recording = self.audio = True + elif isinstance(typing, types.SendMessageRecordRoundAction): + self.recording = self.round = True + elif isinstance(typing, types.SendMessageRecordVideoAction): + self.recording = self.video = True + elif isinstance(typing, types.SendMessageChooseContactAction): + self.uploading = self.contact = True + elif isinstance(typing, types.SendMessageUploadAudioAction): + self.uploading = self.audio = True + elif isinstance(typing, types.SendMessageUploadDocumentAction): + self.uploading = self.document = True + elif isinstance(typing, types.SendMessageUploadPhotoAction): + self.uploading = self.photo = True + elif isinstance(typing, types.SendMessageUploadRoundAction): + self.uploading = self.round = True + elif isinstance(typing, types.SendMessageUploadVideoAction): + self.uploading = self.video = True + + @property + def user(self): + return self.chat