FEATURE: Send a private message when a group membership is accepted (#9822)

FEATURE: Send a private message when a group membership is accepted (#9822)

  • FEATURE: Send a private message when a group membership is accepted

  • DEV: Small code improvements

  • FIX: Send PM as group owner

  • Copy edits

diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index 3d4b828..2e112ed 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -340,18 +340,28 @@ class GroupsController < ApplicationController
     raise Discourse::InvalidParameters.new(:id) if group.blank?
     guardian.ensure_can_edit!(group)
 
-    ActiveRecord::Base.transaction do
-      user = User.find_by(id: params[:user_id])
-      raise Discourse::InvalidParameters.new(:user_id) if user.blank?
+    user = User.find_by(id: params[:user_id])
+    raise Discourse::InvalidParameters.new(:user_id) if user.blank?
 
+    ActiveRecord::Base.transaction do
       if params[:accept]
-        group.add(user, notify: true)
+        group.add(user)
         GroupActionLogger.new(current_user, group).log_add_user_to_group(user)
       end
 
       GroupRequest.where(group_id: group.id, user_id: user.id).delete_all
     end
 
+    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,
+        target_usernames: user.username,
+        skip_validations: true
+      ).create!
+    end
+
     render json: success_json
   end
 
diff --git a/app/models/group.rb b/app/models/group.rb
index b207f1c..89ce80b 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -616,16 +616,15 @@ class Group < ActiveRecord::Base
   PUBLISH_CATEGORIES_LIMIT = 10
 
   def add(user, notify: false, automatic: false)
-    self.users.push(user) unless self.users.include?(user)
+    return self if self.users.include?(user)
+
+    self.users.push(user)
 
     if notify
       Notification.create!(
         notification_type: Notification.types[:membership_request_accepted],
         user_id: user.id,
-        data: {
-          group_id: id,
-          group_name: name
-        }.to_json
+        data: { group_id: id, group_name: name }.to_json
       )
     end
 
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index a190dd4..0433fd0 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -431,6 +431,10 @@ en:
       trust_level_4: "trust_level_4"
     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_hidden_topic_request_reason: "I would like to join the group '%{group_name}', so I may access [this topic](%{topic_url})"
 
   education:
diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rb
index 862b9a6..11eb7eb 100644
--- a/spec/requests/groups_controller_spec.rb
+++ b/spec/requests/groups_controller_spec.rb
@@ -1325,6 +1325,25 @@ describe GroupsController do
     end
   end
 
+  describe "#handle_membership_request" do
+    before do
+      group.add_owner(user)
+      sign_in(user)
+    end
+
+    it "sends a private message when accepted" do
+      group_request = GroupRequest.create!(group: group, user: other_user)
+      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)
+
+      topic = Topic.last
+      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)
+    end
+  end
+
   describe "#histories" do
     context 'when user is not signed in' do
       it 'should raise the right error' do

GitHub sha: f4740047

This commit appears in #9822 which was approved by eviltrout, tgxworld, ZogStriP, and ZogStriP. It was merged by nbianca.