FIX: TopicViewItem and TopicUser for doc topics (#80)

When a documentation topic is viewed, corresponding TopicViewItem and TopicUser should be created.

A method from a TopicController is called, however, specs should warn us if ever that core method would change.
This commit is contained in:
Krzysztof Kotlarek 2022-02-01 16:38:54 +11:00 committed by GitHub
parent 6bad85b1ef
commit 7c54952414
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -51,9 +51,19 @@ module Docs
topic_view = TopicView.new(topic.id, current_user)
guardian = Guardian.new(current_user)
ip = request.remote_ip
user_id = (current_user.id if current_user)
track_visit = should_track_visit_to_topic?
TopicsController.defer_track_visit(topic.id, ip, user_id, track_visit)
TopicViewSerializer.new(topic_view, scope: guardian, root: false)
end
def should_track_visit_to_topic?
!!((!request.format.json? || params[:track_visit]) && current_user)
end
def set_title
title = "#{I18n.t('js.docs.title')} - #{SiteSetting.title}"
if @topic

View File

@ -31,6 +31,7 @@ Docs.reopenClass({
}
if (params.selectedTopic) {
filters.push(`topic=${params.selectedTopic}`);
filters.push("track_visit=true");
}
return ajax(`/docs.json?${filters.join("&")}`).then((data) => {

View File

@ -265,6 +265,26 @@ describe Docs::DocsController do
expect(response.parsed_body['topic']['id']).to eq(topic.id)
end
it 'should create TopicViewItem' do
admin = Fabricate(:admin)
sign_in(admin)
expect do
get "/docs.json?topic=#{topic.id}"
end.to change { TopicViewItem.count }.by(1)
end
it 'should create TopicUser if authenticated' do
expect do
get "/docs.json?topic=#{topic.id}&track_visit=true"
end.not_to change { TopicUser.count }
admin = Fabricate(:admin)
sign_in(admin)
expect do
get "/docs.json?topic=#{topic.id}&track_visit=true"
end.to change { TopicUser.count }.by(1)
end
end
end
end