From e18813895a26428e7388a7fc0a2cb9162ec2f59e Mon Sep 17 00:00:00 2001 From: Earlopain Date: Tue, 23 Mar 2021 15:49:00 +0100 Subject: [PATCH 1/2] [Feature] Move post favorites to seperate path This makes them visible to normal users but moves them to a seperate page. This operation is expensive, let's see if there is an easier way to fetch them. --- app/controllers/post_favorites_controller.rb | 8 ++++++ app/helpers/application_helper.rb | 2 +- app/helpers/posts_helper.rb | 4 --- app/javascript/src/javascripts/posts.js.erb | 8 ------ app/javascript/src/styles/specific/posts.scss | 5 ---- app/views/post_favorites/index.html.erb | 28 +++++++++++++++++++ .../posts/partials/show/_information.html.erb | 6 +--- config/routes.rb | 1 + public/robots.txt | 1 + 9 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 app/controllers/post_favorites_controller.rb create mode 100644 app/views/post_favorites/index.html.erb diff --git a/app/controllers/post_favorites_controller.rb b/app/controllers/post_favorites_controller.rb new file mode 100644 index 000000000..29805cf3a --- /dev/null +++ b/app/controllers/post_favorites_controller.rb @@ -0,0 +1,8 @@ +class PostFavoritesController < ApplicationController + before_action :member_only + respond_to :html + + def index + @users = ::Post.find(params[:post_id]).favorited_users + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bb43b788e..fda028af7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -285,7 +285,7 @@ protected when "notes", "note_versions" /^\/notes/ - when "posts", "uploads", "post_versions", "explore/posts", "moderator/post/dashboards", "favorites" + when "posts", "uploads", "post_versions", "explore/posts", "moderator/post/dashboards", "favorites", "post_favorites" /^\/posts/ when "artists", "artist_versions" diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb index 243e18c8d..6c5f2a861 100644 --- a/app/helpers/posts_helper.rb +++ b/app/helpers/posts_helper.rb @@ -44,10 +44,6 @@ module PostsHelper end end - def post_favlist(post) - post.favorited_users.reverse_each.map {|user| link_to_user(user)}.join(", ").html_safe - end - def has_parent_message(post, parent_post_set) html = "" diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index 756367e58..bd47beedf 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -24,7 +24,6 @@ Post.initialize_all = function() { if ($("#c-posts").length && $("#a-show").length) { this.initialize_links(); this.initialize_post_relationship_previews(); - this.initialize_favlist(); this.initialize_post_sections(); this.initialize_resize(); this.initialize_similar(); @@ -443,13 +442,6 @@ Post.toggle_relationship_preview = function(preview, preview_link) { } } -Post.initialize_favlist = function() { - $("#show-favlist-link, #hide-favlist-link").on("click.danbooru", function(e) { - $("#favlist, #show-favlist-link, #hide-favlist-link").toggle(); - e.preventDefault(); - }); -} - Post.currentPost = function() { if(!this._currentPost) this._currentPost = this.fromDOM($("#image-container")); diff --git a/app/javascript/src/styles/specific/posts.scss b/app/javascript/src/styles/specific/posts.scss index 276e2c3fe..b3db90a59 100644 --- a/app/javascript/src/styles/specific/posts.scss +++ b/app/javascript/src/styles/specific/posts.scss @@ -456,11 +456,6 @@ div#c-posts { } } - #favlist { - margin-left: 1em; - word-wrap: break-word; - } - .search-seq-nav + .pool-nav, .search-seq-nav + .set-nav, .pool-nav + .set-nav { margin-top: 0.5em; } diff --git a/app/views/post_favorites/index.html.erb b/app/views/post_favorites/index.html.erb new file mode 100644 index 000000000..ee7f1e7bf --- /dev/null +++ b/app/views/post_favorites/index.html.erb @@ -0,0 +1,28 @@ +
+
+

Post Favorites

