discourse/spec/jobs/dashboard_stats_spec.rb
Roman Rizzi 1fc06520bd
REFACTOR: Improve support for consolidating notifications. (#14904)
* REFACTOR: Improve support for consolidating notifications.

Before this commit, we didn't have a single way of consolidating notifications. For notifications like group summaries, we manually removed old ones before creating a new one. On the other hand, we used an after_create callback for likes and group membership requests, which caused unnecessary work, as we need to delete the record we created to replace it with a consolidated one.

We now have all the consolidation rules centralized in a single place: the consolidation planner class. Other parts of the app looking to create a consolidable notification can do so by calling Notification#consolidate_or_save!, instead of the default Notification#create! method.

Finally, we added two more rules: one for re-using existing group summaries and another for deleting duplicated dashboard problems PMs notifications when the user is tracking the moderator's inbox. Setting the threshold to one forces the planner to apply this rule every time.

I plan to add plugin support for adding custom rules in another PR to keep this one relatively small.

* DEV: Introduces a plugin API for consolidating notifications.

This commit removes the `Notification#filter_by_consolidation_data` scope since plugins could have to define their criteria. The Plan class now receives two blocks, one to query for an already consolidated notification, which we'll try to update, and another to query for existing ones to consolidate.

It also receives a consolidation window, which accepts an ActiveSupport::Duration object, and filter notifications created since that value.
2021-11-30 13:36:14 -03:00

72 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe ::Jobs::DashboardStats do
let(:group_message) { GroupMessage.new(Group[:admins].name, :dashboard_problems, limit_once_per: 7.days.to_i) }
def clear_recently_sent!
Discourse.redis.del(group_message.sent_recently_key)
end
after do
clear_recently_sent!
end
it 'creates group message when problems are persistent for 2 days' do
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, Time.zone.now.to_s)
expect { described_class.new.execute({}) }.not_to change { Topic.count }
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, 3.days.ago)
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
end
it 'replaces old message' do
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, 3.days.ago)
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
old_topic = Topic.last
clear_recently_sent!
new_topic = described_class.new.execute({}).topic
expect(old_topic.reload.deleted_at.present?).to eq(true)
expect(new_topic.reload.deleted_at).to be_nil
expect(new_topic.title).to eq(old_topic.title)
end
it 'consolidates notifications when not tracking admins group' do
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, 3.days.ago)
Jobs.run_immediately!
admin = Fabricate(:admin)
Group[:admins].add(admin)
described_class.new.execute({})
clear_recently_sent!
new_topic = described_class.new.execute({}).topic
notifications = Notification.where(user: admin, notification_type: Notification.types[:private_message])
expect(notifications.count).to eq(1)
from_topic_id = Post.select(:topic_id).find_by(id: notifications.last.data_hash[:original_post_id]).topic_id
expect(from_topic_id).to eq(new_topic.id)
end
it 'duplicates message if previous one has replies' do
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, 3.days.ago)
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
clear_recently_sent!
_reply_1 = Fabricate(:post, topic: Topic.last)
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
end
it 'duplicates message if previous was 3 months ago' do
freeze_time 3.months.ago do
Discourse.redis.setex(AdminDashboardData.problems_started_key, 14.days.to_i, 3.days.ago)
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
clear_recently_sent!
end
expect { described_class.new.execute({}) }.to change { Topic.count }.by(1)
end
end