From 7e94f9d6f9992a63917d60a562e5f44ee6e92245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 29 Oct 2014 21:26:32 +0100 Subject: [PATCH] FIX: insert different message when auto-closing a topic based on the last post --- app/models/topic.rb | 2 +- app/models/topic_status_update.rb | 38 ++++++++++++++----------- config/locales/server.en.yml | 13 +++++++-- spec/models/topic_status_update_spec.rb | 27 +++++++++++++++--- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index 4c3031c5302..b80519aa008 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -392,7 +392,7 @@ class Topic < ActiveRecord::Base end def update_status(status, enabled, user) - TopicStatusUpdate.new(self, user).update! status, enabled + TopicStatusUpdate.new(self, user).update!(status, enabled) end # Atomically creates the next post number diff --git a/app/models/topic_status_update.rb b/app/models/topic_status_update.rb index 471a7b66553..43ee4055146 100644 --- a/app/models/topic_status_update.rb +++ b/app/models/topic_status_update.rb @@ -15,11 +15,11 @@ TopicStatusUpdate = Struct.new(:topic, :user) do def change(status) if status.pinned? || status.pinned_globally? - topic.update_pinned status.enabled?, status.pinned_globally? + topic.update_pinned(status.enabled?, status.pinned_globally?) elsif status.autoclosed? - topic.update_column 'closed', status.enabled? + topic.update_column('closed', status.enabled?) else - topic.update_column status.name, status.enabled? + topic.update_column(status.name, status.enabled?) end if topic.auto_close_at && (status.reopening_topic? || status.manually_closing_topic?) @@ -39,26 +39,32 @@ TopicStatusUpdate = Struct.new(:topic, :user) do def update_read_state_for(status, old_highest_read) if status.autoclosed? # let's pretend all the people that read up to the autoclose message - # actually read the topic + # actually read the topic PostTiming.pretend_read(topic.id, old_highest_read, topic.highest_post_number) end end def message_for(status) if status.autoclosed? - num_minutes = topic.auto_close_started_at ? ((Time.zone.now - topic.auto_close_started_at) / 1.minute).round : topic.age_in_minutes - if num_minutes.minutes >= 2.days - I18n.t "#{status.locale_key}_days", count: (num_minutes.minutes / 1.day).round - else - num_hours = (num_minutes.minutes / 1.hour).round - if num_hours >= 2 - I18n.t "#{status.locale_key}_hours", count: num_hours - else - I18n.t "#{status.locale_key}_minutes", count: num_minutes - end - end + locale_key = status.locale_key + locale_key << "_lastpost" if topic.auto_close_based_on_last_post + message_for_autoclosed(locale_key) else - I18n.t status.locale_key + I18n.t(status.locale_key) + end + end + + def message_for_autoclosed(locale_key) + num_minutes = topic.auto_close_started_at ? ((Time.zone.now - topic.auto_close_started_at) / 1.minute).round : topic.age_in_minutes + if num_minutes.minutes >= 2.days + I18n.t("#{locale_key}_days", count: (num_minutes.minutes / 1.day).round) + else + num_hours = (num_minutes.minutes / 1.hour).round + if num_hours >= 2 + I18n.t("#{locale_key}_hours", count: num_hours) + else + I18n.t("#{locale_key}_minutes", count: num_minutes) + end end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 4a135e22a57..67a2df56b8b 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1080,18 +1080,25 @@ en: closed_enabled: "This topic is now closed. New replies are no longer allowed." closed_disabled: "This topic is now opened. New replies are allowed." autoclosed_enabled_days: - zero: "This topic was automatically closed after 1 day. New replies are no longer allowed." one: "This topic was automatically closed after 1 day. New replies are no longer allowed." other: "This topic was automatically closed after %{count} days. New replies are no longer allowed." autoclosed_enabled_hours: - zero: "This topic was automatically closed after 1 hour. New replies are no longer allowed." one: "This topic was automatically closed after 1 hour. New replies are no longer allowed." other: "This topic was automatically closed after %{count} hours. New replies are no longer allowed." autoclosed_enabled_minutes: - zero: "This topic was automatically closed after 1 minute. New replies are no longer allowed." one: "This topic was automatically closed after 1 minute. New replies are no longer allowed." other: "This topic was automatically closed after %{count} minutes. New replies are no longer allowed." + autoclosed_enabled_lastpost_days: + one: "This topic was automatically closed 1 day after the last reply. New replies are no longer allowed." + other: "This topic was automatically closed %{count} days after the last reply. New replies are no longer allowed." + autoclosed_enabled_lastpost_hours: + one: "This topic was automatically closed 1 hour after the last reply. New replies are no longer allowed." + other: "This topic was automatically closed count} hours after the last reply. New replies are no longer allowed." + autoclosed_enabled_lastpost_minutes: + one: "This topic was automatically closed 1 minute after the last reply. New replies are no longer allowed." + other: "This topic was automatically closed %{count} minutes after the last reply. New replies are no longer allowed." autoclosed_disabled: "This topic is now opened. New replies are allowed." + autoclosed_disabled_lastpost: "This topic is now opened. New replies are allowed." pinned_enabled: "This topic is now pinned. It will appear at the top of its category until it is unpinned by staff for everyone, or by individual users for themselves." pinned_disabled: "This topic is now unpinned. It will no longer appear at the top of its category." pinned_globally_enabled: "This topic is now pinned globally. It will appear at the top of its category and all topic lists until it is unpinned by staff for everyone, or by individual users for themselves." diff --git a/spec/models/topic_status_update_spec.rb b/spec/models/topic_status_update_spec.rb index d7e2b4f1f81..c6127575883 100644 --- a/spec/models/topic_status_update_spec.rb +++ b/spec/models/topic_status_update_spec.rb @@ -4,18 +4,19 @@ require 'spec_helper' require_dependency 'post_destroyer' describe TopicStatusUpdate do + + let(:user) { Fabricate(:user) } + let(:admin) { Fabricate(:admin) } + it "avoids notifying on automatically closed topics" do # TODO: TopicStatusUpdate should supress message bus updates from the users it "pretends to read" - user = Fabricate(:user) post = PostCreator.create(user, raw: "this is a test post 123 this is a test post", title: "hello world title", ) - # TODO needed so counts sync up, - # PostCreator really should not give back out-of-date Topic + # TODO needed so counts sync up, PostCreator really should not give back out-of-date Topic post.topic.reload - admin = Fabricate(:admin) TopicStatusUpdate.new(post.topic, admin).update!("autoclosed", true) post.topic.posts.count.should == 2 @@ -23,4 +24,22 @@ describe TopicStatusUpdate do tu = TopicUser.find_by(user_id: user.id) tu.last_read_post_number.should == 2 end + + it "adds an autoclosed message" do + topic = create_topic + + TopicStatusUpdate.new(topic, admin).update!("autoclosed", true) + + topic.posts.last.raw.should == I18n.t("topic_statuses.autoclosed_enabled_minutes", count: 0) + end + + it "adds an autoclosed message based on last post" do + topic = create_topic + topic.auto_close_based_on_last_post = true + + TopicStatusUpdate.new(topic, admin).update!("autoclosed", true) + + topic.posts.last.raw.should == I18n.t("topic_statuses.autoclosed_enabled_lastpost_minutes", count: 0) + end + end