From 101e8a13b818cf60167a0152ea2f0c9564ffb7ad Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 16 Jun 2020 12:44:51 -0500 Subject: [PATCH] /posts: fix blank tag sidebars. Fix the sidebar on the /posts index page sometimes being blank. This could happen when either the related tag calculation was too slow and timed out, or when Reporbooru was unavailable and we couldn't fetch the list of popular tags. In the tag list would otherwise be blank, we fall back to frequent tags (the most common tags on the current page of results). Also change it so that if Reportbooru is unconfigured, we fail gracefully by returning blank results instead of failing with an exception. This is so we can still view the popular searches and missed searches pages during testing (even though they'll be blank). --- app/logical/post_sets/post.rb | 14 +++----------- app/logical/reportbooru_service.rb | 7 +++---- app/views/posts/partials/index/_related.html.erb | 6 ++---- app/views/static/site_map.html.erb | 6 ++---- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index 88c415c13..2858b9b64 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -160,20 +160,16 @@ module PostSets elsif query.is_metatag?(:search) saved_search_tags elsif query.is_empty_search? || query.is_metatag?(:order, :rank) - popular_tags + popular_tags.presence || frequent_tags elsif query.is_single_term? - similar_tags + similar_tags.presence || frequent_tags else frequent_tags end end def popular_tags - if reportbooru_service.enabled? - reportbooru_service.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS).map(&:first) - else - frequent_tags - end + ReportbooruService.new.popular_searches(Date.today, limit: MAX_SIDEBAR_TAGS) end def similar_tags @@ -199,10 +195,6 @@ module PostSets def tag_list_html(**options) tag_set_presenter.tag_list_html(name_only: query.is_metatag?(:search), **options) end - - def reportbooru_service - @reportbooru_service ||= ReportbooruService.new - end end end end diff --git a/app/logical/reportbooru_service.rb b/app/logical/reportbooru_service.rb index 83c8bc895..5da79d724 100644 --- a/app/logical/reportbooru_service.rb +++ b/app/logical/reportbooru_service.rb @@ -11,7 +11,7 @@ class ReportbooruService end def missed_search_rankings(expires_in: 1.minutes) - raise NotImplementedError, "Reportbooru not configured, missed searches not available." unless enabled? + return [] unless enabled? response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches") return [] if response.status != 200 @@ -21,7 +21,7 @@ class ReportbooruService end def post_search_rankings(date = Date.today, expires_in: 1.minutes) - raise NotImplementedError, "Reportbooru not configured, popular searches not available." unless enabled? + return [] unless enabled? response = http.cache(expires_in).get("#{reportbooru_server}/post_searches/rank?date=#{date}") return [] if response.status != 200 @@ -29,7 +29,7 @@ class ReportbooruService end def post_view_rankings(date = Date.today, expires_in: 1.minutes) - raise NotImplementedError, "Reportbooru not configured, post views not available." unless enabled? + return [] unless enabled? response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}") return [] if response.status != 200 @@ -38,7 +38,6 @@ class ReportbooruService def popular_searches(date = Date.today, limit: 100) ranking = post_search_rankings(date) - ranking = post_search_rankings(date.yesterday) if ranking.blank? ranking.take(limit).map(&:first) end diff --git a/app/views/posts/partials/index/_related.html.erb b/app/views/posts/partials/index/_related.html.erb index c979869b8..cf0461b3f 100644 --- a/app/views/posts/partials/index/_related.html.erb +++ b/app/views/posts/partials/index/_related.html.erb @@ -5,10 +5,8 @@ - <% if ReportbooruService.enabled? %> -
  • <%= link_to "Searches", searches_explore_posts_path %>
  • -
  • <%= link_to "Viewed", viewed_explore_posts_path %>
  • - <% end %> +
  • <%= link_to "Searches", searches_explore_posts_path %>
  • +
  • <%= link_to "Viewed", viewed_explore_posts_path %>
  • <% end %>
  • <%= link_to "Deleted", posts_path(tags: "#{params[:tags]} status:deleted") %>
  • diff --git a/app/views/static/site_map.html.erb b/app/views/static/site_map.html.erb index 078b9ec7d..bcbfe1208 100644 --- a/app/views/static/site_map.html.erb +++ b/app/views/static/site_map.html.erb @@ -72,10 +72,8 @@