diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js index 4d766b3..a5d7742 100644 --- a/amadeus/static/js/socket.js +++ b/amadeus/static/js/socket.js @@ -10,102 +10,38 @@ socket.onmessage = function(e) { content = JSON.parse(e.data); if (content.type == "mural") { - if (content.subtype == "create") { - muralNotificationCreate(content); - } else if (content.subtype == "update") { - muralNotificationUpdate(content); - } else if (content.subtype == "delete") { - muralNotificationDelete(content); + if (content.subtype == "post") { + muralNotificationPost(content); + } else if (content.subtype == "mural_update") { + muralNotificationMuralUpdate(content); + } else if (content.subtype == "mural_delete") { + muralNotificationMuralDelete(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); - } else if (content.subtype == "create_cat") { - muralNotificationCategory(content); - } else if (content.subtype == "update_cat") { - muralNotificationCategoryUpdate(content); - } else if (content.subtype == "delete_cat") { - muralNotificationCategoryDelete(content); - } else if (content.subtype == "create_sub") { - muralNotificationSubject(content); - } else if (content.subtype == "update_sub") { - muralNotificationSubjectUpdate(content); - } else if (content.subtype == "delete_sub") { - muralNotificationSubjectDelete(content); } } } // Call onopen directly if socket is already open if (socket.readyState == WebSocket.OPEN) socket.onopen(); -function muralNotificationCreate(content) { - if (window.location.pathname == content.pathname) { - $('.posts').prepend(content.complete); - - $('.no-subjects').attr('style', 'display:none'); - } 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); +function muralNotificationPost(content) { + var page = window.location.pathname, + render = (content.paths.indexOf(page) != -1); - setTimeout(notification.close.bind(notification), 3000); - } - } -} - -function muralNotificationUpdate(content) { - if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); - - if (post.is(":visible")) { - post.before(content.complete); + if (render) { + if (content.accordion) { + var section = $(content.container); - post.remove(); - } - } -} - -function muralNotificationDelete(content) { - if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); - - if (post.is(":visible")) { - post.remove(); - } - } -} + if (section.is(':visible')) { + section.find('.posts').prepend(content.complete); -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.find('.no-subjects').hide(); + } + } else { + $(content.container).prepend(content.complete); - section.append(content.complete); - } + $('.no-subjects').attr('style', 'display:none'); + } } else { $('.mural_badge').each(function () { var actual = $(this).text(); @@ -127,7 +63,7 @@ function muralNotificationComment(content) { if (("Notification" in window)) { var options = { icon: content.user_icon, - body: content.simple + body: content.simple_notify } if (Notification.permission === "granted") { @@ -138,98 +74,41 @@ function muralNotificationComment(content) { } } -function muralNotificationCommentUpdate(content) { - if (window.location.pathname == content.pathname) { - var comment = $("#comment-" + content.comment_id); - - if (comment.is(":visible")) { - comment.before(content.complete); +function muralNotificationMuralUpdate(content) { + var page = window.location.pathname, + render = (content.paths.indexOf(page) != -1); - comment.remove(); - } - } -} + if (render) { + var mural_item = $(content.container); -function muralNotificationCommentDelete(content) { - if (window.location.pathname == content.pathname) { - var comment = $("#comment-" + content.comment_id); + if (mural_item.is(":visible") || mural_item.is(":hidden")) { + mural_item.before(content.complete); - if (comment.is(":visible")) { - comment.remove(); + mural_item.remove(); } } } -function muralNotificationCategory(content) { - var cat_section = $("#" + content.cat); - - if (window.location.pathname == content.pathname && cat_section.is(':visible')) { - - cat_section.find('.posts').prepend(content.complete); - - cat_section.find('.no-subjects').hide(); - } 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 muralNotificationCategoryUpdate(content) { - if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); +function muralNotificationMuralDelete(content) { + var page = window.location.pathname, + render = (content.paths.indexOf(page) != -1); - if (post.is(":visible") || post.is(":hidden")) { - post.before(content.complete); + if (render) { + var mural_item = $(content.container); - post.remove(); + if (mural_item.is(":visible") || mural_item.is(":hidden")) { + mural_item.remove(); } } } -function muralNotificationCategoryDelete(content) { +function muralNotificationComment(content) { if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); + if ($("#post-" + content.post_id).is(":visible")) { + var section = $("#post-" + content.post_id).find('.comment-section'); - if (post.is(":visible") || post.is(":hidden")) { - post.remove(); + section.append(content.complete); } - } -} - -function muralNotificationSubject(content) { - var sub_section = $("#" + content.sub); - - if (window.location.pathname == content.pathname && sub_section.is(':visible')) { - - sub_section.find('.posts').prepend(content.complete); - - sub_section.find('.no-subjects').hide(); } else { $('.mural_badge').each(function () { var actual = $(this).text(); @@ -260,26 +139,4 @@ function muralNotificationSubject(content) { setTimeout(notification.close.bind(notification), 3000); } } -} - -function muralNotificationSubjectUpdate(content) { - if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); - - if (post.is(":visible") || post.is(":hidden")) { - post.before(content.complete); - - post.remove(); - } - } -} - -function muralNotificationSubjectDelete(content) { - if (window.location.pathname == content.pathname) { - var post = $("#post-" + content.post_id); - - if (post.is(":visible") || post.is(":hidden")) { - post.remove(); - } - } } \ No newline at end of file diff --git a/mural/views.py b/mural/views.py index c0e6424..8f615d3 100644 --- a/mural/views.py +++ b/mural/views.py @@ -119,15 +119,24 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView): 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)) - pathname = reverse("mural:manage_general") + paths = [reverse("mural:manage_general")] + + notification = { + "type": "mural", + "subtype": "post", + "paths": paths, + "user_icon": self.object.user.image_url, + "simple_notify": _("%s has made a post in General")%(str(self.object.user)), + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": ".post", + "accordion": False + } + + notification = json.dumps(notification) 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})}) + Group("user-%s" % user.id).send({'text': notification}) MuralVisualizations.objects.bulk_create(entries) @@ -166,12 +175,20 @@ class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): users = User.objects.all().exclude(id = self.request.user.id) - notify_type = "mural" - _view = render_to_string("mural/_view.html", {"post": self.object}, self.request) - pathname = reverse("mural:manage_general") + paths = [reverse("mural:manage_general")] + + notification = { + "type": "mural", + "subtype": "mural_update", + "paths": paths, + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) for user in users: - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update", "pathname": pathname, "complete": _view, "post_id": self.object.id})}) + Group("user-%s" % user.id).send({'text': notification}) return super(GeneralUpdate, self).form_valid(form) @@ -202,12 +219,20 @@ class GeneralDelete(LoginRequiredMixin, generic.DeleteView): def get_success_url(self): users = User.objects.all().exclude(id = self.request.user.id) - - notify_type = "mural" - pathname = reverse("mural:manage_general") + + paths = [reverse("mural:manage_general")] + + notification = { + "type": "mural", + "subtype": "mural_delete", + "paths": paths, + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) 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})}) + Group("user-%s" % user.id).send({'text': notification}) return reverse_lazy('mural:deleted_post') @@ -324,16 +349,25 @@ class CategoryCreate(LoginRequiredMixin, generic.edit.CreateView): users = getSpaceUsers(self.request.user.id, self.object) entries = [] + + paths = [reverse("mural:manage_category")] - 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 %s")%(str(self.object.user), str(self.object.space)) - pathname = reverse("mural:manage_category") + notification = { + "type": "mural", + "subtype": "post", + "paths": paths, + "user_icon": self.object.user.image_url, + "simple_notify": _("%s has made a post in %s")%(str(self.object.user), str(self.object.space)), + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": "#" + slug, + "accordion": True + } + + notification = json.dumps(notification) 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_cat", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view, "cat": slug})}) + Group("user-%s" % user.id).send({'text': notification}) MuralVisualizations.objects.bulk_create(entries) @@ -372,12 +406,20 @@ class CategoryUpdate(LoginRequiredMixin, generic.UpdateView): users = getSpaceUsers(self.request.user.id, self.object) - notify_type = "mural" - _view = render_to_string("mural/_view.html", {"post": self.object}, self.request) - pathname = reverse("mural:manage_category") + paths = [reverse("mural:manage_category")] + + notification = { + "type": "mural", + "subtype": "mural_update", + "paths": paths, + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) for user in users: - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update_cat", "pathname": pathname, "complete": _view, "post_id": self.object.id})}) + Group("user-%s" % user.id).send({'text': notification}) return super(CategoryUpdate, self).form_valid(form) @@ -409,11 +451,19 @@ class CategoryDelete(LoginRequiredMixin, generic.DeleteView): def get_success_url(self): users = getSpaceUsers(self.request.user.id, self.object) - notify_type = "mural" - pathname = reverse("mural:manage_category") + paths = [reverse("mural:manage_category")] + + notification = { + "type": "mural", + "subtype": "mural_delete", + "paths": paths, + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) for user in users: - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete_cat", "pathname": pathname, "post_id": self.object.id})}) + Group("user-%s" % user.id).send({'text': notification}) return reverse_lazy('mural:deleted_post') @@ -542,15 +592,24 @@ class SubjectCreate(LoginRequiredMixin, generic.edit.CreateView): users = getSpaceUsers(self.request.user.id, self.object) 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 %s")%(str(self.object.user), str(self.object.space)) - pathname = reverse("mural:manage_subject") + paths = [reverse("mural:manage_subject")] + + notification = { + "type": "mural", + "subtype": "post", + "paths": paths, + "user_icon": self.object.user.image_url, + "simple_notify": _("%s has made a post in %s")%(str(self.object.user), str(self.object.space)), + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": "#" + slug, + "accordion": True + } + + notification = json.dumps(notification) 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_sub", "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view, "sub": slug})}) + Group("user-%s" % user.id).send({'text': notification}) MuralVisualizations.objects.bulk_create(entries) @@ -596,12 +655,20 @@ class SubjectUpdate(LoginRequiredMixin, generic.UpdateView): users = getSpaceUsers(self.request.user.id, self.object) - notify_type = "mural" - _view = render_to_string("mural/_view.html", {"post": self.object}, self.request) - pathname = reverse("mural:manage_subject") + paths = [reverse("mural:manage_subject")] + + notification = { + "type": "mural", + "subtype": "mural_update", + "paths": paths, + "complete": render_to_string("mural/_view.html", {"post": self.object}, self.request), + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) for user in users: - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update_sub", "pathname": pathname, "complete": _view, "post_id": self.object.id})}) + Group("user-%s" % user.id).send({'text': notification}) return super(SubjectUpdate, self).form_valid(form) @@ -633,11 +700,19 @@ class SubjectDelete(LoginRequiredMixin, generic.DeleteView): def get_success_url(self): users = getSpaceUsers(self.request.user.id, self.object) - notify_type = "mural" - pathname = reverse("mural:manage_subject") + paths = [reverse("mural:manage_subject")] + + notification = { + "type": "mural", + "subtype": "mural_delete", + "paths": paths, + "container": "#post-" + str(self.object.id), + } + + notification = json.dumps(notification) for user in users: - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete_sub", "pathname": pathname, "post_id": self.object.id})}) + Group("user-%s" % user.id).send({'text': notification}) return reverse_lazy('mural:deleted_post') @@ -762,12 +837,24 @@ class CommentUpdate(LoginRequiredMixin, generic.UpdateView): users = getSpaceUsers(self.request.user.id, self.object.post) - notify_type = "mural" - _view = render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request) - pathname = reverse("mural:manage_general") + paths = [ + reverse("mural:manage_general"), + reverse("mural:manage_category"), + reverse("mural:manage_subject") + ] + + notification = { + "type": "mural", + "subtype": "mural_update", + "paths": paths, + "complete": render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request), + "container": "#comment-" + str(self.object.id), + } + + notification = json.dumps(notification) 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})}) + Group("user-%s" % user.id).send({'text': notification}) return super(CommentUpdate, self).form_valid(form) @@ -798,12 +885,24 @@ class CommentDelete(LoginRequiredMixin, generic.DeleteView): def get_success_url(self): users = getSpaceUsers(self.request.user.id, self.object.post) - - notify_type = "mural" - pathname = reverse("mural:manage_general") + + paths = [ + reverse("mural:manage_general"), + reverse("mural:manage_category"), + reverse("mural:manage_subject") + ] + + notification = { + "type": "mural", + "subtype": "mural_delete", + "paths": paths, + "container": "#comment-" + str(self.object.id), + } + + notification = json.dumps(notification) 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})}) + Group("user-%s" % user.id).send({'text': notification}) return reverse_lazy('mural:deleted_comment') -- libgit2 0.21.2