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..34dcfe5 100644 --- a/chat_box/views.py +++ b/chat_box/views.py @@ -174,19 +174,46 @@ 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 + + last_msg = Message.objects.filter(room=room).first() + if ( + last_msg + and last_msg.author == request.profile + and last_msg.body == request.POST["body"].strip() + ): + return False + + if not room: + 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 same_author and time_diff.total_seconds() < 300: + 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: + if len(request.POST["body"]) > 5000 or len(request.POST["body"].strip()) == 0: return HttpResponseBadRequest() room = None 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 +256,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 +272,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 +303,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 a843e5a..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'); @@ -245,6 +245,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,8 +310,10 @@ 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; if (encrypted_user) { @@ -318,6 +323,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'); @@ -328,6 +334,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 %} +