* Add `/api/v1/accounts/familiar_followers` to REST API * Change hide network preference to be stored consistently for local and remote accounts * Add dummy classes to migration * Apply suggestions from code review Co-authored-by: Claire <claire.github-309c@sitedethib.com> Co-authored-by: Claire <claire.github-309c@sitedethib.com>closed-social-glitch-2
@ -0,0 +1,25 @@ | |||
# frozen_string_literal: true | |||
class Api::V1::Accounts::FamiliarFollowersController < Api::BaseController | |||
before_action -> { doorkeeper_authorize! :read, :'read:follows' } | |||
before_action :require_user! | |||
before_action :set_accounts | |||
def index | |||
render json: familiar_followers.accounts, each_serializer: REST::FamiliarFollowersSerializer | |||
end | |||
private | |||
def set_accounts | |||
@accounts = Account.without_suspended.where(id: account_ids).select('id, hide_collections').index_by(&:id).values_at(*account_ids).compact | |||
end | |||
def familiar_followers | |||
FamiliarFollowersPresenter.new(@accounts, current_user.account_id) | |||
end | |||
def account_ids | |||
Array(params[:id]).map(&:to_i) | |||
end | |||
end |
@ -0,0 +1,17 @@ | |||
# frozen_string_literal: true | |||
class FamiliarFollowersPresenter | |||
class Result < ActiveModelSerializers::Model | |||
attributes :id, :accounts | |||
end | |||
def initialize(accounts, current_account_id) | |||
@accounts = accounts | |||
@current_account_id = current_account_id | |||
end | |||
def accounts | |||
map = Follow.includes(account: :account_stat).where(target_account_id: @accounts.map(&:id)).where(account_id: Follow.where(account_id: @current_account_id).joins(:target_account).merge(Account.where(hide_collections: [nil, false])).select(:target_account_id)).group_by(&:target_account_id) | |||
@accounts.map { |account| Result.new(id: account.id, accounts: (account.hide_collections? ? [] : (map[account.id] || [])).map(&:account)) } | |||
end | |||
end |
@ -0,0 +1,11 @@ | |||
# frozen_string_literal: true | |||
class REST::FamiliarFollowersSerializer < ActiveModel::Serializer | |||
attribute :id | |||
has_many :accounts, serializer: REST::AccountSerializer | |||
def id | |||
object.id.to_s | |||
end | |||
end |
@ -0,0 +1,37 @@ | |||
class MigrateHideNetworkPreference < ActiveRecord::Migration[6.1] | |||
disable_ddl_transaction! | |||
# Dummy classes, to make migration possible across version changes | |||
class Account < ApplicationRecord | |||
has_one :user, inverse_of: :account | |||
scope :local, -> { where(domain: nil) } | |||
end | |||
class User < ApplicationRecord | |||
belongs_to :account | |||
end | |||
def up | |||
Account.reset_column_information | |||
Setting.unscoped.where(thing_type: 'User', var: 'hide_network').find_each do |setting| | |||
account = User.find(setting.thing_id).account | |||
ApplicationRecord.transaction do | |||
account.update(hide_collections: setting.value) | |||
setting.delete | |||
end | |||
rescue ActiveRecord::RecordNotFound | |||
next | |||
end | |||
end | |||
def down | |||
Account.local.where(hide_collections: true).includes(:user).find_each do |account| | |||
ApplicationRecord.transaction do | |||
Setting.create(thing_type: 'User', thing_id: account.user.id, var: 'hide_network', value: account.hide_collections?) | |||
account.update(hide_collections: nil) | |||
end | |||
end | |||
end | |||
end |
@ -0,0 +1,58 @@ | |||
# frozen_string_literal: true | |||
require 'rails_helper' | |||
RSpec.describe FamiliarFollowersPresenter do | |||
describe '#accounts' do | |||
let(:account) { Fabricate(:account) } | |||
let(:familiar_follower) { Fabricate(:account) } | |||
let(:requested_accounts) { Fabricate.times(2, :account) } | |||
subject { described_class.new(requested_accounts, account.id) } | |||
before do | |||
familiar_follower.follow!(requested_accounts.first) | |||
account.follow!(familiar_follower) | |||
end | |||
it 'returns a result for each requested account' do | |||
expect(subject.accounts.map(&:id)).to eq requested_accounts.map(&:id) | |||
end | |||
it 'returns followers you follow' do | |||
result = subject.accounts.first | |||
expect(result).to_not be_nil | |||
expect(result.id).to eq requested_accounts.first.id | |||
expect(result.accounts).to match_array([familiar_follower]) | |||
end | |||
context 'when requested account hides followers' do | |||
before do | |||
requested_accounts.first.update(hide_collections: true) | |||
end | |||
it 'does not return followers you follow' do | |||
result = subject.accounts.first | |||
expect(result).to_not be_nil | |||
expect(result.id).to eq requested_accounts.first.id | |||
expect(result.accounts).to be_empty | |||
end | |||
end | |||
context 'when familiar follower hides follows' do | |||
before do | |||
familiar_follower.update(hide_collections: true) | |||
end | |||
it 'does not return followers you follow' do | |||
result = subject.accounts.first | |||
expect(result).to_not be_nil | |||
expect(result.id).to eq requested_accounts.first.id | |||
expect(result.accounts).to be_empty | |||
end | |||
end | |||
end | |||
end |