Skip to content

Commit b1021a1

Browse files
Merge pull request NeuromatchAcademy#85 from NeuromatchAcademy/fix-hide-boosts-in-public-feeds
hide boosts from boost-muted accounts in public feeds
2 parents 7536e4a + 5986ae3 commit b1021a1

5 files changed

Lines changed: 92 additions & 1 deletion

File tree

app/models/account.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,17 @@ def excluded_from_timeline_account_ids
380380
Rails.cache.fetch("exclude_account_ids_for:#{id}") { block_relationships.pluck(:target_account_id) + blocked_by_relationships.pluck(:account_id) + mute_relationships.pluck(:target_account_id) }
381381
end
382382

383+
# Accounts that are followed but have reblogs hidden
384+
def excluded_boosts_from_timeline_account_ids
385+
Rails.cache.fetch("exclude_boosts_account_ids_for:#{id}", expires_in: 72.hours) do
386+
active_relationships.where(show_reblogs: false).pluck(:target_account_id)
387+
end
388+
end
389+
390+
def invalidate_mute_boosts_cache
391+
Rails.cache.delete("exclude_boosts_account_ids_for:#{id}")
392+
end
393+
383394
def excluded_from_timeline_domains
384395
Rails.cache.fetch("exclude_domains_for:#{id}") { domain_blocks.pluck(:domain) }
385396
end

app/models/concerns/account/interactions.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def follow!(other_account, reblogs: nil, notify: nil, languages: nil, uri: nil,
5555
rel.languages = languages unless languages.nil?
5656

5757
rel.save! if rel.changed?
58+
try(:invalidate_mute_boosts_cache)
5859

5960
rel
6061
end
@@ -68,6 +69,7 @@ def request_follow!(other_account, reblogs: nil, notify: nil, languages: nil, ur
6869
rel.languages = languages unless languages.nil?
6970

7071
rel.save! if rel.changed?
72+
try(:invalidate_mute_boosts_cache)
7173

7274
rel
7375
end
@@ -100,6 +102,9 @@ def block_domain!(other_domain)
100102
def unfollow!(other_account)
101103
follow = active_relationships.find_by(target_account: other_account)
102104
follow&.destroy
105+
try(:invalidate_mute_boosts_cache)
106+
107+
follow
103108
end
104109

105110
def unblock!(other_account)

app/models/status.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ class Status < ApplicationRecord
127127
scope :not_replying_to_account, ->(account) { where.not(in_reply_to_account: account) }
128128
scope :without_reblogs, -> { where(statuses: { reblog_of_id: nil }) }
129129
scope :tagged_with, ->(tag_ids) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag_ids }) }
130-
scope :not_excluded_by_account, ->(account) { where.not(account_id: account.excluded_from_timeline_account_ids) }
130+
scope :not_excluded_by_account, lambda { |account|
131+
where.not(account_id: account.excluded_from_timeline_account_ids)
132+
.and(where(reblog_of_id: nil).or(where.not(account_id: account.excluded_boosts_from_timeline_account_ids)))
133+
}
131134
scope :not_domain_blocked_by_account, ->(account) { account.excluded_from_timeline_domains.blank? ? left_outer_joins(:account) : left_outer_joins(:account).merge(Account.not_domain_blocked_by_account(account)) }
132135
scope :tagged_with_all, lambda { |tag_ids|
133136
Array(tag_ids).map(&:to_i).reduce(self) do |result, id|

spec/models/concerns/account/interactions_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@
734734
it 'does mute reblogs' do
735735
expect(me.muting_reblogs?(you)).to be(true)
736736
end
737+
738+
it 'adds me to the collection of accounts that are boost muted' do
739+
expect(me.excluded_boosts_from_timeline_account_ids).to include(you.id)
740+
end
737741
end
738742

739743
context 'with the reblogs option set to true' do
@@ -745,6 +749,20 @@
745749
expect(me.muting_reblogs?(you)).to be(false)
746750
end
747751
end
752+
753+
context 'when toggling the reblogs option' do
754+
before do
755+
me.follow!(you)
756+
end
757+
758+
it 'evicts the cache of boost muted accounts' do
759+
expect(me.excluded_boosts_from_timeline_account_ids).to be_empty
760+
me.follow!(you, reblogs: false)
761+
expect(me.excluded_boosts_from_timeline_account_ids).to include(you.id)
762+
me.follow!(you, reblogs: true)
763+
expect(me.excluded_boosts_from_timeline_account_ids).to be_empty
764+
end
765+
end
748766
end
749767

750768
describe '#lists_for_local_distribution' do

spec/models/public_feed_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,5 +664,59 @@
664664
end
665665
end
666666
end
667+
668+
context 'with with_reblogs true' do
669+
subject { described_class.new(viewer, with_reblogs: true).get(20).map(&:id) }
670+
671+
let!(:viewer) { Fabricate(:account, domain: nil) }
672+
let!(:booster) { Fabricate(:account, domain: nil) }
673+
let!(:boosted) { Fabricate(:account, domain: nil) }
674+
let!(:status) { Fabricate(:status, account: boosted) }
675+
let!(:boost) { Fabricate(:status, account: booster, reblog_of_id: status.id) }
676+
677+
shared_examples 'shows boost' do
678+
it 'shows boosts in public feeds' do
679+
expect(subject).to include(boost.id)
680+
end
681+
end
682+
683+
shared_examples 'does not show boost' do
684+
it 'does not show boosts in public feeds' do
685+
expect(subject).to_not include(boost.id)
686+
end
687+
end
688+
689+
it_behaves_like 'shows boost'
690+
691+
context 'with a local_only option set' do
692+
subject { described_class.new(viewer, with_reblogs: true, local: true).get(20).map(&:id) }
693+
694+
context 'with a remote booster' do
695+
let!(:booster) { Fabricate(:account, domain: 'other.net') }
696+
697+
it_behaves_like 'does not show boost'
698+
end
699+
700+
context 'with a remote status' do
701+
let!(:boosted) { Fabricate(:account, domain: 'other.net') }
702+
703+
it_behaves_like 'shows boost'
704+
705+
context 'with a remote booster' do
706+
let!(:booster) { Fabricate(:account, domain: 'other.net') }
707+
708+
it_behaves_like 'does not show boost'
709+
end
710+
end
711+
712+
context 'when hiding boosts from a followed account' do
713+
before do
714+
Fabricate(:follow, account: viewer, target_account: booster, show_reblogs: false)
715+
end
716+
717+
it_behaves_like 'does not show boost'
718+
end
719+
end
720+
end
667721
end
668722
end

0 commit comments

Comments
 (0)