FIX: Set a minimum reading time per post. (#7842)

FIX: Set a minimum reading time per post. (#7842)

Topics containing only images could generate a reading time of zero minutes.

diff --git a/app/assets/javascripts/discourse/widgets/toggle-topic-summary.js.es6 b/app/assets/javascripts/discourse/widgets/toggle-topic-summary.js.es6
index 30adab9..c712a81 100644
--- a/app/assets/javascripts/discourse/widgets/toggle-topic-summary.js.es6
+++ b/app/assets/javascripts/discourse/widgets/toggle-topic-summary.js.es6
@@ -1,15 +1,20 @@
 import RawHtml from "discourse/widgets/raw-html";
 import { createWidget } from "discourse/widgets/widget";
 
+const MIN_POST_READ_TIME = 4;
+
 createWidget("toggle-summary-description", {
   description(attrs) {
     if (attrs.topicSummaryEnabled) {
       return I18n.t("summary.enabled_description");
     }
 
-    if (attrs.topicWordCount) {
-      const readingTime = Math.floor(
-        attrs.topicWordCount / this.siteSettings.read_time_word_count
+    if (attrs.topicWordCount && this.siteSettings.read_time_word_count > 0) {
+      const readingTime = Math.ceil(
+        Math.max(
+          attrs.topicWordCount / this.siteSettings.read_time_word_count,
+          (attrs.topicPostsCount * MIN_POST_READ_TIME) / 60
+        )
       );
       return I18n.t("summary.description_time", {
         replyCount: attrs.topicReplyCount,
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 6f12cda..060eaf7 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -7,6 +7,7 @@ require_dependency 'gaps'
 
 class TopicView
   MEGA_TOPIC_POSTS_COUNT = 10000
+  MIN_POST_READ_TIME = 4.0
 
   attr_reader(
     :topic,
@@ -208,7 +209,13 @@ class TopicView
 
   def read_time
     return nil if @post_number > 1 # only show for topic URLs
-    (@topic.word_count / SiteSetting.read_time_word_count).floor if @topic.word_count
+
+    if @topic.word_count && SiteSetting.read_time_word_count > 0
+      [
+        @topic.word_count / SiteSetting.read_time_word_count,
+        @topic.posts_count * MIN_POST_READ_TIME / 60
+      ].max.ceil
+    end
   end
 
   def like_count
diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb
index 736bf1e..aecb9a9 100644
--- a/spec/components/topic_view_spec.rb
+++ b/spec/components/topic_view_spec.rb
@@ -694,4 +694,21 @@ describe TopicView do
       expect(topic_view.last_post_id).to eq(p3.id)
     end
   end
+
+  describe '#read_time' do
+    let!(:post) { Fabricate(:post, topic: topic) }
+
+    before do
+      PostCreator.create!(Discourse.system_user, topic_id: topic.id, raw: "![image|100x100](upload://upload.png)")
+      topic_view.topic.reload
+    end
+
+    it 'should return the right read time' do
+      SiteSetting.read_time_word_count = 500
+      expect(topic_view.read_time).to eq(1)
+
+      SiteSetting.read_time_word_count = 0
+      expect(topic_view.read_time).to eq(nil)
+    end
+  end
 end

GitHub sha: 9ba2c7cd

1 Like