Add character limit and check validation of messages in Chat (#105)
This commit is contained in:
parent
350492c6e4
commit
f7fa1c01cb
7 changed files with 57 additions and 22 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -174,19 +174,48 @@ 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 = 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:
|
||||
pass
|
||||
|
||||
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 +258,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 +274,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 +305,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)
|
||||
|
|
|
@ -99,6 +99,7 @@
|
|||
}
|
||||
.info-pic {
|
||||
height: 95%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.info-name {
|
||||
|
|
|
@ -88,7 +88,7 @@
|
|||
</ul>
|
||||
</div>
|
||||
<div id="chat-input-container">
|
||||
<textarea maxlength="5000" id="chat-input" placeholder="{{_('Enter your message')}}"></textarea>
|
||||
<textarea maxlength="{{5000 if room else 200}}" id="chat-input" placeholder="{{_('Enter your message')}}"></textarea>
|
||||
<div class="chat-input-icon" id="emoji-button" href="#" title="{{_('Emoji')}}"><i class="icofont-slightly-smile"></i>
|
||||
</div>
|
||||
<div class="chat-input-icon" id="submit-button">
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
{{_('Mute')}}
|
||||
</a>
|
||||
{% endif %}
|
||||
<div class="message-text message-text-other">
|
||||
<div class="message-text message-text-other" id="message-text-{{ message.id }}">
|
||||
{{message.body|markdown(lazy_load=False)|reference|str|safe }}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -7,3 +7,4 @@
|
|||
{% else %}
|
||||
<center id="empty_msg">{{_('You are connect now. Say something to start the conversation.')}}</center>
|
||||
{% endif %}
|
||||
|
||||
|
|
Loading…
Reference in a new issue