Return None in UserUpdate if information is unknown

This commit is contained in:
Lonami Exo 2019-07-04 10:34:48 +02:00
parent 99b15b916c
commit 3f19f6fd50

View File

@ -1,4 +1,5 @@
import datetime import datetime
import functools
from .common import EventBuilder, EventCommon, name_inner_event from .common import EventBuilder, EventCommon, name_inner_event
from .. import utils from .. import utils
@ -8,6 +9,28 @@ from ..tl.custom.sendergetter import SenderGetter
# TODO Either the properties are poorly named or they should be # TODO Either the properties are poorly named or they should be
# different events, but that would be a breaking change. # different events, but that would be a breaking change.
#
# TODO There are more "user updates", but bundling them all up
# in a single place will make it annoying to use (since
# the user needs to check for the existence of `None`).
#
# TODO Handle UpdateUserBlocked, UpdateUserName, UpdateUserPhone, UpdateUserPhoto
def _requires_action(function):
@functools.wraps(function)
def wrapped(self):
return None if self.action is None else function(self)
return wrapped
def _requires_status(function):
@functools.wraps(function)
def wrapped(self):
return None if self.status is None else function(self)
return wrapped
@name_inner_event @name_inner_event
class UserUpdate(EventBuilder): class UserUpdate(EventBuilder):
@ -38,14 +61,14 @@ class UserUpdate(EventBuilder):
The user status if the update is about going online or offline. The user status if the update is about going online or offline.
You should check this attribute first before checking any You should check this attribute first before checking any
of the seen within properties, since they will all be `False` of the seen within properties, since they will all be `None`
if the status is not set. if the status is not set.
action (:tl:`SendMessageAction`, optional): action (:tl:`SendMessageAction`, optional):
The "typing" action if any the user is performing if any. The "typing" action if any the user is performing if any.
You should check this attribute first before checking any You should check this attribute first before checking any
of the typing properties, since they will all be `False` of the typing properties, since they will all be `None`
if the action is not set. if the action is not set.
""" """
def __init__(self, user_id, *, status=None, chat_id=None, typing=None): def __init__(self, user_id, *, status=None, chat_id=None, typing=None):
@ -105,6 +128,7 @@ class UserUpdate(EventBuilder):
return self.sender_id return self.sender_id
@property @property
@_requires_action
def typing(self): def typing(self):
""" """
`True` if the action is typing a message. `True` if the action is typing a message.
@ -112,6 +136,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageTypingAction) return isinstance(self.action, types.SendMessageTypingAction)
@property @property
@_requires_action
def uploading(self): def uploading(self):
""" """
`True` if the action is uploading something. `True` if the action is uploading something.
@ -126,6 +151,7 @@ class UserUpdate(EventBuilder):
)) ))
@property @property
@_requires_action
def recording(self): def recording(self):
""" """
`True` if the action is recording something. `True` if the action is recording something.
@ -137,6 +163,7 @@ class UserUpdate(EventBuilder):
)) ))
@property @property
@_requires_action
def playing(self): def playing(self):
""" """
`True` if the action is playing a game. `True` if the action is playing a game.
@ -144,6 +171,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageGamePlayAction) return isinstance(self.action, types.SendMessageGamePlayAction)
@property @property
@_requires_action
def cancel(self): def cancel(self):
""" """
`True` if the action was cancelling other actions. `True` if the action was cancelling other actions.
@ -151,6 +179,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageCancelAction) return isinstance(self.action, types.SendMessageCancelAction)
@property @property
@_requires_action
def geo(self): def geo(self):
""" """
`True` if what's being uploaded is a geo. `True` if what's being uploaded is a geo.
@ -158,6 +187,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageGeoLocationAction) return isinstance(self.action, types.SendMessageGeoLocationAction)
@property @property
@_requires_action
def audio(self): def audio(self):
""" """
`True` if what's being recorded/uploaded is an audio. `True` if what's being recorded/uploaded is an audio.
@ -168,6 +198,7 @@ class UserUpdate(EventBuilder):
)) ))
@property @property
@_requires_action
def round(self): def round(self):
""" """
`True` if what's being recorded/uploaded is a round video. `True` if what's being recorded/uploaded is a round video.
@ -178,6 +209,7 @@ class UserUpdate(EventBuilder):
)) ))
@property @property
@_requires_action
def video(self): def video(self):
""" """
`True` if what's being recorded/uploaded is an video. `True` if what's being recorded/uploaded is an video.
@ -188,6 +220,7 @@ class UserUpdate(EventBuilder):
)) ))
@property @property
@_requires_action
def contact(self): def contact(self):
""" """
`True` if what's being uploaded (selected) is a contact. `True` if what's being uploaded (selected) is a contact.
@ -195,6 +228,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageChooseContactAction) return isinstance(self.action, types.SendMessageChooseContactAction)
@property @property
@_requires_action
def document(self): def document(self):
""" """
`True` if what's being uploaded is document. `True` if what's being uploaded is document.
@ -202,6 +236,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageUploadDocumentAction) return isinstance(self.action, types.SendMessageUploadDocumentAction)
@property @property
@_requires_action
def photo(self): def photo(self):
""" """
`True` if what's being uploaded is a photo. `True` if what's being uploaded is a photo.
@ -209,6 +244,7 @@ class UserUpdate(EventBuilder):
return isinstance(self.action, types.SendMessageUploadPhotoAction) return isinstance(self.action, types.SendMessageUploadPhotoAction)
@property @property
@_requires_action
def last_seen(self): def last_seen(self):
""" """
Exact `datetime.datetime` when the user was last seen if known. Exact `datetime.datetime` when the user was last seen if known.
@ -217,6 +253,7 @@ class UserUpdate(EventBuilder):
return self.status.was_online return self.status.was_online
@property @property
@_requires_status
def until(self): def until(self):
""" """
The `datetime.datetime` until when the user should appear online. The `datetime.datetime` until when the user should appear online.
@ -235,8 +272,11 @@ class UserUpdate(EventBuilder):
return datetime.timedelta(days=7) return datetime.timedelta(days=7)
elif isinstance(self.status, types.UserStatusLastMonth): elif isinstance(self.status, types.UserStatusLastMonth):
return datetime.timedelta(days=30) return datetime.timedelta(days=30)
else:
return datetime.timedelta(days=365)
@property @property
@_requires_status
def online(self): def online(self):
""" """
`True` if the user is currently online, `True` if the user is currently online,
@ -244,6 +284,7 @@ class UserUpdate(EventBuilder):
return self._last_seen_delta() <= datetime.timedelta(days=0) return self._last_seen_delta() <= datetime.timedelta(days=0)
@property @property
@_requires_status
def recently(self): def recently(self):
""" """
`True` if the user was seen within a day. `True` if the user was seen within a day.
@ -251,6 +292,7 @@ class UserUpdate(EventBuilder):
return self._last_seen_delta() <= datetime.timedelta(days=1) return self._last_seen_delta() <= datetime.timedelta(days=1)
@property @property
@_requires_status
def within_weeks(self): def within_weeks(self):
""" """
`True` if the user was seen within 7 days. `True` if the user was seen within 7 days.
@ -258,6 +300,7 @@ class UserUpdate(EventBuilder):
return self._last_seen_delta() <= datetime.timedelta(days=7) return self._last_seen_delta() <= datetime.timedelta(days=7)
@property @property
@_requires_status
def within_months(self): def within_months(self):
""" """
`True` if the user was seen within 30 days. `True` if the user was seen within 30 days.