Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Let workflows remediate a profile, not just flag it (#42726)

Régis Hanola52992ac3999
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 8f157b61cf5..76e7a258cb2 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -464,7 +464,12 @@ en:
username: "User"
updates: "Fields"
bio_raw: "Bio"
+ website: "Website"
+ location: "Location"
title: "Title"
+ remove_avatar: "Remove avatar"
+ remove_profile_background: "Remove profile background"
+ remove_card_background: "Remove card background"
trust_level: "Trust level"
trust_level_locked: "Lock trust level"
include_extensions: "Include user extensions"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
index 05713cc0976..2eb6e0b84f7 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
@@ -7,11 +7,21 @@ module DiscourseWorkflows
OPERATIONS = %w[get edit].freeze
UPDATE_OPTIONS = [
{ name: "bio_raw", type: :string, required: false, ui: { control: :textarea } },
+ { name: "website", type: :string, required: false },
+ { name: "location", type: :string, required: false },
{ name: "title", type: :string, required: false },
+ { name: "remove_avatar", type: :boolean, required: false },
+ { name: "remove_profile_background", type: :boolean, required: false },
+ { name: "remove_card_background", type: :boolean, required: false },
{ name: "trust_level", type: :options, required: true, options: trust_level_options },
{ name: "trust_level_locked", type: :boolean, required: false },
].freeze
MISSING = DiscourseWorkflows::Executor::NodeExecutionContext::MISSING
+ TEXT_UPDATES = %w[title website location].freeze
+ BACKGROUND_REMOVALS = {
+ "remove_profile_background" => :profile_background_upload_url,
+ "remove_card_background" => :card_background_upload_url,
+ }.freeze
EXTENSIONS = {
"stats" => :stats_extension,
@@ -160,6 +170,7 @@ module DiscourseWorkflows
trust_level_locked = updates.fetch("trust_level_locked", MISSING)
update_profile_fields(user, actor, guardian, attributes) if attributes.present?
+ remove_avatar(user, actor, guardian) if updates["remove_avatar"]
change_trust_level(user, actor, guardian, trust_level) unless trust_level.equal?(MISSING)
unless trust_level_locked.equal?(MISSING)
update_trust_level_lock(user, actor, guardian, trust_level_locked)
@@ -180,17 +191,30 @@ module DiscourseWorkflows
UPDATE_OPTIONS.each_with_object({}) do |field, updates|
name = field[:name]
value = exec_ctx.get_node_parameter(name, item_index, default: MISSING)
- updates[name] = value unless value.equal?(MISSING)
+ next if value.equal?(MISSING)
+
+ value = ActiveModel::Type::Boolean.new.cast(value) == true if field[:type] == :boolean
+ updates[name] = value
end
end
def editable_attributes(updates)
{}.tap do |attributes|
attributes[:bio_raw] = updates["bio_raw"] if updates.key?("bio_raw")
- attributes[:title] = updates["title"].to_s if updates.key?("title")
+ TEXT_UPDATES.each do |name|
+ attributes[name.to_sym] = updates[name].to_s if updates.key?(name)
+ end
+ BACKGROUND_REMOVALS.each do |flag, attribute|
+ attributes[attribute] = "" if updates[flag]
+ end
end
end
+ def remove_avatar(user, actor, guardian)
+ guardian.ensure_can_edit!(user)
+ user.remove_avatar!(actor)
+ end
+
def update_profile_fields(user, actor, guardian, attributes)
guardian.ensure_can_edit!(user)
guardian.ensure_can_grant_title!(user, attributes[:title]) if attributes.key?(:title)
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
index 24e35df241e..407d4789a3b 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/user_spec.rb
@@ -259,6 +259,102 @@ RSpec.describe DiscourseWorkflows::Nodes::User::V1 do
end.to raise_error(DiscourseWorkflows::NodeError, "Unknown user extensions: nope.")
end
+ it "removes the avatar and logs a staff action", :aggregate_failures do
+ avatar = Fabricate(:upload)
+ user.update!(uploaded_avatar_id: avatar.id)
+ user.user_avatar.update!(custom_upload_id: avatar.id)
+
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "edit",
+ "username" => user.username,
+ "updates" => {
+ "remove_avatar" => true,
+ },
+ "actor_username" => admin.username,
+ },
+ )
+
+ expect(result.dig("user", "uploaded_avatar_id")).to be_nil
+ expect(user.reload.user_avatar.custom_upload_id).to be_nil
+ expect(
+ UserHistory.where(
+ action: UserHistory.actions[:removed_avatar],
+ target_user_id: user.id,
+ ).count,
+ ).to eq(1)
+ end
+
+ it "stops on a second pass, so a workflow does not act on its own edit" do
+ avatar = Fabricate(:upload)
+ user.update!(uploaded_avatar_id: avatar.id)
+
+ configuration = {
+ "operation" => "edit",
+ "username" => user.username,
+ "updates" => {
+ "remove_avatar" => true,
+ },
+ "actor_username" => admin.username,
+ }
+
+ execute_node(configuration: configuration)
+ events = DiscourseEvent.track_events(:user_updated) { execute_node(configuration:) }
+
+ expect(events).to be_empty
+ expect(
+ UserHistory.where(
+ action: UserHistory.actions[:removed_avatar],
+ target_user_id: user.id,
+ ).count,
+ ).to eq(1)
+ end
+
+ it "leaves the avatar alone when the flag is not set", :aggregate_failures do
+ avatar = Fabricate(:upload)
+ user.update!(uploaded_avatar_id: avatar.id)
+
+ execute_node(
+ configuration: {
+ "operation" => "edit",
+ "username" => user.username,
+ "updates" => {
+ "remove_avatar" => false,
+ "title" => "Kept",
+ },
+ "actor_username" => admin.username,
+ },
+ )
+
+ expect(user.reload.uploaded_avatar_id).to eq(avatar.id)
+ end
+
+ it "clears the profile and card backgrounds", :aggregate_failures do
+ profile_background = Fabricate(:upload)
+ card_background = Fabricate(:upload)
+ user.user_profile.update!(
+ profile_background_upload_id: profile_background.id,
+ card_background_upload_id: card_background.id,
+ )
+
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "edit",
+ "username" => user.username,
+ "updates" => {
+ "remove_profile_background" => true,
+ "remove_card_background" => true,
+ },
+ "actor_username" => admin.username,
+ },
+ )
+
+ expect(result.dig("user", "profile_background_upload_id")).to be_nil
+ expect(result.dig("user", "card_background_upload_id")).to be_nil
+ end
+
it "returns external ids from the edit operation", :aggregate_failures do
Fabricate(:single_sign_on_record, user: user, external_id: "ext-42")
@@ -316,7 +412,7 @@ RSpec.describe DiscourseWorkflows::Nodes::User::V1 do
expect(result.dig("user", "external_id")).to eq("ext-42")
end
- it "updates the bio and title", :aggregate_failures do
+ it "updates the bio, title, website and location", :aggregate_failures do
result =
execute_node(
configuration: {
@@ -325,6 +421,8 @@ RSpec.describe DiscourseWorkflows::Nodes::User::V1 do
"updates" => {
"bio_raw" => "Updated bio",
"title" => "Updated title",
+ "website" => "https://example.com",
+ "location" => "Paris",
},
"actor_username" => admin.username,
},
@@ -332,8 +430,10 @@ RSpec.describe DiscourseWorkflows::Nodes::User::V1 do
expect(user.reload.user_profile.bio_raw).to eq("Updated bio")
expect(user.title).to eq("Updated title")
+ expect(user.user_profile.location).to eq("Paris")
expect(result.dig("user", "bio_raw")).to eq("Updated bio")
expect(result.dig("user", "title")).to eq("Updated title")
+ expect(result.dig("user", "website")).to eq("https://example.com")
end
it "only changes fields included in updates", :aggregate_failures do