From de16d311351dee580fa11f282d5fb7eefe5d663b Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 3 Jan 2021 19:53:11 -0600 Subject: [PATCH] favorites: remove is_favorited attribute from post API. * Remove the data-is-favorited attribute from post thumbnails. * Remove the is_favorited attribute from the /posts.json API. * Remove the fav_string attribute from the /posts.json API (only visible to moderators). * Change `Post#favorited_by?` to not use the fav_string. Further addresses #4652 by eliminating the last places where fav_string was used. --- app/models/post.rb | 7 +++---- app/policies/post_policy.rb | 4 ++-- app/presenters/post_presenter.rb | 1 - app/views/posts/partials/index/_preview.html.erb | 6 +----- app/views/posts/partials/show/_options.html.erb | 4 ++-- app/views/posts/show.html.erb | 2 +- test/functional/posts_controller_test.rb | 1 - 7 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 4e3300515..a4bfc70e8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -762,12 +762,11 @@ class Post < ApplicationRecord update_column(:fav_count, fav_count) end - def favorited_by?(user_id = CurrentUser.id) - fav_string.match?(/(?:\A| )fav:#{user_id}(?:\Z| )/) + def favorited_by?(user) + return false if user.is_anonymous? + Favorite.exists?(post: self, user: user) end - alias is_favorited? favorited_by? - def append_user_to_fav_string(user_id) update_column(:fav_string, (fav_string + " fav:#{user_id}").strip) clean_fav_string! if clean_fav_string? diff --git a/app/policies/post_policy.rb b/app/policies/post_policy.rb index 316e7034b..3bfa5d05c 100644 --- a/app/policies/post_policy.rb +++ b/app/policies/post_policy.rb @@ -88,11 +88,11 @@ class PostPolicy < ApplicationPolicy def api_attributes attributes = super - attributes += [:has_large, :has_visible_children, :is_favorited?] + attributes += [:has_large, :has_visible_children] attributes += TagCategory.categories.map {|x| "tag_string_#{x}".to_sym} attributes += [:file_url, :large_file_url, :preview_file_url] if visible? attributes -= [:id, :md5, :file_ext] if !visible? - attributes -= [:fav_string] if !user.is_moderator? + attributes -= [:fav_string] attributes end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 60d6ba4b1..6135e1020 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -131,7 +131,6 @@ class PostPresenter "data-source" => post.source, "data-uploader-id" => post.uploader_id, "data-normalized-source" => post.normalized_source, - "data-is-favorited" => post.favorited_by?(CurrentUser.user.id) } if post.visible? diff --git a/app/views/posts/partials/index/_preview.html.erb b/app/views/posts/partials/index/_preview.html.erb index a9f00ebb1..0e03f2613 100644 --- a/app/views/posts/partials/index/_preview.html.erb +++ b/app/views/posts/partials/index/_preview.html.erb @@ -38,11 +38,7 @@ <%= link_to recommended_posts_path(search: { post_id: post.id }), class: "more-recommended-posts", "data-post-id": post.id do %> <%= post.fav_count %> - <% if post.favorited_by?(CurrentUser.id) %> - - <% else %> - - <% end %> +
more ยป <% end %> diff --git a/app/views/posts/partials/show/_options.html.erb b/app/views/posts/partials/show/_options.html.erb index d9d2949c8..7190bdf4c 100644 --- a/app/views/posts/partials/show/_options.html.erb +++ b/app/views/posts/partials/show/_options.html.erb @@ -23,10 +23,10 @@ <% if policy(Favorite).create? %>
  • - <%= link_to "Favorite", favorites_path(post_id: post.id), remote: true, method: :post, id: "add-to-favorites", "data-shortcut": "f", style: ("display: none;" if @post.is_favorited?) %> + <%= link_to "Favorite", favorites_path(post_id: post.id), remote: true, method: :post, id: "add-to-favorites", "data-shortcut": "f", style: ("display: none;" if @post.favorited_by?(CurrentUser.user)) %>
  • - <%= link_to "Unfavorite", favorite_path(post), remote: true, method: :delete, id: "remove-from-favorites", "data-shortcut": "shift+f", "data-shortcut-when": ":visible", style: ("display: none;" if !@post.is_favorited?) %> + <%= link_to "Unfavorite", favorite_path(post), remote: true, method: :delete, id: "remove-from-favorites", "data-shortcut": "shift+f", "data-shortcut-when": ":visible", style: ("display: none;" if !@post.favorited_by?(CurrentUser.user)) %>
  • <% end %> <% if policy(post).update? %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index b035b389c..a2f202f27 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -50,7 +50,7 @@ <% end -%> <% if policy(Favorite).create? %> - <%= content_tag(:div, class: "fav-buttons fav-buttons-#{@post.is_favorited?}") do %> + <%= content_tag(:div, class: "fav-buttons fav-buttons-#{@post.favorited_by?(CurrentUser.user)}") do %> <%= form_tag(favorites_path(post_id: @post.id), method: "post", id: "add-fav-button", "data-remote": true) do %> <%= button_tag tag.i(class: "far fa-heart"), class: "ui-button ui-widget ui-corner-all", "data-disable-with": tag.i(class: "fas fa-spinner fa-spin") %> <% end %> diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 18ad1ba37..af4c0b2e2 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -633,7 +633,6 @@ class PostsControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_nil(response.parsed_body["md5"]) assert_nil(response.parsed_body["file_url"]) - assert_nil(response.parsed_body["fav_string"]) end end end