diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index 0260d8a..7e116b9 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -8,6 +8,7 @@ class ContentViewerController < ApplicationController helper TagsHelper def view_page + path = get_path(params[:page], params[:format]) @version = params[:version].to_i @@ -38,7 +39,7 @@ class ContentViewerController < ApplicationController end # At this point the page will be showed - @page.hit unless user_is_a_bot? + @page.hit unless user_is_a_bot? || already_visited?(@page) @page = FilePresenter.for @page @@ -272,4 +273,18 @@ class ContentViewerController < ApplicationController @comment_order = params[:comment_order].nil? ? 'oldest' : params[:comment_order] end + private + + def already_visited?(element) + user_id = if user.nil? then -1 else current_user.id end + user_id = "#{user_id}_#{element.id}_#{element.class}" + + if cookies.signed[:visited] == user_id + return true + else + cookies.permanent.signed[:visited] = user_id + return false + end + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 3006e97..d1b76e9 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1587,4 +1587,33 @@ class ContentViewerControllerTest < ActionController::TestCase assert_tag :tag => 'div', :attributes => { :class => 'article-compact-image' } assert_tag :tag => 'div', :attributes => { :class => 'article-compact-abstract-with-image' } end + + should 'not count a visit twice for the same user' do + profile = create_user('someone').person + login_as(@profile.identifier) + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + + get :view_page, :profile => profile.identifier, :page => 'myarticle' + page.reload + assert_equal 1, page.hits + + get :view_page, :profile => profile.identifier, :page => 'myarticle' + page.reload + assert_equal 1, page.hits + end + + should 'not count a visit twice for unlogged users' do + profile = create_user('someone').person + page = profile.articles.build(:name => 'myarticle', :body => 'the body of the text') + page.save! + + get :view_page, :profile => profile.identifier, :page => 'myarticle' + page.reload + assert_equal 1, page.hits + + get :view_page, :profile => profile.identifier, :page => 'myarticle' + page.reload + assert_equal 1, page.hits + end end -- libgit2 0.21.2