Commit 968668f4034a8296b126521bf4d5715056e7d122

Authored by Zambom
1 parent 2146f0a6

Adding post update/delete propagation to other users

Showing 2 changed files with 76 additions and 28 deletions   Show diff stats
amadeus/static/js/socket.js
... ... @@ -4,48 +4,79 @@ if (("Notification" in window)) {
4 4 }
5 5 }
6 6  
7   -
8 7 socket = new WebSocket("ws://" + window.location.host + "/");
9 8  
10 9 socket.onmessage = function(e) {
11 10 content = JSON.parse(e.data);
12 11  
13 12 if (content.type == "mural") {
14   - if (window.location.pathname == content.pathname) {
15   - $('.posts').prepend(content.complete);
  13 + if (content.subtype == "create") {
  14 + muralNotificationCreate(content);
  15 + } else if (content.subtype == "update") {
  16 + muralNotificationUpdate(content);
  17 + } else if (content.subtype == "delete") {
  18 + muralNotificationDelete(content);
  19 + }
  20 + }
  21 +}
  22 +// Call onopen directly if socket is already open
  23 +if (socket.readyState == WebSocket.OPEN) socket.onopen();
16 24  
17   - $('.no-subjects').attr('style', 'display:none');
18   - } else {
19   - $('.mural_badge').each(function () {
20   - var actual = $(this).text();
  25 +function muralNotificationCreate(content) {
  26 + if (window.location.pathname == content.pathname) {
  27 + $('.posts').prepend(content.complete);
21 28  
22   - if (actual != "+99") {
23   - actual = parseInt(actual, 10) + 1;
  29 + $('.no-subjects').attr('style', 'display:none');
  30 + } else {
  31 + $('.mural_badge').each(function () {
  32 + var actual = $(this).text();
24 33  
25   - if (actual > 99) {
26   - actual = "+99";
27   - }
  34 + if (actual != "+99") {
  35 + actual = parseInt(actual, 10) + 1;
28 36  
29   - $(this).text(actual);
  37 + if (actual > 99) {
  38 + actual = "+99";
30 39 }
31 40  
32   - $(this).show();
33   - });
  41 + $(this).text(actual);
  42 + }
  43 +
  44 + $(this).show();
  45 + });
  46 + }
  47 +
  48 + if (("Notification" in window)) {
  49 + var options = {
  50 + icon: content.user_icon,
  51 + body: content.simple
34 52 }
35 53  
36   - if (("Notification" in window)) {
37   - var options = {
38   - icon: content.user_icon,
39   - body: content.simple
40   - }
  54 + if (Notification.permission === "granted") {
  55 + var notification = new Notification("", options);
41 56  
42   - if (Notification.permission === "granted") {
43   - var notification = new Notification("", options);
  57 + setTimeout(notification.close.bind(notification), 3000);
  58 + }
  59 + }
  60 +}
  61 +
  62 +function muralNotificationUpdate(content) {
  63 + if (window.location.pathname == content.pathname) {
  64 + var post = $("#post-" + content.post_id);
  65 +
  66 + if (post.is(":visible")) {
  67 + post.before(content.complete);
44 68  
45   - setTimeout(notification.close.bind(notification), 3000);
46   - }
47   - }
  69 + post.remove();
  70 + }
48 71 }
49 72 }
50   -// Call onopen directly if socket is already open
51   -if (socket.readyState == WebSocket.OPEN) socket.onopen();
52 73 \ No newline at end of file
  74 +
  75 +function muralNotificationDelete(content) {
  76 + if (window.location.pathname == content.pathname) {
  77 + var post = $("#post-" + content.post_id);
  78 +
  79 + if (post.is(":visible")) {
  80 + post.remove();
  81 + }
  82 + }
  83 +}
53 84 \ No newline at end of file
... ...
mural/views.py
... ... @@ -83,7 +83,7 @@ class GeneralCreate(LoginRequiredMixin, generic.edit.CreateView):
83 83  
84 84 for user in users:
85 85 entries.append(MuralVisualizations(viewed = False, user = user, post = self.object))
86   - Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "user_icon": user_icon, "pathname": pathname, "simple": simple_notify, "complete": _view})})
  86 + 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})})
87 87  
88 88 MuralVisualizations.objects.bulk_create(entries)
89 89  
... ... @@ -119,6 +119,15 @@ class GeneralUpdate(LoginRequiredMixin, generic.UpdateView):
119 119 self.object.edited = True
120 120  
121 121 self.object.save()
  122 +
  123 + users = User.objects.all().exclude(id = self.request.user.id)
  124 +
  125 + notify_type = "mural"
  126 + _view = render_to_string("mural/_view.html", {"post": self.object}, self.request)
  127 + pathname = reverse("mural:manage_general")
  128 +
  129 + for user in users:
  130 + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update", "pathname": pathname, "complete": _view, "post_id": self.object.id})})
122 131  
123 132 return super(GeneralUpdate, self).form_valid(form)
124 133  
... ... @@ -147,6 +156,14 @@ class GeneralDelete(LoginRequiredMixin, generic.DeleteView):
147 156 return context
148 157  
149 158 def get_success_url(self):
  159 + users = User.objects.all().exclude(id = self.request.user.id)
  160 +
  161 + notify_type = "mural"
  162 + pathname = reverse("mural:manage_general")
  163 +
  164 + for user in users:
  165 + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})})
  166 +
150 167 return reverse_lazy('mural:deleted_post')
151 168  
152 169 def render_gen_post(request, post, msg):
... ...