92 lines
2.4 KiB
Ruby
92 lines
2.4 KiB
Ruby
# 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
|
|
|
|
validate :check_regexp
|
|
after_commit :invalidate_cache!
|
|
|
|
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
|
|
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
|
|
|
|
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
|