Skip to content

Commit

Permalink
Add: #778 アカウント名・紹介文のフィルタリング (#779)
Browse files Browse the repository at this point in the history
* Add: #778 アカウント名・紹介文のフィルタリング

* Fix test
  • Loading branch information
kmycode authored Jul 10, 2024
1 parent d413e2d commit 8e59cd0
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def set_filter
end

def resource_params
params.permit(:phrase, :expires_in, :irreversible, :exclude_follows, :exclude_localusers, :with_quote, :whole_word, context: [])
params.permit(:phrase, :expires_in, :irreversible, :exclude_follows, :exclude_localusers, :with_quote, :with_profile, :whole_word, context: [])
end

def filter_params
resource_params.slice(:phrase, :expires_in, :irreversible, :exclude_follows, :exclude_localusers, :with_quote, :context)
resource_params.slice(:phrase, :expires_in, :irreversible, :exclude_follows, :exclude_localusers, :with_quote, :with_profile, :context)
end

def keyword_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v2/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def set_filter
end

def resource_params
params.permit(:title, :expires_in, :filter_action, :exclude_follows, :exclude_localusers, :with_quote, context: [], keywords_attributes: [:id, :keyword, :whole_word, :_destroy])
params.permit(:title, :expires_in, :filter_action, :exclude_follows, :exclude_localusers, :with_quote, :with_profile, context: [], keywords_attributes: [:id, :keyword, :whole_word, :_destroy])
end
end
2 changes: 1 addition & 1 deletion app/controllers/filters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def set_filter
end

def resource_params
params.require(:custom_filter).permit(:title, :expires_in, :filter_action, :exclude_follows, :exclude_localusers, :exclude_quote, context: [], keywords_attributes: [:id, :keyword, :whole_word, :_destroy])
params.require(:custom_filter).permit(:title, :expires_in, :filter_action, :exclude_follows, :exclude_localusers, :exclude_quote, :exclude_profile, context: [], keywords_attributes: [:id, :keyword, :whole_word, :_destroy])
end

def set_body_classes
Expand Down
10 changes: 10 additions & 0 deletions app/models/custom_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# exclude_follows :boolean default(FALSE), not null
# exclude_localusers :boolean default(FALSE), not null
# with_quote :boolean default(TRUE), not null
# with_profile :boolean default(FALSE), not null
#

class CustomFilter < ApplicationRecord
Expand Down Expand Up @@ -76,6 +77,14 @@ def exclude_quote
!with_quote
end

def exclude_profile=(value)
self.with_profile = !ActiveModel::Type::Boolean.new.cast(value)
end

def exclude_profile
!with_profile
end

def self.cached_filters_for(account_id)
active_filters = Rails.cache.fetch("filters:v3:#{account_id}") do
filters_hash = {}
Expand Down Expand Up @@ -111,6 +120,7 @@ def self.apply_cached_filters(cached_filters, status, following: false)

if rules[:keywords].present?
match = rules[:keywords].match(status.proper.searchable_text)
match = rules[:keywords].match([status.account.display_name, status.account.note].join("\n\n")) if !match && filter.with_profile
if match.nil? && filter.with_quote && status.proper.reference_objects.exists?
references_text_cache = status.proper.references.pluck(:text).join("\n\n") if references_text_cache.nil?
references_spoiler_text_cache = status.proper.references.pluck(:spoiler_text).join("\n\n") if references_spoiler_text_cache.nil?
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/rest/filter_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class REST::FilterSerializer < ActiveModel::Serializer
attributes :id, :title, :exclude_follows, :exclude_localusers, :with_quote, :context, :expires_at, :filter_action, :filter_action_ex
attributes :id, :title, :exclude_follows, :exclude_localusers, :with_quote, :with_profile, :context, :expires_at, :filter_action, :filter_action_ex
has_many :keywords, serializer: REST::FilterKeywordSerializer, if: :rules_requested?
has_many :statuses, serializer: REST::FilterStatusSerializer, if: :rules_requested?

Expand Down
1 change: 1 addition & 0 deletions app/views/filters/_filter_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

.fields-group
= f.input :exclude_quote, wrapper: :with_label, as: :boolean, kmyblue: true, label: t('simple_form.labels.filters.options.exclude_quote')
= f.input :exclude_profile, wrapper: :with_label, as: :boolean, kmyblue: true, label: t('simple_form.labels.filters.options.exclude_profile')

%hr.spacer/

Expand Down
1 change: 1 addition & 0 deletions config/locales/simple_form.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ en:
options:
exclude_follows: Exclude following users
exclude_localusers: Exclude local users
exclude_profile: Exclude account name and bio
exclude_quote: Exclude quote or references
form_admin_settings:
activity_api_enabled: Publish aggregate statistics about user activity in the API
Expand Down
1 change: 1 addition & 0 deletions config/locales/simple_form.ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ ja:
options:
exclude_follows: フォロー中のユーザーをフィルターの対象にしない
exclude_localusers: ローカルユーザーをフィルターの対象にしない
exclude_profile: アカウント名および紹介文をフィルターの対象にしない
exclude_quote: 引用の内容をフィルターの対象にしない
form_admin_settings:
activity_api_enabled: APIでユーザーアクティビティに関する集計統計を公開する
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddWithProfileToCustomFilters < ActiveRecord::Migration[7.1]
def change
add_column :custom_filters, :with_profile, :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_06_07_094856) do
ActiveRecord::Schema[7.1].define(version: 2024_07_09_063700) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -539,6 +539,7 @@
t.boolean "exclude_follows", default: false, null: false
t.boolean "exclude_localusers", default: false, null: false
t.boolean "with_quote", default: true, null: false
t.boolean "with_profile", default: false, null: false
t.index ["account_id"], name: "index_custom_filters_on_account_id"
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 @@ -14,6 +14,7 @@ namespace :dangerous do
end

