From 90c3695ab052263e29216ca36a4a66fa8267c82c Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Mon, 21 Feb 2022 22:45:01 +0200 Subject: [PATCH] FEATURE: Rename Reset Read bulk action to Defer (#15972) It is enabled only if defer is enabled in user options too and if the button shows up in the topic's footer. --- .../app/controllers/topic-bulk-actions.js | 11 +++++++---- .../tests/acceptance/topic-bulk-actions-test.js | 6 ++---- app/controllers/topics_controller.rb | 2 +- app/models/post_timing.rb | 4 ++-- config/locales/client.en.yml | 2 +- lib/topics_bulk_action.rb | 9 ++++++--- spec/lib/topics_bulk_action_spec.rb | 15 +++++++++++---- spec/models/post_timing_spec.rb | 8 ++++---- 8 files changed, 34 insertions(+), 23 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/topic-bulk-actions.js b/app/assets/javascripts/discourse/app/controllers/topic-bulk-actions.js index b31187f77c8..50399e35c64 100644 --- a/app/assets/javascripts/discourse/app/controllers/topic-bulk-actions.js +++ b/app/assets/javascripts/discourse/app/controllers/topic-bulk-actions.js @@ -57,9 +57,12 @@ addBulkButton("showNotificationLevel", "notification_level", { icon: "d-regular", class: "btn-default", }); -addBulkButton("resetRead", "reset_read", { - icon: "backward", +addBulkButton("deletePostTiming", "defer", { + icon: "circle", class: "btn-default", + buttonVisible() { + return this.currentUser.enable_defer; + }, }); addBulkButton("unlistTopics", "unlist_topics", { icon: "far-eye-slash", @@ -299,8 +302,8 @@ export default Controller.extend(ModalFunctionality, { ); }, - resetRead() { - this.performAndRefresh({ type: "reset_read" }); + deletePostTiming() { + this.performAndRefresh({ type: "destroy_post_timing" }); }, removeTags() { diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-bulk-actions-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-bulk-actions-test.js index f31f4cb2720..95565ad9dc0 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/topic-bulk-actions-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/topic-bulk-actions-test.js @@ -20,7 +20,7 @@ acceptance("Topic - Bulk Actions", function (needs) { }); test("bulk select - modal", async function (assert) { - updateCurrentUser({ moderator: true }); + updateCurrentUser({ moderator: true, enable_defer: true }); await visit("/latest"); await click("button.bulk-select"); @@ -65,9 +65,7 @@ acceptance("Topic - Bulk Actions", function (needs) { ); assert.ok( - queryAll(".bulk-buttons") - .html() - .includes(I18n.t("topics.bulk.reset_read")), + queryAll(".bulk-buttons").html().includes(I18n.t("topics.bulk.defer")), "it shows an option to reset read" ); diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index e1bb02f7993..73fb6838f41 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -292,7 +292,7 @@ class TopicsController < ApplicationController topic_id = params[:topic_id].to_i if params[:last].to_s == "1" - PostTiming.destroy_last_for(current_user, topic_id) + PostTiming.destroy_last_for(current_user, topic_id: topic_id) else PostTiming.destroy_for(current_user.id, [topic_id]) end diff --git a/app/models/post_timing.rb b/app/models/post_timing.rb index e272d7f08d3..690d5e38557 100644 --- a/app/models/post_timing.rb +++ b/app/models/post_timing.rb @@ -58,8 +58,8 @@ class PostTiming < ActiveRecord::Base record_new_timing(args) if rows == 0 end - def self.destroy_last_for(user, topic_id) - topic = Topic.find(topic_id) + def self.destroy_last_for(user, topic_id: nil, topic: nil) + topic ||= Topic.find(topic_id) post_number = user.staff? ? topic.highest_staff_post_number : topic.highest_post_number last_read = post_number - 1 diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index c6796171056..7e58eac33a6 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2500,7 +2500,7 @@ en: clear_all: "Clear All" unlist_topics: "Unlist Topics" relist_topics: "Relist Topics" - reset_read: "Reset Read" + defer: "Defer" delete: "Delete Topics" dismiss: "Dismiss" dismiss_read: "Dismiss all unread" diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb index f38f4b17cef..4939b792fec 100644 --- a/lib/topics_bulk_action.rb +++ b/lib/topics_bulk_action.rb @@ -12,7 +12,7 @@ class TopicsBulkAction def self.operations @operations ||= %w(change_category close archive change_notification_level - reset_read dismiss_posts delete unlist archive_messages + destroy_post_timing dismiss_posts delete unlist archive_messages move_messages_to_inbox change_tags append_tags remove_tags relist dismiss_topics) end @@ -98,8 +98,11 @@ class TopicsBulkAction @changed_ids = rows.map { |row| row[:topic_id] } end - def reset_read - PostTiming.destroy_for(@user.id, @topic_ids) + def destroy_post_timing + topics.each do |t| + PostTiming.destroy_last_for(@user, topic: t) + @changed_ids << t.id + end end def change_category diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb index 1cf4a93dbee..79d3194b708 100644 --- a/spec/lib/topics_bulk_action_spec.rb +++ b/spec/lib/topics_bulk_action_spec.rb @@ -197,11 +197,18 @@ describe TopicsBulkAction do end end - describe "reset_read" do + describe "destroy_post_timing" do + fab!(:fist_post) { Fabricate(:post, topic: topic) } + + before do + PostTiming.process_timings(topic.user, topic.id, 10, [[1, 10]]) + end + it "delegates to PostTiming.destroy_for" do - tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'reset_read') - PostTiming.expects(:destroy_for).with(topic.user_id, [topic.id]) - topic_ids = tba.perform! + tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'destroy_post_timing') + topic_ids = nil + expect { topic_ids = tba.perform! }.to change { PostTiming.count }.by(-1) + expect(topic_ids).to contain_exactly(topic.id) end end diff --git a/spec/models/post_timing_spec.rb b/spec/models/post_timing_spec.rb index ecdf9c00293..54edb51ebb5 100644 --- a/spec/models/post_timing_spec.rb +++ b/spec/models/post_timing_spec.rb @@ -176,7 +176,7 @@ describe PostTiming do end it '#destroy_last_for decrements the reads count for a post' do - PostTiming.destroy_last_for(post.user, post.topic_id) + PostTiming.destroy_last_for(post.user, topic_id: post.topic_id) expect(post.reload.reads).to eq initial_read_count end @@ -193,7 +193,7 @@ describe PostTiming do post.topic.update!(updated_at: 10.minutes.ago) PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]]) - PostTiming.destroy_last_for(post.user, post.topic_id) + PostTiming.destroy_last_for(post.user, topic_id: post.topic_id) expect(post.user.user_stat.reload.first_unread_at).to eq_time(post.topic.updated_at) end @@ -203,7 +203,7 @@ describe PostTiming do post.topic.update!(updated_at: 10.minutes.ago) PostTiming.process_timings(post.user, post.topic_id, 1, [[post.post_number, 100]]) - PostTiming.destroy_last_for(post.user, post.topic_id) + PostTiming.destroy_last_for(post.user, topic_id: post.topic_id) expect(post.user.user_stat.reload.first_unread_pm_at).to eq_time(post.topic.updated_at) end @@ -217,7 +217,7 @@ describe PostTiming do topic.allowed_groups << group PostTiming.process_timings(user, topic.id, 1, [[post.post_number, 100]]) - PostTiming.destroy_last_for(user, topic.id) + PostTiming.destroy_last_for(user, topic_id: topic.id) expect(GroupUser.find_by(user: user, group: group).first_unread_pm_at) .to eq_time(post.topic.updated_at)