Skip to content

Commit

Permalink
Merge pull request #2470 from samvera/i1019-marks-notifications-as-read
Browse files Browse the repository at this point in the history
🎁 Marks Notifications as Read on Dashboard
  • Loading branch information
sjproctor authored Mar 3, 2025
2 parents 3e1e084 + e49e7d3 commit b14e5de
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/controllers/hyrax/notifications_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

# OVERRIDE: Hyrax v5.0.4 to mark all unread messages as read when the user visits the notifications dashboard

module Hyrax
module NotificationsControllerDecorator
def index
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
add_breadcrumb t(:'hyrax.admin.sidebar.notifications'), hyrax.notifications_path
@messages = user_mailbox.inbox

# OVERRIDE: Mark all unread messages as read when user visits the notifications dashboard
mark_messages_as_read

# Update the notifications now that there are zero unread
StreamNotificationsJob.perform_later(current_user)
end

private

def mark_messages_as_read
Mailboxer::Receipt.where(
receiver: current_user,
is_read: false,
deleted: false
).find_each do |receipt|
receipt.update(is_read: true)
end
end
end
end

Hyrax::NotificationsController.prepend Hyrax::NotificationsControllerDecorator
48 changes: 48 additions & 0 deletions spec/controllers/hyrax/notifications_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

RSpec.describe Hyrax::NotificationsController, type: :controller do
routes { Hyrax::Engine.routes }

let(:user) { create(:user) }

before do
sign_in user
end

describe "#index" do
let!(:conversation1) { create(:mailboxer_conversation) }
let!(:conversation2) { create(:mailboxer_conversation) }
let!(:message1) { create(:mailboxer_message, conversation: conversation1, sender: user) }
let!(:message2) { create(:mailboxer_message, conversation: conversation2, sender: user) }
let!(:receipt1) { create(:mailboxer_receipt, notification: message1, receiver: user, mailbox_type: 'inbox', is_read: false) }
let!(:receipt2) { create(:mailboxer_receipt, notification: message2, receiver: user, mailbox_type: 'inbox', is_read: false) }

it "shows notifications page" do
get :index
expect(response).to be_successful
end

it "assigns inbox messages" do
get :index
expect(assigns(:messages)).to match_array([conversation1, conversation2])
end

it "marks unread messages as read" do
expect do
get :index
end.to change {
Mailboxer::Receipt.where(receiver: user, is_read: false).count
}.from(2).to(0)
end

it "enqueues the StreamNotificationsJob" do
expect(StreamNotificationsJob).to receive(:perform_later).with(user)
get :index
end

it "sets breadcrumbs" do
expect(controller).to receive(:add_breadcrumb).exactly(3).times
get :index
end
end
end

0 comments on commit b14e5de

Please sign in to comment.