-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2470 from samvera/i1019-marks-notifications-as-read
🎁 Marks Notifications as Read on Dashboard
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
app/controllers/hyrax/notifications_controller_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |