2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
class WatchedWord < ActiveRecord::Base
|
2020-12-16 10:13:15 -05:00
|
|
|
MAX_WORDS_PER_ACTION = 2000
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2023-11-01 10:41:10 -04:00
|
|
|
before_validation { self.word = WatchedWord.normalize_word(self.word) }
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
before_validation do
|
2023-11-01 10:41:10 -04:00
|
|
|
if self.action == WatchedWord.actions[:link] && self.replacement !~ %r{\Ahttps?://}
|
2021-06-02 21:36:07 -04:00
|
|
|
self.replacement =
|
|
|
|
"#{Discourse.base_url}#{self.replacement&.starts_with?("/") ? "" : "/"}#{self.replacement}"
|
2021-06-02 01:36:49 -04:00
|
|
|
end
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2020-12-09 01:45:34 -05:00
|
|
|
validates :word, presence: true, uniqueness: true, length: { maximum: 100 }
|
2017-06-28 16:56:44 -04:00
|
|
|
validates :action, presence: true
|
2021-06-02 01:36:49 -04:00
|
|
|
validate :replacement_is_url, if: -> { action == WatchedWord.actions[:link] }
|
2022-06-27 15:16:33 -04:00
|
|
|
validate :replacement_is_tag_list, if: -> { action == WatchedWord.actions[:tag] }
|
2024-05-06 11:08:34 -04:00
|
|
|
validate :replacement_is_html, if: -> { replacement.present? && html? }
|
2021-06-02 01:36:49 -04:00
|
|
|
|
2017-06-28 16:56:44 -04: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
|
|
|
|
|
2023-11-01 10:41:10 -04:00
|
|
|
after_save -> { WordWatcher.clear_cache! }
|
|
|
|
after_destroy -> { WordWatcher.clear_cache! }
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2022-08-05 06:18:17 -04:00
|
|
|
scope :for,
|
2023-11-29 00:38:07 -05:00
|
|
|
->(word:) do
|
2022-08-05 06:18:17 -04:00
|
|
|
where(
|
|
|
|
"(word ILIKE :word AND case_sensitive = 'f') OR (word LIKE :word AND case_sensitive = 't')",
|
|
|
|
word: word,
|
|
|
|
)
|
2023-11-29 00:38:07 -05:00
|
|
|
end
|
2017-06-28 16:56:44 -04:00
|
|
|
|
2023-11-01 10:41:10 -04:00
|
|
|
def self.actions
|
|
|
|
@actions ||=
|
|
|
|
Enum.new(
|
|
|
|
block: 1,
|
|
|
|
censor: 2,
|
|
|
|
require_approval: 3,
|
|
|
|
flag: 4,
|
|
|
|
link: 8,
|
|
|
|
replace: 5,
|
|
|
|
tag: 6,
|
|
|
|
silence: 7,
|
|
|
|
)
|
2022-06-27 15:16:33 -04:00
|
|
|
end
|
|
|
|
|
2024-04-29 06:20:55 -04:00
|
|
|
belongs_to :watched_word_group
|
|
|
|
|
|
|
|
scope :for,
|
|
|
|
->(word:) do
|
|
|
|
where(
|
|
|
|
"(word ILIKE :word AND case_sensitive = 'f') OR (word LIKE :word AND case_sensitive = 't')",
|
|
|
|
word: word,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
def self.create_or_update_word(params)
|
2023-11-01 10:41:10 -04:00
|
|
|
word = normalize_word(params[:word])
|
|
|
|
word = self.for(word: word).first_or_initialize(word: word)
|
|
|
|
word.replacement = params[:replacement] if params[:replacement]
|
|
|
|
word.action_key = params[:action_key] if params[:action_key]
|
|
|
|
word.action = params[:action] if params[:action]
|
|
|
|
word.case_sensitive = params[:case_sensitive] if !params[:case_sensitive].nil?
|
2024-05-06 11:08:34 -04:00
|
|
|
word.html = params[:html] if params[:html]
|
2024-04-29 06:20:55 -04:00
|
|
|
word.watched_word_group_id = params[:watched_word_group_id]
|
2023-11-01 10:41:10 -04:00
|
|
|
word.save
|
|
|
|
word
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2021-03-22 16:32:18 -04:00
|
|
|
def self.has_replacement?(action)
|
2021-06-02 01:36:49 -04:00
|
|
|
action == :replace || action == :tag || action == :link
|
2021-03-22 16:32:18 -04:00
|
|
|
end
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
def action_key=(arg)
|
2023-11-01 10:41:10 -04:00
|
|
|
self.action = WatchedWord.actions[arg.to_sym]
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
2021-06-30 01:52:46 -04:00
|
|
|
def action_log_details
|
2024-05-06 11:08:34 -04:00
|
|
|
replacement.present? ? "#{word} → #{replacement}" : word
|
2021-06-30 01:52:46 -04:00
|
|
|
end
|
|
|
|
|
2023-11-01 10:41:10 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.normalize_word(word)
|
|
|
|
# When a regular expression is converted to a string, it is wrapped with
|
|
|
|
# '(?-mix:' and ')'
|
|
|
|
word = word[7..-2] if word.start_with?("(?-mix:")
|
|
|
|
|
|
|
|
word.strip.squeeze("*")
|
|
|
|
end
|
|
|
|
|
|
|
|
def replacement_is_url
|
|
|
|
errors.add(:base, :invalid_url) if replacement !~ URI.regexp
|
|
|
|
end
|
|
|
|
|
|
|
|
def replacement_is_tag_list
|
|
|
|
tag_list = replacement&.split(",")
|
|
|
|
tags = Tag.where(name: tag_list)
|
|
|
|
if tag_list.blank? || tags.empty? || tag_list.size != tags.size
|
|
|
|
errors.add(:base, :invalid_tag_list)
|
|
|
|
end
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
2024-05-06 11:08:34 -04:00
|
|
|
|
|
|
|
def replacement_is_html
|
|
|
|
errors.add(:base, :invalid_html) if action != WatchedWord.actions[:replace]
|
|
|
|
end
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
2017-07-27 22:37:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: watched_words
|
|
|
|
#
|
2024-04-29 06:20:55 -04: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
|
|
|
|
# case_sensitive :boolean default(FALSE), not null
|
|
|
|
# watched_word_group_id :bigint
|
2024-05-06 11:08:34 -04:00
|
|
|
# html :boolean default(FALSE), not null
|
2017-07-27 22:37:10 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2024-04-29 06:20:55 -04:00
|
|
|
# index_watched_words_on_action_and_word (action,word) UNIQUE
|
|
|
|
# index_watched_words_on_watched_word_group_id (watched_word_group_id)
|
2017-07-27 22:37:10 -04:00
|
|
|
#
|