forked from mastodon/mastodon
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Change: #647 NGワードの入力フォーム * Wip: 画面改造 * テストコード、画面 * Fix: 複数の問題
- Loading branch information
Showing
33 changed files
with
526 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class NgWords::KeywordsController < NgWordsController | ||
def show | ||
super | ||
@ng_words = ::NgWord.caches.presence || [::NgWord.new] | ||
end | ||
|
||
protected | ||
|
||
def validate | ||
begin | ||
::NgWord.save_from_raws(settings_params_test) | ||
return true | ||
rescue | ||
flash[:alert] = I18n.t('admin.ng_words.test_error') | ||
redirect_to after_update_redirect_path | ||
end | ||
|
||
false | ||
end | ||
|
||
private | ||
|
||
def after_update_redirect_path | ||
admin_ng_words_keywords_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class NgWords::SettingsController < NgWordsController | ||
protected | ||
|
||
def after_update_redirect_path | ||
admin_ng_words_settings_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class NgWords::WhiteListController < NgWordsController | ||
protected | ||
|
||
def after_update_redirect_path | ||
admin_ng_words_white_list_path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: ng_words | ||
# | ||
# id :bigint(8) not null, primary key | ||
# keyword :string not null | ||
# regexp :boolean default(FALSE), not null | ||
# stranger :boolean default(TRUE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class NgWord < ApplicationRecord | ||
attr_accessor :keywords, :regexps, :strangers | ||
|
||
validate :check_regexp | ||
|
||
class << self | ||
def caches | ||
Rails.cache.fetch('ng_words') { NgWord.where.not(id: 0).order(:keyword).to_a } | ||
end | ||
|
||
def save_from_hashes(rows) | ||
unmatched = caches | ||
matched = [] | ||
|
||
NgWord.transaction do | ||
rows.filter { |item| item[:keyword].present? }.each do |item| | ||
exists = unmatched.find { |i| i.keyword == item[:keyword] } | ||
|
||
if exists.present? | ||
unmatched.delete(exists) | ||
matched << exists | ||
|
||
next if exists.regexp == item[:regexp] && exists.stranger == item[:stranger] | ||
|
||
exists.update!(regexp: item[:regexp], stranger: item[:stranger]) | ||
elsif matched.none? { |i| i.keyword == item[:keyword] } | ||
NgWord.create!( | ||
keyword: item[:keyword], | ||
regexp: item[:regexp], | ||
stranger: item[:stranger] | ||
) | ||
end | ||
end | ||
|
||
NgWord.destroy(unmatched.map(&:id)) | ||
end | ||
|
||
true | ||
end | ||
|
||
def save_from_raws(rows) | ||
regexps = rows['regexps'] || [] | ||
strangers = rows['strangers'] || [] | ||
|
||
hashes = (rows['keywords'] || []).zip(rows['temporary_ids'] || []).map do |item| | ||
temp_id = item[1] | ||
{ | ||
keyword: item[0], | ||
regexp: regexps.include?(temp_id), | ||
stranger: strangers.include?(temp_id), | ||
} | ||
end | ||
|
||
save_from_hashes(hashes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def invalidate_cache! | ||
Rails.cache.delete('ng_words') | ||
end | ||
|
||
def check_regexp | ||
return if keyword.blank? || !regexp | ||
|
||
begin | ||
Admin::NgWord.reject_with_custom_word?('Sample text', keyword) | ||
rescue | ||
raise Mastodon::ValidationError, I18n.t('admin.ng_words.test_error') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.