Browse Source

Add federation support for the "hide network" preference (#11673)

* Change ActivityPub follower/following collections to not link first page

* Add support for hiding followers and following of remote users

* Switch to using a single `hide_collections` column

* Address code style remarks
closed-social-v3
ThibG 4 years ago
committed by GitHub
parent
commit
b154428e14
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 55 additions and 11 deletions
  1. +1
    -1
      app/controllers/api/v1/accounts/follower_accounts_controller.rb
  2. +1
    -1
      app/controllers/api/v1/accounts/following_accounts_controller.rb
  3. +10
    -1
      app/controllers/follower_accounts_controller.rb
  4. +10
    -1
      app/controllers/following_accounts_controller.rb
  5. +9
    -0
      app/models/account.rb
  6. +18
    -7
      app/services/activitypub/process_account_service.rb
  7. +5
    -0
      db/migrate/20191212163405_add_hide_collections_to_accounts.rb
  8. +1
    -0
      db/schema.rb

+ 1
- 1
app/controllers/api/v1/accounts/follower_accounts_controller.rb View File

@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController
end end
def hide_results? def hide_results?
(@account.user_hides_network? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
(@account.hides_followers? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
end end
def default_accounts def default_accounts

+ 1
- 1
app/controllers/api/v1/accounts/following_accounts_controller.rb View File

@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController
end end
def hide_results? def hide_results?
(@account.user_hides_network? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
(@account.hides_following? && current_account&.id != @account.id) || (current_account && @account.blocking?(current_account))
end end
def default_accounts def default_accounts

+ 10
- 1
app/controllers/follower_accounts_controller.rb View File

@ -28,7 +28,8 @@ class FollowerAccountsController < ApplicationController
render json: collection_presenter, render json: collection_presenter,
serializer: ActivityPub::CollectionSerializer, serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter, adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
content_type: 'application/activity+json',
fields: restrict_fields_to
end end
end end
end end
@ -71,4 +72,12 @@ class FollowerAccountsController < ApplicationController
) )
end end
end end
def restrict_fields_to
if page_requested? || !@account.user_hides_network?
# Return all fields
else
%i(id type totalItems)
end
end
end end

+ 10
- 1
app/controllers/following_accounts_controller.rb View File

@ -28,7 +28,8 @@ class FollowingAccountsController < ApplicationController
render json: collection_presenter, render json: collection_presenter,
serializer: ActivityPub::CollectionSerializer, serializer: ActivityPub::CollectionSerializer,
adapter: ActivityPub::Adapter, adapter: ActivityPub::Adapter,
content_type: 'application/activity+json'
content_type: 'application/activity+json',
fields: restrict_fields_to
end end
end end
end end
@ -71,4 +72,12 @@ class FollowingAccountsController < ApplicationController
) )
end end
end end
def restrict_fields_to
if page_requested? || !@account.user_hides_network?
# Return all fields
else
%i(id type totalItems)
end
end
end end

+ 9
- 0
app/models/account.rb View File

@ -46,6 +46,7 @@
# silenced_at :datetime # silenced_at :datetime
# suspended_at :datetime # suspended_at :datetime
# trust_level :integer # trust_level :integer
# hide_collections :boolean
# #
class Account < ApplicationRecord class Account < ApplicationRecord
@ -323,6 +324,14 @@ class Account < ApplicationRecord
save! save!
end end
def hides_followers?
hide_collections? || user_hides_network?
end
def hides_following?
hide_collections? || user_hides_network?
end
def object_type def object_type
:person :person
end end

+ 18
- 7
app/services/activitypub/process_account_service.rb View File

@ -94,6 +94,7 @@ class ActivityPub::ProcessAccountService < BaseService
@account.statuses_count = outbox_total_items if outbox_total_items.present? @account.statuses_count = outbox_total_items if outbox_total_items.present?
@account.following_count = following_total_items if following_total_items.present? @account.following_count = following_total_items if following_total_items.present?
@account.followers_count = followers_total_items if followers_total_items.present? @account.followers_count = followers_total_items if followers_total_items.present?
@account.hide_collections = following_private? || followers_private?
@account.moved_to_account = @json['movedTo'].present? ? moved_account : nil @account.moved_to_account = @json['movedTo'].present? ? moved_account : nil
end end
@ -166,26 +167,36 @@ class ActivityPub::ProcessAccountService < BaseService
end end
def outbox_total_items def outbox_total_items
collection_total_items('outbox')
collection_info('outbox').first
end end
def following_total_items def following_total_items
collection_total_items('following')
collection_info('following').first
end end
def followers_total_items def followers_total_items
collection_total_items('followers')
collection_info('followers').first
end end
def collection_total_items(type)
return if @json[type].blank?
def following_private?
!collection_info('following').last
end
def followers_private?
!collection_info('followers').last
end
def collection_info(type)
return [nil, nil] if @json[type].blank?
return @collections[type] if @collections.key?(type) return @collections[type] if @collections.key?(type)
collection = fetch_resource_without_id_validation(@json[type]) collection = fetch_resource_without_id_validation(@json[type])
@collections[type] = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
total_items = collection.is_a?(Hash) && collection['totalItems'].present? && collection['totalItems'].is_a?(Numeric) ? collection['totalItems'] : nil
has_first_page = collection.is_a?(Hash) && collection['first'].present?
@collections[type] = [total_items, has_first_page]
rescue HTTP::Error, OpenSSL::SSL::SSLError rescue HTTP::Error, OpenSSL::SSL::SSLError
@collections[type] = nil
@collections[type] = [nil, nil]
end end
def moved_account def moved_account

+ 5
- 0
db/migrate/20191212163405_add_hide_collections_to_accounts.rb View File

@ -0,0 +1,5 @@
class AddHideCollectionsToAccounts < ActiveRecord::Migration[5.2]
def change
add_column :accounts, :hide_collections, :boolean
end
end

+ 1
- 0
db/schema.rb View File

@ -170,6 +170,7 @@ ActiveRecord::Schema.define(version: 2020_03_06_035625) do
t.datetime "silenced_at" t.datetime "silenced_at"
t.datetime "suspended_at" t.datetime "suspended_at"
t.integer "trust_level" t.integer "trust_level"
t.boolean "hide_collections"
t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin
t.index "lower((username)::text), lower((domain)::text)", name: "index_accounts_on_username_and_domain_lower", unique: true t.index "lower((username)::text), lower((domain)::text)", name: "index_accounts_on_username_and_domain_lower", unique: true
t.index ["moved_to_account_id"], name: "index_accounts_on_moved_to_account_id" t.index ["moved_to_account_id"], name: "index_accounts_on_moved_to_account_id"

Loading…
Cancel
Save