Skip to content

Commit

Permalink
Add: #40 拡張ドメインブロックに「トレンドに掲載しない」を追加 (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode authored Jan 22, 2024
1 parent 96da1fc commit 8793bc2
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 10 deletions.
7 changes: 4 additions & 3 deletions app/controllers/admin/domain_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ def set_domain_block

def update_params
params.require(:domain_block).permit(:severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_sensitive, :reject_hashtag,
:reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
:reject_straight_follow, :reject_new_follow, :reject_friend, :block_trends, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
end

def resource_params
params.require(:domain_block).permit(:domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_sensitive, :reject_hashtag,
:reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
:reject_straight_follow, :reject_new_follow, :reject_friend, :block_trends, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
end

def form_domain_block_batch_params
params.require(:form_domain_block_batch).permit(domain_blocks_attributes: [:enabled, :domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers,
:reject_send_sensitive, :reject_hashtag, :reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden])
:reject_send_sensitive, :reject_hashtag, :reject_straight_follow, :reject_new_follow, :reject_friend, :block_trends, :detect_invalid_subscription,
:reject_reports, :private_comment, :public_comment, :obfuscate, :hidden])
end

def action_from_button
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/api/v1/admin/domain_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def filtered_domain_blocks

def domain_block_params
params.permit(:severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_reports, :reject_send_sensitive, :reject_hashtag, :reject_straight_follow,
:reject_new_follow, :reject_friend, :detect_invalid_subscription, :private_comment, :public_comment, :obfuscate, :hidden)
:reject_new_follow, :reject_friend, :block_trends, :detect_invalid_subscription, :private_comment, :public_comment, :obfuscate, :hidden)
end

def insert_pagination_headers
Expand Down Expand Up @@ -103,6 +103,6 @@ def pagination_params(core_params)

def resource_params
params.permit(:domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_sensitive, :reject_hashtag, :reject_straight_follow,
:reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
:reject_new_follow, :reject_friend, :block_trends, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden)
end
end
7 changes: 7 additions & 0 deletions app/models/domain_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# detect_invalid_subscription :boolean default(FALSE), not null
# reject_reply_exclude_followers :boolean default(FALSE), not null
# reject_friend :boolean default(FALSE), not null
# block_trends :boolean default(FALSE), not null
#

class DomainBlock < ApplicationRecord
Expand All @@ -49,6 +50,7 @@ class DomainBlock < ApplicationRecord
.or(where(reject_new_follow: true))
.or(where(reject_straight_follow: true))
.or(where(reject_friend: true))
.or(where(block_trends: true))
}
scope :by_severity, -> { in_order_of(:severity, %w(noop silence suspend)).order(:domain) }

Expand All @@ -70,6 +72,7 @@ def policies
reject_straight_follow? ? :reject_straight_follow : nil,
reject_new_follow? ? :reject_new_follow : nil,
reject_friend? ? :reject_friend : nil,
block_trends? ? :block_trends : nil,
detect_invalid_subscription? ? :detect_invalid_subscription : nil,
reject_reports? ? :reject_reports : nil].reject { |policy| policy == :noop || policy.nil? }
end
Expand Down Expand Up @@ -116,6 +119,10 @@ def reject_friend?(domain)
!!rule_for(domain)&.reject_friend?
end

def block_trends?(domain)
!!rule_for(domain)&.block_trends?
end

def detect_invalid_subscription?(domain)
!!rule_for(domain)&.detect_invalid_subscription?
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/trends/statuses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ def eligible?(status)
(status.public_visibility? || status.public_unlisted_visibility?) &&
status.account.discoverable? && !status.account.silenced? && !status.account.sensitized? &&
status.spoiler_text.blank? && (!status.sensitive? || status.media_attachments.none?) &&
!status.reply? && valid_locale?(status.language)
!status.reply? && valid_locale?(status.language) && !domain_blocked?(status)
end

def domain_blocked?(status)
return false if status.account.local?

DomainBlock.block_trends?(status.account.domain)
end

def calculate_scores(statuses, at_time)
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/rest/admin/domain_block_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class REST::Admin::DomainBlockSerializer < ActiveModel::Serializer
attributes :id, :domain, :created_at, :severity,
:reject_media, :reject_favourite, :reject_reply, :reject_reports,
:reject_reply_exclude_followers, :reject_send_sensitive,
:reject_reply_exclude_followers, :reject_send_sensitive, :block_trends,
:reject_hashtag, :reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription,
:private_comment, :public_comment, :obfuscate

Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/domain_blocks/_domain_block_list.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@

.fields-group
= f.input :reject_send_sensitive, as: :boolean, kmyblue: true, wrapper: :with_label, label: I18n.t('admin.domain_blocks.reject_send_sensitive'), hint: I18n.t('admin.domain_blocks.reject_send_sensitive_hint')

