Skip to content

Commit

Permalink
Allow author deletes chat
Browse files Browse the repository at this point in the history
  • Loading branch information
cuom1999 committed Aug 13, 2024
1 parent 34e8ac6 commit 1f91299
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
16 changes: 11 additions & 5 deletions chat_box/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
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"
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down
47 changes: 24 additions & 23 deletions templates/chat/chat_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion templates/chat/message.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
</div>
<span class="content-message">
<div class="body-block" id="body-block-{{ message.id }}" title="{{ message.time|date('g:i a') }}">
{% if request.user.is_staff %}
{% if request.user.is_staff or request.profile == message.author %}
<a class="red chatbtn_remove_mess chat_remove" value="{{message.id}}" style="cursor: pointer;">
{{_('Delete')}}
</a>
{% endif %}
{% if request.user.is_staff and request.profile != message.author %}
<a class="red chatbtn_remove_mess chat_mute" value="{{message.id}}" style="cursor: pointer;">
{{_('Mute')}}
</a>
Expand Down

0 comments on commit 1f91299

Please sign in to comment.