diff options
| author | 2020-10-12 00:43:29 +0100 | |
|---|---|---|
| committer | 2020-10-12 00:43:29 +0100 | |
| commit | 30ceab5ec9e3e5ebd1ab8766327e8aa3e4d80c3d (patch) | |
| tree | 6c486312fc97270df4d0b996d86299af7423692b /pydis_site/apps/api/viewsets | |
| parent | Responsively size YouTube embeds (diff) | |
| parent | Merge pull request #411 from ks129/voice-gate (diff) | |
Merge branch 'master' into feat/timeline
Diffstat (limited to 'pydis_site/apps/api/viewsets')
| -rw-r--r-- | pydis_site/apps/api/viewsets/bot/user.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 3e4b627e..5205dc97 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -1,6 +1,7 @@ import typing from collections import OrderedDict +from django.core.exceptions import ObjectDoesNotExist from rest_framework import status from rest_framework.decorators import action from rest_framework.pagination import PageNumberPagination @@ -9,6 +10,8 @@ from rest_framework.response import Response from rest_framework.serializers import ModelSerializer from rest_framework.viewsets import ModelViewSet +from pydis_site.apps.api.models.bot.infraction import Infraction +from pydis_site.apps.api.models.bot.metricity import Metricity, NotFound from pydis_site.apps.api.models.bot.user import User from pydis_site.apps.api.serializers import UserSerializer @@ -101,6 +104,19 @@ class UserViewSet(ModelViewSet): - 200: returned on success - 404: if a user with the given `snowflake` could not be found + ### GET /bot/users/<snowflake:int>/metricity_data + Gets metricity data for a single user by ID. + + #### Response format + >>> { + ... "verified_at": "2020-10-06T21:54:23.540766", + ... "total_messages": 2 + ...} + + #### Status codes + - 200: returned on success + - 404: if a user with the given `snowflake` could not be found + ### POST /bot/users Adds a single or multiple new users. The roles attached to the user(s) must be roles known by the site. @@ -221,3 +237,25 @@ class UserViewSet(ModelViewSet): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) + + @action(detail=True) + def metricity_data(self, request: Request, pk: str = None) -> Response: + """Request handler for metricity_data endpoint.""" + user = self.get_object() + + try: + Infraction.objects.get(user__id=user.id, active=True, type="voice_ban") + except ObjectDoesNotExist: + voice_banned = False + else: + voice_banned = True + + with Metricity() as metricity: + try: + data = metricity.user(user.id) + data["total_messages"] = metricity.total_messages(user.id) + data["voice_banned"] = voice_banned + return Response(data, status=status.HTTP_200_OK) + except NotFound: + return Response(dict(detail="User not found in metricity"), + status=status.HTTP_404_NOT_FOUND) |