From 1f91299d419c7931b4c686dc094b031d4d8f8cbc Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Tue, 13 Aug 2024 22:10:25 +0700 Subject: [PATCH] Allow author deletes chat --- chat_box/views.py | 16 +++++++++---- templates/chat/chat_js.html | 47 +++++++++++++++++++------------------ templates/chat/message.html | 4 +++- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/chat_box/views.py b/chat_box/views.py index 5214725..e3d1646 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -37,6 +37,8 @@ from judge.models import Friend from chat_box.models import Message, Profile, Room, UserRoom, Ignore, get_room_info from chat_box.utils import encrypt_url, decrypt_url, encrypt_channel, get_unread_boxes +from reversion import revisions + class ChatView(ListView): context_object_name = "message" @@ -137,15 +139,15 @@ def delete_message(request): if request.method == "GET": return HttpResponseBadRequest() - if not request.user.is_staff: - return HttpResponseBadRequest() - try: messid = int(request.POST.get("message")) mess = Message.objects.get(id=messid) except: return HttpResponseBadRequest() + if not request.user.is_staff and request.profile != mess.author: + return HttpResponseBadRequest() + mess.hidden = True mess.save() @@ -167,8 +169,12 @@ def mute_message(request): except: return HttpResponseBadRequest() - mess.author.mute = True - mess.author.save() + with revisions.create_revision(): + revisions.set_comment(_("Mute chat") + ": " + mess.body) + revisions.set_user(request.user) + mess.author.mute = True + mess.author.save() + Message.objects.filter(room=None, author=mess.author).update(hidden=True) return JsonResponse(ret) diff --git a/templates/chat/chat_js.html b/templates/chat/chat_js.html index 4180638..68e58d8 100644 --- a/templates/chat/chat_js.html +++ b/templates/chat/chat_js.html @@ -411,30 +411,31 @@ scrollContainer($('#chat-box'), $('#loader')) - {% if request.user.is_staff %} - $(document).on("click", ".chat_remove", function() { - var elt = $(this); - $.ajax({ - url: "{{ url('delete_chat_message') }}", - type: 'post', - data: { - message: elt.attr('value'), - }, - dataType: 'json', - success: function(data){ - var $block = elt.parent(); - if ($block.parent().find('.body-block').length > 1) { - $block.remove(); - } - else { - elt.closest('li').remove(); - } - }, - fail: function(data) { - console.log('Fail to delete'); - }, - }); + $(document).on("click", ".chat_remove", function() { + var elt = $(this); + $.ajax({ + url: "{{ url('delete_chat_message') }}", + type: 'post', + data: { + message: elt.attr('value'), + }, + dataType: 'json', + success: function(data){ + var $block = elt.parent(); + if ($block.parent().find('.body-block').length > 1) { + $block.remove(); + } + else { + elt.closest('li').remove(); + } + }, + fail: function(data) { + console.log('Fail to delete'); + }, }); + }); + + {% if request.user.is_staff %} $(document).on("click", ".chat_mute", function() { if (confirm("{{_('Mute this user and delete all messages?')}}")) { var elt = $(this); diff --git a/templates/chat/message.html b/templates/chat/message.html index a253fab..3bac1c0 100644 --- a/templates/chat/message.html +++ b/templates/chat/message.html @@ -15,10 +15,12 @@
- {% if request.user.is_staff %} + {% if request.user.is_staff or request.profile == message.author %} {{_('Delete')}} + {% endif %} + {% if request.user.is_staff and request.profile != message.author %} {{_('Mute')}}