diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index 429f8969..9676dbf8 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -12,6 +12,7 @@ from .api_cvstats_latest import ApiCvstatsLatestStar from .api_cvstats_avg import ApiCvstatsAvgStar from .api_user_ryg import ApiUserRygStar from .api_user_ryg_list import ApiUserRygListStar +from .api_user_avatar import ApiUserAvatarStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -28,6 +29,7 @@ available_page_stars = [ ApiCvstatsAvgStar, ApiUserRygStar, ApiUserRygListStar, + ApiUserAvatarStar, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/stars/api_user_avatar.py b/royalpack/stars/api_user_avatar.py new file mode 100644 index 00000000..9128ce8f --- /dev/null +++ b/royalpack/stars/api_user_avatar.py @@ -0,0 +1,38 @@ +import re +import royalnet.utils as ru +import royalnet.constellation.api as rca + + +url_validation = re.compile(r'^(?:http|ftp)s?://' + r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' + r'localhost|' + r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' + r'(?::\d+)?' + r'(?:/?|[/?]\S+)$', re.IGNORECASE) + + +class ApiUserAvatarStar(rca.ApiStar): + path = "/api/user/avatar/v2" + + parameters = { + "put": { + "avatar_url": "The url that the user wants to set as avatar." + } + } + + auth = { + "put": True, + } + + tags = ["user"] + + @rca.magic + async def put(self, data: rca.ApiData) -> ru.JSON: + """Set the avatar of current user.""" + avatar_url = data["avatar_url"] + user = await data.user() + if not re.match(url_validation, avatar_url): + raise rca.InvalidParameterError("avatar_url is not a valid url.") + user.avatar_url = avatar_url + await data.session_commit() + return user.json()