FEATURE: invite user/group to PM if not a participant already. (#471)

When a group or user is assigned to a personal message, if they are not already a participant, invite them to the PM before assignment.
This commit is contained in:
Vinoth Kannan 2023-05-22 14:48:08 +05:30 committed by GitHub
parent c5e0c2c0b3
commit dcaadb75dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 1 deletions

View File

@ -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."

View File

@ -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"

View File

@ -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

View File

@ -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 }