2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-22 15:32:18 -05:00
|
|
|
require "csv"
|
|
|
|
|
2022-10-31 07:02:26 -05:00
|
|
|
class Admin::WatchedWordsController < Admin::StaffController
|
2019-07-22 06:59:56 -05:00
|
|
|
skip_before_action :check_xhr, only: [:download]
|
2017-06-28 15:56:44 -05:00
|
|
|
|
|
|
|
def index
|
2023-11-01 09:41:10 -05:00
|
|
|
watched_words = WatchedWord.order(:action, :word)
|
2021-05-14 08:52:10 -05:00
|
|
|
watched_words =
|
|
|
|
watched_words.where.not(action: WatchedWord.actions[:tag]) if !SiteSetting.tagging_enabled
|
|
|
|
render_json_dump WatchedWordListSerializer.new(watched_words, scope: guardian, root: false)
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-04-29 05:20:55 -05:00
|
|
|
opts = watched_words_params
|
2024-04-30 11:08:37 -05:00
|
|
|
action = WatchedWord.actions[opts[:action_key].to_sym]
|
2024-04-29 05:20:55 -05:00
|
|
|
words = opts.delete(:words)
|
|
|
|
|
|
|
|
watched_word_group = WatchedWordGroup.new(action: action)
|
|
|
|
watched_word_group.create_or_update_members(words, opts)
|
|
|
|
|
|
|
|
if watched_word_group.valid?
|
|
|
|
StaffActionLogger.new(current_user).log_watched_words_creation(watched_word_group)
|
|
|
|
render_json_dump WatchedWordListSerializer.new(
|
|
|
|
watched_word_group.watched_words,
|
|
|
|
scope: guardian,
|
|
|
|
root: false,
|
|
|
|
)
|
2017-06-28 15:56:44 -05:00
|
|
|
else
|
2024-04-29 05:20:55 -05:00
|
|
|
render_json_error(watched_word_group)
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2019-07-14 21:22:46 -05:00
|
|
|
watched_word = WatchedWord.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless watched_word
|
2024-04-29 05:20:55 -05:00
|
|
|
|
|
|
|
watched_word_group = watched_word.watched_word_group
|
2024-05-10 10:11:56 -05:00
|
|
|
|
2024-04-29 05:20:55 -05:00
|
|
|
if watched_word_group&.watched_words&.count == 1
|
|
|
|
watched_word_group.destroy!
|
2024-05-10 10:11:56 -05:00
|
|
|
StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word_group)
|
2024-04-29 05:20:55 -05:00
|
|
|
else
|
|
|
|
watched_word.destroy!
|
2024-05-10 10:11:56 -05:00
|
|
|
StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word)
|
2024-04-29 05:20:55 -05:00
|
|
|
end
|
|
|
|
|
2017-06-28 15:56:44 -05:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload
|
|
|
|
file = params[:file] || params[:files].first
|
|
|
|
action_key = params[:action_key].to_sym
|
2021-03-22 15:32:18 -05:00
|
|
|
has_replacement = WatchedWord.has_replacement?(action_key)
|
2017-06-28 15:56:44 -05:00
|
|
|
|
|
|
|
Scheduler::Defer.later("Upload watched words") do
|
|
|
|
begin
|
2021-03-22 15:32:18 -05:00
|
|
|
CSV.foreach(file.tempfile, encoding: "bom|utf-8") do |row|
|
|
|
|
if row[0].present? && (!has_replacement || row[1].present?)
|
2021-06-30 00:52:46 -05:00
|
|
|
watched_word =
|
|
|
|
WatchedWord.create_or_update_word(
|
2021-03-22 15:32:18 -05:00
|
|
|
word: row[0],
|
|
|
|
replacement: has_replacement ? row[1] : nil,
|
2022-08-02 03:06:03 -05:00
|
|
|
action_key: action_key,
|
|
|
|
case_sensitive: "true" == row[2]&.strip&.downcase,
|
2021-03-22 15:32:18 -05:00
|
|
|
)
|
2021-06-30 00:52:46 -05:00
|
|
|
if watched_word.valid?
|
|
|
|
StaffActionLogger.new(current_user).log_watched_words_creation(watched_word)
|
|
|
|
end
|
2021-03-22 15:32:18 -05:00
|
|
|
end
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
2021-03-22 15:32:18 -05:00
|
|
|
|
2017-06-28 15:56:44 -05:00
|
|
|
data = { url: "/ok" }
|
|
|
|
rescue => e
|
|
|
|
data = failed_json.merge(errors: [e.message])
|
|
|
|
end
|
2019-07-22 06:59:56 -05:00
|
|
|
MessageBus.publish("/uploads/txt", data.as_json, client_ids: [params[:client_id]])
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2019-07-22 06:59:56 -05:00
|
|
|
def download
|
|
|
|
params.require(:id)
|
|
|
|
name = watched_words_params[:id].to_sym
|
|
|
|
action = WatchedWord.actions[name]
|
|
|
|
raise Discourse::NotFound if !action
|
|
|
|
|
2021-03-22 15:32:18 -05:00
|
|
|
content = WatchedWord.where(action: action)
|
|
|
|
if WatchedWord.has_replacement?(name)
|
|
|
|
content = content.pluck(:word, :replacement).map(&:to_csv).join
|
|
|
|
else
|
|
|
|
content = content.pluck(:word).join("\n")
|
|
|
|
end
|
|
|
|
|
2019-07-22 06:59:56 -05:00
|
|
|
headers["Content-Length"] = content.bytesize.to_s
|
|
|
|
send_data content,
|
2021-03-22 15:32:18 -05:00
|
|
|
filename: "#{Discourse.current_hostname}-watched-words-#{name}.csv",
|
|
|
|
content_type: "text/csv"
|
2019-07-22 06:59:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_all
|
|
|
|
params.require(:id)
|
|
|
|
name = watched_words_params[:id].to_sym
|
|
|
|
action = WatchedWord.actions[name]
|
|
|
|
raise Discourse::NotFound if !action
|
|
|
|
|
2021-06-30 00:52:46 -05:00
|
|
|
WatchedWord
|
|
|
|
.where(action: action)
|
|
|
|
.find_each do |watched_word|
|
|
|
|
watched_word.destroy!
|
|
|
|
StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word)
|
|
|
|
end
|
2019-07-22 06:59:56 -05:00
|
|
|
WordWatcher.clear_cache!
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2017-06-28 15:56:44 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def watched_words_params
|
2024-04-29 05:20:55 -05:00
|
|
|
@watched_words_params ||=
|
2024-05-06 10:08:34 -05:00
|
|
|
params.permit(:id, :replacement, :action_key, :case_sensitive, :html, words: [])
|
2017-06-28 15:56:44 -05:00
|
|
|
end
|
|
|
|
end
|