FEATURE: Delete previous assign reminder PMs (#265)
This commit is contained in:
parent
a9ef43e12f
commit
fe2f629975
|
@ -3,14 +3,17 @@
|
||||||
class PendingAssignsReminder
|
class PendingAssignsReminder
|
||||||
REMINDED_AT = 'last_reminded_at'
|
REMINDED_AT = 'last_reminded_at'
|
||||||
REMINDERS_FREQUENCY = 'remind_assigns_frequency'
|
REMINDERS_FREQUENCY = 'remind_assigns_frequency'
|
||||||
|
CUSTOM_FIELD_NAME = 'assigns_reminder'
|
||||||
REMINDER_THRESHOLD = 2
|
REMINDER_THRESHOLD = 2
|
||||||
|
|
||||||
def remind(user)
|
def remind(user)
|
||||||
newest_topics = assigned_topics(user, order: :desc)
|
newest_topics = assigned_topics(user, order: :desc)
|
||||||
return if newest_topics.size < REMINDER_THRESHOLD
|
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))
|
oldest_topics = assigned_topics(user, order: :asc).where.not(id: newest_topics.map(&:id))
|
||||||
assigned_topics_count = assigned_count_for(user)
|
assigned_topics_count = assigned_count_for(user)
|
||||||
|
|
||||||
title = I18n.t('pending_assigns_reminder.title', pending_assignments: assigned_topics_count)
|
title = I18n.t('pending_assigns_reminder.title', pending_assignments: assigned_topics_count)
|
||||||
|
|
||||||
PostCreator.create!(
|
PostCreator.create!(
|
||||||
|
@ -18,8 +21,9 @@ class PendingAssignsReminder
|
||||||
title: title,
|
title: title,
|
||||||
raw: reminder_body(user, assigned_topics_count, newest_topics, oldest_topics),
|
raw: reminder_body(user, assigned_topics_count, newest_topics, oldest_topics),
|
||||||
archetype: Archetype.private_message,
|
archetype: Archetype.private_message,
|
||||||
|
subtype: TopicSubtype.system_message,
|
||||||
target_usernames: user.username,
|
target_usernames: user.username,
|
||||||
validate: false
|
custom_fields: { CUSTOM_FIELD_NAME => true }
|
||||||
)
|
)
|
||||||
|
|
||||||
update_last_reminded(user)
|
update_last_reminded(user)
|
||||||
|
@ -27,6 +31,28 @@ class PendingAssignsReminder
|
||||||
|
|
||||||
private
|
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)
|
def assigned_count_for(user)
|
||||||
Assignment.joins_with_topics.where(assigned_to_id: user.id, assigned_to_type: 'User').count
|
Assignment.joins_with_topics.where(assigned_to_id: user.id, assigned_to_type: 'User').count
|
||||||
end
|
end
|
||||||
|
|
|
@ -74,5 +74,43 @@ RSpec.describe PendingAssignsReminder do
|
||||||
user.reload.custom_fields[described_class::REMINDED_AT].to_datetime
|
user.reload.custom_fields[described_class::REMINDED_AT].to_datetime
|
||||||
).to eq_time(DateTime.now)
|
).to eq_time(DateTime.now)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue