FEATURE: Delete previous reviewable reminders. (#15250)

We send the reminder using the GroupMessage class, which supports removing previous messages. We can't match them by raw because they could mention different moderators. Also, I had to change the subject to remove dynamically generated values, which is necessary for finding them.
This commit is contained in:
Roman Rizzi 2021-12-10 10:17:39 -03:00 committed by GitHub
parent ac31c2bbb2
commit 3602f83cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 18 deletions

View File

@ -22,14 +22,19 @@ module Jobs
usernames = active_moderator_usernames
mentions = usernames.size > 0 ? "@#{usernames.join(', @')} " : ""
@sent_reminder = PostCreator.create(
Discourse.system_user,
target_group_names: Group[:moderators].name,
archetype: Archetype.private_message,
subtype: TopicSubtype.system_message,
title: I18n.t('reviewables_reminder.subject_template', count: reviewable_ids.size),
raw: mentions + I18n.t('reviewables_reminder.submitted', count: SiteSetting.notify_about_flags_after, base_path: Discourse.base_path)
).present?
message = GroupMessage.new(
Group[:moderators].name,
'reviewables_reminder',
{
limit_once_per: false,
message_params: { mentions: mentions, count: SiteSetting.notify_about_flags_after }
}
)
Topic.transaction do
message.delete_previous!(match_raw: false)
@sent_reminder = message.create.present?
end
self.class.last_notified_id = reviewable_ids[0]
end

View File

@ -43,7 +43,7 @@ class GroupMessage
end
end
def delete_previous!
def delete_previous!(match_raw: true)
posts = Post
.joins(topic: { topic_allowed_groups: :group })
.where(topic: {
@ -57,7 +57,10 @@ class GroupMessage
}
})
.where("posts.created_at > ?", RECENT_MESSAGE_PERIOD.ago)
.where(raw: I18n.t("system_messages.#{@message_type}.text_body_template", message_params).rstrip)
if match_raw
posts = posts.where(raw: I18n.t("system_messages.#{@message_type}.text_body_template", message_params).rstrip)
end
posts.find_each do |post|
PostDestroyer.new(Discourse.system_user, post).destroy

View File

@ -2630,14 +2630,6 @@ en:
one: "User %{username} has %{count} post in a public topic or personal message, so they can't be deleted."
other: "User %{username} has %{count} posts in public topics or personal messages, so they can't be deleted."
reviewables_reminder:
submitted:
one: "Items were submitted over %{count} hour ago. [Please review them](%{base_path}/review)."
other: "Items were submitted over %{count} hours ago. [Please review them](%{base_path}/review)."
subject_template:
one: "%{count} item needs to be reviewed"
other: "%{count} items need to be reviewed"
unsubscribe_mailer:
title: "Unsubscribe Mailer"
subject_template: "Confirm you no longer want to receive email updates from %{site_title}"
@ -2829,6 +2821,12 @@ en:
other: "This topic is temporarily closed for at least %{count} hours due to a large number of community flags."
system_messages:
reviewables_reminder:
subject_template: "There're items in the review queue that need reviewing"
text_body_template:
one: "%{mentions} Items were submitted over %{count} hour ago. [Please review them](%{base_path}/review)."
other: "%{mentions} Items were submitted over %{count} hours ago. [Please review them](%{base_path}/review)."
private_topic_title: "Topic #%{id}"
contents_hidden: "Please visit the post to see its contents."

View File

@ -74,5 +74,17 @@ describe Jobs::PendingReviewablesReminder do
expect(execute.sent_reminder).to eq(true)
end
end
it 'deletes previous messages' do
GroupMessage.create(
Group[:moderators].name, 'reviewables_reminder',
{ limit_once_per: false, message_params: { mentions: [], count: 1 } }
)
create_flag(49.hours.ago)
execute
expect(Topic.where(title: I18n.t("system_messages.reviewables_reminder.subject_template")).count).to eq(1)
end
end
end