Class: Admin::WatchedWordsController

Inherits:
StaffController
  • Object
show all
Defined in:
app/controllers/admin/watched_words_controller.rb

Instance Method Summary collapse

Instance Method Details

#clear_allObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/admin/watched_words_controller.rb', line 84

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)
    .find_each do |watched_word|
      watched_word.destroy!
      StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word)
    end
  WordWatcher.clear_cache!
  render json: success_json
end

#createObject



15
16
17
18
19
20
21
22
23
# File 'app/controllers/admin/watched_words_controller.rb', line 15

def create
  watched_word = WatchedWord.create_or_update_word(watched_words_params)
  if watched_word.valid?
    StaffActionLogger.new(current_user).log_watched_words_creation(watched_word)
    render json: watched_word, root: false
  else
    render_json_error(watched_word)
  end
end

#destroyObject



25
26
27
28
29
30
31
# File 'app/controllers/admin/watched_words_controller.rb', line 25

def destroy
  watched_word = WatchedWord.find_by(id: params[:id])
  raise Discourse::InvalidParameters.new(:id) unless watched_word
  watched_word.destroy!
  StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word)
  render json: success_json
end

#downloadObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/admin/watched_words_controller.rb', line 65

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)
  if WatchedWord.has_replacement?(name)
    content = content.pluck(:word, :replacement).map(&:to_csv).join
  else
    content = content.pluck(:word).join("\n")
  end

  headers["Content-Length"] = content.bytesize.to_s
  send_data content,
            filename: "#{Discourse.current_hostname}-watched-words-#{name}.csv",
            content_type: "text/csv"
end

#indexObject



8
9
10
11
12
13
# File 'app/controllers/admin/watched_words_controller.rb', line 8

def index
  watched_words = WatchedWord.by_action
  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)
end

#uploadObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/admin/watched_words_controller.rb', line 33

def upload
  file = params[:file] || params[:files].first
  action_key = params[:action_key].to_sym
  has_replacement = WatchedWord.has_replacement?(action_key)

  Scheduler::Defer.later("Upload watched words") do
    begin
      CSV.foreach(file.tempfile, encoding: "bom|utf-8") do |row|
        if row[0].present? && (!has_replacement || row[1].present?)
          watched_word =
            WatchedWord.create_or_update_word(
              word: row[0],
              replacement: has_replacement ? row[1] : nil,
              action_key: action_key,
              case_sensitive: "true" == row[2]&.strip&.downcase,
            )
          if watched_word.valid?
            StaffActionLogger.new(current_user).log_watched_words_creation(watched_word)
          end
        end
      end

      data = { url: "/ok" }
    rescue => e
      data = failed_json.merge(errors: [e.message])
    end
    MessageBus.publish("/uploads/txt", data.as_json, client_ids: [params[:client_id]])
  end

  render json: success_json
end