diff options
author | 2022-12-29 00:58:15 +0100 | |
---|---|---|
committer | 2022-12-29 00:58:15 +0100 | |
commit | ab1b5460f4f089283ccd43f9bb19ddcc4e600a3a (patch) | |
tree | 609ebd37878fb5ba5532048d40236fa3c4a53736 /pydis_site/apps/api/viewsets | |
parent | remove useless blank constraint (diff) | |
parent | Update help channels guide (#814) (diff) |
Merge branch 'main' into 2304-link-previous-nomination-threads
Diffstat (limited to 'pydis_site/apps/api/viewsets')
-rw-r--r-- | pydis_site/apps/api/viewsets/bot/user.py | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index ba1bcd9d..db73a83c 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -3,8 +3,9 @@ from collections import OrderedDict from django.db.models import Q from django_filters.rest_framework import DjangoFilterBackend -from rest_framework import status +from rest_framework import fields, status from rest_framework.decorators import action +from rest_framework.exceptions import ParseError from rest_framework.pagination import PageNumberPagination from rest_framework.request import Request from rest_framework.response import Response @@ -138,6 +139,29 @@ class UserViewSet(ModelViewSet): - 200: returned on success - 404: if a user with the given `snowflake` could not be found + ### POST /bot/users/metricity_activity_data + Returns a mapping of user ID to message count in a given period for + the given user IDs. + + #### Required Query Parameters + - days: how many days into the past to count message from. + + #### Request Format + >>> [ + ... 409107086526644234, + ... 493839819168808962 + ... ] + + #### Response format + >>> { + ... "409107086526644234": 54, + ... "493839819168808962": 0 + ... } + + #### Status codes + - 200: returned on success + - 400: if request body or query parameters were missing or invalid + ### POST /bot/users Adds a single or multiple new users. The roles attached to the user(s) must be roles known by the site. @@ -298,3 +322,34 @@ class UserViewSet(ModelViewSet): except NotFoundError: return Response(dict(detail="User not found in metricity"), status=status.HTTP_404_NOT_FOUND) + + @action(detail=False, methods=["POST"]) + def metricity_activity_data(self, request: Request) -> Response: + """Request handler for metricity_activity_data endpoint.""" + if "days" in request.query_params: + try: + days = int(request.query_params["days"]) + except ValueError: + raise ParseError(detail={ + "days": ["This query parameter must be an integer."] + }) + else: + raise ParseError(detail={ + "days": ["This query parameter is required."] + }) + + user_id_list_validator = fields.ListField( + child=fields.IntegerField(min_value=0), + allow_empty=False + ) + user_ids = [ + str(user_id) for user_id in + user_id_list_validator.run_validation(request.data) + ] + + with Metricity() as metricity: + data = metricity.total_messages_in_past_n_days(user_ids, days) + + default_data = {user_id: 0 for user_id in user_ids} + response_data = default_data | dict(data) + return Response(response_data, status=status.HTTP_200_OK) |