A platform for community discussion. Free, open, simple.
FIX: Avoid marking notifications as seen in readonly mode.
Guo Xiang Tan9c7d3b2dc0b3
diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rbindex 94779fc0a1f..a928b493bc5 100644--- a/app/controllers/notifications_controller.rb+++ b/app/controllers/notifications_controller.rb@@ -25,11 +25,12 @@ class NotificationsController < ApplicationControllernotifications = Notification.recent_report(current_user, limit)changed = false- if notifications.present?+ if notifications.present? && !(params.has_key?(:slient) || @readonly_mode)# ordering can be off due to PMsmax_id = notifications.map(&:id).max- changed = current_user.saw_notification_id(max_id) unless params.has_key?(:silent)+ changed = current_user.saw_notification_id(max_id)end+user.reloaduser.publish_notifications_state if changeddiff --git a/spec/requests/notifications_controller_spec.rb b/spec/requests/notifications_controller_spec.rbindex bb3c9022a34..4270afb728c 100644--- a/spec/requests/notifications_controller_spec.rb+++ b/spec/requests/notifications_controller_spec.rb@@ -33,7 +33,8 @@ enddescribe NotificationsController docontext 'when logged in' docontext 'as normal user' do- let!(:user) { sign_in(Fabricate(:user)) }+ fab!(:user) { sign_in(Fabricate(:user)) }+ fab!(:notification) { Fabricate(:notification, user: user) }describe '#index' doit 'should succeed for recent' do@@ -47,7 +48,6 @@ describe NotificationsController doendit 'should mark notifications as viewed' do- Fabricate(:notification, user: user)expect(user.reload.unread_notifications).to eq(1)expect(user.reload.total_unread_notifications).to eq(1)get "/notifications.json", params: { recent: true }@@ -56,7 +56,6 @@ describe NotificationsController doendit 'should not mark notifications as viewed if silent param is present' do- Fabricate(:notification, user: user)expect(user.reload.unread_notifications).to eq(1)expect(user.reload.total_unread_notifications).to eq(1)get "/notifications", params: { recent: true, silent: true }@@ -64,6 +63,17 @@ describe NotificationsController doexpect(user.reload.total_unread_notifications).to eq(1)end+ it 'should not mark notifications as viewed in readonly mode' do+ Discourse.received_redis_readonly!+ expect(user.reload.unread_notifications).to eq(1)+ expect(user.reload.total_unread_notifications).to eq(1)+ get "/notifications", params: { recent: true, silent: true }+ expect(user.reload.unread_notifications).to eq(1)+ expect(user.reload.total_unread_notifications).to eq(1)+ ensure+ Discourse.clear_redis_readonly!+ end+context 'when username params is not valid' doit 'should raise the right error' doget "/notifications.json", params: { username: 'somedude' }@@ -78,7 +88,6 @@ describe NotificationsController doendit "can update a single notification" do- notification = Fabricate(:notification, user: user)notification2 = Fabricate(:notification, user: user)put "/notifications/mark-read.json", params: { id: notification.id }expect(response.status).to eq(200)@@ -91,7 +100,6 @@ describe NotificationsController doendit "updates the `read` status" do- Fabricate(:notification, user: user)expect(user.reload.unread_notifications).to eq(1)expect(user.reload.total_unread_notifications).to eq(1)put "/notifications/mark-read.json"@@ -120,7 +128,7 @@ describe NotificationsController doendcontext 'as admin' do- let!(:admin) { sign_in(Fabricate(:admin)) }+ fab!(:admin) { sign_in(Fabricate(:admin)) }describe '#create' doit "can create notification" do