Files
danbooru/app/controllers/explore/posts_controller.rb
evazion 7a1efc2744 Fix #4522: Sidebar doesn't show most searched tags at certain times of day.
Revert back to previous workaround of fetching previous day if current
day returns no result. A terrible hack, really we should convert dates
to Reportbooru's timezone, but that has other complications.
2020-06-19 14:00:50 -05:00

58 lines
1.7 KiB
Ruby

module Explore
class PostsController < ApplicationController
respond_to :html, :xml, :json
def popular
@date, @scale, @min_date, @max_date = parse_date(params)
limit = params.fetch(:limit, CurrentUser.user.per_page)
@posts = popular_posts(@min_date, @max_date).paginate(params[:page], limit: limit, search_count: false)
respond_with(@posts)
end
def curated
@date, @scale, @min_date, @max_date = parse_date(params)
limit = params.fetch(:limit, CurrentUser.user.per_page)
@posts = curated_posts(@min_date, @max_date).paginate(params[:page], limit: limit, search_count: false)
respond_with(@posts)
end
def viewed
@date, @scale, @min_date, @max_date = parse_date(params)
@posts = ReportbooruService.new.popular_posts(@date)
respond_with(@posts)
end
def searches
@date, @scale, @min_date, @max_date = parse_date(params)
@searches = ReportbooruService.new.popular_searches(@date)
end
def missed_searches
@missed_searches = ReportbooruService.new.missed_search_rankings
end
private
def parse_date(params)
date = params[:date].present? ? Date.parse(params[:date]) : Date.today
scale = params[:scale].in?(["day", "week", "month"]) ? params[:scale] : "day"
min_date = date.send("beginning_of_#{scale}")
max_date = date.send("next_#{scale}").send("beginning_of_#{scale}")
[date, scale, min_date, max_date]
end
def popular_posts(min_date, max_date)
Post.where(created_at: min_date..max_date).user_tag_match("order:score")
end
def curated_posts(min_date, max_date)
Post.where(created_at: min_date..max_date).user_tag_match("order:curated")
end
end
end