From 463b1b734e47151e9ae6fceac878f1539878c82f Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 23 Feb 2020 16:43:06 -0600 Subject: [PATCH] Add curated posts page. Add a curated posts page at /explore/posts/curated. Curated posts are the most favorited posts by contributor-level users (users with unlimited upload permissions). Also add an order:curated tag using for use in regular searches. --- app/controllers/explore/posts_controller.rb | 13 +++++++++++ app/helpers/popular_posts_helper.rb | 10 +++++++++ app/javascript/src/styles/specific/posts.scss | 2 +- app/logical/post_query_builder.rb | 10 +++++++++ app/models/tag.rb | 1 + app/views/explore/posts/_nav_links.html.erb | 22 +++++-------------- .../posts/_nav_links_for_scale.html.erb | 17 ++++++++++++++ app/views/explore/posts/curated.html.erb | 15 +++++++++++++ app/views/explore/posts/popular.html.erb | 21 +++--------------- .../posts/partials/index/_related.html.erb | 1 + app/views/static/site_map.html.erb | 1 + config/routes.rb | 1 + 12 files changed, 79 insertions(+), 35 deletions(-) create mode 100644 app/views/explore/posts/_nav_links_for_scale.html.erb create mode 100644 app/views/explore/posts/curated.html.erb diff --git a/app/controllers/explore/posts_controller.rb b/app/controllers/explore/posts_controller.rb index 5a7ad1461..baf67ebad 100644 --- a/app/controllers/explore/posts_controller.rb +++ b/app/controllers/explore/posts_controller.rb @@ -11,6 +11,15 @@ module Explore 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) + + respond_with(@posts) + end + def viewed @date, @scale, @min_date, @max_date = parse_date(params) @posts = PostViewCountService.new.popular_posts(@date) @@ -40,5 +49,9 @@ module Explore def popular_posts(min_date, max_date) Post.where(created_at: min_date..max_date).tag_match("order:score") end + + def curated_posts(min_date, max_date) + Post.where(created_at: min_date..max_date).tag_match("order:curated") + end end end diff --git a/app/helpers/popular_posts_helper.rb b/app/helpers/popular_posts_helper.rb index 82a4721d4..5d655c7bf 100644 --- a/app/helpers/popular_posts_helper.rb +++ b/app/helpers/popular_posts_helper.rb @@ -20,4 +20,14 @@ module PopularPostsHelper 1.month.ago(date) end end + + def date_range_description(date, scale, min_date, max_date) + if scale == "day" + date.strftime("%B %d, %Y") + elsif scale == "week" + "#{min_date.strftime("%B %d, %Y")} - #{max_date.strftime("%B %d, %Y")}" + elsif scale == "month" + date.strftime("%B %Y") + end + end end diff --git a/app/javascript/src/styles/specific/posts.scss b/app/javascript/src/styles/specific/posts.scss index fdbb5ac33..42a1f929e 100644 --- a/app/javascript/src/styles/specific/posts.scss +++ b/app/javascript/src/styles/specific/posts.scss @@ -473,7 +473,7 @@ div#c-explore-posts { margin: 0 0.5em; } - #popular-nav-links { + .date-nav-links { text-align: center; } diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 6355da35d..d5b2a64bc 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -596,6 +596,16 @@ class PostQueryBuilder when "rank" relation = relation.order(Arel.sql("log(3, posts.score) + (extract(epoch from posts.created_at) - extract(epoch from timestamp '2005-05-24')) / 35000 DESC")) + when "curated" + contributors = User.bit_prefs_match(:can_upload_free, true) + + relation = relation + .joins(:favorites) + .where(favorites: { user: contributors }) + .group("posts.id") + .select("posts.*, COUNT(*) AS contributor_fav_count") + .order("contributor_fav_count DESC, posts.fav_count DESC, posts.id DESC") + when "custom" if q[:post_id].present? && q[:post_id][0] == :in relation = relation.find_ordered(q[:post_id][1]) diff --git a/app/models/tag.rb b/app/models/tag.rb index 50e2013ae..e6b3e4b03 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -37,6 +37,7 @@ class Tag < ApplicationRecord filesize filesize_asc tagcount tagcount_asc rank + curated random custom ] + diff --git a/app/views/explore/posts/_nav_links.html.erb b/app/views/explore/posts/_nav_links.html.erb index c2f19b938..18f47e56b 100644 --- a/app/views/explore/posts/_nav_links.html.erb +++ b/app/views/explore/posts/_nav_links.html.erb @@ -1,17 +1,7 @@ -<%# date, selected_scale, scale %> +<%# path, date, scale, %> - - <% if selected_scale == scale %> - <%= link_to "« prev", popular_explore_posts_path(date: prev_date_for_scale(date, scale), scale: scale), id: "paginator-prev", rel: "prev", "data-shortcut": "a left" %> - <% else %> - <%= link_to "« prev", popular_explore_posts_path(date: prev_date_for_scale(date, scale), scale: scale) %> - <% end %> - - <%= link_to scale.capitalize, popular_explore_posts_path(date: date, scale: scale), class: "desc" %> - - <% if selected_scale == scale %> - <%= link_to "next »", popular_explore_posts_path(date: next_date_for_scale(date, scale), scale: scale), id: "paginator-next", rel: "next", "data-shortcut": "d right" %> - <% else %> - <%= link_to "next »", popular_explore_posts_path(date: next_date_for_scale(date, scale), scale: scale) %> - <% end %> - + diff --git a/app/views/explore/posts/_nav_links_for_scale.html.erb b/app/views/explore/posts/_nav_links_for_scale.html.erb new file mode 100644 index 000000000..e50267610 --- /dev/null +++ b/app/views/explore/posts/_nav_links_for_scale.html.erb @@ -0,0 +1,17 @@ +<%# path, date, selected_scale, scale %> + + + <% if selected_scale == scale %> + <%= link_to "« prev", send(path, date: prev_date_for_scale(date, scale), scale: scale), id: "paginator-prev", rel: "prev", "data-shortcut": "a left" %> + <% else %> + <%= link_to "« prev", send(path, date: prev_date_for_scale(date, scale), scale: scale) %> + <% end %> + + <%= link_to scale.capitalize, send(path, date: date, scale: scale), class: "desc" %> + + <% if selected_scale == scale %> + <%= link_to "next »", send(path, date: next_date_for_scale(date, scale), scale: scale), id: "paginator-next", rel: "next", "data-shortcut": "d right" %> + <% else %> + <%= link_to "next »", send(path, date: next_date_for_scale(date, scale), scale: scale) %> + <% end %> + diff --git a/app/views/explore/posts/curated.html.erb b/app/views/explore/posts/curated.html.erb new file mode 100644 index 000000000..618e1ffbe --- /dev/null +++ b/app/views/explore/posts/curated.html.erb @@ -0,0 +1,15 @@ +<% page_title "Curated Posts" %> +<% meta_description "The best posts of #{date_range_description(@date, @scale, @min_date, @max_date)}" %> + +<%= render "posts/partials/common/secondary_links" %> + +
+
+

Curated: <%= date_range_description(@date, @scale, @min_date, @max_date) %>

+ + <%= render "explore/posts/nav_links", path: :curated_explore_posts_path, date: @date, scale: @scale %> + <%= render "posts/partials/common/inline_blacklist" %> + + <%= post_previews_html(@posts) %> +
+
diff --git a/app/views/explore/posts/popular.html.erb b/app/views/explore/posts/popular.html.erb index 8ab8f4c5f..e6efc8010 100644 --- a/app/views/explore/posts/popular.html.erb +++ b/app/views/explore/posts/popular.html.erb @@ -1,28 +1,13 @@ <% page_title "Popular Posts" %> -<% meta_description "The most popular posts per #{@scale}" %> +<% meta_description "The most popular posts of #{date_range_description(@date, @scale, @min_date, @max_date)}" %> <%= render "posts/partials/common/secondary_links" %>