From e06da1821d4e86aad6fcf1c918e6c44d5bc25679 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 23 May 2014 11:34:34 +1000 Subject: [PATCH] BUGFIX: notifications were not updated correctly live - missing notifications when multiple replies in topic (till refresh) - not cleared properly on click --- .../components/notification_item_component.js | 11 +++++++++++ .../javascripts/discourse/controllers/header.js.es6 | 9 +++++++-- .../initializers/subscribe-user-notifications.js.es6 | 7 +++++++ .../components/notification-item.js.handlebars | 1 + .../discourse/templates/notifications.js.handlebars | 2 +- app/models/notification.rb | 7 +------ app/services/post_alerter.rb | 2 ++ 7 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 app/assets/javascripts/discourse/components/notification_item_component.js create mode 100644 app/assets/javascripts/discourse/templates/components/notification-item.js.handlebars diff --git a/app/assets/javascripts/discourse/components/notification_item_component.js b/app/assets/javascripts/discourse/components/notification_item_component.js new file mode 100644 index 00000000000..238f79651bc --- /dev/null +++ b/app/assets/javascripts/discourse/components/notification_item_component.js @@ -0,0 +1,11 @@ +Discourse.NotificationItemComponent = Ember.Component.extend({ + tagName: 'span', + didInsertElement: function(){ + var self = this; + this.$('a').click(function(){ + self.set('model.read', true); + self.rerender(); + return true; + }); + } +}); diff --git a/app/assets/javascripts/discourse/controllers/header.js.es6 b/app/assets/javascripts/discourse/controllers/header.js.es6 index dcfe1ee6d62..ca4198d527a 100644 --- a/app/assets/javascripts/discourse/controllers/header.js.es6 +++ b/app/assets/javascripts/discourse/controllers/header.js.es6 @@ -16,6 +16,10 @@ export default Discourse.Controller.extend({ return Discourse.User.current() && !this.get('topic.isPrivateMessage'); }.property('topic.isPrivateMessage'), + resetCachedNotifications: function(){ + this.set("notifications", null); + }.observes("currentUser.lastNotificationChange"), + actions: { toggleStar: function() { var topic = this.get('topic'); @@ -29,10 +33,11 @@ export default Discourse.Controller.extend({ if (self.get('currentUser.unread_notifications') || self.get('currentUser.unread_private_messages') || !self.get('notifications')) { self.set("loading_notifications", true); Discourse.ajax("/notifications").then(function(result) { + self.set('currentUser.unread_notifications', 0); + self.setProperties({ notifications: result, - loading_notifications: false, - 'currentUser.unread_notifications': 0 + loading_notifications: false }); }); } diff --git a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 index 8922f42c513..69deb2c28c4 100644 --- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 +++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 @@ -18,8 +18,15 @@ export default { }); } bus.subscribe("/notification/" + user.get('id'), (function(data) { + var oldUnread = user.get('unread_notifications'); + var oldPM = user.get('unread_private_messages'); + user.set('unread_notifications', data.unread_notifications); user.set('unread_private_messages', data.unread_private_messages); + + if(oldUnread !== data.unread_notifications || oldPM !== data.unread_private_messages) { + user.set('lastNotificationChange', new Date()); + } }), user.notification_channel_position); bus.subscribe("/categories", function(data){ diff --git a/app/assets/javascripts/discourse/templates/components/notification-item.js.handlebars b/app/assets/javascripts/discourse/templates/components/notification-item.js.handlebars new file mode 100644 index 00000000000..50879cae4ff --- /dev/null +++ b/app/assets/javascripts/discourse/templates/components/notification-item.js.handlebars @@ -0,0 +1 @@ +{{unbound boundI18n scope linkBinding="model.link" usernameBinding="model.username"}} diff --git a/app/assets/javascripts/discourse/templates/notifications.js.handlebars b/app/assets/javascripts/discourse/templates/notifications.js.handlebars index f86ddcb4dcd..dd856989ea0 100644 --- a/app/assets/javascripts/discourse/templates/notifications.js.handlebars +++ b/app/assets/javascripts/discourse/templates/notifications.js.handlebars @@ -3,7 +3,7 @@ {{#if content}}