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
This commit is contained in:
Bianca Nenciu 2020-05-26 16:28:03 +03:00 committed by GitHub
parent 41f742c2f9
commit f47400475e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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