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

Add: #568 ハッシュタグ・メンション上限を超えた投稿もNGワード履歴に記録 #579

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
6 changes: 3 additions & 3 deletions app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def process_status_params

def valid_status?
valid = !Admin::NgWord.reject?("#{@params[:spoiler_text]}\n#{@params[:text]}", uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?)
valid = !Admin::NgWord.hashtag_reject?(@tags.size) if valid
valid = !Admin::NgWord.mention_reject?(@mentions.count { |m| !m.silent }) if valid
valid = !Admin::NgWord.stranger_mention_reject_with_count?(@mentions.count { |m| !m.silent }) if valid && (mention_to_local_stranger? || reference_to_local_stranger?)
valid = !Admin::NgWord.hashtag_reject?(@tags.size, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid
valid = !Admin::NgWord.mention_reject?(@mentions.count { |m| !m.silent }, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid
valid = !Admin::NgWord.stranger_mention_reject_with_count?(@mentions.count { |m| !m.silent }, uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?, text: "#{@params[:spoiler_text]}\n#{@params[:text]}") if valid && (mention_to_local_stranger? || reference_to_local_stranger?)
valid = !Admin::NgWord.stranger_mention_reject?("#{@params[:spoiler_text]}\n#{@params[:text]}", uri: @params[:uri], target_type: :status, public: @status_parser.distributable_visibility?) if valid && (mention_to_local_stranger? || reference_to_local_stranger?)

valid
Expand Down
32 changes: 26 additions & 6 deletions app/models/admin/ng_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@ def reject_with_custom_words?(text, custom_ng_words)
custom_ng_words.any? { |word| include?(text, word) }
end

def hashtag_reject?(hashtag_count)
post_hash_tags_max.positive? && post_hash_tags_max < hashtag_count
def hashtag_reject?(hashtag_count, **options)
hit = post_hash_tags_max.positive? && post_hash_tags_max < hashtag_count
record_count!(:hashtag_count, hashtag_count, options) if hit
hit
end

def hashtag_reject_with_extractor?(text)
hashtag_reject?(Extractor.extract_hashtags(text)&.size || 0)
end

def mention_reject?(mention_count)
post_mentions_max.positive? && post_mentions_max < mention_count
def mention_reject?(mention_count, **options)
hit = post_mentions_max.positive? && post_mentions_max < mention_count
record_count!(:mention_count, mention_count, options) if hit
hit
end

def mention_reject_with_extractor?(text)
mention_reject?(text.gsub(Account::MENTION_RE)&.count || 0)
end

def stranger_mention_reject_with_count?(mention_count)
post_stranger_mentions_max.positive? && post_stranger_mentions_max < mention_count
def stranger_mention_reject_with_count?(mention_count, **options)
hit = post_stranger_mentions_max.positive? && post_stranger_mentions_max < mention_count
record_count!(:stranger_mention_count, mention_count, options) if hit
hit
end

def stranger_mention_reject_with_extractor?(text)
Expand Down Expand Up @@ -87,5 +93,19 @@ def record!(type, text, keyword, options)
keyword: keyword,
}))
end

def record_count!(type, count, options)
return unless options[:text] && options[:uri] && options[:target_type]
return if options.key?(:public) && !options.delete(:public)

return if NgwordHistory.where('created_at > ?', 1.day.ago).exists?(uri: options[:uri], reason: type)

NgwordHistory.create(options.merge({
reason: type,
text: options[:text],
keyword: '',
count: count,
}))
end
end
end
3 changes: 2 additions & 1 deletion app/models/ngword_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# keyword :string not null
# created_at :datetime not null
# updated_at :datetime not null
# count :integer default(0), not null
#
class NgwordHistory < ApplicationRecord
include Paginable

enum target_type: { status: 0, account_note: 1, account_name: 2 }, _suffix: :blocked
enum reason: { ng_words: 0, ng_words_for_stranger_mention: 1 }, _prefix: :within
enum reason: { ng_words: 0, ng_words_for_stranger_mention: 1, hashtag_count: 2, mention_count: 3, stranger_mention_count: 4 }, _prefix: :within
end
6 changes: 3 additions & 3 deletions app/services/activitypub/process_status_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def valid_status?
end

def validate_status_mentions!
raise AbortError if (mention_to_stranger? || reference_to_stranger?) && Admin::NgWord.stranger_mention_reject?("#{@status.spoiler_text}\n#{@status.text}", uri: @status.uri, target_type: :status)
raise AbortError if Admin::NgWord.mention_reject?(@raw_mentions.size)
raise AbortError if (mention_to_stranger? || reference_to_stranger?) && Admin::NgWord.stranger_mention_reject_with_count?(@raw_mentions.size)
raise AbortError if (mention_to_stranger? || reference_to_stranger?) && Admin::NgWord.stranger_mention_reject?("#{@status_parser.spoiler_text}\n#{@status_parser.text}", uri: @status.uri, target_type: :status)
raise AbortError if Admin::NgWord.mention_reject?(@raw_mentions.size, uri: @status.uri, target_type: :status, text: "#{@status_parser.spoiler_text}\n#{@status_parser.text}")
raise AbortError if (mention_to_stranger? || reference_to_stranger?) && Admin::NgWord.stranger_mention_reject_with_count?(@raw_mentions.size, uri: @status.uri, target_type: :status, text: "#{@status_parser.spoiler_text}\n#{@status_parser.text}")
end

