Conflicts: - `app/controllers/settings/preferences_controller.rb`: Conflicts due to us having more user settings and upstream dropping `hide_network` (to replace it with an account attribute, properly migrated). Dropped `hide_network` like upstream. - `app/lib/user_settings_decorator.rb`: Conflicts due to us having more user settings and upstream dropping `hide_network` (to replace it with an account attribute, properly migrated). Dropped `hide_network` like upstream. - `app/models/status.rb`: Conflict because of slight change in how glitch-soc handles the scope to filter out local-only posts for anonymous viewers. Took upstream's changes and re-applied glitch-soc's change. - `app/models/user.rb`: Conflicts due to us having more user settings and upstream dropping `hide_network` (to replace it with an account attribute, properly migrated). Dropped `hide_network` like upstream. - `app/views/directories/index.html.haml`: Conflict because upstream redesigned that page while glitch-soc had a minor change to support hiding the number of followers. Ported glitch-soc's change on top of upstream's redesign. Additional changes: - `app/models/account_statuses_filter.rb`: See change to `app/models/status.rb`.local
commit
1fbd1fa5c4
75 changed files with 906 additions and 597 deletions
@ -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,134 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
class AccountStatusesFilter |
||||
KEYS = %i( |
||||
pinned |
||||
tagged |
||||
only_media |
||||
exclude_replies |
||||
exclude_reblogs |
||||
).freeze |
||||
|
||||
attr_reader :params, :account, :current_account |
||||
|
||||
def initialize(account, current_account, params = {}) |
||||
@account = account |
||||
@current_account = current_account |
||||
@params = params |
||||
end |
||||
|
||||
def results |
||||
scope = initial_scope |
||||
|
||||
scope.merge!(pinned_scope) if pinned? |
||||
scope.merge!(only_media_scope) if only_media? |
||||
scope.merge!(no_replies_scope) if exclude_replies? |
||||
scope.merge!(no_reblogs_scope) if exclude_reblogs? |
||||
scope.merge!(hashtag_scope) if tagged? |
||||
|
||||
scope |
||||
end |
||||
|
||||
private |
||||
|
||||
def initial_scope |
||||
if suspended? |
||||
Status.none |
||||
elsif anonymous? |
||||
account.statuses.not_local_only.where(visibility: %i(public unlisted)) |
||||
elsif author? |
||||
account.statuses.all # NOTE: #merge! does not work without the #all |
||||
elsif blocked? |
||||
Status.none |
||||
else |
||||
filtered_scope |
||||
end |
||||
end |
||||
|
||||
def filtered_scope |
||||
scope = account.statuses.left_outer_joins(:mentions) |
||||
|
||||
scope.merge!(scope.where(visibility: follower? ? %i(public unlisted private) : %i(public unlisted)).or(scope.where(mentions: { account_id: current_account.id })).group(Status.arel_table[:id])) |
||||
scope.merge!(filtered_reblogs_scope) if reblogs_may_occur? |
||||
|
||||
scope |
||||
end |
||||
|
||||
def filtered_reblogs_scope |
||||
Status.left_outer_joins(:reblog).where(reblog_of_id: nil).or(Status.where.not(reblogs_statuses: { account_id: current_account.excluded_from_timeline_account_ids })) |
||||
end |
||||
|
||||
def only_media_scope |
||||
Status.joins(:media_attachments).merge(account.media_attachments.reorder(nil)).group(Status.arel_table[:id]) |
||||
end |
||||
|
||||
def no_replies_scope |
||||
Status.without_replies |
||||
end |
||||
|
||||
def no_reblogs_scope |
||||
Status.without_reblogs |
||||
end |
||||
|
||||
def pinned_scope |
||||
account.pinned_statuses.group(Status.arel_table[:id], StatusPin.arel_table[:created_at]) |
||||
end |
||||
|
||||
def hashtag_scope |
||||
tag = Tag.find_normalized(params[:tagged]) |
||||
|
||||
if tag |
||||
Status.tagged_with(tag.id) |
||||
else |
||||
Status.none |
||||
end |
||||
end |
||||
|
||||
def suspended? |
||||
account.suspended? |
||||
end |
||||
|
||||
def anonymous? |
||||
current_account.nil? |
||||
end |
||||
|
||||
def author? |
||||
current_account.id == account.id |
||||
end |
||||
|
||||
def blocked? |
||||
account.blocking?(current_account) || (current_account.domain.present? && account.domain_blocking?(current_account.domain)) |
||||
end |
||||
|
||||
def follower? |
||||
current_account.following?(account) |
||||
end |
||||
|
||||
def reblogs_may_occur? |
||||
!exclude_reblogs? && !only_media? && !tagged? |
||||
end |
||||
|
||||
def pinned? |
||||
truthy_param?(:pinned) |
||||
end |
||||
|
||||
def only_media? |
||||
truthy_param?(:only_media) |
||||
end |
||||
|
||||
def exclude_replies? |
||||
truthy_param?(:exclude_replies) |
||||
end |
||||
|
||||
def exclude_reblogs? |
||||
truthy_param?(:exclude_reblogs) |
||||
end |
||||
|
||||
def tagged? |
||||
params[:tagged].present? |
||||
end |
||||
|
||||
def truthy_param?(key) |
||||
ActiveModel::Type::Boolean.new.cast(params[key]) |
||||
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,229 @@ |
||||
# frozen_string_literal: true |
||||
|
||||
require 'rails_helper' |
||||
|
||||
RSpec.describe AccountStatusesFilter do |
||||
let(:account) { Fabricate(:account) } |
||||
let(:current_account) { nil } |
||||
let(:params) { {} } |
||||
|
||||
subject { described_class.new(account, current_account, params) } |
||||
|
||||
def status!(visibility) |
||||
Fabricate(:status, account: account, visibility: visibility) |
||||
end |
||||
|
||||
def status_with_tag!(visibility, tag) |
||||
Fabricate(:status, account: account, visibility: visibility, tags: [tag]) |
||||
end |
||||
|
||||
def status_with_parent!(visibility) |
||||
Fabricate(:status, account: account, visibility: visibility, thread: Fabricate(:status)) |
||||
end |
||||
|
||||
def status_with_reblog!(visibility) |
||||
Fabricate(:status, account: account, visibility: visibility, reblog: Fabricate(:status)) |
||||
end |
||||
|
||||
def status_with_mention!(visibility, mentioned_account = nil) |
||||
Fabricate(:status, account: account, visibility: visibility).tap do |status| |
||||
Fabricate(:mention, status: status, account: mentioned_account || Fabricate(:account)) |
||||
end |
||||
end |
||||
|
||||
def status_with_media_attachment!(visibility) |
||||
Fabricate(:status, account: account, visibility: visibility).tap do |status| |
||||
Fabricate(:media_attachment, account: account, status: status) |
||||
end |
||||
end |
||||
|
||||
describe '#results' do |
||||
let(:tag) { Fabricate(:tag) } |
||||
|
||||
before do |
||||
status!(:public) |
||||
status!(:unlisted) |
||||
status!(:private) |
||||
status_with_parent!(:public) |
||||
status_with_reblog!(:public) |
||||
status_with_tag!(:public, tag) |
||||
status_with_mention!(:direct) |
||||
status_with_media_attachment!(:public) |
||||
end |
||||
|
||||
shared_examples 'filter params' do |
||||
context 'with only_media param' do |
||||
let(:params) { { only_media: true } } |
||||
|
||||
it 'returns only statuses with media' do |
||||
expect(subject.results.all?(&:with_media?)).to be true |
||||
end |
||||
end |
||||
|
||||
context 'with tagged param' do |
||||
let(:params) { { tagged: tag.name } } |
||||
|
||||
it 'returns only statuses with tag' do |
||||
expect(subject.results.all? { |s| s.tags.include?(tag) }).to be true |
||||
end |
||||
end |
||||
|
||||
context 'with exclude_replies param' do |
||||
let(:params) { { exclude_replies: true } } |
||||
|
||||
it 'returns only statuses that are not replies' do |
||||
expect(subject.results.none?(&:reply?)).to be true |
||||
end |
||||
end |
||||
|
||||
context 'with exclude_reblogs param' do |
||||
let(:params) { { exclude_reblogs: true } } |
||||
|
||||
it 'returns only statuses that are not reblogs' do |
||||
expect(subject.results.none?(&:reblog?)).to be true |
||||
end |
||||
end |
||||
end |
||||
|
||||
context 'when accessed anonymously' do |
||||
let(:current_account) { nil } |
||||
let(:direct_status) { nil } |
||||
|
||||
it 'returns only public statuses' do |
||||
expect(subject.results.pluck(:visibility).uniq).to match_array %w(unlisted public) |
||||
end |
||||
|
||||
it 'returns public replies' do |
||||
expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty |
||||
end |
||||
|
||||
it 'returns public reblogs' do |
||||
expect(subject.results.pluck(:reblog_of_id)).to_not be_empty |
||||
end |
||||
|
||||
it_behaves_like 'filter params' |
||||
end |
||||
|
||||
context 'when accessed with a blocked account' do |
||||
let(:current_account) { Fabricate(:account) } |
||||
|
||||
before do |
||||
account.block!(current_account) |
||||
end |
||||
|
||||
it 'returns nothing' do |
||||
expect(subject.results.to_a).to be_empty |
||||
end |
||||
end |
||||
|
||||
context 'when accessed by self' do |
||||
let(:current_account) { account } |
||||
|
||||
it 'returns everything' do |
||||
expect(subject.results.pluck(:visibility).uniq).to match_array %w(direct private unlisted public) |
||||
end |
||||
|
||||
it 'returns replies' do |
||||
expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty |
||||
end |
||||
|
||||
it 'returns reblogs' do |
||||
expect(subject.results.pluck(:reblog_of_id)).to_not be_empty |
||||
end |
||||
|
||||
it_behaves_like 'filter params' |
||||
end |
||||
|
||||
context 'when accessed by a follower' do |
||||
let(:current_account) { Fabricate(:account) } |
||||
|
||||
before do |
||||
current_account.follow!(account) |
||||
end |
||||
|
||||
it 'returns private statuses' do |
||||
expect(subject.results.pluck(:visibility).uniq).to match_array %w(private unlisted public) |
||||
end |
||||
|
||||
it 'returns replies' do |
||||
expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty |
||||
end |
||||
|
||||
it 'returns reblogs' do |
||||
expect(subject.results.pluck(:reblog_of_id)).to_not be_empty |
||||
end |
||||
|
||||
context 'when there is a direct status mentioning the non-follower' do |
||||
let!(:direct_status) { status_with_mention!(:direct, current_account) } |
||||
|
||||
it 'returns the direct status' do |
||||
expect(subject.results.pluck(:id)).to include(direct_status.id) |
||||
end |
||||
end |
||||
|
||||
it_behaves_like 'filter params' |
||||
end |
||||
|
||||
context 'when accessed by a non-follower' do |
||||
let(:current_account) { Fabricate(:account) } |
||||
|
||||
it 'returns only public statuses' do |
||||
expect(subject.results.pluck(:visibility).uniq).to match_array %w(unlisted public) |
||||
end |
||||
|
||||
it 'returns public replies' do |
||||
expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty |
||||
end |
||||
|
||||
it 'returns public reblogs' do |
||||
expect(subject.results.pluck(:reblog_of_id)).to_not be_empty |
||||
end |
||||
|
||||
context 'when there is a private status mentioning the non-follower' do |
||||
let!(:private_status) { status_with_mention!(:private, current_account) } |
||||
|
||||
it 'returns the private status' do |
||||
expect(subject.results.pluck(:id)).to include(private_status.id) |
||||
end |
||||
end |
||||
|
||||
context 'when blocking a reblogged account' do |
||||
let(:reblog) { status_with_reblog!('public') } |
||||
|
||||
before do |
||||
current_account.block!(reblog.reblog.account) |
||||
end |
||||
|
||||
it 'does not return reblog of blocked account' do |
||||
expect(subject.results.pluck(:id)).to_not include(reblog.id) |
||||
end |
||||
end |
||||
|
||||
context 'when muting a reblogged account' do |
||||
let(:reblog) { status_with_reblog!('public') } |
||||
|
||||
before do |
||||
current_account.mute!(reblog.reblog.account) |
||||
end |
||||
|
||||
it 'does not return reblog of muted account' do |
||||
expect(subject.results.pluck(:id)).to_not include(reblog.id) |
||||
end |
||||
end |
||||
|
||||
context 'when blocked by a reblogged account' do |
||||
let(:reblog) { status_with_reblog!('public') } |
||||
|
||||
before do |
||||
reblog.reblog.account.block!(current_account) |
||||
end |
||||
|
||||
it 'does not return reblog of blocked-by account' do |
||||
expect(subject.results.pluck(:id)).to_not include(reblog.id) |
||||
end |
||||
end |
||||
|
||||
it_behaves_like 'filter params' |
||||
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 |
Loading…
Reference in new issue