2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
class Admin::WatchedWordsController < Admin::AdminController
|
2019-07-22 19:59:56 +08:00
|
|
|
skip_before_action :check_xhr, only: [:download]
|
2017-06-29 04:56:44 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
render_json_dump WatchedWordListSerializer.new(WatchedWord.by_action, scope: guardian, root: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
watched_word = WatchedWord.create_or_update_word(watched_words_params)
|
|
|
|
if watched_word.valid?
|
|
|
|
render json: watched_word, root: false
|
|
|
|
else
|
|
|
|
render_json_error(watched_word)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2019-07-15 10:22:46 +08:00
|
|
|
watched_word = WatchedWord.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless watched_word
|
|
|
|
watched_word.destroy!
|
2017-06-29 04:56:44 +08:00
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload
|
|
|
|
file = params[:file] || params[:files].first
|
|
|
|
action_key = params[:action_key].to_sym
|
|
|
|
|
|
|
|
Scheduler::Defer.later("Upload watched words") do
|
|
|
|
begin
|
2019-03-22 01:46:10 +08:00
|
|
|
File.open(file.tempfile, encoding: "bom|utf-8").each_line do |line|
|
2017-06-29 04:56:44 +08:00
|
|
|
WatchedWord.create_or_update_word(word: line, action_key: action_key) unless line.empty?
|
|
|
|
end
|
|
|
|
data = { url: '/ok' }
|
|
|
|
rescue => e
|
|
|
|
data = failed_json.merge(errors: [e.message])
|
|
|
|
end
|
2019-07-22 19:59:56 +08:00
|
|
|
MessageBus.publish("/uploads/txt", data.as_json, client_ids: [params[:client_id]])
|
2017-06-29 04:56:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2019-07-22 19:59:56 +08:00
|
|
|
def download
|
|
|
|
params.require(:id)
|
|
|
|
name = watched_words_params[:id].to_sym
|
|
|
|
action = WatchedWord.actions[name]
|
|
|
|
raise Discourse::NotFound if !action
|
|
|
|
|
|
|
|
content = WatchedWord.where(action: action).pluck(:word).join("\n")
|
|
|
|
headers['Content-Length'] = content.bytesize.to_s
|
|
|
|
send_data content,
|
|
|
|
filename: "#{Discourse.current_hostname}-watched-words-#{name}.txt",
|
|
|
|
content_type: "text/plain"
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_all
|
|
|
|
params.require(:id)
|
|
|
|
name = watched_words_params[:id].to_sym
|
|
|
|
action = WatchedWord.actions[name]
|
|
|
|
raise Discourse::NotFound if !action
|
|
|
|
|
|
|
|
WatchedWord.where(action: action).delete_all
|
|
|
|
WordWatcher.clear_cache!
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2017-06-29 04:56:44 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def watched_words_params
|
|
|
|
params.permit(:id, :word, :action_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|