diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b7b1613..6dd4b54 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -10,6 +10,7 @@ en: unassign_on_group_archive: "When a message is archived by a group, unassign message (reassign if moved back to inbox)" unassign_on_close: "When a topic is closed unassign topic" reassign_on_open: "When a topic is opened reassign previously assigned users/groups" + invite_on_assign: "When a personal message is assigned to a user or group, invite them to the PM if they are not already a participant." assign_mailer: "When to send notification email for assignments" remind_assigns: "Remind users about pending assigns." remind_assigns_frequency: "Frequency for reminding users about assigned topics." diff --git a/config/settings.yml b/config/settings.yml index ee2f55e..421fc9b 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -12,6 +12,7 @@ plugins: unassign_on_close: false unassign_on_group_archive: false reassign_on_open: false + invite_on_assign: false assigns_user_url_path: client: true default: "/u/{username}/activity/assigned" diff --git a/lib/assigner.rb b/lib/assigner.rb index 0bddf59..48c1a99 100644 --- a/lib/assigner.rb +++ b/lib/assigner.rb @@ -258,6 +258,21 @@ class ::Assigner def assign(assign_to, note: nil, skip_small_action_post: false, status: nil) assigned_to_type = assign_to.is_a?(User) ? "User" : "Group" + if topic.private_message? && SiteSetting.invite_on_assign + guardian = Guardian.new(@assigned_by) + if assigned_to_type == "Group" + unless topic.topic_allowed_groups.exists?(group_id: assign_to.id) + guardian.ensure_can_invite_group_to_private_message!(assign_to, topic) + topic.invite_group(@assigned_by, assign_to) + end + else + unless topic.topic_allowed_users.exists?(user_id: assign_to.id) + guardian.ensure_can_invite_to!(topic) + topic.invite(@assigned_by, assign_to.username) + end + end + end + forbidden_reason = forbidden_reasons(assign_to: assign_to, type: assigned_to_type, note: note, status: status) return { success: false, reason: forbidden_reason } if forbidden_reason diff --git a/spec/lib/assigner_spec.rb b/spec/lib/assigner_spec.rb index b9debb4..06813f7 100644 --- a/spec/lib/assigner_spec.rb +++ b/spec/lib/assigner_spec.rb @@ -360,7 +360,7 @@ RSpec.describe Assigner do it "triggers error for incorrect type" do expect do described_class.new(secure_category, moderator).assign(moderator) - end.to raise_error(Discourse::InvalidAccess) + end.to raise_error(Discourse::InvalidParameters) end describe "updating notes" do @@ -708,6 +708,45 @@ RSpec.describe Assigner do end end + describe "invite_on_assign" do + let(:admin) { Fabricate(:admin) } + let(:topic) { Fabricate(:private_message_topic) } + let(:post) { Fabricate(:post, topic: topic) } + let(:assigner) { described_class.new(topic, admin) } + + before do + SiteSetting.invite_on_assign = true + post + end + + it "invites user to the PM" do + user = Fabricate(:user) + assigner.assign(user) + expect(topic.allowed_users).to include(user) + end + + it "invites group to the PM" do + group = + Fabricate( + :group, + assignable_level: Group::ALIAS_LEVELS[:only_admins], + messageable_level: Group::ALIAS_LEVELS[:only_admins], + ) + assigner.assign(group) + expect(topic.allowed_groups).to include(group) + end + + it "doesn't invite group to the PM if it's not messageable" do + group = + Fabricate( + :group, + assignable_level: Group::ALIAS_LEVELS[:only_admins], + messageable_level: Group::ALIAS_LEVELS[:nobody], + ) + expect { assigner.assign(group) }.to raise_error(Discourse::InvalidAccess) + end + end + describe "assign_emailer" do let(:post) { Fabricate(:post) } let(:topic) { post.topic }