users: rework privacy mode into private favorites (fix #4257).

* Rename 'privacy mode' to 'private favorites'.
* Make the private favorites setting only hide favorites, not favgroups
  and not the user's uploads on their profile page.
* Make the favgroup is_public flag default to true instead of false and
  fix existing favgroups to be public if the user didn't have privacy mode
  enabled before.
* List _all_ public favgroups on the /favorite_groups index, not just
  favgroups belonging to the current user.
* Add a /users/<id>/favorite_groups endpoint.
This commit is contained in:
evazion
2020-01-17 19:59:47 -06:00
parent 2095dd3084
commit 4a7322b197
15 changed files with 44 additions and 38 deletions

View File

@@ -3,6 +3,7 @@ class FavoriteGroupsController < ApplicationController
respond_to :html, :xml, :json, :js respond_to :html, :xml, :json, :js
def index def index
params[:search][:creator_id] ||= params[:user_id]
@favorite_groups = FavoriteGroup.paginated_search(params) @favorite_groups = FavoriteGroup.paginated_search(params)
respond_with(@favorite_groups) respond_with(@favorite_groups)
end end

View File

@@ -109,7 +109,7 @@ class UsersController < ApplicationController
time_zone per_page custom_style theme time_zone per_page custom_style theme
receive_email_notifications always_resize_images enable_post_navigation receive_email_notifications always_resize_images enable_post_navigation
new_post_navigation_layout enable_privacy_mode new_post_navigation_layout enable_private_favorites
enable_sequential_post_navigation hide_deleted_posts style_usernames enable_sequential_post_navigation hide_deleted_posts style_usernames
enable_auto_complete show_deleted_children enable_auto_complete show_deleted_children
disable_categorized_saved_searches disable_tagged_filenames disable_categorized_saved_searches disable_tagged_filenames

View File

@@ -21,14 +21,8 @@ class FavoriteGroup < ApplicationRecord
where_ilike(:name, name) where_ilike(:name, name)
end end
def hide_private(user, params) def visible(user)
if user.hide_favorites? where(is_public: true).or(where(creator_id: user.id))
where("is_public = true")
elsif params[:is_public].present?
where("is_public = ?", params[:is_public])
else
all
end
end end
def default_order def default_order
@@ -37,20 +31,8 @@ class FavoriteGroup < ApplicationRecord
def search(params) def search(params)
q = super q = super
q = q.search_attributes(params, :name, :is_public, :post_ids) q = q.visible(CurrentUser.user)
q = q.search_attributes(params, :name, :is_public, :post_ids, :creator)
if params[:creator_id].present?
user = User.find(params[:creator_id])
q = q.hide_private(user, params)
q = q.where("creator_id = ?", user.id)
elsif params[:creator_name].present?
user = User.find_by_name(params[:creator_name])
q = q.hide_private(user, params)
q = q.where("creator_id = ?", user.id)
else
q = q.hide_private(CurrentUser.user, params)
q = q.where("creator_id = ?", CurrentUser.user.id)
end
if params[:name_matches].present? if params[:name_matches].present?
q = q.name_matches(params[:name_matches]) q = q.name_matches(params[:name_matches])
@@ -177,6 +159,6 @@ class FavoriteGroup < ApplicationRecord
end end
def viewable_by?(user) def viewable_by?(user)
creator_id == user.id || !creator.hide_favorites? || is_public creator_id == user.id || is_public
end end
end end

View File

@@ -40,7 +40,7 @@ class User < ApplicationRecord
always_resize_images always_resize_images
enable_post_navigation enable_post_navigation
new_post_navigation_layout new_post_navigation_layout
enable_privacy_mode enable_private_favorites
enable_sequential_post_navigation enable_sequential_post_navigation
hide_deleted_posts hide_deleted_posts
style_usernames style_usernames
@@ -653,7 +653,7 @@ class User < ApplicationRecord
end end
def favorite_group_count def favorite_group_count
favorite_groups.count favorite_groups.visible(CurrentUser.user).count
end end
def appeal_count def appeal_count
@@ -792,7 +792,7 @@ class User < ApplicationRecord
end end
def hide_favorites? def hide_favorites?
!CurrentUser.is_admin? && enable_privacy_mode? && CurrentUser.user.id != id !CurrentUser.is_admin? && enable_private_favorites? && CurrentUser.user.id != id
end end
def initialize_attributes def initialize_attributes

View File

@@ -88,7 +88,7 @@ class UserPresenter
end end
def favorite_group_count(template) def favorite_group_count(template)
template.link_to(user.favorite_group_count, template.favorite_groups_path(:search => {:creator_id => user.id})) template.link_to(user.favorite_group_count, template.user_favorite_groups_path(user.id))
end end
def comment_count(template) def comment_count(template)

View File

@@ -13,6 +13,8 @@
<% end %> <% end %>
<% end %> <% end %>
<%= numbered_paginator(@favorite_groups) %>
<%= render "secondary_links" %> <%= render "secondary_links" %>
</div> </div>
</div> </div>

View File

@@ -7,7 +7,7 @@
<% end %> <% end %>
<% unless CurrentUser.is_anonymous? %> <% unless CurrentUser.is_anonymous? %>
<%= subnav_link_to "Favorites", posts_path(tags: "ordfav:#{CurrentUser.user.name}") %> <%= subnav_link_to "Favorites", posts_path(tags: "ordfav:#{CurrentUser.user.name}") %>
<%= subnav_link_to "Fav groups", favorite_groups_path %> <%= subnav_link_to "Fav groups", user_favorite_groups_path(CurrentUser.id) %>
<%= subnav_link_to "Saved searches", posts_path(tags: "search:all") %> <%= subnav_link_to "Saved searches", posts_path(tags: "search:all") %>
<% end %> <% end %>
<%= subnav_link_to "Changes", post_versions_path %> <%= subnav_link_to "Changes", post_versions_path %>

View File

@@ -11,7 +11,7 @@
</div> </div>
<% end %> <% end %>
<% if presenter.has_favorites? %> <% if presenter.has_favorites? && !user.hide_favorites? %>
<div class="box user-favorites"> <div class="box user-favorites">
<h2> <h2>
<%= link_to "Favorites", posts_path(tags: "ordfav:#{user.name}") %> <%= link_to "Favorites", posts_path(tags: "ordfav:#{user.name}") %>

View File

@@ -57,7 +57,7 @@
<%= f.input :show_deleted_children, :as => :select, :label => "Show deleted children", :hint => "Show thumbnail borders on parent posts even if the children are deleted", :include_blank => false, :collection => [["Yes", "true"], ["No", "false"]] %> <%= f.input :show_deleted_children, :as => :select, :label => "Show deleted children", :hint => "Show thumbnail borders on parent posts even if the children are deleted", :include_blank => false, :collection => [["Yes", "true"], ["No", "false"]] %>
<%= f.input :enable_auto_complete, :as => :select, :hint => "Enable tag autocomplete in the search box", :collection => [["Yes", "true"], ["No", "false"]], :include_blank => false %> <%= f.input :enable_auto_complete, :as => :select, :hint => "Enable tag autocomplete in the search box", :collection => [["Yes", "true"], ["No", "false"]], :include_blank => false %>
<%= f.input :disable_categorized_saved_searches, :hint => "Don't show dialog box when creating a new saved search", :as => :select, :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %> <%= f.input :disable_categorized_saved_searches, :hint => "Don't show dialog box when creating a new saved search", :as => :select, :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>
<%= f.input :enable_privacy_mode, :as => :select, :hint => "Make your favorites private", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %> <%= f.input :enable_private_favorites, :as => :select, :hint => "Make your favorites private", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>
<%= f.input :disable_tagged_filenames, :as => :select, :hint => "Don't include tags in image filenames", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %> <%= f.input :disable_tagged_filenames, :as => :select, :hint => "Don't include tags in image filenames", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>
<%= f.input :disable_mobile_gestures, :as => :select, :hint => "Disable swipe left / swipe right gestures on mobile", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %> <%= f.input :disable_mobile_gestures, :as => :select, :hint => "Disable swipe left / swipe right gestures on mobile", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>
<%= f.input :disable_post_tooltips, :as => :select, :hint => "Disable advanced tooltips when hovering over thumbnails", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %> <%= f.input :disable_post_tooltips, :as => :select, :hint => "Disable advanced tooltips when hovering over thumbnails", :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>

View File

@@ -4,10 +4,8 @@
<%= render "statistics", presenter: @user.presenter, user: @user %> <%= render "statistics", presenter: @user.presenter, user: @user %>
<% if !CurrentUser.is_admin? && !@user.enable_privacy_mode? || CurrentUser.id == @user.id %> <%= render "posts/partials/common/inline_blacklist" %>
<%= render "posts/partials/common/inline_blacklist" %> <%= render "post_summary", presenter: @user.presenter, user: @user %>
<%= render "post_summary", presenter: @user.presenter, user: @user %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -268,6 +268,7 @@ Rails.application.routes.draw do
end end
end end
resources :users do resources :users do
resources :favorite_groups, controller: "favorite_groups", only: [:index], as: "favorite_groups"
resource :password, :only => [:edit], :controller => "maintenance/user/passwords" resource :password, :only => [:edit], :controller => "maintenance/user/passwords"
resource :api_key, :only => [:show, :view, :update, :destroy], :controller => "maintenance/user/api_keys" do resource :api_key, :only => [:show, :view, :update, :destroy], :controller => "maintenance/user/api_keys" do
post :view post :view

View File

@@ -0,0 +1,6 @@
class ChangeIsPublicDefaultOnFavoriteGroups < ActiveRecord::Migration[6.0]
def change
change_column_default :favorite_groups, :is_public, from: false, to: true
add_index :favorite_groups, :is_public
end
end

View File

@@ -924,7 +924,7 @@ CREATE TABLE public.favorite_groups (
post_ids integer[] DEFAULT '{}'::integer[] NOT NULL, post_ids integer[] DEFAULT '{}'::integer[] NOT NULL,
created_at timestamp without time zone NOT NULL, created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL,
is_public boolean DEFAULT false NOT NULL is_public boolean DEFAULT true NOT NULL
); );
@@ -4967,6 +4967,13 @@ CREATE INDEX index_dtext_links_on_model_type_and_model_id ON public.dtext_links
CREATE INDEX index_favorite_groups_on_creator_id ON public.favorite_groups USING btree (creator_id); CREATE INDEX index_favorite_groups_on_creator_id ON public.favorite_groups USING btree (creator_id);
--
-- Name: index_favorite_groups_on_is_public; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_favorite_groups_on_is_public ON public.favorite_groups USING btree (is_public);
-- --
-- Name: index_favorite_groups_on_lower_name; Type: INDEX; Schema: public; Owner: - -- Name: index_favorite_groups_on_lower_name; Type: INDEX; Schema: public; Owner: -
-- --
@@ -7407,6 +7414,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191119061018'), ('20191119061018'),
('20191223032633'), ('20191223032633'),
('20200114204550'), ('20200114204550'),
('20200115010442'); ('20200115010442'),
('20200118015014');

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env ruby
require_relative "../../config/environment"
users = User.bit_prefs_match(:enable_private_favorites, true)
favgroups = FavoriteGroup.where(is_public: false).where.not(creator_id: users.select(:id))
favgroups.update_all(is_public: true)

View File

@@ -1706,7 +1706,7 @@ class PostTest < ActiveSupport::TestCase
@parent = FactoryBot.create(:post) @parent = FactoryBot.create(:post)
@child = FactoryBot.create(:post, parent: @parent) @child = FactoryBot.create(:post, parent: @parent)
@user1 = FactoryBot.create(:user, enable_privacy_mode: true) @user1 = FactoryBot.create(:user, enable_private_favorites: true)
@gold1 = FactoryBot.create(:gold_user) @gold1 = FactoryBot.create(:gold_user)
@supervoter1 = FactoryBot.create(:user, is_super_voter: true) @supervoter1 = FactoryBot.create(:user, is_super_voter: true)