Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PublicFeed: add a configuration setting to filter out bot accounts #1296

Open
wants to merge 1 commit into
base: hometown-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/api/v1/timelines/public_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def public_feed
current_account,
local: truthy_param?(:local),
remote: truthy_param?(:remote),
only_media: truthy_param?(:only_media)
only_media: truthy_param?(:only_media),
without_bots: without_bots?
)
end

def without_bots?
Rails.configuration.x.local_timeline_exclude_bots && truthy_param?(:local)
end

def insert_pagination_headers
set_pagination_headers(next_path, prev_path)
end
Expand Down
1 change: 1 addition & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Account < ApplicationRecord
scope :without_suspended, -> { where(suspended_at: nil) }
scope :without_silenced, -> { where(silenced_at: nil) }
scope :without_instance_actor, -> { where.not(id: -99) }
scope :without_bots, -> { where.not(actor_type: %w(Application Service)).or(where(actor_type: nil)) }
scope :recent, -> { reorder(id: :desc) }
scope :bots, -> { where(actor_type: %w(Application Service)) }
scope :groups, -> { where(actor_type: 'Group') }
Expand Down
11 changes: 10 additions & 1 deletion app/models/public_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PublicFeed
# @option [Boolean] :local
# @option [Boolean] :remote
# @option [Boolean] :only_media
# @option [Boolean] :without_bots
def initialize(account, options = {})
@account = account
@options = options
Expand Down Expand Up @@ -68,8 +69,16 @@ def media_only?
options[:only_media]
end

def without_bots?
options[:without_bots]
end

def public_scope
Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
account_scope = Account.without_suspended.without_silenced
if without_bots?
account_scope = account_scope.without_bots
end
scope = Status.with_public_visibility.joins(:account).merge(account_scope)
end

def local_only_scope
Expand Down
1 change: 1 addition & 0 deletions app/models/tag_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TagFeed < PublicFeed
# @option [Boolean] :local
# @option [Boolean] :remote
# @option [Boolean] :only_media
# @option [Boolean] :without_bots
def initialize(tag, account, options = {})
@tag = tag
super(account, options)
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/99_hometown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Rails.application.configure do
config.x.local_timeline_exclude_bots = ENV['LOCAL_TIMELINE_EXCLUDE_BOTS'] == 'true'
end
33 changes: 33 additions & 0 deletions spec/models/public_feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@
expect(subject).not_to include(silenced_status.id)
end

describe '#without_bots=' do
let(:person_account) { Fabricate(:account, actor_type: 'Person') }
let(:bot_account) { Fabricate(:account, actor_type: 'Application') }

context 'when the setting is false' do
subject { described_class.new(nil, without_bots: false).get(20).map(&:id) }

it 'includes bots' do
status = Fabricate(:status, account: account)
person_status = Fabricate(:status, account: person_account)
bot_status = Fabricate(:status, account: bot_account)

expect(subject).to include(status.id)
expect(subject).to include(person_status.id)
expect(subject).to include(bot_status.id)
end
end

context 'when the setting is true' do
subject { described_class.new(nil, without_bots: true).get(20).map(&:id) }

it 'filters out bot accounts' do
status = Fabricate(:status, account: account)
person_status = Fabricate(:status, account: person_account)
bot_status = Fabricate(:status, account: bot_account)

expect(subject).to include(status.id)
expect(subject).to include(person_status.id)
expect(subject).not_to include(bot_status.id)
end
end
end

context 'without local_only option' do
let(:viewer) { nil }

Expand Down