From d09386ea0e659c9369c05081a7731ec9b757f468 Mon Sep 17 00:00:00 2001 From: Jpadilla1 Date: Tue, 29 Apr 2014 22:11:14 -0400 Subject: [PATCH] js to jump to last message --- apps/chats/views.py | 2 ++ templates/chats/room.html | 8 ++++++-- templates/users/myrooms.html | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/chats/views.py b/apps/chats/views.py index 61ac6cb..2d13b4e 100644 --- a/apps/chats/views.py +++ b/apps/chats/views.py @@ -57,6 +57,7 @@ def get_context_data(self, **kwargs): context['form'] = self.get_form(self.get_form_class()) context['room'] = get_object_or_404(ChatRoom, slug=self.kwargs['slug']) context['room_messages'] = Message.objects.filter(room=context['room']) + context['room_message_last'] = context['room_messages'].last().id - 1 users = User.objects.filter( last_login__gt=self.request.user.last_logged_out, is_active__exact=1, ).order_by('-last_login') @@ -84,6 +85,7 @@ def get_ajax(self, request, *args, **kwargs): m.append(i['fields']) data = { 'messages': json.dumps(m), + 'last_message_id': messages.last().id - 1, 'online_users': json.dumps(list(ChatRoom.objects.get( slug=kwargs['slug']).members.filter( last_login__gt=self.request.user.last_logged_out, diff --git a/templates/chats/room.html b/templates/chats/room.html index c0e7bdb..6945a5f 100644 --- a/templates/chats/room.html +++ b/templates/chats/room.html @@ -23,7 +23,7 @@
{{ room.name }}
{% for m in room_messages %} -

{{ m.created_by.username }} | {{ m.created_at|naturaltime }}

+

{{ m.created_by.username }} | {{ m.created_at|naturaltime }}

{{ m }}


{% endfor %} @@ -57,14 +57,18 @@

{{ m.created_by.username }} | {{ m.created_at|naturaltime }}

var html = ""; d = $.parseJSON(messages); $.each(d, function(i, item) { - html += "

" + item.created_by + " | " + item.created_at +"

" + "

" + item.body + "

" + "
"; + html += "

" + item.created_by + " | " + item.created_at +"

" + "

" + item.body + "

" + "
"; }); $('#messages').html(html); + + location.href = '#message_' + data['last_message_id']; }, }); }; +location.href = '#message_' + {{ room_message_last }}; + var interval = 1000 * 10; setInterval(ajax_call, interval); diff --git a/templates/users/myrooms.html b/templates/users/myrooms.html index 1a530c8..a893b8f 100644 --- a/templates/users/myrooms.html +++ b/templates/users/myrooms.html @@ -5,7 +5,7 @@
{% if myrooms|length > 0 %} -

My Rooms

+

My Rooms


{% for room in myrooms %}

{{ room }} - {{ room.members.count }} members