forum topics: don't record a topic visit for api requests.

This commit is contained in:
evazion
2017-05-13 13:50:20 -05:00
parent 78b08d8394
commit 71a19c28f1
3 changed files with 13 additions and 3 deletions

View File

@@ -33,7 +33,7 @@ class ForumTopicsController < ApplicationController
end end
def show def show
unless CurrentUser.user.is_anonymous? if request.format == Mime::HTML
@forum_topic.mark_as_read!(CurrentUser.user) @forum_topic.mark_as_read!(CurrentUser.user)
end end
@forum_posts = ForumPost.search(:topic_id => @forum_topic.id).order("forum_posts.id").paginate(params[:page]) @forum_posts = ForumPost.search(:topic_id => @forum_topic.id).order("forum_posts.id").paginate(params[:page])

View File

@@ -103,8 +103,8 @@ class ForumTopic < ActiveRecord::Base
ForumTopicVisit.where("user_id = ? and forum_topic_id = ? and last_read_at >= ?", user.id, id, updated_at).exists? ForumTopicVisit.where("user_id = ? and forum_topic_id = ? and last_read_at >= ?", user.id, id, updated_at).exists?
end end
def mark_as_read!(user = nil) def mark_as_read!(user = CurrentUser.user)
user ||= CurrentUser.user return if user.is_anonymous?
match = ForumTopicVisit.where(:user_id => user.id, :forum_topic_id => id).first match = ForumTopicVisit.where(:user_id => user.id, :forum_topic_id => id).first
if match if match

View File

@@ -56,6 +56,16 @@ class ForumTopicsControllerTest < ActionController::TestCase
get :show, {:id => @forum_topic.id} get :show, {:id => @forum_topic.id}
assert_response :success assert_response :success
end end
should "record a topic visit for html requests" do
get :show, {id: @forum_topic.id}, {user_id: @user.id}
assert_not_nil(@user.reload.last_forum_read_at)
end
should "not record a topic visit for non-html requests" do
get :show, {id: @forum_topic.id, format: :json}, {user_id: @user.id}
assert_nil(@user.reload.last_forum_read_at)
end
end end
context "index action" do context "index action" do