FEATURE: Allow selective dismissal of new and unread topics (#12976)

This PR improves the UI of bulk select so that its context is applied to the Dismiss Unread and Dismiss New buttons. Regular users (not just staff) are now able to use topic bulk selection on the /new and /unread routes to perform these dismiss actions more selectively.

For Dismiss Unread, there is a new count in the text of the button and in the modal when one or more topic is selected with the bulk select checkboxes.

For Dismiss New, there is a count in the button text, and we have added functionality to the server side to accept an array of topic ids to dismiss new for, instead of always having to dismiss all new, the same as the bulk dismiss unread functionality. To clean things up, the `DismissTopics` service has been rolled into the `TopicsBulkAction` service.

We now also show the top Dismiss/Dismiss New button based on whether the bottom one is in the viewport, not just based on the topic count.
This commit is contained in:
Martin Brennan
2021-05-26 09:38:46 +10:00
committed by GitHub
parent de0f2b9546
commit 7a79bd7da3
22 changed files with 399 additions and 292 deletions

View File

@ -14,7 +14,7 @@ class TopicsBulkAction
@operations ||= %w(change_category close archive change_notification_level
reset_read dismiss_posts delete unlist archive_messages
move_messages_to_inbox change_tags append_tags remove_tags
relist)
relist dismiss_topics)
end
def self.register_operation(name, &block)
@ -26,7 +26,7 @@ class TopicsBulkAction
raise Discourse::InvalidParameters.new(:operation) unless TopicsBulkAction.operations.include?(@operation[:type])
# careful these are private methods, we need send
send(@operation[:type])
@changed_ids
@changed_ids.sort
end
private
@ -81,6 +81,24 @@ class TopicsBulkAction
@changed_ids.concat @topic_ids
end
def dismiss_topics
rows = Topic.where(id: @topic_ids)
.joins("LEFT JOIN topic_users ON topic_users.topic_id = topics.id AND topic_users.user_id = #{@user.id}")
.where("topics.created_at >= ?", dismiss_topics_since_date)
.where("topic_users.last_read_post_number IS NULL")
.where("topics.archetype <> ?", Archetype.private_message)
.order("topics.created_at DESC")
.limit(SiteSetting.max_new_topics).map do |topic|
{
topic_id: topic.id,
user_id: @user.id,
created_at: Time.zone.now
}
end
DismissedTopicUser.insert_all(rows) if rows.present?
@changed_ids = rows.map { |row| row[:topic_id] }
end
def reset_read
PostTiming.destroy_for(@user.id, @topic_ids)
end
@ -211,4 +229,18 @@ class TopicsBulkAction
@topics ||= Topic.where(id: @topic_ids)
end
def dismiss_topics_since_date
new_topic_duration_minutes = @user.user_option&.new_topic_duration_minutes || SiteSetting.default_other_new_topic_duration_minutes
setting_date =
case new_topic_duration_minutes
when User::NewTopicDuration::LAST_VISIT
@user.previous_visit_at || @user.created_at
when User::NewTopicDuration::ALWAYS
@user.created_at
else
new_topic_duration_minutes.minutes.ago
end
[setting_date, @user.created_at, Time.at(SiteSetting.min_new_topics_time).to_datetime].max
end
end