diff --git a/lib/pending_assigns_reminder.rb b/lib/pending_assigns_reminder.rb index 5d2c612..a9a8578 100644 --- a/lib/pending_assigns_reminder.rb +++ b/lib/pending_assigns_reminder.rb @@ -3,14 +3,17 @@ class PendingAssignsReminder REMINDED_AT = 'last_reminded_at' REMINDERS_FREQUENCY = 'remind_assigns_frequency' + CUSTOM_FIELD_NAME = 'assigns_reminder' REMINDER_THRESHOLD = 2 def remind(user) newest_topics = assigned_topics(user, order: :desc) return if newest_topics.size < REMINDER_THRESHOLD + + delete_previous_reminders(user) + oldest_topics = assigned_topics(user, order: :asc).where.not(id: newest_topics.map(&:id)) assigned_topics_count = assigned_count_for(user) - title = I18n.t('pending_assigns_reminder.title', pending_assignments: assigned_topics_count) PostCreator.create!( @@ -18,8 +21,9 @@ class PendingAssignsReminder title: title, raw: reminder_body(user, assigned_topics_count, newest_topics, oldest_topics), archetype: Archetype.private_message, + subtype: TopicSubtype.system_message, target_usernames: user.username, - validate: false + custom_fields: { CUSTOM_FIELD_NAME => true } ) update_last_reminded(user) @@ -27,6 +31,28 @@ class PendingAssignsReminder private + def delete_previous_reminders(user) + posts = Post + .joins(topic: { topic_allowed_users: :user }) + .where(topic: { + posts_count: 1, + user_id: Discourse.system_user, + archetype: Archetype.private_message, + subtype: TopicSubtype.system_message, + topic_allowed_users: { + users: { id: user.id } + } + }) + .joins(topic: :_custom_fields) + .where(topic_custom_fields: { + name: CUSTOM_FIELD_NAME + }) + + posts.find_each do |post| + PostDestroyer.new(Discourse.system_user, post).destroy + end + end + def assigned_count_for(user) Assignment.joins_with_topics.where(assigned_to_id: user.id, assigned_to_type: 'User').count end diff --git a/spec/lib/pending_assigns_reminder_spec.rb b/spec/lib/pending_assigns_reminder_spec.rb index e8b56aa..0e4cad2 100644 --- a/spec/lib/pending_assigns_reminder_spec.rb +++ b/spec/lib/pending_assigns_reminder_spec.rb @@ -74,5 +74,43 @@ RSpec.describe PendingAssignsReminder do user.reload.custom_fields[described_class::REMINDED_AT].to_datetime ).to eq_time(DateTime.now) end + + it 'deletes previous reminders when creating a new one' do + subject.remind(user) + subject.remind(user) + + reminders_count = Topic.joins(:_custom_fields) + .where(topic_custom_fields: { name: described_class::CUSTOM_FIELD_NAME }).count + + expect(reminders_count).to eq(1) + end + + it "doesn't delete reminders from a different user" do + subject.remind(user) + another_user = Fabricate(:user) + add_to_assign_allowed_group(another_user) + 3.times do + post = Fabricate(:post) + Assigner.new(post.topic, user).assign(another_user) + end + + subject.remind(another_user) + + reminders_count = Topic.joins(:_custom_fields) + .where(topic_custom_fields: { name: described_class::CUSTOM_FIELD_NAME }).count + + expect(reminders_count).to eq(2) + end + + it "doesn't delete reminders if they have replies" do + subject.remind(user) + Fabricate(:post, topic: Topic.last) + subject.remind(user) + + reminders_count = Topic.joins(:_custom_fields) + .where(topic_custom_fields: { name: described_class::CUSTOM_FIELD_NAME }).count + + expect(reminders_count).to eq(2) + end end end