From 89f396ff23e996d88c1fa84c942bac0d56ee8840 Mon Sep 17 00:00:00 2001 From: anhkha2003 Date: Tue, 16 Jan 2024 00:46:18 -0600 Subject: [PATCH] Add character limit and check validation of messages in Chat --- chat_box/models.py | 1 - chat_box/views.py | 38 +++++++++++++++++++++++++++----- resources/chatbox.scss | 1 + templates/chat/chat.html | 2 +- templates/chat/chat_js.html | 6 +++++ templates/chat/message.html | 2 +- templates/chat/message_list.html | 3 ++- 7 files changed, 43 insertions(+), 10 deletions(-) diff --git a/chat_box/models.py b/chat_box/models.py index 9ede6db..00a76a2 100644 --- a/chat_box/models.py +++ b/chat_box/models.py @@ -66,7 +66,6 @@ class Message(models.Model): ) def save(self, *args, **kwargs): - new_message = self.id self.body = self.body.strip() super(Message, self).save(*args, **kwargs) diff --git a/chat_box/views.py b/chat_box/views.py index b01994a..a20bb0a 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -174,9 +174,37 @@ def mute_message(request): return JsonResponse(ret) +def check_valid_message(request, room): + if not room and len(request.POST["body"]) > 200: + return False + + if not can_access_room(request, room) or request.profile.mute: + return False + + try: + last_msg_all = Message.objects.filter(room=room).latest("time") + except Message.DoesNotExist: + last_msg_all = None + + if last_msg_all and last_msg_all.body == request.POST["body"].strip(): + return False + + if not room: + four_last_msg = Message.objects.filter( + author=request.profile, room=room + ).order_by("-time")[:4] + if len(four_last_msg) >= 4: + time_diff = timezone.now() - four_last_msg[3].time + if time_diff.total_seconds() < 15: + return False + + return True + + @login_required def post_message(request): ret = {"msg": "posted"} + if request.method != "POST": return HttpResponseBadRequest() if len(request.POST["body"]) > 5000: @@ -186,7 +214,7 @@ def post_message(request): if request.POST["room"]: room = Room.objects.get(id=request.POST["room"]) - if not can_access_room(request, room) or request.profile.mute: + if not check_valid_message(request, room): return HttpResponseBadRequest() new_message = Message(author=request.profile, body=request.POST["body"], room=room) @@ -229,9 +257,7 @@ def post_message(request): def can_access_room(request, room): - return ( - not room or room.user_one == request.profile or room.user_two == request.profile - ) + return not room or room.contain(request.profile) @login_required @@ -247,7 +273,7 @@ def chat_message_ajax(request): try: message = Message.objects.filter(hidden=False).get(id=message_id) room = message.room - if room and not room.contain(request.profile): + if not can_access_room(request, room): return HttpResponse("Unauthorized", status=401) except Message.DoesNotExist: return HttpResponseBadRequest() @@ -278,7 +304,7 @@ def update_last_seen(request, **kwargs): except Room.DoesNotExist: return HttpResponseBadRequest() - if room and not room.contain(profile): + if not can_access_room(request, room): return HttpResponseBadRequest() user_room, _ = UserRoom.objects.get_or_create(user=profile, room=room) diff --git a/resources/chatbox.scss b/resources/chatbox.scss index 2e47cf6..f45244b 100644 --- a/resources/chatbox.scss +++ b/resources/chatbox.scss @@ -99,6 +99,7 @@ } .info-pic { height: 95%; + width: 100%; } .info-name { diff --git a/templates/chat/chat.html b/templates/chat/chat.html index 8ad9e16..fce740f 100644 --- a/templates/chat/chat.html +++ b/templates/chat/chat.html @@ -88,7 +88,7 @@
- +
diff --git a/templates/chat/chat_js.html b/templates/chat/chat_js.html index f09ca45..46ed077 100644 --- a/templates/chat/chat_js.html +++ b/templates/chat/chat_js.html @@ -243,6 +243,9 @@ $.post("{{ url('post_chat_message') }}", message) .fail(function(res) { console.log('Fail to send message'); + var $body = $('#message-text-'+ message.tmp_id); + $body.css('text-decoration', 'line-through'); + $body.css('background', 'red'); }) .done(function(res, status) { $('#empty_msg').hide(); @@ -307,6 +310,7 @@ refresh_status(true); $('#chat-input').focus(); show_right_panel(); + $('#chat-input').val('').trigger('input'); } window.lock_click_space = true; if (encrypted_user) { @@ -316,6 +320,7 @@ window.other_user_id = data.other_user_id; color_selected_room(); callback(); + $('#chat-input').attr('maxlength', 5000); }) .fail(function() { console.log('Fail to get_or_create_room'); @@ -326,6 +331,7 @@ window.other_user_id = ''; color_selected_room(); callback(); + $('#chat-input').attr('maxlength', 200); } window.lock_click_space = false; } diff --git a/templates/chat/message.html b/templates/chat/message.html index ba20db9..ad6a004 100644 --- a/templates/chat/message.html +++ b/templates/chat/message.html @@ -23,7 +23,7 @@ {{_('Mute')}} {% endif %} -
+
{{message.body|markdown(lazy_load=False)|reference|str|safe }}
diff --git a/templates/chat/message_list.html b/templates/chat/message_list.html index 3dcdc97..3f7e188 100644 --- a/templates/chat/message_list.html +++ b/templates/chat/message_list.html @@ -6,4 +6,5 @@ {% endfor %} {% else %}
{{_('You are connect now. Say something to start the conversation.')}}
-{% endif %} \ No newline at end of file +{% endif %} +