From 85a7b925c70f9f673ce2c59a61283ab32373c4bf Mon Sep 17 00:00:00 2001 From: riking Date: Mon, 5 Jan 2015 08:02:32 -0800 Subject: [PATCH] Miscellaneous fixes from PR#3000 FIX: Don't require login to view post raw FIX: Don't submit read-guidelines for anonymous users (causes unnecessary 403 errors from ensure_logged_in) FIX: Don't pass nil to an array serializer --- .../discourse/controllers/static.js.es6 | 4 +++- app/controllers/topics_controller.rb | 11 ++++++---- spec/controllers/posts_controller_spec.rb | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/static.js.es6 b/app/assets/javascripts/discourse/controllers/static.js.es6 index 0dd1f25b6..f2e18692d 100644 --- a/app/assets/javascripts/discourse/controllers/static.js.es6 +++ b/app/assets/javascripts/discourse/controllers/static.js.es6 @@ -3,7 +3,9 @@ export default Em.ObjectController.extend({ actions: { markFaqRead: function() { - Discourse.ajax("/users/read-faq", { method: "POST" }); + if (Discourse.User.current()) { + Discourse.ajax("/users/read-faq", { method: "POST" }); + } } } }); diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 6818f7956..721f0373c 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -55,9 +55,11 @@ class TopicsController < ApplicationController begin @topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts) rescue Discourse::NotFound - topic = Topic.find_by(slug: params[:id].downcase) if params[:id] - raise Discourse::NotFound unless topic - redirect_to_correct_topic(topic, opts[:post_number]) && return + if params[:id] + topic = Topic.find_by(slug: params[:id].downcase) + return redirect_to_correct_topic(topic, opts[:post_number]) if topic + end + raise Discourse::NotFound end page = params[:page].to_i @@ -150,8 +152,9 @@ class TopicsController < ApplicationController [:title, :raw].each { |key| check_length_of(key, params[key]) } # Only suggest similar topics if the site has a minimum amount of topics present. - topics = Topic.similar_to(title, raw, current_user).to_a if Topic.count_exceeds_minimum? + return render json: [] unless Topic.count_exceeds_minimum? + topics = Topic.similar_to(title, raw, current_user).to_a render_serialized(topics, BasicTopicSerializer) end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 651efcb36..aee2f0a01 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -799,4 +799,26 @@ describe PostsController do end + describe "view raw" do + describe "by ID" do + it "can be viewed by anonymous" do + post = Fabricate(:post, raw: "123456789") + xhr :get, :markdown_id, id: post.id + response.should be_success + response.body.should == "123456789" + end + end + + describe "by post number" do + it "can be viewed by anonymous" do + topic = Fabricate(:topic) + post = Fabricate(:post, topic: topic, post_number: 1, raw: "123456789") + post.save + xhr :get, :markdown_num, topic_id: topic.id, post_number: 1 + response.should be_success + response.body.should == "123456789" + end + end + end + end