diff --git a/chat_box/views.py b/chat_box/views.py index a20bb0a..e472b56 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -182,20 +182,21 @@ def check_valid_message(request, room): return False try: - last_msg_all = Message.objects.filter(room=room).latest("time") + last_msg = Message.objects.filter(room=room).first() + if ( + last_msg.author == request.profile + and last_msg.body == request.POST["body"].strip() + ): + return False except Message.DoesNotExist: - last_msg_all = None - - if last_msg_all and last_msg_all.body == request.POST["body"].strip(): - return False + pass if not room: - four_last_msg = Message.objects.filter( - author=request.profile, room=room - ).order_by("-time")[:4] + four_last_msg = Message.objects.filter(room=room).order_by("-id")[:4] if len(four_last_msg) >= 4: + same_author = all(msg.author == request.profile for msg in four_last_msg) time_diff = timezone.now() - four_last_msg[3].time - if time_diff.total_seconds() < 15: + if same_author and time_diff.total_seconds() < 300: return False return True @@ -207,7 +208,7 @@ def post_message(request): if request.method != "POST": return HttpResponseBadRequest() - if len(request.POST["body"]) > 5000: + if len(request.POST["body"]) > 5000 or len(request.POST["body"].strip()) == 0: return HttpResponseBadRequest() room = None diff --git a/templates/chat/chat_js.html b/templates/chat/chat_js.html index ed61f89..e259b87 100644 --- a/templates/chat/chat_js.html +++ b/templates/chat/chat_js.html @@ -36,8 +36,7 @@ $('#chat-log').prepend(data); } - register_time($('.time-with-rel')); - merge_authors(); + postProcessMessages(); if (!refresh_html) { $chat_box.scrollTop(scrollTopOfBottom($chat_box) - lastMsgPos); @@ -51,6 +50,13 @@ }) } + function postProcessMessages() { + register_time($('.time-with-rel')); + MathJax.typeset(); + populateCopyButton(); + merge_authors(); + } + function scrollTopOfBottom(container) { return container[0].scrollHeight - container.innerHeight() } @@ -111,10 +117,7 @@ $('#chat-log').append($data); $('#chat-box').scrollTop($('#chat-box')[0].scrollHeight); - register_time($('.time-with-rel')); - MathJax.typeset(); - populateCopyButton(); - merge_authors(); + postProcessMessages(); } function add_new_message(message, room, is_self_author) { @@ -167,11 +170,8 @@ else { add_new_message(message, room, true); } - MathJax.typeset(); - populateCopyButton(); - register_time($('.time-with-rel')); remove_unread_current_user(); - merge_authors(); + postProcessMessages(); }, error: function (data) { console.log('Fail to check message'); @@ -310,8 +310,9 @@ load_next_page(null, true); update_last_seen(); refresh_status(true); - $('#chat-input').focus(); + show_right_panel(); + $('#chat-input').focus(); $('#chat-input').val('').trigger('input'); } window.lock_click_space = true;