BUGFIX: notifications were not updated correctly live

- missing notifications when multiple replies in topic (till refresh)
- not cleared properly on click
This commit is contained in:
Sam 2014-05-23 11:34:34 +10:00
parent 1fcd72231a
commit e06da1821d
7 changed files with 30 additions and 9 deletions

View File

@ -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;
});
}
});

View File

@ -16,6 +16,10 @@ export default Discourse.Controller.extend({
return Discourse.User.current() && !this.get('topic.isPrivateMessage'); return Discourse.User.current() && !this.get('topic.isPrivateMessage');
}.property('topic.isPrivateMessage'), }.property('topic.isPrivateMessage'),
resetCachedNotifications: function(){
this.set("notifications", null);
}.observes("currentUser.lastNotificationChange"),
actions: { actions: {
toggleStar: function() { toggleStar: function() {
var topic = this.get('topic'); 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')) { if (self.get('currentUser.unread_notifications') || self.get('currentUser.unread_private_messages') || !self.get('notifications')) {
self.set("loading_notifications", true); self.set("loading_notifications", true);
Discourse.ajax("/notifications").then(function(result) { Discourse.ajax("/notifications").then(function(result) {
self.set('currentUser.unread_notifications', 0);
self.setProperties({ self.setProperties({
notifications: result, notifications: result,
loading_notifications: false, loading_notifications: false
'currentUser.unread_notifications': 0
}); });
}); });
} }

View File

@ -18,8 +18,15 @@ export default {
}); });
} }
bus.subscribe("/notification/" + user.get('id'), (function(data) { 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_notifications', data.unread_notifications);
user.set('unread_private_messages', data.unread_private_messages); 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); }), user.notification_channel_position);
bus.subscribe("/categories", function(data){ bus.subscribe("/categories", function(data){

View File

@ -0,0 +1 @@
{{unbound boundI18n scope linkBinding="model.link" usernameBinding="model.username"}}

View File

@ -3,7 +3,7 @@
{{#if content}} {{#if content}}
<ul> <ul>
{{#each}} {{#each}}
<li {{bind-attr class="read"}}>{{unbound boundI18n scope linkBinding="link" usernameBinding="username"}}</li> <li {{bind-attr class="read"}}>{{notification-item model=this scope=scope}}</li>
{{/each}} {{/each}}
<li class="read last"> <li class="read last">
<a {{bind-attr href="currentUser.path"}}>{{i18n notifications.more}} &hellip;</a> <a {{bind-attr href="currentUser.path"}}>{{i18n notifications.more}} &hellip;</a>

View File

@ -123,12 +123,7 @@ class Notification < ActiveRecord::Base
protected protected
def refresh_notification_count def refresh_notification_count
user_id = user.id user.publish_notifications_state
MessageBus.publish("/notification/#{user_id}",
{unread_notifications: user.unread_notifications,
unread_private_messages: user.unread_private_messages},
user_ids: [user_id] # only publish the notification to this user
)
end end
end end

View File

@ -76,6 +76,8 @@ class PostAlerter
user.notifications.where(notification_type: type, user.notifications.where(notification_type: type,
topic_id: topic.id).destroy_all topic_id: topic.id).destroy_all
# HACK so notification counts sync up correctly
user.reload
end end
def create_notification(user, type, post, opts={}) def create_notification(user, type, post, opts={})