Commit b731c25744bf60be4746a2f26fd22782a396d1ae
1 parent
1d8d5041
Exists in
master
and in
3 other branches
Adding mural comments notifications
Showing
3 changed files
with
95 additions
and
18 deletions
Show diff stats
amadeus/static/js/socket.js
... | ... | @@ -16,6 +16,12 @@ socket.onmessage = function(e) { |
16 | 16 | muralNotificationUpdate(content); |
17 | 17 | } else if (content.subtype == "delete") { |
18 | 18 | muralNotificationDelete(content); |
19 | + } else if (content.subtype == "create_comment") { | |
20 | + muralNotificationComment(content); | |
21 | + } else if (content.subtype == "update_comment") { | |
22 | + muralNotificationCommentUpdate(content); | |
23 | + } else if (content.subtype == "delete_comment") { | |
24 | + muralNotificationCommentDelete(content); | |
19 | 25 | } |
20 | 26 | } |
21 | 27 | } |
... | ... | @@ -79,4 +85,65 @@ function muralNotificationDelete(content) { |
79 | 85 | post.remove(); |
80 | 86 | } |
81 | 87 | } |
88 | +} | |
89 | + | |
90 | +function muralNotificationComment(content) { | |
91 | + if (window.location.pathname == content.pathname) { | |
92 | + if ($("#post-" + content.post_id).is(":visible")) { | |
93 | + var section = $("#post-" + content.post_id).find('.comment-section'); | |
94 | + | |
95 | + section.append(content.complete); | |
96 | + } | |
97 | + } else { | |
98 | + $('.mural_badge').each(function () { | |
99 | + var actual = $(this).text(); | |
100 | + | |
101 | + if (actual != "+99") { | |
102 | + actual = parseInt(actual, 10) + 1; | |
103 | + | |
104 | + if (actual > 99) { | |
105 | + actual = "+99"; | |
106 | + } | |
107 | + | |
108 | + $(this).text(actual); | |
109 | + } | |
110 | + | |
111 | + $(this).show(); | |
112 | + }); | |
113 | + } | |
114 | + | |
115 | + if (("Notification" in window)) { | |
116 | + var options = { | |
117 | + icon: content.user_icon, | |
118 | + body: content.simple | |
119 | + } | |
120 | + | |
121 | + if (Notification.permission === "granted") { | |
122 | + var notification = new Notification("", options); | |
123 | + | |
124 | + setTimeout(notification.close.bind(notification), 3000); | |
125 | + } | |
126 | + } | |
127 | +} | |
128 | + | |
129 | +function muralNotificationCommentUpdate(content) { | |
130 | + if (window.location.pathname == content.pathname) { | |
131 | + var comment = $("#comment-" + content.comment_id); | |
132 | + | |
133 | + if (comment.is(":visible")) { | |
134 | + comment.before(content.complete); | |
135 | + | |
136 | + comment.remove(); | |
137 | + } | |
138 | + } | |
139 | +} | |
140 | + | |
141 | +function muralNotificationCommentDelete(content) { | |
142 | + if (window.location.pathname == content.pathname) { | |
143 | + var comment = $("#comment-" + content.comment_id); | |
144 | + | |
145 | + if (comment.is(":visible")) { | |
146 | + comment.remove(); | |
147 | + } | |
148 | + } | |
82 | 149 | } |
83 | 150 | \ No newline at end of file | ... | ... |
mural/models.py
... | ... | @@ -27,6 +27,10 @@ class Mural(KnowsChild): |
27 | 27 | edited = models.BooleanField(_('Edited'), default = False) |
28 | 28 | |
29 | 29 | @always_as_child |
30 | + def get_id(self): | |
31 | + pass | |
32 | + | |
33 | + @always_as_child | |
30 | 34 | def update_link(self): |
31 | 35 | pass |
32 | 36 | |
... | ... | @@ -37,6 +41,9 @@ class Mural(KnowsChild): |
37 | 41 | class GeneralPost(Mural): |
38 | 42 | space = models.IntegerField(_('Space'), default = 0, blank = True) |
39 | 43 | |
44 | + def get_id(self): | |
45 | + return self.id | |
46 | + | |
40 | 47 | def update_link(self): |
41 | 48 | return "mural:update_general" |
42 | 49 | |
... | ... | @@ -46,6 +53,12 @@ class GeneralPost(Mural): |
46 | 53 | class CategoryPost(Mural): |
47 | 54 | space = models.ForeignKey(Category, verbose_name = ('Category'), related_name = 'post_category') |
48 | 55 | |
56 | + def get_id(self): | |
57 | + return self.id | |
58 | + | |
59 | + def get_id(self): | |
60 | + return self.id | |
61 | + | |
49 | 62 | def update_link(self): |
50 | 63 | return "" |
51 | 64 | ... | ... |
mural/views.py
... | ... | @@ -262,16 +262,19 @@ class CommentCreate(LoginRequiredMixin, generic.edit.CreateView): |
262 | 262 | self.object.save() |
263 | 263 | |
264 | 264 | users = User.objects.all().exclude(id = self.request.user.id) |
265 | + entries = [] | |
265 | 266 | |
266 | 267 | notify_type = "mural" |
267 | 268 | user_icon = self.object.user.image_url |
268 | - #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) | |
269 | - simple_notify = _("%s has made a post in General")%(str(self.object.user)) | |
269 | + _view = render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request) | |
270 | + simple_notify = _("%s has commented in a post in General")%(str(self.object.user)) | |
270 | 271 | pathname = reverse("mural:manage_general") |
271 | 272 | |
272 | - #for user in users: | |
273 | - # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) | |
274 | - # 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})}) | |
273 | + for user in users: | |
274 | + entries.append(MuralVisualizations(viewed = False, user = user, comment = self.object)) | |
275 | + 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()})}) | |
276 | + | |
277 | + MuralVisualizations.objects.bulk_create(entries) | |
275 | 278 | |
276 | 279 | return super(CommentCreate, self).form_valid(form) |
277 | 280 | |
... | ... | @@ -309,20 +312,14 @@ class CommentUpdate(LoginRequiredMixin, generic.UpdateView): |
309 | 312 | self.object.save() |
310 | 313 | |
311 | 314 | users = User.objects.all().exclude(id = self.request.user.id) |
312 | - entries = [] | |
313 | - | |
315 | + | |
314 | 316 | notify_type = "mural" |
315 | - user_icon = self.object.user.image_url | |
316 | - #_view = render_to_string("mural/_view.html", {"post": self.object}, self.request) | |
317 | - simple_notify = _("%s has made a post in General")%(str(self.object.user)) | |
317 | + _view = render_to_string("mural/_view_comment.html", {"comment": self.object}, self.request) | |
318 | 318 | pathname = reverse("mural:manage_general") |
319 | 319 | |
320 | - #for user in users: | |
321 | - # entries.append(MuralVisualizations(viewed = False, user = user, post = self.object)) | |
322 | - # 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})}) | |
323 | - | |
324 | - #MuralVisualizations.objects.bulk_create(entries) | |
325 | - | |
320 | + for user in users: | |
321 | + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "update_comment", "pathname": pathname, "complete": _view, "comment_id": self.object.id})}) | |
322 | + | |
326 | 323 | return super(CommentUpdate, self).form_valid(form) |
327 | 324 | |
328 | 325 | def get_context_data(self, *args, **kwargs): |
... | ... | @@ -356,8 +353,8 @@ class CommentDelete(LoginRequiredMixin, generic.DeleteView): |
356 | 353 | notify_type = "mural" |
357 | 354 | pathname = reverse("mural:manage_general") |
358 | 355 | |
359 | - #for user in users: | |
360 | - # Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete", "pathname": pathname, "post_id": self.object.id})}) | |
356 | + for user in users: | |
357 | + Group("user-%s" % user.id).send({'text': json.dumps({"type": notify_type, "subtype": "delete_comment", "pathname": pathname, "comment_id": self.object.id})}) | |
361 | 358 | |
362 | 359 | return reverse_lazy('mural:deleted_comment') |
363 | 360 | ... | ... |