From b731c25744bf60be4746a2f26fd22782a396d1ae Mon Sep 17 00:00:00 2001 From: Zambom Date: Fri, 10 Feb 2017 17:29:14 -0200 Subject: [PATCH] Adding mural comments notifications --- amadeus/static/js/socket.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mural/models.py | 13 +++++++++++++ mural/views.py | 33 +++++++++++++++------------------ 3 files changed, 95 insertions(+), 18 deletions(-) diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js index 7261c31..981252a 100644 --- a/amadeus/static/js/socket.js +++ b/amadeus/static/js/socket.js @@ -16,6 +16,12 @@ socket.onmessage = function(e) { muralNotificationUpdate(content); } else if (content.subtype == "delete") { muralNotificationDelete(content); + } else if (content.subtype == "create_comment") { + muralNotificationComment(content); + } else if (content.subtype == "update_comment") { + muralNotificationCommentUpdate(content); + } else if (content.subtype == "delete_comment") { + muralNotificationCommentDelete(content); } } } @@ -79,4 +85,65 @@ function muralNotificationDelete(content) { post.remove(); } } +} + +function muralNotificationComment(content) { + if (window.location.pathname == content.pathname) { + if ($("#post-" + content.post_id).is(":visible")) { + var section = $("#post-" + content.post_id).find('.comment-section'); + + section.append(content.complete); + } + } else { + $('.mural_badge').each(function () { + var actual = $(this).text(); + + if (actual != "+99") { + actual = parseInt(actual, 10) + 1; + + if (actual > 99) { + actual = "+99"; + } + + $(this).text(actual); + } + + $(this).show(); + }); + } + + if (("Notification" in window)) { + var options = { + icon: content.user_icon, + body: content.simple + } + + if (Notification.permission === "granted") { + var notification = new Notification("", options); + + setTimeout(notification.close.bind(notification), 3000); + } + } +} + +function muralNotificationCommentUpdate(content) { + if (window.location.pathname == content.pathname) { + var comment = $("#comment-" + content.comment_id); + + if (comment.is(":visible")) { + comment.before(content.complete); + + comment.remove(); + } + } +} + +function muralNotificationCommentDelete(content) { + if (window.location.pathname == content.pathname) { + var comment = $("#comment-" + content.comment_id); + + if (comment.is(":visible")) { + comment.remove(); + } + } } \ No newline at end of file diff --git a/mural/models.py b/mural/models.py index ac09167..2504800 100644 --- a/mural/models.py +++ b/mural/models.py @@ -27,6 +27,10 @@ class Mural(KnowsChild): edited = models.BooleanField(_('Edited'), default = False) @always_as_child + def get_id(self): + pass + + @always_as_child def update_link(self): pass @@ -37,6 +41,9 @@ class Mural(KnowsChild): class GeneralPost(Mural): space = models.IntegerField(_('Space'), default = 0, blank = True) + def get_id(self): + return self.id + def update_link(self): return "mural:update_general" @@ -46,6 +53,12 @@ class GeneralPost(Mural): class CategoryPost(Mural): space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'post_category') + def get_id(self): + return self.id + + def get_id(self): + return self.id + def update_link(self): return "" diff --git a/mural/views.py b/mural/views.py index 2344dd2..39b26e0 100644 --- a/mural/views.py +++ b/mural/views.py @@ -262,16 +262,19 @@ class CommentCreate(LoginRequiredMixin, generic.edit.CreateView): self.object.save() users = User.objects.all().exclude(id = self.request.user.id) + entries = [] notify_type = "mural" user_icon = self.object.user.image_url - #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) - simple_notify = _("%s has made a post in General")%(str(self.object.user)) + _view = render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request) + simple_notify = _("%s has commented in a post in General")%(str(self.object.user)) pathname = reverse("mural:manage_general") - #for user in users: - # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) - # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})}) + for user in users: + entries.append(MuralVisualizations(viewed = False, user = user, comment = self.object)) + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create_comment", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view, "post_id": post.get_id()})}) + + MuralVisualizations.objects.bulk_create(entries) return super(CommentCreate, self).form_valid(form) @@ -309,20 +312,14 @@ class CommentUpdate(LoginRequiredMixin, generic.UpdateView): self.object.save() users = User.objects.all().exclude(id = self.request.user.id) - entries = [] - + notify_type = "mural" - user_icon = self.object.user.image_url - #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) - simple_notify = _("%s has made a post in General")%(str(self.object.user)) + _view = render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request) pathname = reverse("mural:manage_general") - #for user in users: - # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) - # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "create", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})}) - - #MuralVisualizations.objects.bulk_create(entries) - + for user in users: + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update_comment", "pathname": pathname, "complete": _view, "comment_id": self.object.id})}) + return super(CommentUpdate, self).form_valid(form) def get_context_data(self, *args, **kwargs): @@ -356,8 +353,8 @@ class CommentDelete(LoginRequiredMixin, generic.DeleteView): notify_type = "mural" pathname = reverse("mural:manage_general") - #for user in users: - # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})}) + for user in users: + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete_comment", "pathname": pathname, "comment_id": self.object.id})}) return reverse_lazy('mural:deleted_comment') -- libgit2 0.21.2