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.
Browse files
Browse the repository at this point in the history
* Change: #648 センシティブワードの入力フォーム * Wip: 行の追加削除 * Wip: 設定の保存、マイグレーション * 不要な処理を削除 * マイグレーションコード調整
- Loading branch information
Showing
17 changed files
with
380 additions
and
64 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
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: sensitive_words | ||
# | ||
# id :bigint(8) not null, primary key | ||
# keyword :string not null | ||
# regexp :boolean default(FALSE), not null | ||
# remote :boolean default(FALSE), not null | ||
# spoiler :boolean default(TRUE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class SensitiveWord < ApplicationRecord | ||
attr_accessor :keywords, :regexps, :remotes, :spoilers | ||
|
||
class << self | ||
def caches | ||
Rails.cache.fetch('sensitive_words') { SensitiveWord.where.not(id: 0).order(:keyword).to_a } | ||
end | ||
|
||
def save_from_hashes(rows) | ||
unmatched = caches | ||
matched = [] | ||
|
||
SensitiveWord.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.remote == item[:remote] && exists.spoiler == item[:spoiler] | ||
|
||
exists.update!(regexp: item[:regexp], remote: item[:remote], spoiler: item[:spoiler]) | ||
elsif matched.none? { |i| i.keyword == item[:keyword] } | ||
SensitiveWord.create!( | ||
keyword: item[:keyword], | ||
regexp: item[:regexp], | ||
remote: item[:remote], | ||
spoiler: item[:spoiler] | ||
) | ||
end | ||
end | ||
|
||
SensitiveWord.destroy(unmatched.map(&:id)) | ||
end | ||
|
||
true | ||
# rescue | ||
# false | ||
end | ||
|
||
def save_from_raws(rows) | ||
regexps = rows['regexps'] || [] | ||
remotes = rows['remotes'] || [] | ||
spoilers = rows['spoilers'] || [] | ||
|
||
hashes = (rows['keywords'] || []).zip(rows['temporary_ids'] || []).map do |item| | ||
temp_id = item[1] | ||
{ | ||
keyword: item[0], | ||
regexp: regexps.include?(temp_id), | ||
remote: remotes.include?(temp_id), | ||
spoiler: spoilers.include?(temp_id), | ||
} | ||
end | ||
|
||
save_from_hashes(hashes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def invalidate_cache! | ||
Rails.cache.delete('sensitive_words') | ||
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,12 @@ | ||
- temporary_id = defined?(@temp_id) ? @temp_id += 1 : @temp_id = 1 | ||
%tr{ class: template ? 'template-row' : nil } | ||
%td= f.input :keywords, as: :string, input_html: { multiple: true, value: sensitive_word.keyword } | ||
%td | ||
.label_input__wrapper= f.check_box :regexps, { multiple: true, checked: sensitive_word.regexp }, temporary_id, nil | ||
%td | ||
.label_input__wrapper= f.check_box :remotes, { multiple: true, checked: sensitive_word.remote }, temporary_id, nil | ||
%td | ||
.label_input__wrapper= f.check_box :spoilers, { multiple: true, checked: sensitive_word.spoiler }, temporary_id, nil | ||
%td | ||
= hidden_field_tag :'form_admin_settings[sensitive_words_test][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' |
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
Oops, something went wrong.