diff --git a/api/urls.py b/api/urls.py index 5888ff4..889057c 100644 --- a/api/urls.py +++ b/api/urls.py @@ -21,16 +21,9 @@ # api/create_note/ path('create_note/', views.NoteCreationApi.as_view(), name="create-note"), - # TODO: DEPRECATE - # api/comments/ - path('/comments/', views.AllComments.as_view(), name='comments'), - # api/v2/33/comments/ path('v2//comments/', views.AllCommentsV2.as_view(), name='comments-v2'), - # api/comment/4/actions/ - path('comment//actions/', views.comment_actions, name='comment-actions'), - # api/v2/comment/4/actions/ path('v2/comment//actions/', views.comment_actions_v2, name='comment-actions-v2'), diff --git a/api/views.py b/api/views.py index 4d7f4c2..687dac5 100644 --- a/api/views.py +++ b/api/views.py @@ -1,4 +1,3 @@ -from datetime import datetime from itertools import chain from typing import Dict @@ -71,128 +70,6 @@ def post(request): }) -# TODO: DEPRECATE -class AllComments(views.APIView, CommentProcessor): - @staticmethod - def shape(comment, user_id, host): - if comment.modified: - stamp = comment.modified.timestamp() - else: - stamp = comment.created.timestamp() - - return { - 'key': stamp + comment.pk, - 'username': comment.user.username, - 'full_name': comment.user.get_full_name(), - 'profile': comment.user.profile.image.url, - 'comment_id': comment.pk, - 'time': comment.get_created(), - 'text': comment.original_comment, - 'edited': comment.is_modified(), - 'editable': comment.user.pk == user_id, - 'replies': comment.reply_set.count(), - 'reply_url': "http://" + host + reverse('notes:reply-comment', kwargs={'comment_id': str(comment.pk)}), - 'action_url': "http://" + host + reverse('api:comment-actions', kwargs={'pk': str(comment.pk)}), - } - - def get(self, *args, **kwargs): - host = self.request.META.get('HTTP_HOST', '') - comments = Comment.objects.filter(note=kwargs.get('pk')) - note = get_object_or_404(Note, pk=kwargs.get('pk')) - - user = self.request.user - - return Response({ - 'note': str(note), - 'owner_id': note.user.pk, - 'user': { - 'id': user.pk, - 'username': user.username, - 'full_name': user.get_full_name(), - 'profile': user.profile.image.url, - }, - 'comments': [self.shape(comment, user.pk, host) for comment in comments] - }) - - def post(self, *args, **kwargs): - host = self.request.META.get('HTTP_HOST', '') - note = get_object_or_404(Note, pk=kwargs.get('pk', None)) - user_comment = self.request.data.get('comment', '') - processed_data = self.mark(user_comment) - user = self.request.user - - comment = Comment( - note=note, - user=user, - comment_text=processed_data.get('processed_comment', ''), - original_comment=user_comment, - ) - comment.save() - - for mentioned_user in processed_data.get('mentioned'): - comment.mentioned.add(mentioned_user.profile) - - notes_signal.send(self.__class__, comment=comment, mentioned=processed_data.get('mentioned')) - - return Response({ - 'comment': self.shape(comment, user.pk, host) - }) - - -# TODO: DEPRECATE -@api_view(['POST']) -def comment_actions(request, pk): - comment = get_object_or_404(Comment, pk=pk) - response = Response(status=status.HTTP_403_FORBIDDEN) - - if 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META: - http_method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE'] - - # handle a delete request - if http_method == "DELETE": - if request.user == comment.user or request.user == comment.note.user: - comment.delete() - response = Response({'success': True}, status=status.HTTP_200_OK) - else: - response = Response(status=status.HTTP_403_FORBIDDEN) - - # handle a patch request - elif http_method == 'PATCH': - if comment.user == request.user: - processor = CommentProcessor() - comment_text = request.data.get('original_comment', '') - processed_comment = processor.mark(comment_text) - comment.comment_text = processed_comment.get('processed_comment') - comment.original_comment = comment_text - comment.modified = datetime.now() - comment.save() - notify = [] - - # add mentioned users - for mentioned in processed_comment.get('mentioned'): - if mentioned.profile not in comment.mentioned.all(): - comment.mentioned.add(mentioned.profile) - notify.append(mentioned) - - # notify new mentioned users - if notify: - notes_signal.send(comment_actions, comment=comment, mentioned=notify) - - response = Response({ - 'new_key': comment.modified.timestamp() + comment.pk, - 'success': True, - 'changed': True, - 'comment_id': comment.pk, - 'comment': comment_text, - 'replies': comment.reply_set.count(), - 'error_message': None, - }) - else: - response = Response(status=status.HTTP_403_FORBIDDEN) - - return response - - def get_user(request): username_query = request.GET.get('username', None) ava_users = User.objects.filter(username__iexact=username_query) @@ -288,7 +165,7 @@ def comment_actions_v2(request, comment_pk): # notify new mentioned users if notify: - notes_signal.send(comment_actions, comment=comment, mentioned=notify) + notes_signal.send(comment_actions_v2, comment=comment, mentioned=notify) res_status = status.HTTP_200_OK response.update({ diff --git a/notifications/signals.py b/notifications/signals.py index c878a4e..18eaeca 100644 --- a/notifications/signals.py +++ b/notifications/signals.py @@ -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, AllCommentsV2 +from api.views import comment_actions_v2, AllCommentsV2 from account.views import accept, account_signal from notes.models import Comment, Reply from account.models import Connection @@ -52,23 +52,12 @@ def notify_mention_middle(type_, users, obj, url, **kwargs): def notify_mention_top(type_, mentioned, location_url, **kwargs): - if type_ in [CommentProcessing, EditComment, AllComments, AllCommentsV2, comment_actions]: + if type_ in [CommentProcessing, EditComment, AllCommentsV2, comment_actions_v2]: 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 - 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) - - @receiver(notes_signal, sender=AllCommentsV2) def notify_mentioning_comment_creation_api_v2(sender, **kwargs): type_ = sender @@ -79,7 +68,7 @@ def notify_mentioning_comment_creation_api_v2(sender, **kwargs): # notify a user they have been mentioned using edit through the API app -@receiver(notes_signal, sender=comment_actions) +@receiver(notes_signal, sender=comment_actions_v2) def notify_mentioning_comment_api(sender, **kwargs): type_ = sender comment = kwargs.get('comment', None)