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

Topics containing only images could generate a reading time of zero minutes.
This commit is contained in:
Bianca Nenciu 2019-07-19 18:15:38 +03:00 committed by GitHub
parent 5a3a6824c4
commit 9ba2c7cd8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View File

@ -1,15 +1,20 @@
import RawHtml from "discourse/widgets/raw-html"; import RawHtml from "discourse/widgets/raw-html";
import { createWidget } from "discourse/widgets/widget"; import { createWidget } from "discourse/widgets/widget";
const MIN_POST_READ_TIME = 4;
createWidget("toggle-summary-description", { createWidget("toggle-summary-description", {
description(attrs) { description(attrs) {
if (attrs.topicSummaryEnabled) { if (attrs.topicSummaryEnabled) {
return I18n.t("summary.enabled_description"); return I18n.t("summary.enabled_description");
} }
if (attrs.topicWordCount) { if (attrs.topicWordCount && this.siteSettings.read_time_word_count > 0) {
const readingTime = Math.floor( const readingTime = Math.ceil(
attrs.topicWordCount / this.siteSettings.read_time_word_count Math.max(
attrs.topicWordCount / this.siteSettings.read_time_word_count,
(attrs.topicPostsCount * MIN_POST_READ_TIME) / 60
)
); );
return I18n.t("summary.description_time", { return I18n.t("summary.description_time", {
replyCount: attrs.topicReplyCount, replyCount: attrs.topicReplyCount,

View File

@ -7,6 +7,7 @@ require_dependency 'gaps'
class TopicView class TopicView
MEGA_TOPIC_POSTS_COUNT = 10000 MEGA_TOPIC_POSTS_COUNT = 10000
MIN_POST_READ_TIME = 4.0
attr_reader( attr_reader(
:topic, :topic,
@ -208,7 +209,13 @@ class TopicView
def read_time def read_time
return nil if @post_number > 1 # only show for topic URLs 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 end
def like_count def like_count

View File

@ -694,4 +694,21 @@ describe TopicView do
expect(topic_view.last_post_id).to eq(p3.id) expect(topic_view.last_post_id).to eq(p3.id)
end end
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 end