A platform for community discussion. Free, open, simple.
FIX: ensure 'notifications_state' is up to date after creating a notification
Régis Hanolf988fa31aa6d
diff --git a/app/assets/javascripts/discourse/components/site-header.js.es6 b/app/assets/javascripts/discourse/components/site-header.js.es6index 458e45c93dc..ac19edd63dd 100644--- a/app/assets/javascripts/discourse/components/site-header.js.es6+++ b/app/assets/javascripts/discourse/components/site-header.js.es6@@ -16,7 +16,7 @@ const SiteHeaderComponent = MountWidget.extend(Docking, {_topic: null,@observes('currentUser.unread_notifications', 'currentUser.unread_private_messages')- _notificationsChanged() {+ notificationsChanged() {this.queueRerender();},diff --git a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6index fd2c8ff418f..75fe71d1d78 100644--- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6+++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6@@ -35,7 +35,6 @@ export default {bus.subscribe(`/notification/${user.get('id')}`, data => {const store = container.lookup('service:store');-const oldUnread = user.get('unread_notifications');const oldPM = user.get('unread_private_messages');@@ -66,7 +65,7 @@ export default {oldNotifications.insertAt(insertPosition, Em.Object.create(lastNotification));}- for (let idx=0; idx < data.recent.length; idx++) {+ for (let idx = 0; idx < data.recent.length; idx++) {let old;while(old = oldNotifications[idx]) {const info = data.recent[idx];@@ -96,21 +95,16 @@ export default {});bus.subscribe("/client_settings", data => Ember.set(siteSettings, data.name, data.value));-- bus.subscribe("/refresh_client", data => {- Discourse.set("assetVersion", data);- });+ bus.subscribe("/refresh_client", data => Discourse.set("assetVersion", data));if (!Ember.testing) {-bus.subscribe(alertChannel(user), data => onNotification(data, user));initDesktopNotifications(bus, appEvents);- if(isPushNotificationsEnabled(user, site.mobileView)) {+ if (isPushNotificationsEnabled(user, site.mobileView)) {disableDesktopNotifications();registerPushNotifications(Discourse.User.current(), site.mobileView, router, appEvents);- }- else {+ } else {unsubscribePushNotifications(user);}}diff --git a/app/models/notification.rb b/app/models/notification.rbindex 48e3293c5c2..ef2343fd79a 100644--- a/app/models/notification.rb+++ b/app/models/notification.rb@@ -59,22 +59,21 @@ class Notification < ActiveRecord::Basedef self.mark_posts_read(user, topic_id, post_numbers)count = Notification- .where(user_id: user.id,- topic_id: topic_id,- post_number: post_numbers,- read: false)- .update_all("read = 't'")-- if count > 0- user.publish_notifications_state- end+ .where(user_id: user.id)+ .where(topic_id: topic_id)+ .where(post_number: post_numbers)+ .where(read: false)+ .update_all(read: true)++ user.publish_notifications_state if count > 0countenddef self.read(user, notification_ids)- count = Notification.where(user_id: user.id)+ count = Notification.where(id: notification_ids)+ .where(user_id: user.id).where(read: false).update_all(read: true)@@ -202,7 +201,7 @@ class Notification < ActiveRecord::Baseprotecteddef refresh_notification_count- user.publish_notifications_state+ user.reload.publish_notifications_stateenddef send_emaildiff --git a/app/models/user.rb b/app/models/user.rbindex cb26bba3e23..90f5064c8f4 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -377,14 +377,15 @@ class User < ActiveRecord::Basedef unread_notifications_of_type(notification_type)# perf critical, much more efficient than AR- sql = "- SELECT COUNT(*) FROM notifications n- LEFT JOIN topics t ON n.topic_id = t.id- WHERE- t.deleted_at IS NULL AND- n.notification_type = :type AND- n.user_id = :user_id AND- NOT read"+ sql = <<~SQL+ SELECT COUNT(*)+ FROM notifications n+ LEFT JOIN topics t ON t.id = n.topic_id+ WHERE t.deleted_at IS NULL+ AND n.notification_type = :type+ AND n.user_id = :user_id+ AND NOT read+ SQLUser.exec_sql(sql, user_id: id, type: notification_type).getvalue(0, 0).to_iend@@ -394,24 +395,25 @@ class User < ActiveRecord::Baseenddef unread_notifications- @unread_notifications ||=- begin- # perf critical, much more efficient than AR- sql = "- SELECT COUNT(*) FROM notifications n- LEFT JOIN topics t ON n.topic_id = t.id- WHERE- t.deleted_at IS NULL AND- n.notification_type <> :pm AND- n.user_id = :user_id AND- NOT read AND- n.id > :seen_notification_id"-- User.exec_sql(sql, user_id: id,- seen_notification_id: seen_notification_id,- pm: Notification.types[:private_message])- .getvalue(0, 0).to_i- end+ @unread_notifications ||= begin+ # perf critical, much more efficient than AR+ sql = <<~SQL+ SELECT COUNT(*)+ FROM notifications n+ LEFT JOIN topics t ON t.id = n.topic_id+ WHERE t.deleted_at IS NULL+ AND n.notification_type <> :pm+ AND n.user_id = :user_id+ AND n.id > :seen_notification_id+ AND NOT read+ SQL++ User.exec_sql(sql,+ user_id: id,+ seen_notification_id: seen_notification_id,+ pm: Notification.types[:private_message]+ ).getvalue(0, 0).to_i+ endenddef total_unread_notifications@@ -440,8 +442,7 @@ class User < ActiveRecord::Baseenddef publish_notifications_state- # publish last notification json with the message so we- # can apply an update+ # publish last notification json with the message so we can apply an updatenotification = notifications.visible.order('notifications.id desc').firstjson = NotificationSerializer.new(notification).as_json if notification@@ -477,17 +478,17 @@ class User < ActiveRecord::Base[id.to_i, read]end- MessageBus.publish("/notification/#{id}",- { unread_notifications: unread_notifications,- unread_private_messages: unread_private_messages,- total_unread_notifications: total_unread_notifications,- read_first_notification: read_first_notification?,- last_notification: json,- recent: recent,- seen_notification_id: seen_notification_id- },- user_ids: [id] # only publish the notification to this user- )+ payload = {+ unread_notifications: unread_notifications,+ unread_private_messages: unread_private_messages,+ total_unread_notifications: total_unread_notifications,+ read_first_notification: read_first_notification?,+ last_notification: json,+ recent: recent,+ seen_notification_id: seen_notification_id,+ }++ MessageBus.publish("/notification/#{id}", payload, user_ids: [id])end# A selection of people to autocomplete on @mentiondiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex 05e72dab601..476856253b8 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -425,8 +425,10 @@ class PostAlerterif SiteSetting.allow_user_api_key_scopes.split("|").include?("push") && SiteSetting.allowed_user_api_push_urls.present?clients = user.user_api_keys- .where("('push' = ANY(scopes) OR 'notifications' = ANY(scopes)) AND push_url IS NOT NULL AND position(push_url in ?) > 0 AND revoked_at IS NULL",- SiteSetting.allowed_user_api_push_urls)+ .where("('push' = ANY(scopes) OR 'notifications' = ANY(scopes))")+ .where("push_url IS NOT NULL")+ .where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)+ .where("revoked_at IS NULL").pluck(:client_id, :push_url)if clients.length > 0diff --git a/lib/post_jobs_enqueuer.rb b/lib/post_jobs_enqueuer.rbindex 2c4ac7218bc..4dde3b73afa 100644--- a/lib/post_jobs_enqueuer.rb+++ b/lib/post_jobs_enqueuer.rb@@ -30,7 +30,7 @@ class PostJobsEnqueuerpost_id: @post.id,new_record: true,options: @opts[:post_alert_options],- )+ )enddef feature_topic_users@@ -45,10 +45,9 @@ class PostJobsEnqueuerTopicTrackingState.publish_unread(@post) if @post.post_number > 1TopicTrackingState.publish_latest(@topic, @post.whisper?)- Jobs.enqueue_in(- SiteSetting.email_time_window_mins.minutes,- :notify_mailing_list_subscribers,- post_id: @post.id+ Jobs.enqueue_in(SiteSetting.email_time_window_mins.minutes,+ :notify_mailing_list_subscribers,+ post_id: @post.id,)end