Skip to content

Commit

Permalink
Comments V2 POST
Browse files Browse the repository at this point in the history
  • Loading branch information
muremwa committed Dec 18, 2022
1 parent 0562418 commit 47c3f97
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
39 changes: 38 additions & 1 deletion api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from rest_framework import status
from django.http import Http404

from .serializers import NotesSerializer, NoteSerializer, UserSerializer, ApiUserSerializer, ApiNoteSerializer
from .serializers import NotesSerializer, NoteSerializer, UserSerializer, ApiUserSerializer, ApiNoteSerializer, \
ApiCommentSerializer
from notes.views import CommentProcessor, notes_signal
from notes.models import Note, Comment

Expand Down Expand Up @@ -205,3 +206,39 @@ def get(self, *args, **kwargs):
'current_user': ApiUserSerializer(self.request.user).data,
'notes_info': ApiNoteSerializer(note).data
})

def post(self, *args, **kwargs):
response = {'success': False}
res_status = status.HTTP_400_BAD_REQUEST
note = get_object_or_404(Note, pk=kwargs.get('note_pk'))
posted_comment = self.request.data.get('comment')

if posted_comment:
processed_comment = self.mark(posted_comment)
comment = Comment.objects.create(
note=note,
user=self.request.user,
comment_text=processed_comment.get('processed_comment'),
original_comment=posted_comment
)

# add mentioned
for mentioned_user in processed_comment.get('mentioned'):
comment.mentioned.add(mentioned_user.profile)

# notify mentioned
notes_signal.send(self.__class__, comment=comment, mentioned=processed_comment.get('mentioned'))

res_status = status.HTTP_201_CREATED
response.update({
'success': True,
'comment': ApiCommentSerializer(comment).data
})

else:
response.update({
'success': False,
'message': 'Missing data \'comment\''
})

return Response(response, status=res_status)
14 changes: 12 additions & 2 deletions notifications/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.urls import reverse

from notes.views import add_collaborator, notes_signal, CommentProcessing, EditComment, CommentReply, ReplyActions
from api.views import AllComments, comment_actions
from api.views import AllComments, comment_actions, AllCommentsV2
from account.views import accept, account_signal
from notes.models import Comment, Reply
from account.models import Connection
Expand Down Expand Up @@ -52,13 +52,14 @@ def notify_mention_middle(type_, users, obj, url, **kwargs):


def notify_mention_top(type_, mentioned, location_url, **kwargs):
if type_ in [CommentProcessing, EditComment, AllComments, comment_actions]:
if type_ in [CommentProcessing, EditComment, AllComments, AllCommentsV2, comment_actions]:
notify_mention_middle('comment', mentioned, kwargs['comment'], location_url, to_what=kwargs['comment'].note)
elif type_ == CommentReply or type_ == ReplyActions:
notify_mention_middle('reply', mentioned, kwargs['reply'], location_url, to_what=kwargs['reply'].comment)


# notify a user they have been mentioned using create through the API app
# TODO: DEPRECATE
@receiver(notes_signal, sender=AllComments)
def notify_mentioning_comment_creation_api(sender, **kwargs):
type_ = sender
Expand All @@ -68,6 +69,15 @@ def notify_mentioning_comment_creation_api(sender, **kwargs):
notify_mention_top(type_, mentioned, url, comment=comment)


@receiver(notes_signal, sender=AllCommentsV2)
def notify_mentioning_comment_creation_api_v2(sender, **kwargs):
type_ = sender
comment = kwargs.get('comment', None)
mentioned = kwargs.get('mentioned')
url = reverse("notes:note-page", args=[str(comment.note.id)])+"#comment"+str(comment.id)
notify_mention_top(type_, mentioned, url, comment=comment)


# notify a user they have been mentioned using edit through the API app
@receiver(notes_signal, sender=comment_actions)
def notify_mentioning_comment_api(sender, **kwargs):
Expand Down

0 comments on commit 47c3f97

Please # to comment.