Skip to content

Commit

Permalink
FIX: Don't use topic reply count as answer count in 'Question' schema
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Feb 19, 2019
1 parent 93c08c8 commit 3b7baa1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
3 changes: 2 additions & 1 deletion plugin.rb
Expand Up @@ -238,7 +238,7 @@ def before_head_close_meta(controller)
'name' => topic.title,
'text' => first_post.excerpt,
'upvoteCount' => first_post.like_count,
'answerCount' => topic.reply_count,
'answerCount' => 0,
'dateCreated' => topic.created_at,
'author' => {
'@type' => 'Person',
Expand All @@ -247,6 +247,7 @@ def before_head_close_meta(controller)
}

if accepted_answer = Post.find_by(id: topic.custom_fields["accepted_answer_post_id"])
question_json['answerCount'] = 1
question_json[:acceptedAnswer] = {
'@type' => 'Answer',
'text' => accepted_answer.excerpt,
Expand Down
43 changes: 29 additions & 14 deletions spec/requests/topics_controller_spec.rb
Expand Up @@ -5,31 +5,46 @@
let(:topic) { p1.topic }
let(:p2) { Fabricate(:post, like_count: 2, topic: topic, user: Fabricate(:user)) }

def schema_json(answerCount)
if answerCount > 0
answer_json = ',"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"dateCreated":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}"}}' % {
answer_text: p2.excerpt,
answer_likes: p2.like_count,
answered_at: p2.created_at.as_json,
answer_url: p2.full_url,
username2: p2.user&.username
}
else
answer_json = ""
end

'<script type="application/ld+json">{"@context":"http://schema.org","@type":"QAPage","name":"%{title}","mainEntity":{"@type":"Question","name":"%{title}","text":"%{question_text}","upvoteCount":%{question_likes},"answerCount":%{answerCount},"dateCreated":"%{created_at}","author":{"@type":"Person","name":"%{username1}"}%{answer_json}}}</script>' % {
title: topic.title,
question_text: p1.excerpt,
question_likes: p1.like_count,
answerCount: answerCount,
created_at: topic.created_at.as_json,
username1: topic.user&.name,
answer_json: answer_json
}
end

before do
SiteSetting.allow_solved_on_all_topics = true
end

it 'should include correct schema information in header' do
get "/t/#{topic.slug}/#{topic.id}"

expect(response.body).to include(schema_json(0))

p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields

topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields

get "/t/#{topic.slug}/#{topic.id}"

expect(response.body).to include('<script type="application/ld+json">{"@context":"http://schema.org","@type":"QAPage","name":"%{title}","mainEntity":{"@type":"Question","name":"%{title}","text":"%{question_text}","upvoteCount":%{question_likes},"answerCount":%{reply_count},"dateCreated":"%{created_at}","author":{"@type":"Person","name":"%{username1}"},"acceptedAnswer":{"@type":"Answer","text":"%{answer_text}","upvoteCount":%{answer_likes},"dateCreated":"%{answered_at}","url":"%{answer_url}","author":{"@type":"Person","name":"%{username2}"}}}}</script>' % {
title: topic.title,
question_text: p1.excerpt,
question_likes: p1.like_count,
reply_count: topic.reply_count,
created_at: topic.created_at.as_json,
username1: topic.user&.name,
answer_text: p2.excerpt,
answer_likes: p2.like_count,
answered_at: p2.created_at.as_json,
answer_url: p2.full_url,
username2: p2.user&.username
})
expect(response.body).to include(schema_json(1))
end
end

0 comments on commit 3b7baa1

Please sign in to comment.