.fields-group
= f.input :block_trends, as: :boolean, kmyblue: true, wrapper: :with_label, label: I18n.t('admin.domain_blocks.block_trends'), hint: I18n.t('admin.domain_blocks.block_trends_hint')
5 changes: 4 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ en:
undo: Disallow federation with domain
domain_blocks:
add_new: Add new domain block
block_trends: Reject trends
block_trends_hint: Reject trends in the future
confirm_suspension:
cancel: Cancel
confirm: Suspend
Expand All @@ -416,7 +418,7 @@ en:
existing_domain_block_html: You have already imposed stricter limits on %{name}, you need to <a href="%{unblock_url}">unblock it</a> first.
export: Export
headers:
disagreement: Protect sensitive posts from political disagreement
disagreement: Political disagreement
harassment: Harassment or spam
invalid_privacy: Privacy is not protected
mastodon_default: Original Mastodon supports
Expand Down Expand Up @@ -558,6 +560,7 @@ en:
description_html: You can define content policies that will be applied to all accounts from this domain and any of its subdomains.
limited_federation_mode_description_html: You can chose whether to allow federation with this domain.
policies:
block_trends: Reject trends
reject_favourite: Reject favorite
reject_friend: Reject friend server application
reject_hashtag: Reject hashtags
Expand Down
5 changes: 4 additions & 1 deletion config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ ja:
undo: 連合許可を外す
domain_blocks:
add_new: ドメインブロックを追加
block_trends: トレンド掲載を拒否
block_trends_hint: 今後の投稿のトレンド掲載を拒否します。反映には時間がかかります。停止とは無関係です
confirm_suspension:
cancel: キャンセル
confirm: 停止
Expand All @@ -409,7 +411,7 @@ ja:
existing_domain_block_html: 既に%{name}に対して、より厳しい制限を課しています。先に<a href="%{unblock_url}">その制限を解除</a>する必要があります。
export: エクスポート
headers:
disagreement: 政治的な意見の相違からの敏感な投稿の保護
disagreement: 意見の相違
harassment: 嫌がらせまたはスパム
invalid_privacy: プライバシーが守られていない
mastodon_default: 本家Mastodonの設定項目
Expand Down Expand Up @@ -550,6 +552,7 @@ ja:
description_html: このドメインとそのサブドメインのすべてのアカウントに適用されるコンテンツポリシーを定義できます。
limited_federation_mode_description_html: このドメインとの連合を許可するかどうかを選択できます。
policies:
block_trends: トレンドへの掲載を拒否
detect_invalid_subscription: 購読のプライバシーなし
reject_favourite: お気に入りを拒否
reject_friend: フレンドサーバー申請を拒否
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AddBlockTrendsToDomainBlocks < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
add_column :domain_blocks, :block_trends, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_01_17_022353) do
ActiveRecord::Schema[7.1].define(version: 2024_01_21_231131) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -574,6 +574,7 @@
t.boolean "detect_invalid_subscription", default: false, null: false
t.boolean "reject_reply_exclude_followers", default: false, null: false
t.boolean "reject_friend", default: false, null: false
t.boolean "block_trends", default: false, null: false
t.index ["domain"], name: "index_domain_blocks_on_domain", unique: true
end

Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/dangerous.rake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace :dangerous do
20230222232121
20240117021025
20240117022353
20240121231131
)
# Removed: account_groups
target_tables = %w(
Expand Down Expand Up @@ -122,6 +123,7 @@ namespace :dangerous do
%w(custom_filters exclude_follows),
%w(custom_filters exclude_localusers),
%w(custom_filters with_quote),
%w(domain_blocks block_trends),
%w(domain_blocks detect_invalid_subscription),
%w(domain_blocks hidden),
# Removed: domain_blocks hidden_anonymous
Expand Down
23 changes: 23 additions & 0 deletions spec/models/trends/statuses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@
end
end

describe '#register for remote user' do
let(:account) { Fabricate(:account, searchability: :public, discoverable: true, domain: 'example.com', uri: 'https://example.com/actor') }
let(:status) { Fabricate(:status, account: account, searchability: :public, language: 'en') }
let(:domain_block) { false }

before do
Fabricate(:domain_block, domain: 'example.com', severity: :noop, block_trends: true) if domain_block
subject.register(status, at_time)
end

it 'records use' do
expect(subject.send(:recently_used_ids, at_time)).to eq [status.id]
end

context 'when domain-blocked' do
let(:domain_block) { true }

it 'does not record use' do
expect(subject.send(:recently_used_ids, at_time)).to eq []
end
end
end

describe '#query' do
it 'returns a composable query scope' do
expect(subject.query).to be_a Trends::Query
Expand Down
3 changes: 3 additions & 0 deletions spec/requests/api/v1/admin/domain_blocks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
private_comment: domain_block.private_comment,
public_comment: domain_block.public_comment,
obfuscate: domain_block.obfuscate,
block_trends: domain_block.block_trends,
reject_favourite: domain_block.reject_favourite,
reject_hashtag: domain_block.reject_hashtag,
detect_invalid_subscription: domain_block.detect_invalid_subscription,
Expand Down Expand Up @@ -105,6 +106,7 @@
private_comment: domain_block.private_comment,
public_comment: domain_block.public_comment,
obfuscate: domain_block.obfuscate,
block_trends: domain_block.block_trends,
reject_favourite: domain_block.reject_favourite,
reject_hashtag: domain_block.reject_hashtag,
detect_invalid_subscription: domain_block.detect_invalid_subscription,
Expand Down Expand Up @@ -136,6 +138,7 @@
private_comment: domain_block.private_comment,
public_comment: domain_block.public_comment,
obfuscate: domain_block.obfuscate,
block_trends: domain_block.block_trends,
reject_favourite: domain_block.reject_favourite,
reject_hashtag: domain_block.reject_hashtag,
detect_invalid_subscription: domain_block.detect_invalid_subscription,
Expand Down

0 comments on commit 8793bc2

Please sign in to comment.