From 6ca9b65716e15d88d8d94f6a175297000a68c322 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Thu, 11 Oct 2018 10:13:34 -0700 Subject: [PATCH] add dynamic sitemap.xml --- app/controllers/static_controller.rb | 8 ++++- app/logical/post_sets/popular.rb | 9 ++--- app/views/static/sitemap.xml.erb | 41 +++++++++++++++++++++++ config/routes.rb | 1 + public/sitemap.xml | 49 ---------------------------- 5 files changed, 52 insertions(+), 56 deletions(-) create mode 100644 app/views/static/sitemap.xml.erb delete mode 100644 public/sitemap.xml diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index d7e902b5c..cb69e00f8 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -17,5 +17,11 @@ class StaticController < ApplicationController def site_map end - + + def sitemap + @popular_search_service = PopularSearchService.new(Date.today) + @post_set = PostSets::Popular.new(Date.today.to_s, "week", limit: 100) + @posts = @post_set.posts + render layout: false + end end diff --git a/app/logical/post_sets/popular.rb b/app/logical/post_sets/popular.rb index f5f2df4f8..6e7a0c427 100644 --- a/app/logical/post_sets/popular.rb +++ b/app/logical/post_sets/popular.rb @@ -1,10 +1,11 @@ module PostSets class Popular < PostSets::Base - attr_reader :date, :scale + attr_reader :date, :scale, :limit - def initialize(date, scale) + def initialize(date, scale, limit: nil) @date = date.blank? ? Time.zone.now : Time.zone.parse(date) @scale = scale + @limit = limit || CurrentUser.per_page end def posts @@ -15,10 +16,6 @@ module PostSets end end - def limit - CurrentUser.user.per_page - end - def min_date case scale when "week" diff --git a/app/views/static/sitemap.xml.erb b/app/views/static/sitemap.xml.erb new file mode 100644 index 000000000..79dfe190b --- /dev/null +++ b/app/views/static/sitemap.xml.erb @@ -0,0 +1,41 @@ + + + + + <%= posts_url %> + daily + + + <%= wiki_pages_url %> + daily + + + <%= pools_url %> + daily + + + <%# cache("sitemap", :expires_in => 24.hours) do %> + <% @popular_search_service.each_search do |tags, count| %> + + <%= posts_url(tags: tags) %> + <%= Date.today %> + + <% end %> + + <% @posts.each do |post| %> + + <%= post_url(post) %> + + + <%= post.file_url %> + + + <%= post.presenter.humanized_essential_tag_string %> + + + <%= post.created_at.to_date %> + + <% end %> + <%# end %> + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 0821ed676..ad3bbea85 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -421,6 +421,7 @@ Rails.application.routes.draw do get "/wiki/recent_changes" => redirect {|params, req| "/wiki_page_versions?search[updater_id]=#{req.params[:user_id]}"} get "/wiki/history/:title" => redirect("/wiki_page_versions?title=%{title}") + get "/sitemap" => "static#sitemap" get "/static/keyboard_shortcuts" => "static#keyboard_shortcuts", :as => "keyboard_shortcuts" get "/static/bookmarklet" => "static#bookmarklet", :as => "bookmarklet" get "/static/site_map" => "static#site_map", :as => "site_map" diff --git a/public/sitemap.xml b/public/sitemap.xml deleted file mode 100644 index 3ec56a3ba..000000000 --- a/public/sitemap.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - http://danbooru.donmai.us/posts - hourly - 1.0 - - - http://danbooru.donmai.us/posts?tags=kantai_collection - hourly - 0.7 - - - http://danbooru.donmai.us/posts?tags=touhou - hourly - 0.7 - - - http://danbooru.donmai.us/posts?tags=kemono_friends - daily - 0.3 - - - http://danbooru.donmai.us/posts?tags=fate%2fgrand_order - daily - 0.3 - - - http://danbooru.donmai.us/posts?tags=pokemon - daily - 0.3 - - - http://danbooru.donmai.us/posts?tags=overwatch - daily - 0.3 - - - http://danbooru.donmai.us/wiki_pages - daily - 0.3 - - - http://danbooru.donmai.us/pools - daily - 0.5 - - \ No newline at end of file