-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from swecc-uw/injest-message-event
- Loading branch information
Showing
3 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path( | ||
"message/", | ||
views.InjestMessageEventView.as_view(), | ||
name="injest-message-event", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,51 @@ | ||
from django.shortcuts import render | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import generics, status | ||
from rest_framework.response import Response | ||
from django.http import Http404 | ||
from django.db import transaction | ||
from django.db.models import F | ||
from engagement.models import DiscordMessageStats | ||
from members.models import User | ||
from members.permissions import IsApiKey | ||
|
||
# Create your views here. | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class InjestMessageEventView(generics.CreateAPIView): | ||
permission_classes = [IsApiKey] | ||
|
||
@transaction.atomic | ||
def post(self, request, *args, **kwargs): | ||
discord_id = request.data.get("discord_id") | ||
channel_id = request.data.get("channel_id") | ||
try: | ||
user = get_object_or_404(User, discord_id=discord_id) | ||
|
||
( | ||
stats, | ||
created, | ||
) = DiscordMessageStats.objects.select_for_update().get_or_create( | ||
member_id=user.id, channel_id=channel_id, defaults={"message_count": 1} | ||
) | ||
|
||
if not created: | ||
stats.message_count = F("message_count") + 1 | ||
stats.save() | ||
stats.refresh_from_db() | ||
|
||
logger.info( | ||
"member %s has %d messages in channel %s", | ||
user.id, | ||
stats.message_count, | ||
channel_id, | ||
) | ||
|
||
return Response(status=status.HTTP_202_ACCEPTED) | ||
|
||
except Http404: | ||
logger.error("member not found for discord_id: %s", discord_id) | ||
return Response( | ||
{"error": "member not found"}, status=status.HTTP_404_NOT_FOUND | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters