FEATURE: Allow watched words to be created as a group (#26632)

At the moment, there is no way to create a group of related watched words together.  If a user needed a set of words to be created together, they'll have to create them individually one at a time.

This change attempts to allow related watched words to be created as a group. The idea here is to have a list of words be tied together via a common `WatchedWordGroup` record.  Given a list of words, a `WatchedWordGroup` record is created and assigned to each `WatchedWord` record. The existing WatchedWord creation behaviour remains largely unchanged.

Co-authored-by: Selase Krakani <skrakani@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
This commit is contained in:
Vinoth Kannan
2024-04-29 15:50:55 +05:30
committed by GitHub
parent 0c8f531909
commit 143f06f2c6
18 changed files with 327 additions and 88 deletions

View File

@ -13,19 +13,36 @@ class Admin::WatchedWordsController < Admin::StaffController
end
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
opts = watched_words_params
action = opts[:action] || self.class.actions[opts[:action_key].to_sym]
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,
)
else
render_json_error(watched_word)
render_json_error(watched_word_group)
end
end
def destroy
watched_word = WatchedWord.find_by(id: params[:id])
raise Discourse::InvalidParameters.new(:id) unless watched_word
watched_word.destroy!
watched_word_group = watched_word.watched_word_group
if watched_word_group&.watched_words&.count == 1
watched_word_group.destroy!
else
watched_word.destroy!
end
StaffActionLogger.new(current_user).log_watched_words_deletion(watched_word)
render json: success_json
end
@ -100,6 +117,7 @@ class Admin::WatchedWordsController < Admin::StaffController
private
def watched_words_params
params.permit(:id, :word, :replacement, :action_key, :case_sensitive)
@watched_words_params ||=
params.permit(:id, :replacement, :action, :action_key, :case_sensitive, words: [])
end
end