def mention_to_stranger?
Expand Down
20 changes: 13 additions & 7 deletions app/views/admin/ngword_histories/_history.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@
= html_aware_format(history.text, false)

.detailed-status__meta
%span.negative-hint= history.keyword
- if history.within_ng_words? || history.within_ng_words_for_stranger_mention?
%span.negative-hint= history.keyword
- else
%span.negative-hint= history.count.to_s
·
- if history.within_ng_words?
= t('admin.ng_words.keywords')
- elsif history.within_ng_words_for_stranger_mention?
= t('admin.ng_words.keywords_for_stranger_mention')
- elsif history.within_hashtag_count?
= t('admin.ng_words.post_hash_tags_max')
- elsif history.within_mention_count?
= t('admin.ng_words.post_mentions_max')
- elsif history.within_stranger_mention_count?
= t('admin.ng_words.post_stranger_mentions_max')

%br/

%time.formatted{ datetime: history.created_at.iso8601, title: l(history.created_at) }= l(history.created_at)
·
- if history.account_note_blocked?
= t('admin.ngword_history.target_types.account_note')
= t('admin.ngword_histories.target_types.account_note')
- elsif history.account_name_blocked?
= t('admin.ngword_history.target_types.account_name')
= t('admin.ngword_histories.target_types.account_name')
- elsif history.status_blocked?
= t('admin.ngword_history.target_types.status')
= t('admin.ngword_histories.target_types.status')
·
= history.uri
-# if history.application
= history.application.name
·
11 changes: 11 additions & 0 deletions db/migrate/20240217022038_add_count_to_ngword_histories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

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

def change
add_column :ngword_histories, :count, :integer, null: false, default: 0

add_index :ngword_histories, [:uri, :reason, :created_at], unique: false, algorithm: :concurrently
end
end
4 changes: 3 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_02_16_042730) do
ActiveRecord::Schema[7.1].define(version: 2024_02_17_022038) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -876,7 +876,9 @@
t.string "keyword", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "count", default: 0, null: false
t.index ["uri", "keyword", "created_at"], name: "index_ngword_histories_on_uri_and_keyword_and_created_at"
t.index ["uri", "reason", "created_at"], name: "index_ngword_histories_on_uri_and_reason_and_created_at"
end

create_table "notifications", force: :cascade do |t|
Expand Down
3 changes: 3 additions & 0 deletions lib/tasks/dangerous.rake
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace :dangerous do
20240121231131
20240212224800
20240212230358
20240216042730
20240217022038
)
# Removed: account_groups
target_tables = %w(
Expand All @@ -100,6 +102,7 @@ namespace :dangerous do
friend_domains
instance_infos
list_statuses
ngword_histories
scheduled_expiration_statuses
status_capability_tokens
status_references
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/activitypub/activity/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,7 @@ def activity_for_object(json)
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
to: 'https://www.w3.org/ns/activitystreams#Public',
tag: [
{
type: 'Hashtag',
Expand All @@ -2055,6 +2056,9 @@ def activity_for_object(json)
context 'when limit is enough' do
it 'creates status' do
expect(sender.statuses.first).to_not be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to be_nil
end
end

Expand All @@ -2063,6 +2067,13 @@ def activity_for_object(json)

it 'creates status' do
expect(sender.statuses.first).to be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to_not be_nil
expect(history.status_blocked?).to be true
expect(history.within_hashtag_count?).to be true
expect(history.count).to eq 2
expect(history.text).to eq "\nLorem ipsum"
end
end
end
Expand All @@ -2080,6 +2091,7 @@ def activity_for_object(json)
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
to: 'https://www.w3.org/ns/activitystreams#Public',
tag: [
{
type: 'Mention',
Expand Down Expand Up @@ -2110,6 +2122,9 @@ def activity_for_object(json)
context 'when limit is enough' do
it 'creates status' do
expect(sender.statuses.first).to_not be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to be_nil
end
end

Expand All @@ -2118,6 +2133,12 @@ def activity_for_object(json)

it 'creates status' do
expect(sender.statuses.first).to be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to_not be_nil
expect(history.status_blocked?).to be true
expect(history.within_mention_count?).to be true
expect(history.count).to eq 3
end
end

Expand All @@ -2126,6 +2147,9 @@ def activity_for_object(json)

it 'creates status' do
expect(sender.statuses.first).to_not be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to be_nil
end
end

Expand All @@ -2135,6 +2159,12 @@ def activity_for_object(json)

it 'creates status' do
expect(sender.statuses.first).to be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to_not be_nil
expect(history.status_blocked?).to be true
expect(history.within_stranger_mention_count?).to be true
expect(history.count).to eq 3
end
end
end
Expand All @@ -2147,6 +2177,7 @@ def activity_for_object(json)
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
type: 'Note',
content: 'Lorem ipsum',
to: 'https://www.w3.org/ns/activitystreams#Public',
tag: [
{
type: 'Mention',
Expand All @@ -2168,6 +2199,9 @@ def activity_for_object(json)
context 'when limit is enough' do
it 'creates status' do
expect(sender.statuses.first).to_not be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to be_nil
end
end

Expand All @@ -2176,6 +2210,12 @@ def activity_for_object(json)

it 'creates status' do
expect(sender.statuses.first).to be_nil

history = NgwordHistory.find_by(uri: object_json[:id])
expect(history).to_not be_nil
expect(history.status_blocked?).to be true
expect(history.within_stranger_mention_count?).to be true
expect(history.count).to eq 2
end
end
end
Expand Down
Loading