DEV: Dedicated route for current user notification counts (#26106)

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
Penar Musaraj
2024-03-15 12:08:37 -04:00
committed by GitHub
co-authored by Alan Guo Xiang Tan
parent ad7e3e04f3
commit 8cf2f909f5
11 changed files with 399 additions and 0 deletions
+83
View File
@@ -755,6 +755,89 @@ RSpec.describe TopicTrackingState do
end
end
describe ".report_totals" do
fab!(:user2) { Fabricate(:user) }
it "correctly returns new/unread totals" do
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0, unread: 0 })
post.topic.notifier.watch_topic!(post.topic.user_id)
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 1, unread: 0 })
create_post(user: user, topic: post.topic)
# when user replies, they have 0 new count
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0, unread: 0 })
# when we reply the poster will have an unread item
report = TopicTrackingState.report_totals(post.user)
expect(report).to eq({ new: 0, unread: 1 })
create_post(user: user2, topic: post.topic)
# when a third user replies, the original user should have an unread item
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0, unread: 1 })
# the post user still has one unread
report = TopicTrackingState.report_totals(post.user)
expect(report).to eq({ new: 0, unread: 1 })
post2 = create_post
post2.topic.notifier.watch_topic!(user.id)
# watching another new topic bumps the new count
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 1, unread: 1 })
end
it "respects treat_as_new_topic_start_date user option" do
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0, unread: 0 })
post.topic.notifier.watch_topic!(post.topic.user_id)
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 1, unread: 0 })
user.user_option.new_topic_duration_minutes = 5
user.user_option.save
post.topic.created_at = 10.minutes.ago
post.topic.save
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0, unread: 0 })
end
it "respects new_new_view_enabled" do
new_new_group = Fabricate(:group)
SiteSetting.experimental_new_new_view_groups = new_new_group.name
user.groups << new_new_group
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 0 })
post.topic.notifier.watch_topic!(post.topic.user_id)
post2 = create_post
Fabricate(:post, topic: post2.topic)
tracking = {
notification_level: TopicUser.notification_levels[:tracking],
last_read_post_number: 1,
}
TopicUser.change(user.id, post2.topic_id, tracking)
report = TopicTrackingState.report_totals(user)
expect(report).to eq({ new: 2 })
end
end
describe ".publish_recover" do
include_examples("publishes message to right groups and users", "/recover", :publish_recover)
include_examples("does not publish message for private topics", :publish_recover)
@@ -618,5 +618,12 @@ RSpec.describe NotificationsController do
delete_notification(403, :to)
end
end
describe "#totals" do
it "can't see notification totals" do
get "/notifications/totals.json"
expect(response.status).to eq(403)
end
end
end
end
@@ -0,0 +1,61 @@
# frozen_string_literal: true
RSpec.describe UserNotificationTotalSerializer do
fab!(:user) { Fabricate(:user, trust_level: 3) }
fab!(:notification) { Fabricate(:notification, user: user, read: false) }
fab!(:pm_notification) do
Fabricate(:notification, user: user, notification_type: Notification.types[:private_message])
end
fab!(:pm_notification2) do
Fabricate(:notification, user: user, notification_type: Notification.types[:private_message])
end
fab!(:group_message_notification) do
Fabricate(
:notification,
user: user,
notification_type: Notification.types[:group_message_summary],
data: { group_id: 1, group_name: "Group", inbox_count: 5 }.to_json,
)
end
fab!(:reviewable)
let(:serializer) { described_class.new(user, scope: Guardian.new(user), root: false) }
let(:serialized_data) { serializer.as_json }
it "includes the user's unread regular notifications count" do
# notification + group_message_notification - pm_notifications
expect(serialized_data[:unread_notifications]).to eq(2)
end
it "includes the user's unread private messages count" do
expect(serialized_data[:unread_personal_messages]).to eq(2)
end
context "when the user has PMs disabled" do
it "does not include the user's unread private messages count" do
SiteSetting.personal_message_enabled_groups = Group::AUTO_GROUPS[:trust_level_4]
expect(serialized_data).not_to have_key(:unread_personal_messages)
end
end
it "includes group inbox notification counts" do
expect(serialized_data[:group_inboxes]).to contain_exactly(
{ group_id: 1, group_name: "Group", count: 5 },
)
end
context "when the user is staff" do
before { user.update!(admin: true) }
it "includes the count of unseen reviewables" do
expect(serialized_data[:unseen_reviewables]).to eq(1)
end
end
context "when the user is not staff" do
it "does not include unseen reviewables counts" do
expect(serialized_data).not_to have_key(:unseen_reviewables)
end
end
end