target_migrations = %w(
20240709063700
20240426233435
20240426000034
20240401222541
Expand Down Expand Up @@ -149,6 +150,7 @@ namespace :dangerous do
%w(custom_emojis license),
%w(custom_filters exclude_follows),
%w(custom_filters exclude_localusers),
%w(custom_filters with_profile),
%w(custom_filters with_quote),
%w(domain_blocks block_trends),
%w(domain_blocks detect_invalid_subscription),
Expand Down
6 changes: 5 additions & 1 deletion spec/requests/api/v2/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
filter_action: 'hide',
exclude_follows: true,
exclude_localusers: true,
with_profile: true,
keywords_attributes: [keyword: 'magic', whole_word: true],
}
end
Expand All @@ -75,6 +76,7 @@
expect(json[:keywords].map { |keyword| keyword.slice(:keyword, :whole_word) }).to eq [{ keyword: 'magic', whole_word: true }]
expect(json[:exclude_follows]).to be true
expect(json[:exclude_localusers]).to be true
expect(json[:with_profile]).to be true
end

it 'creates a filter', :aggregate_failures do
Expand All @@ -87,6 +89,7 @@
expect(filter.context).to eq %w(home)
expect(filter.exclude_follows).to be true
expect(filter.exclude_localusers).to be true
expect(filter.with_profile).to be true
expect(filter.irreversible?).to be true
expect(filter.expires_at).to be_nil
end
Expand Down Expand Up @@ -175,7 +178,7 @@

context 'when updating filter parameters' do
context 'with valid params' do
let(:params) { { title: 'updated', context: %w(home public), exclude_follows: true, exclude_localusers: true } }
let(:params) { { title: 'updated', context: %w(home public), exclude_follows: true, exclude_localusers: true, exclude_profile: true } }

it 'updates the filter successfully', :aggregate_failures do
subject
Expand All @@ -187,6 +190,7 @@
expect(filter.reload.context).to eq %w(home public)
expect(filter.exclude_follows).to be true
expect(filter.exclude_localusers).to be true
expect(filter.exclude_profile).to be true
end
end

Expand Down
7 changes: 5 additions & 2 deletions streaming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ const startServer = async () => {
// @ts-ignore
if (!payload.filtered && !req.cachedFilters) {
// @ts-ignore
queries.push(client.query('SELECT filter.id AS id, filter.phrase AS title, filter.context AS context, filter.expires_at AS expires_at, filter.action AS filter_action, filter.with_quote AS with_quote, keyword.keyword AS keyword, keyword.whole_word AS whole_word, filter.exclude_follows AS exclude_follows, filter.exclude_localusers AS exclude_localusers FROM custom_filter_keywords keyword JOIN custom_filters filter ON keyword.custom_filter_id = filter.id WHERE filter.account_id = $1 AND (filter.expires_at IS NULL OR filter.expires_at > NOW())', [req.accountId]));
queries.push(client.query('SELECT filter.id AS id, filter.phrase AS title, filter.context AS context, filter.expires_at AS expires_at, filter.action AS filter_action, filter.with_quote AS with_quote, filter.with_profile AS with_profile, keyword.keyword AS keyword, keyword.whole_word AS whole_word, filter.exclude_follows AS exclude_follows, filter.exclude_localusers AS exclude_localusers FROM custom_filter_keywords keyword JOIN custom_filters filter ON keyword.custom_filter_id = filter.id WHERE filter.account_id = $1 AND (filter.expires_at IS NULL OR filter.expires_at > NOW())', [req.accountId]));
}
if (!payload.filtered) {
// @ts-ignore
Expand Down Expand Up @@ -989,6 +989,7 @@ const startServer = async () => {
filter_action: filter.filter_action === 2 ? 'warn' : ['warn', 'hide', 'half_warn'][filter.filter_action],
filter_action_ex: ['warn', 'hide', 'half_warn'][filter.filter_action],
with_quote: filter.with_quote,
withAccountName: filter.with_profile,
excludeFollows: filter.exclude_follows,
excludeLocalusers: filter.exclude_localusers,
},
Expand Down Expand Up @@ -1032,6 +1033,7 @@ const startServer = async () => {
// @ts-ignore
const searchableContent = ([status.spoiler_text || '', status.content, ...(reference_texts || [])].concat((status.poll && status.poll.options) ? status.poll.options.map(option => option.title) : [])).concat(status.media_attachments.map(att => att.description)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
const searchableTextContent = JSDOM.fragment(searchableContent).textContent;
const searchableAccountContent = JSDOM.fragment([status.account.display_name, status.account.note].join('\n\n')).textContent;

const now = new Date();
// @ts-ignore
Expand All @@ -1054,7 +1056,8 @@ const startServer = async () => {
return results;
}

const keyword_matches = searchableTextContent.match(cachedFilter.regexp);
const keyword_matches = searchableTextContent.match(cachedFilter.regexp) ||
((cachedFilter.withAccountName && searchableAccountContent) ? searchableAccountContent.match(cachedFilter.regexp) : null);
if (keyword_matches) {
// results is an Array of FilterResult; status_matches is always
// null as we only are only applying the keyword-based custom
Expand Down

0 comments on commit 8e59cd0

Please sign in to comment.