2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
class WatchedWord < ActiveRecord::Base
|
|
|
|
|
|
|
|
def self.actions
|
|
|
|
@actions ||= Enum.new(
|
|
|
|
block: 1,
|
|
|
|
censor: 2,
|
|
|
|
require_approval: 3,
|
2021-02-25 20:00:58 +08:00
|
|
|
flag: 4,
|
2021-06-02 13:36:49 +08:00
|
|
|
link: 8,
|
2021-03-03 16:53:38 +08:00
|
|
|
replace: 5,
|
|
|
|
tag: 6,
|
2021-06-02 13:36:49 +08:00
|
|
|
silence: 7,
|
2017-06-29 04:56:44 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-12-16 23:13:15 +08:00
|
|
|
MAX_WORDS_PER_ACTION = 2000
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.word = self.class.normalize_word(self.word)
|
2021-06-02 13:36:49 +08:00
|
|
|
if self.action == WatchedWord.actions[:link] && !(self.replacement =~ /^https?:\/\//)
|
2021-06-03 09:36:07 +08:00
|
|
|
self.replacement = "#{Discourse.base_url}#{self.replacement&.starts_with?("/") ? "" : "/"}#{self.replacement}"
|
2021-06-02 13:36:49 +08:00
|
|
|
end
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
2020-12-09 14:45:34 +08:00
|
|
|
validates :word, presence: true, uniqueness: true, length: { maximum: 100 }
|
2017-06-29 04:56:44 +08:00
|
|
|
validates :action, presence: true
|
2021-06-02 13:36:49 +08:00
|
|
|
|
|
|
|
validate :replacement_is_url, if: -> { action == WatchedWord.actions[:link] }
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
validates_each :word do |record, attr, val|
|
|
|
|
if WatchedWord.where(action: record.action).count >= MAX_WORDS_PER_ACTION
|
|
|
|
record.errors.add(:word, :too_many)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_save :clear_cache
|
|
|
|
after_destroy :clear_cache
|
|
|
|
|
|
|
|
scope :by_action, -> { order("action ASC, word ASC") }
|
|
|
|
|
|
|
|
def self.normalize_word(w)
|
2018-01-10 05:51:45 +08:00
|
|
|
w.strip.squeeze('*')
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
2021-06-02 13:36:49 +08:00
|
|
|
def replacement_is_url
|
|
|
|
if !(replacement =~ URI::regexp)
|
|
|
|
errors.add(:base, :invalid_url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
def self.create_or_update_word(params)
|
2018-01-10 05:51:45 +08:00
|
|
|
new_word = normalize_word(params[:word])
|
|
|
|
w = WatchedWord.where("word ILIKE ?", new_word).first || WatchedWord.new(word: new_word)
|
2021-02-25 20:00:58 +08:00
|
|
|
w.replacement = params[:replacement] if params[:replacement]
|
2017-06-29 04:56:44 +08:00
|
|
|
w.action_key = params[:action_key] if params[:action_key]
|
|
|
|
w.action = params[:action] if params[:action]
|
|
|
|
w.save
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
2021-03-23 04:32:18 +08:00
|
|
|
def self.has_replacement?(action)
|
2021-06-02 13:36:49 +08:00
|
|
|
action == :replace || action == :tag || action == :link
|
2021-03-23 04:32:18 +08:00
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
def action_key=(arg)
|
|
|
|
self.action = self.class.actions[arg.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
WordWatcher.clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2017-07-28 10:37:10 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: watched_words
|
|
|
|
#
|
2021-02-25 20:00:58 +08:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# word :string not null
|
|
|
|
# action :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# replacement :string
|
2017-07-28 10:37:10 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_watched_words_on_action_and_word (action,word) UNIQUE
|
|
|
|
#
|