Files
danbooru/app/logical/reportbooru_service.rb
evazion 101e8a13b8 /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).
2020-06-16 12:58:41 -05:00

49 lines
1.4 KiB
Ruby

class ReportbooruService
attr_reader :http, :reportbooru_server
def initialize(http: Danbooru::Http.new, reportbooru_server: Danbooru.config.reportbooru_server)
@reportbooru_server = reportbooru_server
@http = http
end
def enabled?
reportbooru_server.present?
end
def missed_search_rankings(expires_in: 1.minutes)
return [] unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/missed_searches")
return [] if response.status != 200
body = response.to_s.force_encoding("utf-8")
body.lines.map(&:split).map { [_1, _2.to_i] }
end
def post_search_rankings(date = Date.today, expires_in: 1.minutes)
return [] unless enabled?
response = http.cache(expires_in).get("#{reportbooru_server}/post_searches/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s.force_encoding("utf-8"))
end
def post_view_rankings(date = Date.today, expires_in: 1.minutes)
return [] unless enabled?
response = http.get("#{reportbooru_server}/post_views/rank?date=#{date}")
return [] if response.status != 200
JSON.parse(response.to_s.force_encoding("utf-8"))
end
def popular_searches(date = Date.today, limit: 100)
ranking = post_search_rankings(date)
ranking.take(limit).map(&:first)
end
def popular_posts(date = Date.today, limit: 100)
ranking = post_view_rankings(date)
ranking.take(limit).map { |x| Post.find(x[0]) }
end
end