FIX: Default to first page when page params is an array.

This commit is contained in:
Guo Xiang Tan 2016-02-25 11:32:16 +08:00
parent 15ce3b2f49
commit e8de80de98
3 changed files with 13 additions and 7 deletions

View file

@ -49,6 +49,10 @@ class TopicsController < ApplicationController
# existing installs. # existing installs.
return wordpress if params[:best].present? return wordpress if params[:best].present?
# work around people somehow sending in arrays,
# arrays are not supported
params[:page] = params[:page].to_i rescue 1
opts = params.slice(:username_filters, :filter, :page, :post_number, :show_deleted) opts = params.slice(:username_filters, :filter, :page, :post_number, :show_deleted)
username_filters = opts[:username_filters] username_filters = opts[:username_filters]
@ -65,7 +69,7 @@ class TopicsController < ApplicationController
raise Discourse::NotFound raise Discourse::NotFound
end end
page = params[:page].to_i page = params[:page]
if (page < 0) || ((page - 1) * @topic_view.chunk_size > @topic_view.topic.highest_post_number) if (page < 0) || ((page - 1) * @topic_view.chunk_size > @topic_view.topic.highest_post_number)
raise Discourse::NotFound raise Discourse::NotFound
end end
@ -529,7 +533,7 @@ class TopicsController < ApplicationController
url << "/#{post_number}" if post_number.to_i > 0 url << "/#{post_number}" if post_number.to_i > 0
url << ".json" if request.format.json? url << ".json" if request.format.json?
page = params[:page].to_i page = params[:page]
url << "?page=#{page}" if page != 0 url << "?page=#{page}" if page != 0
redirect_to url, status: 301 redirect_to url, status: 301

View file

@ -43,10 +43,7 @@ class TopicView
self.instance_variable_set("@#{key}".to_sym, value) self.instance_variable_set("@#{key}".to_sym, value)
end end
# work around people somehow sending in arrays, @page = 1 if (!@page || @page.zero?)
# arrays are not supported
@page = @page.to_i rescue 1
@page = 1 if @page.zero?
@chunk_size = options[:slow_platform] ? TopicView.slow_chunk_size : TopicView.chunk_size @chunk_size = options[:slow_platform] ? TopicView.slow_chunk_size : TopicView.chunk_size
@limit ||= @chunk_size @limit ||= @chunk_size
@ -183,7 +180,7 @@ class TopicView
return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present? return filter_posts_by_ids(opts[:post_ids]) if opts[:post_ids].present?
return filter_best(opts[:best], opts) if opts[:best].present? return filter_best(opts[:best], opts) if opts[:best].present?
filter_posts_paged(opts[:page].to_i) filter_posts_paged(@page)
end end
def primary_group_names def primary_group_names

View file

@ -563,6 +563,11 @@ describe TopicsController do
expect(response).to redirect_to(topic.relative_url + "/42?page=123") expect(response).to redirect_to(topic.relative_url + "/42?page=123")
end end
it 'does not accept page params as an array' do
xhr :get, :show, id: topic.slug, post_number: 42, page: [2]
expect(response).to redirect_to("#{topic.relative_url}/42?page=1")
end
it 'returns 404 when an invalid slug is given and no id' do it 'returns 404 when an invalid slug is given and no id' do
xhr :get, :show, id: 'nope-nope' xhr :get, :show, id: 'nope-nope'
expect(response.status).to eq(404) expect(response.status).to eq(404)