1
0
Fork 0
forked from gitea/nas

Change: #591 ホワイトリストのドメイン一覧の保存先・画面変更 (#689)

* Change: #591 ホワイトリストのドメイン一覧の保存先・画面変更

* Update account_batch.rb

* 表示まわりを改善

* Update dangerous.rake
This commit is contained in:
KMY(雪あすか) 2024-04-03 12:09:43 +09:00 committed by GitHub
parent 8c399cefce
commit ff2860d0df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 211 additions and 49 deletions

View file

@ -2,10 +2,33 @@
module Admin
class NgWords::WhiteListController < NgWordsController
def show
super
@white_list_domains = SpecifiedDomain.white_list_domain_caches.presence || [SpecifiedDomain.new]
end
protected
def validate
begin
SpecifiedDomain.save_from_raws_as_white_list(settings_params_list)
return true
rescue
flash[:alert] = I18n.t('admin.ng_words.save_error')
redirect_to after_update_redirect_path
end
false
end
def after_update_redirect_path
admin_ng_words_white_list_path
end
private
def settings_params_list
params.require(:form_admin_settings)[:specified_domains]
end
end
end

View file

@ -316,40 +316,21 @@ const removeTableRow = (target: EventTarget | null, tableId: string) => {
tableElement.removeChild(tableRowElement);
};
Rails.delegate(
document,
'#sensitive-words-table .add-row-button',
'click',
(ev) => {
const setupTableList = (id: string) => {
Rails.delegate(document, `#${id} .add-row-button`, 'click', (ev) => {
ev.preventDefault();
addTableRow('sensitive-words-table');
},
);
addTableRow(id);
});
Rails.delegate(
document,
'#sensitive-words-table .delete-row-button',
'click',
(ev) => {
Rails.delegate(document, `#${id} .delete-row-button`, 'click', (ev) => {
ev.preventDefault();
removeTableRow(ev.target, 'sensitive-words-table');
},
);
removeTableRow(ev.target, id);
});
};
Rails.delegate(document, '#ng-words-table .add-row-button', 'click', (ev) => {
ev.preventDefault();
addTableRow('ng-words-table');
});
Rails.delegate(
document,
'#ng-words-table .delete-row-button',
'click',
(ev) => {
ev.preventDefault();
removeTableRow(ev.target, 'ng-words-table');
},
);
setupTableList('sensitive-words-table');
setupTableList('ng-words-table');
setupTableList('white-list-table');
async function mountReactComponent(element: Element) {
const componentName = element.getAttribute('data-admin-component');

View file

@ -98,10 +98,10 @@ class Form::AccountBatch
def approve_remote_domain!
domains = accounts.group_by(&:domain).pluck(0)
if (Setting.permit_new_account_domains || []).compact_blank.present?
list = ((Setting.permit_new_account_domains || []) + domains).compact_blank.uniq.join("\n")
Form::AdminSettings.new(permit_new_account_domains: list).save
(domains - SpecifiedDomain.where(domain: domains, table: 0).pluck(:domain)).each do |domain|
SpecifiedDomain.create!(domain: domain, table: 0)
end
Account.where(domain: domains, remote_pending: true).find_each do |account|
approve_remote_account(account)
end

View file

@ -61,7 +61,6 @@ class Form::AdminSettings
unlocked_friend
enable_local_timeline
emoji_reaction_disallow_domains
permit_new_account_domains
block_unfollow_account_mention
hold_remote_new_accounts
).freeze
@ -121,7 +120,6 @@ class Form::AdminSettings
STRING_ARRAY_KEYS = %i(
emoji_reaction_disallow_domains
permit_new_account_domains
).freeze
attr_accessor(*KEYS)

View file

@ -0,0 +1,78 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: specified_domains
#
# id :bigint(8) not null, primary key
# domain :string not null
# table :integer default(0), not null
# options :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
#
class SpecifiedDomain < ApplicationRecord
attr_accessor :domains
validates :domain, uniqueness: { scope: :table }
after_commit :invalidate_cache!
scope :white_list_domains, -> { where(table: 0) }
class << self
def white_list_domain_caches
Rails.cache.fetch('specified_domains:white_list') { white_list_domains.to_a }
end
def save_from_hashes(rows, type, caches)
unmatched = caches
matched = []
SpecifiedDomain.transaction do
rows.filter { |item| item[:domain].present? }.each do |item|
exists = unmatched.find { |i| i.domain == item[:domain] }
if exists.present?
unmatched.delete(exists)
matched << exists
next unless item.key?(:options) && item[:options] == exists.options
exists.update!(options: item[:options])
elsif matched.none? { |i| i.domain == item[:domain] }
SpecifiedDomain.create!(
domain: item[:domain],
table: type,
options: item[:options] || {}
)
end
end
SpecifiedDomain.destroy(unmatched.map(&:id))
end
true
end
def save_from_raws(rows, type, caches)
hashes = (rows['domains'] || []).map do |domain|
{
domain: domain,
type: type,
}
end
save_from_hashes(hashes, type, caches)
end
def save_from_raws_as_white_list(rows)
save_from_raws(rows, 0, white_list_domain_caches)
end
end
private
def invalidate_cache!
Rails.cache.delete('specified_domains:white_list')
end
end

View file

@ -142,11 +142,7 @@ class ActivityPub::ProcessAccountService < BaseService
def blocking_new_account?
return false unless Setting.hold_remote_new_accounts
permit_new_account_domains.exclude?(@domain)
end
def permit_new_account_domains
(Setting.permit_new_account_domains || []).compact_blank
SpecifiedDomain.white_list_domain_caches.none? { |item| item.domain == @domain }
end
def valid_account?

View file

@ -0,0 +1,6 @@
- temporary_id = defined?(@temp_id) ? @temp_id += 1 : @temp_id = 1
%tr{ class: template ? 'template-row' : nil }
%td= f.input :domains, as: :string, input_html: { multiple: true, value: specified_domain.domain }
%td
= hidden_field_tag :'form_admin_settings[specified_domains][temporary_ids][]', temporary_id, class: 'temporary_id'
= link_to safe_join([fa_icon('times'), t('filters.index.delete')]), '#', class: 'table-action-link delete-row-button'

View file

@ -18,8 +18,24 @@
.fields-group
= f.input :hold_remote_new_accounts, wrapper: :with_label, as: :boolean, label: t('admin.ng_words.hold_remote_new_accounts'), hint: t('admin.ng_words.remote_approval_hint')
.fields-group
= f.input :permit_new_account_domains, wrapper: :with_label, as: :text, kmyblue: true, input_html: { rows: 6 }, label: t('admin.ng_words.permit_new_account_domains')
%h4= t('admin.ng_words.white_list_header')
.table-wrapper
%table.table.keywords-table#white-list-table
%thead
%tr
%th= t('simple_form.labels.defaults.domain')
%th
%tbody
= f.simple_fields_for :specified_domains, @white_list_domains do |domain|
= render partial: 'specified_domain', collection: @white_list_domains, locals: { f: domain, template: false }
= f.simple_fields_for :specified_domains, @white_list_domains do |domain|
= render partial: 'specified_domain', collection: [SpecifiedDomain.new], locals: { f: domain, template: true }
%tfoot
%tr
%td{ colspan: 2 }
= link_to safe_join([fa_icon('plus'), t('admin.ng_words.edit.add_domain')]), '#', class: 'table-action-link add-row-button'
.actions
= f.button :button, t('generic.save_changes'), type: :submit