diff --git a/amadeus/routing.py b/amadeus/routing.py index 4512d74..9b8f5c2 100644 --- a/amadeus/routing.py +++ b/amadeus/routing.py @@ -1,6 +1,7 @@ from channels.routing import route -from mural.consumers import ws_message +from mural.consumers import ws_add, ws_message channel_routing = [ + route("websocket.connect", ws_add), route("websocket.receive", ws_message), ] \ No newline at end of file diff --git a/amadeus/settings.py b/amadeus/settings.py index 0167493..33b1493 100644 --- a/amadeus/settings.py +++ b/amadeus/settings.py @@ -54,7 +54,7 @@ INSTALLED_APPS = [ 'session_security', 'django_crontab', 'django_cron', - #'channels', + 'channels', 'amadeus', 'users', diff --git a/amadeus/static/js/socket.js b/amadeus/static/js/socket.js new file mode 100644 index 0000000..9395c7c --- /dev/null +++ b/amadeus/static/js/socket.js @@ -0,0 +1,37 @@ +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); + + $('.no-subjects').attr('style', 'display:none'); + } + + 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); + } else if (Notification.permission !== 'denied') { + Notification.requestPermission(function (permission) { + // If the user accepts, let's create a notification + if (permission === "granted") { + var notification = new Notification("", options); + + setTimeout(notification.close.bind(notification), 3000); + } + }); + } + } + } +} +// Call onopen directly if socket is already open +if (socket.readyState == WebSocket.OPEN) socket.onopen(); \ No newline at end of file diff --git a/amadeus/templates/base.html b/amadeus/templates/base.html index adb84e2..039dcdf 100644 --- a/amadeus/templates/base.html +++ b/amadeus/templates/base.html @@ -260,6 +260,7 @@ + \ No newline at end of file diff --git a/mural/consumers.py b/mural/consumers.py index d19ff3a..f5d5d5e 100644 --- a/mural/consumers.py +++ b/mural/consumers.py @@ -1,14 +1,15 @@ from channels import Group from channels.sessions import channel_session -from django.http import HttpResponse -from channels.handler import AsgiHandler +from channels.auth import channel_session_user, channel_session_user_from_http + +# Connected to websocket.connect +@channel_session_user_from_http +def ws_add(message): + # Accept connection + message.reply_channel.send({"accept": True}) + # Add them to the right group + Group("user-%s" % message.user.id).add(message.reply_channel) -def http_consumer(message): - # Make standard HTTP response - access ASGI path attribute directly - response = HttpResponse("Hello world! You asked for %s" % message.content['path']) - # Encode that response into message format (ASGI) - for chunk in AsgiHandler.encode_response(response): - message.reply_channel.send(chunk) def ws_message(message): # ASGI WebSocket packet-received and send-packet message types diff --git a/mural/views.py b/mural/views.py index e9778b3..eaa054b 100644 --- a/mural/views.py +++ b/mural/views.py @@ -8,6 +8,9 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import Q, Count +from channels import Group +import json + from users.models import User from .models import GeneralPost, CategoryPost, SubjectPost, MuralVisualizations @@ -67,8 +70,15 @@ 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") + for user in users: - entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) + 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})}) MuralVisualizations.objects.bulk_create(entries) -- libgit2 0.21.2