UX: Group membership PMs thread (#26974)

Instead of creating two separate Topics when a user (1) requests to join a group and (2) gets accepted in, this makes the acceptance message into a Post under the origin group request Topic.
This commit is contained in:
benj 2024-05-22 10:47:28 -05:00 committed by GitHub
parent 0012d9626f
commit e42ba6e90a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 83 additions and 9 deletions

View File

@ -480,6 +480,17 @@ class GroupsController < ApplicationController
user = User.find_by(id: params[:user_id])
raise Discourse::InvalidParameters.new(:user_id) if user.blank?
# find original membership request PM
request_topic =
Topic.find_by(
title:
(
I18n.t "groups.request_membership_pm.title", group_name: group.name, locale: user.locale
),
archetype: "private_message",
user_id: user.id,
)
ActiveRecord::Base.transaction do
if params[:accept]
group.add(user)
@ -492,9 +503,11 @@ class GroupsController < ApplicationController
if params[:accept]
PostCreator.new(
current_user,
title: I18n.t("groups.request_accepted_pm.title", group_name: group.name),
raw: I18n.t("groups.request_accepted_pm.body", group_name: group.name),
archetype: Archetype.private_message,
post_type: Post.types[:regular],
topic_id: request_topic.id,
raw:
(I18n.t "groups.request_accepted_pm.body", group_name: group.name, locale: user.locale),
reply_to_post_number: 1,
target_usernames: user.username,
skip_validations: true,
).create!

View File

@ -638,7 +638,6 @@ en:
request_membership_pm:
title: "Membership Request for @%{group_name}"
request_accepted_pm:
title: "You've been accepted into @%{group_name}"
body: |
Your request to enter @%{group_name} has been accepted and you are now a member.

View File

@ -2267,21 +2267,83 @@ RSpec.describe GroupsController do
sign_in(user)
end
it "sends a private message when accepted" do
it "sends a reply to the request membership topic when accepted" do
GroupRequest.create!(group: group, user: other_user)
# send the initial request PM
PostCreator.new(
other_user,
title: I18n.t("groups.request_membership_pm.title", group_name: group.name),
raw: "*British accent* Please, sir, may I have some group?",
archetype: Archetype.private_message,
target_usernames: "#{user.username}",
skip_validations: true,
).create!
topic = Topic.last
expect {
put "/groups/#{group.id}/handle_membership_request.json",
params: {
user_id: other_user.id,
accept: true,
}
}.to change { Topic.count }.by(1).and change { Post.count }.by(1)
}.to_not change { Topic.count }
expect(topic.archetype).to eq(Archetype.private_message)
expect(Topic.first.title).to eq(
I18n.t("groups.request_membership_pm.title", group_name: group.name),
)
post = Post.last
expect(post.topic_id).to eq(Topic.last.id)
expect(topic.posts.count).to eq(2)
expect(post.raw).to eq(
I18n.t("groups.request_accepted_pm.body", group_name: group.name).strip,
)
end
it "sends accepted membership request reply even if request is in another language" do
SiteSetting.allow_user_locale = true
other_user.update!(locale: "fr")
GroupRequest.create!(group: group, user: other_user)
# send the initial request PM
PostCreator.new(
other_user,
title:
(
I18n.t "groups.request_membership_pm.title",
group_name: group.name,
locale: other_user.locale
),
raw: "*French accent* Please let me in!",
archetype: Archetype.private_message,
target_usernames: "#{user.username}",
skip_validations: true,
).create!
topic = Topic.last
expect {
put "/groups/#{group.id}/handle_membership_request.json",
params: {
user_id: other_user.id,
accept: true,
}
}.to_not change { Topic.count }
expect(topic.archetype).to eq(Archetype.private_message)
expect(topic.title).to eq(I18n.t("groups.request_accepted_pm.title", group_name: group.name))
expect(topic.first_post.raw).to eq(
I18n.t("groups.request_accepted_pm.body", group_name: group.name).strip,
expect(Topic.first.title).to eq(
I18n.t("groups.request_membership_pm.title", group_name: group.name, locale: "fr"),
)
post = Post.last
expect(post.topic_id).to eq(Topic.last.id)
expect(topic.posts.count).to eq(2)
expect(post.raw).to eq(
I18n.t("groups.request_accepted_pm.body", group_name: group.name, locale: "fr").strip,
)
end
end