diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js index 3b17f75..7261c31 100644 --- a/amadeus/static/js/socket.js +++ b/amadeus/static/js/socket.js @@ -4,48 +4,79 @@ if (("Notification" in window)) { } } - socket = new WebSocket("ws://" + window.location.host + "/"); socket.onmessage = function(e) { content = JSON.parse(e.data); if (content.type == "mural") { - if (window.location.pathname == content.pathname) { - $('.posts').prepend(content.complete); + if (content.subtype == "create") { + muralNotificationCreate(content); + } else if (content.subtype == "update") { + muralNotificationUpdate(content); + } else if (content.subtype == "delete") { + muralNotificationDelete(content); + } + } +} +// Call onopen directly if socket is already open +if (socket.readyState == WebSocket.OPEN) socket.onopen(); - $('.no-subjects').attr('style', 'display:none'); - } else { - $('.mural_badge').each(function () { - var actual = $(this).text(); +function muralNotificationCreate(content) { + if (window.location.pathname == content.pathname) { + $('.posts').prepend(content.complete); - if (actual != "+99") { - actual = parseInt(actual, 10) + 1; + $('.no-subjects').attr('style', 'display:none'); + } else { + $('.mural_badge').each(function () { + var actual = $(this).text(); - if (actual > 99) { - actual = "+99"; - } + if (actual != "+99") { + actual = parseInt(actual, 10) + 1; - $(this).text(actual); + if (actual > 99) { + actual = "+99"; } - $(this).show(); - }); + $(this).text(actual); + } + + $(this).show(); + }); + } + + if (("Notification" in window)) { + var options = { + icon: content.user_icon, + body: content.simple } - if (("Notification" in window)) { - var options = { - icon: content.user_icon, - body: content.simple - } + if (Notification.permission === "granted") { + var notification = new Notification("", options); - if (Notification.permission === "granted") { - var notification = new Notification("", options); + 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); - setTimeout(notification.close.bind(notification), 3000); - } - } + post.remove(); + } } } -// Call onopen directly if socket is already open -if (socket.readyState == WebSocket.OPEN) socket.onopen(); \ No newline at end of file + +function muralNotificationDelete(content) { + if (window.location.pathname == content.pathname) { + var post = $("#post-" + content.post_id); + + if (post.is(":visible")) { + post.remove(); + } + } +} \ No newline at end of file diff --git a/mural/views.py b/mural/views.py index 9ac23d5..522c250 100644 --- a/mural/views.py +++ b/mural/views.py @@ -83,7 +83,7 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView): 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, "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})}) + 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) @@ -119,6 +119,15 @@ class GeneralUpdate(LoginRequiredMixin, generic.UpdateView): self.object.edited = True self.object.save() + + 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") + + 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})}) return super(GeneralUpdate, self).form_valid(form) @@ -147,6 +156,14 @@ class GeneralDelete(LoginRequiredMixin, generic.DeleteView): return context def get_success_url(self): + users = User.objects.all().exclude(id = self.request.user.id) + + 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})}) + return reverse_lazy('mural:deleted_post') def render_gen_post(request, post, msg): -- libgit2 0.21.2