+ + + + + + + + + <% @users.each do |user| %> + + + + <% end %> + +
Name
+ <%= link_to_user user %> +
+
+
+ +<%= render "posts/partials/common/secondary_links" %> + +<% content_for(:page_title) do %> + Post Favorites - #<%= params[:post_id] %> +<% end %> diff --git a/app/views/posts/partials/show/_information.html.erb b/app/views/posts/partials/show/_information.html.erb index 79ed420a7..a597508ce 100644 --- a/app/views/posts/partials/show/_information.html.erb +++ b/app/views/posts/partials/show/_information.html.erb @@ -41,9 +41,5 @@ <% end %>
  • Favorites: <%= post.fav_count %> - <% if CurrentUser.is_privileged? %> - <%= link_to "Show »", "#", id: "show-favlist-link", style: ("display: none;" if post.fav_count == 0) %> - <%= link_to "« Hide", "#", id: "hide-favlist-link", style: "display: none;" %> - - <% end %>
  • + <%= link_to "Show", post_favorites_path(post.id), style: ("display: none;" if post.fav_count == 0) %> diff --git a/config/routes.rb b/config/routes.rb index a7d241e06..ed136f5c6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -252,6 +252,7 @@ Rails.application.routes.draw do resources :replacements, :only => [:index, :new, :create], :controller => "post_replacements" resource :votes, :controller => "post_votes", :only => [:create, :destroy] resource :flag, controller: 'post_flags', only: [:destroy] + resources :favorites, :controller => "post_favorites", :only => [:index] collection do get :random end diff --git a/public/robots.txt b/public/robots.txt index 41f4a57d0..0b7ece9e7 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -56,6 +56,7 @@ Disallow: /posts*?*pool_id* Disallow: /posts*?*post_set_id* Disallow: /posts.json Disallow: /posts.xml +Disallow: /posts/*/favorites Disallow: /reports Disallow: /session Disallow: /sources From c5eb6ce2c343ef57a4805734bc6d16746e9cd87a Mon Sep 17 00:00:00 2001 From: Earlopain Date: Mon, 29 Mar 2021 12:23:40 +0200 Subject: [PATCH 2/2] [Feature] Add pagination to list of favorites for post This reduces the entities which need to be loaded to render the users to the per page size. - Use query to get from post to favorites - Correctly order by favorites - Remove scope from user.favorites - Order by user name - Fix template namespace --- app/controllers/post_favorites_controller.rb | 9 ++++++++- app/models/user.rb | 2 +- app/views/post_favorites/index.html.erb | 11 ++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/post_favorites_controller.rb b/app/controllers/post_favorites_controller.rb index 29805cf3a..de88e4164 100644 --- a/app/controllers/post_favorites_controller.rb +++ b/app/controllers/post_favorites_controller.rb @@ -3,6 +3,13 @@ class PostFavoritesController < ApplicationController respond_to :html def index - @users = ::Post.find(params[:post_id]).favorited_users + @post = Post.find(params[:post_id]) + query = User.includes(:user_status).joins(:favorites) + unless CurrentUser.is_admin? + query = query.where("bit_prefs & :value != :value", {value: 2**User::BOOLEAN_ATTRIBUTES.find_index("enable_privacy_mode")}).or(query.where(favorites: {user_id: CurrentUser.id})) + end + query = query.where(favorites: {post_id: @post.id}) + query = query.order("users.name asc") + @users = query.paginate(params[:page], limit: 75) end end diff --git a/app/models/user.rb b/app/models/user.rb index 427404bf7..6e51f8d41 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -122,7 +122,7 @@ class User < ApplicationRecord has_many :forum_posts, -> {order("forum_posts.created_at, forum_posts.id")}, :foreign_key => "creator_id" has_many :user_name_change_requests, -> {visible.order("user_name_change_requests.id desc")} has_many :post_sets, -> {order(name: :asc)}, foreign_key: :creator_id - has_many :favorites, ->(rec) {where("user_id = ?", rec.id).order("id desc")} + has_many :favorites, -> {order(id: :desc)} belongs_to :avatar, class_name: 'Post', optional: true accepts_nested_attributes_for :dmail_filter diff --git a/app/views/post_favorites/index.html.erb b/app/views/post_favorites/index.html.erb index ee7f1e7bf..22fc9dfe6 100644 --- a/app/views/post_favorites/index.html.erb +++ b/app/views/post_favorites/index.html.erb @@ -1,11 +1,12 @@ -
    +

    Post Favorites

    - + <%= PostPresenter.preview(@post) %> - + + @@ -14,10 +15,14 @@ + <% end %>
    NameFavorited byOther Favorites
    <%= link_to_user user %> + <%= link_to(user.favorite_count, favorites_path(:user_id => user.id)) %> +
    + <%= numbered_paginator(@users) %>