This commit is contained in:
cuom1999 2024-01-29 13:17:46 -06:00
commit a079827aa6
7 changed files with 55 additions and 22 deletions

View file

@ -66,7 +66,6 @@ class Message(models.Model):
) )
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
new_message = self.id
self.body = self.body.strip() self.body = self.body.strip()
super(Message, self).save(*args, **kwargs) super(Message, self).save(*args, **kwargs)

View file

@ -174,19 +174,46 @@ def mute_message(request):
return JsonResponse(ret) 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 @login_required
def post_message(request): def post_message(request):
ret = {"msg": "posted"} ret = {"msg": "posted"}
if request.method != "POST": if request.method != "POST":
return HttpResponseBadRequest() return HttpResponseBadRequest()
if len(request.POST["body"]) > 5000: if len(request.POST["body"]) > 5000 or len(request.POST["body"].strip()) == 0:
return HttpResponseBadRequest() return HttpResponseBadRequest()
room = None room = None
if request.POST["room"]: if request.POST["room"]:
room = Room.objects.get(id=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() return HttpResponseBadRequest()
new_message = Message(author=request.profile, body=request.POST["body"], room=room) 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): def can_access_room(request, room):
return ( return not room or room.contain(request.profile)
not room or room.user_one == request.profile or room.user_two == request.profile
)
@login_required @login_required
@ -247,7 +272,7 @@ def chat_message_ajax(request):
try: try:
message = Message.objects.filter(hidden=False).get(id=message_id) message = Message.objects.filter(hidden=False).get(id=message_id)
room = message.room room = message.room
if room and not room.contain(request.profile): if not can_access_room(request, room):
return HttpResponse("Unauthorized", status=401) return HttpResponse("Unauthorized", status=401)
except Message.DoesNotExist: except Message.DoesNotExist:
return HttpResponseBadRequest() return HttpResponseBadRequest()
@ -278,7 +303,7 @@ def update_last_seen(request, **kwargs):
except Room.DoesNotExist: except Room.DoesNotExist:
return HttpResponseBadRequest() return HttpResponseBadRequest()
if room and not room.contain(profile): if not can_access_room(request, room):
return HttpResponseBadRequest() return HttpResponseBadRequest()
user_room, _ = UserRoom.objects.get_or_create(user=profile, room=room) user_room, _ = UserRoom.objects.get_or_create(user=profile, room=room)

View file

@ -99,6 +99,7 @@
} }
.info-pic { .info-pic {
height: 95%; height: 95%;
width: 100%;
} }
.info-name { .info-name {

View file

@ -88,7 +88,7 @@
</ul> </ul>
</div> </div>
<div id="chat-input-container"> <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 class="chat-input-icon" id="emoji-button" href="#" title="{{_('Emoji')}}"><i class="icofont-slightly-smile"></i>
</div> </div>
<div class="chat-input-icon" id="submit-button"> <div class="chat-input-icon" id="submit-button">

View file

@ -36,8 +36,7 @@
$('#chat-log').prepend(data); $('#chat-log').prepend(data);
} }
register_time($('.time-with-rel')); postProcessMessages();
merge_authors();
if (!refresh_html) { if (!refresh_html) {
$chat_box.scrollTop(scrollTopOfBottom($chat_box) - lastMsgPos); $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) { function scrollTopOfBottom(container) {
return container[0].scrollHeight - container.innerHeight() return container[0].scrollHeight - container.innerHeight()
} }
@ -111,10 +117,7 @@
$('#chat-log').append($data); $('#chat-log').append($data);
$('#chat-box').scrollTop($('#chat-box')[0].scrollHeight); $('#chat-box').scrollTop($('#chat-box')[0].scrollHeight);
register_time($('.time-with-rel')); postProcessMessages();
MathJax.typeset();
populateCopyButton();
merge_authors();
} }
function add_new_message(message, room, is_self_author) { function add_new_message(message, room, is_self_author) {
@ -167,11 +170,8 @@
else { else {
add_new_message(message, room, true); add_new_message(message, room, true);
} }
MathJax.typeset();
populateCopyButton();
register_time($('.time-with-rel'));
remove_unread_current_user(); remove_unread_current_user();
merge_authors(); postProcessMessages();
}, },
error: function (data) { error: function (data) {
console.log('Fail to check message'); console.log('Fail to check message');
@ -245,6 +245,9 @@
$.post("{{ url('post_chat_message') }}", message) $.post("{{ url('post_chat_message') }}", message)
.fail(function(res) { .fail(function(res) {
console.log('Fail to send message'); 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) { .done(function(res, status) {
$('#empty_msg').hide(); $('#empty_msg').hide();
@ -307,8 +310,10 @@
load_next_page(null, true); load_next_page(null, true);
update_last_seen(); update_last_seen();
refresh_status(true); refresh_status(true);
$('#chat-input').focus();
show_right_panel(); show_right_panel();
$('#chat-input').focus();
$('#chat-input').val('').trigger('input');
} }
window.lock_click_space = true; window.lock_click_space = true;
if (encrypted_user) { if (encrypted_user) {
@ -318,6 +323,7 @@
window.other_user_id = data.other_user_id; window.other_user_id = data.other_user_id;
color_selected_room(); color_selected_room();
callback(); callback();
$('#chat-input').attr('maxlength', 5000);
}) })
.fail(function() { .fail(function() {
console.log('Fail to get_or_create_room'); console.log('Fail to get_or_create_room');
@ -328,6 +334,7 @@
window.other_user_id = ''; window.other_user_id = '';
color_selected_room(); color_selected_room();
callback(); callback();
$('#chat-input').attr('maxlength', 200);
} }
window.lock_click_space = false; window.lock_click_space = false;
} }

View file

@ -23,7 +23,7 @@
{{_('Mute')}} {{_('Mute')}}
</a> </a>
{% endif %} {% 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 }} {{message.body|markdown(lazy_load=False)|reference|str|safe }}
</div> </div>
</div> </div>

View file

@ -7,3 +7,4 @@
{% else %} {% else %}
<center id="empty_msg">{{_('You are connect now. Say something to start the conversation.')}}</center> <center id="empty_msg">{{_('You are connect now. Say something to start the conversation.')}}</center>
{% endif %} {% endif %}