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 { 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,

View File

@ -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

View File

@ -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