diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eebdfe9..b6176b5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,14 @@ All notable changes to this project will be documented in this file. -## [0.4.0 - 2023-10-2x] +## [0.4.0 - 2023-10-15] As the project moves closer to `beta`, final unification changes are being made. This release contains some breaking changes in `users`, `notifications` API. ### Added +- Support for users avatars(`get_avatar`). #149 - `__repr__` method added for most objects(previously it was only present for `FsNode`). #147 ### Changed @@ -21,7 +22,7 @@ This release contains some breaking changes in `users`, `notifications` API. ### Fixed - `users.get_details` with empty parameter in some cases was raised exception. -- ClientMode: in case when LDAP was used as user backend, user login differs from user_id and most API failed with 404. #148 +- ClientMode: in case when LDAP was used as user backend, user login differs from `user id`, and most API failed with 404. #148 ## [0.3.1 - 2023-10-07] diff --git a/nc_py_api/users.py b/nc_py_api/users.py index f613b7e1..69fb7be6 100644 --- a/nc_py_api/users.py +++ b/nc_py_api/users.py @@ -287,3 +287,21 @@ def demote_from_subadmin(self, user_id: str, group_id: str) -> None: :param group_id: group where user should be removed from administrators. """ self._session.ocs(method="DELETE", path=f"{self._ep_base}/{user_id}/subadmins", params={"groupid": group_id}) + + def get_avatar( + self, user_id: str = "", size: typing.Literal[64, 512] = 512, dark: bool = False, guest: bool = False + ) -> bytes: + """Returns user avatar binary data. + + :param user_id: The ID of the user whose avatar should be returned. + .. note:: To return the current user's avatar, leave the field blank. + :param size: Size of the avatar. Currently supported values: ``64`` and ``512``. + :param dark: Flag indicating whether a dark theme avatar should be returned or not. + :param guest: Flag indicating whether user ID is a guest name or not. + """ + if not user_id and not guest: + user_id = self._session.user + url_path = f"/index.php/avatar/{user_id}/{size}" if not guest else f"/index.php/avatar/guest/{user_id}/{size}" + if dark: + url_path += "/dark" + return self._session.request(method="GET", path=url_path).content diff --git a/tests/actual_tests/users_test.py b/tests/actual_tests/users_test.py index 3905c49c..e2e28063 100644 --- a/tests/actual_tests/users_test.py +++ b/tests/actual_tests/users_test.py @@ -1,8 +1,10 @@ import contextlib import datetime +from io import BytesIO from os import environ import pytest +from PIL import Image from nc_py_api import ( NextcloudApp, @@ -141,3 +143,15 @@ def test_edit_user(nc_client): def test_resend_user_email(nc_client): nc_client.users.resend_welcome_email(nc_client.user) + + +def test_avatars(nc): + im = nc.users.get_avatar() + im_64 = nc.users.get_avatar(size=64) + im_black = nc.users.get_avatar(dark=True) + im_64_black = nc.users.get_avatar(size=64, dark=True) + assert len(im_64) < len(im) + assert len(im_64_black) < len(im_black) + for i in (im, im_64, im_black, im_64_black): + img = Image.open(BytesIO(i)) + img.load()