From c17678d509f35b85f1955ae559dfbc1abdb4bd4f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 24 Dec 2020 00:07:19 -0600 Subject: [PATCH] routes: add a new 404 page. * Fix a bug where non-GET 404 requests weren't handled. * Fix a bug where non-HTML 404 requests weren't handled. * Show a random image from a specified pool on the 404 page. --- app/controllers/application_controller.rb | 6 ++-- app/controllers/static_controller.rb | 10 +++++-- app/views/static/not_found.html.erb | 26 +++++++++++++++++ config/danbooru_default_config.rb | 5 ++++ config/routes.rb | 2 +- public/404.html | 15 ---------- public/500.html | 15 ---------- test/functional/static_controller_test.rb | 34 ++++++++++++++++++++++- 8 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 app/views/static/not_found.html.erb delete mode 100644 public/404.html delete mode 100644 public/500.html diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 843b72670..ad4f918b0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -124,17 +124,17 @@ class ApplicationController < ActionController::Base end end - def render_error_page(status, exception, message: exception.message, template: "static/error", format: request.format.symbol) + def render_error_page(status, exception = nil, message: exception.message, template: "static/error", format: request.format.symbol) @exception = exception @expected = status < 500 @message = message.encode("utf-8", invalid: :replace, undef: :replace) - @backtrace = Rails.backtrace_cleaner.clean(@exception.backtrace) + @backtrace = Rails.backtrace_cleaner.clean(@exception.backtrace) if @exception format = :html unless format.in?(%i[html json xml js atom]) # if InvalidAuthenticityToken was raised, CurrentUser isn't set so we have to use the blank layout. layout = CurrentUser.user.present? ? "default" : "blank" - DanbooruLogger.log(@exception, expected: @expected) + DanbooruLogger.log(@exception, expected: @expected) if @exception render template, layout: layout, status: status, formats: format rescue ActionView::MissingTemplate render "static/error", layout: layout, status: status, formats: format diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index 709be20f3..331f20a42 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -1,4 +1,6 @@ class StaticController < ApplicationController + respond_to :html, :json, :xml + def privacy_policy end @@ -6,7 +8,11 @@ class StaticController < ApplicationController end def not_found - render plain: "not found", status: :not_found + @pool = Pool.find(Danbooru.config.page_not_found_pool_id) if Danbooru.config.page_not_found_pool_id.present? + @post = @pool.posts.sample if @pool.present? + @artist = @post.tags.select(&:artist?).first if @post.present? + + render_error_page(404, nil, template: "static/not_found", message: "Page not found") end def error @@ -38,7 +44,7 @@ class StaticController < ApplicationController @search = { is_deleted: "false" } when "posts" @relation = Post.order(id: :asc) - @serach = {} + @search = {} when "tags" @relation = Tag.nonempty @search = {} diff --git a/app/views/static/not_found.html.erb b/app/views/static/not_found.html.erb new file mode 100644 index 000000000..8393490a5 --- /dev/null +++ b/app/views/static/not_found.html.erb @@ -0,0 +1,26 @@ +<% page_title "Page not found" %> + +
+
+
+

Page not found

+ + <% if @post.present? && @artist.present? %> +

+ <%= link_to @post do %> + <%= tag.img src: @post.large_file_url %> + <% end %> + +

+ <%= link_to "post ##{@post.id}", @post %> + by <%= link_to @artist.name, posts_path(tags: @artist.name), class: tag_class(@artist) %> +

+

+ <% else %> +

Nobody here but us chickens!

+ <% end %> + +

<%= link_to "Return to previous page", :back %>

+
+
+
diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index beed3096d..41fd594d9 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -281,6 +281,11 @@ module Danbooru restricted_tags + %w[censored condom nipples nude penis pussy sexually_suggestive] end + # If present, the 404 page will show a random post from this pool. + def page_not_found_pool_id + nil + end + # Tags that are only visible to Gold+ users. def restricted_tags [] diff --git a/config/routes.rb b/config/routes.rb index 94cd168a7..9ab7cacfa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -321,5 +321,5 @@ Rails.application.routes.draw do get "/mock/iqdbs/similar" => "mock_services#iqdbs_similar", as: "mock_iqdbs_similar" post "/mock/iqdbs/similar" => "mock_services#iqdbs_similar" - get "*other", :to => "static#not_found" + match "*other", to: "static#not_found", via: :all end diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 108e3337c..000000000 --- a/public/404.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - Page not found - - - - - -
-

That page does not exist

-

Return to index

-
- - diff --git a/public/500.html b/public/500.html deleted file mode 100644 index b6f61c973..000000000 --- a/public/500.html +++ /dev/null @@ -1,15 +0,0 @@ - - - - Failbooru - - - - - -
-

Something broke

-

Return to index

-
- - diff --git a/test/functional/static_controller_test.rb b/test/functional/static_controller_test.rb index faca87b71..dff4ff680 100644 --- a/test/functional/static_controller_test.rb +++ b/test/functional/static_controller_test.rb @@ -47,10 +47,42 @@ class StaticControllerTest < ActionDispatch::IntegrationTest end context "not_found action" do - should "work" do + should "return the 404 page for GET requests" do get "/qwoiqogieqg" assert_response 404 end + + should "return the 404 page for POST requests" do + post "/qwoiqogieqg" + assert_response 404 + end + + should "return a JSON response for a 404'd JSON request" do + get "/qwoiqogieqg", as: :json + + assert_response 404 + assert_equal("Page not found", response.parsed_body["message"]) + end + + should "return an XML response for a 404'd XML request" do + get "/qwoiqogieqg", as: :xml + + assert_response 404 + assert_equal("Page not found", response.parsed_body.at("result").text) + end + + should "render the 404 page when page_not_found_pool_id is configured" do + as(create(:user)) do + @post = create(:post, tag_string: "artist:bkub") + @pool = create(:pool, post_ids: [@post.id]) + Danbooru.config.stubs(:page_not_found_pool_id).returns(@pool.id) + end + + get "/qwoiqogieqg" + + assert_response 404 + assert_select "#c-static #a-not-found img", count: 1 + end end context "bookmarklet action" do