[ForumTopics] Fix page number mismatch (#675)

Fixes an issue introduced in #674.
This commit is contained in:
Donovan Daniels 2024-07-20 11:23:26 -05:00 committed by GitHub
parent 8dea243a58
commit b28bb5c99f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 7 deletions

View File

@ -42,7 +42,7 @@ class ForumTopicsController < ApplicationController
if request.format == Mime::Type.lookup("text/html")
@forum_topic.mark_as_read!(CurrentUser.user)
end
@forum_posts = ForumPost.includes(topic: [:category]).search(:topic_id => @forum_topic.id).reorder("forum_posts.id").paginate(params[:page])
@forum_posts = ForumPost.permitted(CurrentUser.user).includes(topic: [:category]).search(topic_id: @forum_topic.id).reorder("forum_posts.id").paginate(params[:page])
@original_forum_post_id = @forum_topic.original_post.id
respond_with(@forum_topic)
end

View File

@ -47,18 +47,22 @@ class ForumPost < ApplicationRecord
end
def visible(user)
active(user).permitted(user)
end
def permitted(user)
q = joins(topic: :category).where("forum_categories.can_view <= ?", user.level)
unless user.is_moderator?
q = q.where("forum_topics.is_hidden = FALSE OR forum_topics.creator_id = ?", user.id)
q = q.where("forum_posts.is_hidden = FALSE OR forum_posts.creator_id = ?", user.id)
end
q = q.joins(:topic).where("forum_topics.is_hidden = FALSE OR forum_topics.creator_id = ?", user.id) if user.is_moderator?
q
end
def active(user)
return all if user.is_moderator?
where("forum_posts.is_hidden = FALSE OR forum_posts.creator_id = ?", user.id)
end
def search(params)
q = super
q = q.visible(CurrentUser.user)
q = q.where_user(:creator_id, :creator, params)
if params[:topic_id].present?

View File

@ -31,6 +31,26 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
@user.reload
assert_nil(@user.last_forum_read_at)
end
should "have the correct page number" do
Danbooru.config.stubs(:records_per_page).returns(2)
assert_equal(1, @forum_topic.last_page)
as(@user) { @forum_posts = create_list(:forum_post, 3, topic: @forum_topic) }
assert_equal(2, @forum_topic.last_page)
get_auth forum_topic_path(@forum_topic), @user, params: { page: 2 }
assert_select "#forum_post_#{@forum_posts.second.id}"
assert_select "#forum_post_#{@forum_posts.third.id}"
assert_equal([1, 2, 2], @forum_posts.map(&:forum_topic_page))
assert_equal(2, @forum_topic.last_page)
as(@mod) { @forum_posts.first.hide! }
get_auth forum_topic_path(@forum_topic), @user, params: { page: 2 }
assert_select "#forum_post_#{@forum_posts.second.id}"
assert_select "#forum_post_#{@forum_posts.third.id}"
assert_equal([1, 2, 2], @forum_posts.map(&:forum_topic_page))
assert_equal(2, @forum_topic.last_page)
end
end
context "index action" do