Skip to content

Commit 9d1d8ab

Browse files
author
Pietro Albini
committed
Add support for fetching an user's avatar history
This commits adds support for obtaining the current avatar and all the past ones for an user, with the botogram.User.avatar_history() method.
1 parent ebe7bea commit 9d1d8ab

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

botogram/objects/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ def avatar(self):
4444

4545
return self._avatar
4646

47+
@mixins._require_api
48+
def avatar_history(self):
49+
"""Get all the avatars of the user"""
50+
avatars = []
51+
52+
while True:
53+
chunk = self._api.call("getUserProfilePhotos", {
54+
"user_id": self.id,
55+
"offset": len(avatars),
56+
"limit": 1,
57+
}, expect=UserProfilePhotos)
58+
59+
avatars += chunk.photos
60+
if len(avatars) >= chunk.total_count:
61+
break
62+
63+
return avatars
64+
4765

4866
class Chat(BaseObject, mixins.ChatMixin):
4967
"""Telegram API representation of a chat

docs/api/telegram.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ Available Classes
7474

7575
.. versionadded:: 0.2
7676

77+
.. py:method:: avatar_history()
78+
79+
Get the user's avatar history. This returns a list of the current and all
80+
the past avatars for the user, represented as :py:class:`~botogram.Photo`
81+
objects. If the user has no avatars this returns an empty list.
82+
83+
.. versionadded:: 0.2
84+
7785
.. py:method:: send(message, [preview=True, reply_to=None, syntax=None, extra=None])
7886
7987
Send the textual *message* to the user. You may optionally stop clients

tests/test_objects.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,105 @@ def test_user_avatar_with_no_photos(api, mock_req):
7171
assert user.avatar is None
7272

7373

74+
def test_user_avatar_history(api, mock_req):
75+
mock_req({
76+
"getUserProfilePhotos": {
77+
"ok": True,
78+
"result": {
79+
"total_count": 3,
80+
"photos": [
81+
[
82+
{
83+
"file_id": "aaaaaa",
84+
"width": 50,
85+
"height": 50,
86+
"file_size": 128,
87+
},
88+
],
89+
[
90+
{
91+
"file_id": "bbbbbb",
92+
"width": 50,
93+
"height": 50,
94+
"file_size": 128,
95+
},
96+
],
97+
[
98+
{
99+
"file_id": "cccccc",
100+
"width": 50,
101+
"height": 50,
102+
"file_size": 128,
103+
},
104+
],
105+
],
106+
},
107+
},
108+
})
109+
110+
# First of all, make sure the API wrapper is required to fetch avatars
111+
user = botogram.objects.User({"id": 123, "first_name": "Bob"})
112+
with pytest.raises(RuntimeError):
113+
user.avatar_history() # Access the avatar without an API wrapper
114+
115+
# Now use an API
116+
user = botogram.objects.User({"id": 123, "first_name": "Bob"}, api)
117+
118+
files = [avatar.file_id for avatar in user.avatar_history()]
119+
assert files == ["aaaaaa", "bbbbbb", "cccccc"]
120+
121+
122+
def test_user_avatar_history_multiple_requests(api, mock_req):
123+
mock_req({
124+
"getUserProfilePhotos": {
125+
"ok": True,
126+
"result": {
127+
# This is the double of the avatars provided with this request
128+
# This simulates if the user has more than 100 avatars
129+
"total_count": 4,
130+
"photos": [
131+
[
132+
{
133+
"file_id": "aaaaaa",
134+
"width": 50,
135+
"height": 50,
136+
"file_size": 128,
137+
},
138+
],
139+
[
140+
{
141+
"file_id": "bbbbbb",
142+
"width": 50,
143+
"height": 50,
144+
"file_size": 128,
145+
},
146+
],
147+
],
148+
},
149+
},
150+
})
151+
152+
user = botogram.objects.User({"id": 123, "first_name": "Bob"}, api)
153+
154+
files = [avatar.file_id for avatar in user.avatar_history()]
155+
assert files == ["aaaaaa", "bbbbbb", "aaaaaa", "bbbbbb"]
156+
157+
158+
def test_user_avatar_history_no_photos(api, mock_req):
159+
mock_req({
160+
"getUserProfilePhotos": {
161+
"ok": True,
162+
"result": {
163+
"total_count": 0,
164+
"photos": [],
165+
},
166+
},
167+
})
168+
169+
user = botogram.objects.User({"id": 123, "first_name": "Bob"}, api)
170+
assert user.avatar_history() == []
171+
172+
74173
def test_photo_object():
75174
# The Photo object is custom-made, so it's better to ensure all it's
76175
# working as expected

0 commit comments

Comments
 (0)