add most viewed posts page

This commit is contained in:
r888888888
2017-11-10 16:21:01 -08:00
parent 6c4eb62957
commit 8b1fca4662
8 changed files with 109 additions and 1 deletions

View File

@@ -8,6 +8,12 @@ module Explore
respond_with(@posts) respond_with(@posts)
end end
def viewed
@post_set = PostSets::MostViewed.new(params[:date])
@posts = @post_set.posts
respond_with(@posts)
end
def searches def searches
@date = params[:date] ? Date.parse(params[:date]) : Date.today @date = params[:date] ? Date.parse(params[:date]) : Date.today
@search_service = PopularSearchService.new(@date) @search_service = PopularSearchService.new(@date)

View File

@@ -0,0 +1,17 @@
module PostSets
class MostViewed < PostSets::Base
attr_reader :date
def initialize(date)
@date = date.blank? ? Time.zone.now : Time.zone.parse(date)
end
def posts
@posts ||= PostViewCountService.new.popular_posts(date)
end
def presenter
::PostSetPresenters::MostViewed.new(self)
end
end
end

View File

@@ -4,7 +4,7 @@ class PostViewCountService
end end
def initialize def initialize
if !MissedSearchService.enabled? if !PostViewCountService.enabled?
raise NotImplementedError.new("the Reportbooru service isn't configured. Missed searches are not available.") raise NotImplementedError.new("the Reportbooru service isn't configured. Missed searches are not available.")
end end
end end
@@ -30,4 +30,9 @@ class PostViewCountService
rescue JSON::ParserError rescue JSON::ParserError
nil nil
end end
def popular_posts(date = Date.today)
ranking = fetch_rank(date)
ranking.map {|x| Post.find(x[0])}
end
end end

View File

@@ -0,0 +1,54 @@
module PostSetPresenters
class MostViewed < Base
attr_accessor :post_set, :tag_set_presenter
delegate :posts, :date, :to => :post_set
def initialize(post_set)
@post_set = post_set
end
def prev_day
date - 1.day
end
def next_day
date + 1.day
end
def nav_links_for_scale(template)
html = []
html << '<span class="period">'
html << template.link_to(
"< Back".html_safe,
template.viewed_explore_posts_path(
:date => prev_day
),
:rel => "prev"
)
html << template.link_to(
date.to_s,
template.viewed_explore_posts_path(
:date => date
),
:class => "desc"
)
html << template.link_to(
"Next >".html_safe,
template.viewed_explore_posts_path(
:date => next_day
),
:rel => "next"
)
html << '</span>'
html.join("\n").html_safe
end
def nav_links(template)
html = []
html << '<p id="popular-nav-links">'
html << nav_links_for_scale(template)
html << '</p>'
html.join("\n").html_safe
end
end
end

View File

@@ -0,0 +1,21 @@
<div id="c-explore-posts">
<div id="a-popular">
<h1>Most Viewed: <%= @post_set.presenter.date %></h1>
<%= @post_set.presenter.nav_links(self) %>
<%= render "posts/partials/common/inline_blacklist" %>
<%= @post_set.presenter.post_previews_html(self) %>
</div>
</div>
<%= render "posts/partials/common/secondary_links" %>
<% content_for(:page_title) do %>
Most Viewed - <%= Danbooru.config.app_name %>
<% end %>
<% content_for(:html_header) do %>
<meta name="description" content="The most viewed posts per day">
<% end %>

View File

@@ -12,6 +12,8 @@
<li><%= link_to("Appeals", post_appeals_path) %></li> <li><%= link_to("Appeals", post_appeals_path) %></li>
<li><%= link_to("Flags", post_flags_path) %></li> <li><%= link_to("Flags", post_flags_path) %></li>
<li><%= link_to("Replacements", post_replacements_path) %></li> <li><%= link_to("Replacements", post_replacements_path) %></li>
<li><%= link_to("Popular", popular_explore_posts_path) %></li>
<li><%= link_to("Most Viewed", viewed_explore_posts_path) %></li>
<% if CurrentUser.can_approve_posts? %> <% if CurrentUser.can_approve_posts? %>
<li><%= link_to("Moderate", moderator_post_queue_path) %></li> <li><%= link_to("Moderate", moderator_post_queue_path) %></li>
<% end %> <% end %>

View File

@@ -51,6 +51,7 @@ Rails.application.routes.draw do
resources :posts do resources :posts do
collection do collection do
get :popular get :popular
get :viewed
get :searches get :searches
get :missed_searches get :missed_searches
get :intro get :intro

View File

@@ -0,0 +1,2 @@
class PostViewCountServiceTest < ActiveSupport::TestCase
end