From b5ff371960eb628d697a96110270e4d4ab6272f6 Mon Sep 17 00:00:00 2001 From: Zambom Date: Thu, 6 Apr 2017 22:15:50 -0300 Subject: [PATCH] Decreasing chat notification count when open talk --- amadeus/static/js/chat.js | 42 ++++++++++++++++++++++++++++++++++++++++++ amadeus/static/js/mural_ungeneral.js | 1 + amadeus/static/js/socket.js | 7 ++++++- chat/templates/chat/_view.html | 2 +- chat/templates/chat/talk.html | 2 +- chat/urls.py | 1 + chat/views.py | 15 +++++++++++++++ 7 files changed, 67 insertions(+), 3 deletions(-) diff --git a/amadeus/static/js/chat.js b/amadeus/static/js/chat.js index 779138f..170c157 100644 --- a/amadeus/static/js/chat.js +++ b/amadeus/static/js/chat.js @@ -79,6 +79,48 @@ function getModalInfo(btn, space, space_type) { } }); + var viewed = $(".messages-container").data('viewed'), + sub_badge = $(".messages-container").data('space'), + father = $(".messages-container").parent().parent().parent().parent(), + chat_list_item_id = "#talk-" + father.attr("id"); + + $('.chat_badge').each(function () { + var actual = $(this).text(); + + if (actual != "+99") { + actual = parseInt(actual, 10) - viewed; + + if (actual <= 0) { + $(this).text("0"); + $(this).hide(); + } else { + $(this).text(actual); + } + } + }); + + var counter = $(chat_list_item_id).find('.chat_notify_list').text(); + + counter = parseInt(counter, 10) - viewed; + + if (counter <= 0) { + counter = "0"; + } + + $(chat_list_item_id).find('.chat_notify_list').text(counter); + + var subject_cbadge = $("#subject_" + sub_badge).find('.chat_notify'), + actual = subject_cbadge.text(); + + if (actual != "+99") { + actual = parseInt(actual, 10) - viewed; + + if (actual <= 0) { + subject_cbadge.text("0"); + subject_cbadge.hide(); + } + } + setShortChatFormSubmit(); setFiltersSubmitAndPagination(); }); diff --git a/amadeus/static/js/mural_ungeneral.js b/amadeus/static/js/mural_ungeneral.js index f240e8b..15e0fe4 100644 --- a/amadeus/static/js/mural_ungeneral.js +++ b/amadeus/static/js/mural_ungeneral.js @@ -50,6 +50,7 @@ $('.mural-ungeneral').on('shown.bs.collapse', function(e) { actual = parseInt(actual, 10) - data.unviewed; if (actual <= 0) { + $(this).text("0"); $(this).hide(); } else { $(this).text(actual); diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js index f715017..5c22a17 100644 --- a/amadeus/static/js/socket.js +++ b/amadeus/static/js/socket.js @@ -241,9 +241,14 @@ function messageReceived(content) { $(this).hide(); }); + + $.ajax({ + type: 'GET', + url: content.view_url + }); } else { var talk_line = $("#talk-" + content.container), - talk_notifies = talk_line.find('.chat_notify'), + talk_notifies = talk_line.find('.chat_notify_list'), actual_count = talk_notifies.text(), actual_date = talk_line.find(".talk-last_msg"); diff --git a/chat/templates/chat/_view.html b/chat/templates/chat/_view.html index 6b97d15..ba47ddb 100644 --- a/chat/templates/chat/_view.html +++ b/chat/templates/chat/_view.html @@ -9,7 +9,7 @@
-

{{ talking_to }} ({{ chat|notifies:request.user }})

+

{{ talking_to }} ({{ chat|notifies:request.user }})

{% trans 'Last message in' %} {{ chat|last_message }}

diff --git a/chat/templates/chat/talk.html b/chat/templates/chat/talk.html index 2c5a562..7ef4a30 100644 --- a/chat/templates/chat/talk.html +++ b/chat/templates/chat/talk.html @@ -39,7 +39,7 @@
-
+
diff --git a/chat/urls.py b/chat/urls.py index 0b86392..a1ea98c 100644 --- a/chat/urls.py +++ b/chat/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ url(r'^subject/participants/(?P[\w_-]+)/$', views.SubjectParticipants.as_view(), name='participants_subject'), url(r'^render_message/([\w_-]+)/([\w_-]+)/([\w_-]+)/([\w_-]+)/([\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.render_message, name='render_message'), url(r'^favorite/([\w_-]+)/$', views.favorite, name='favorite'), + url(r'^view_message/([\w_-]+)/$', views.message_viewed, name='view_message'), url(r'^load_messages/([\w_-]+)/$', views.load_messages, name='load_messages'), url(r'^talk/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/$', views.GetTalk.as_view(), name = 'talk'), url(r'^send_message/(?P[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})/(?P[\w_-]+)/(?P[\w_-]+)/(?P[\w_-]+)/$', views.SendMessage.as_view(), name = 'create'), diff --git a/chat/views.py b/chat/views.py index e2796e9..468da85 100644 --- a/chat/views.py +++ b/chat/views.py @@ -258,6 +258,7 @@ class GetTalk(LoginRequiredMixin, LogMixin, generic.ListView): template_name = 'chat/talk.html' paginate_by = 20 talk_id = "-1" + n_viewed = 0 def get_queryset(self): user = self.request.user @@ -274,6 +275,7 @@ class GetTalk(LoginRequiredMixin, LogMixin, generic.ListView): messages = TalkMessages.objects.filter(talk = talk).order_by('-create_date') views = ChatVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & (Q(message__talk = talk))) + self.n_viewed = views.count() views.update(viewed = True, date_viewed = datetime.now()) return messages @@ -283,6 +285,7 @@ class GetTalk(LoginRequiredMixin, LogMixin, generic.ListView): user_email = self.kwargs.get('email', '') + context['messages_viewed'] = self.n_viewed context['participant'] = get_object_or_404(User, email = user_email) context['talk_id'] = self.talk_id context['space'] = self.request.GET.get('space', '0') @@ -290,6 +293,10 @@ class GetTalk(LoginRequiredMixin, LogMixin, generic.ListView): context['form'] = ChatMessageForm() context['form_url'] = reverse_lazy('chat:create', args = (), kwargs = {'email': self.kwargs.get('email', ''), 'talk_id': self.talk_id, 'space': self.request.GET.get('space', '0'), 'space_type': self.request.GET.get('space_type', 'general')}) + if context['space_type'] == "subject": + subject = get_object_or_404(Subject, id = context['space']) + context['subject'] = subject.slug + self.log_context['talk_id'] = self.talk_id self.log_context['user_id'] = context['participant'].id self.log_context['user_name'] = str(context['participant']) @@ -352,6 +359,7 @@ class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView): "user_icon": self.object.user.image_url, "notify_title": str(self.object.user), "simple_notify": simple_notify, + "view_url": reverse("chat:view_message", args = (self.object.id, ), kwargs = {}), "complete": render_to_string("chat/_message.html", {"talk_msg": self.object}, self.request), "container": "chat-" + str(self.object.user.id), "last_date": _("Last message in %s")%(formats.date_format(self.object.create_date, "SHORT_DATETIME_FORMAT")) @@ -417,6 +425,13 @@ def favorite(request, message): return JsonResponse({'label': _('Favorite')}) +def message_viewed(request, message): + view = ChatVisualizations.objects.filter(message__id = message, user = request.user) + + view.update(viewed = True, date_viewed = datetime.now()) + + return JsonResponse({'msg': 'ok'}) + def load_messages(request, talk): context = { 'request': request, -- libgit2 0.21.2