FIX: Create group membership request on behalf of user.

This commit is contained in:
Guo Xiang Tan 2017-06-13 17:10:14 +09:00
parent 69dc8188e3
commit 84d46bceb9
10 changed files with 84 additions and 52 deletions

View File

@ -1,8 +1,10 @@
import { default as computed } from 'ember-addons/ember-computed-decorators'; import { default as computed } from 'ember-addons/ember-computed-decorators';
import { popupAjaxError } from 'discourse/lib/ajax-error'; import { popupAjaxError } from 'discourse/lib/ajax-error';
import Group from 'discourse/models/group'; import DiscourseURL from 'discourse/lib/url';
export default Ember.Component.extend({ export default Ember.Component.extend({
loading: false,
@computed("model.public") @computed("model.public")
canJoinGroup(publicGroup) { canJoinGroup(publicGroup) {
return publicGroup; return publicGroup;
@ -51,13 +53,12 @@ export default Ember.Component.extend({
requestMembership() { requestMembership() {
if (this.currentUser) { if (this.currentUser) {
const groupName = this.get('model.name'); this.set('loading', true);
Group.loadOwners(groupName).then(result => { this.get('model').requestMembership().then(result => {
const names = result.map(owner => owner.username).join(","); DiscourseURL.routeTo(result.relative_url);
const title = I18n.t('groups.request_membership_pm.title'); }).catch(popupAjaxError).finally(() => {
const body = I18n.t('groups.request_membership_pm.body', { groupName }); this.set('loading', false);
this.sendAction("createNewMessageViaParams", names, title, body);
}); });
} else { } else {
this._showLoginModal(); this._showLoginModal();

View File

@ -2,7 +2,6 @@ import { ajax } from 'discourse/lib/ajax';
import { default as computed, observes } from "ember-addons/ember-computed-decorators"; import { default as computed, observes } from "ember-addons/ember-computed-decorators";
import GroupHistory from 'discourse/models/group-history'; import GroupHistory from 'discourse/models/group-history';
import RestModel from 'discourse/models/rest'; import RestModel from 'discourse/models/rest';
import { popupAjaxError } from 'discourse/lib/ajax-error';
const Group = RestModel.extend({ const Group = RestModel.extend({
limit: 50, limit: 50,
@ -202,7 +201,13 @@ const Group = RestModel.extend({
data: { notification_level, user_id: userId }, data: { notification_level, user_id: userId },
type: "POST" type: "POST"
}); });
} },
requestMembership() {
return ajax(`/groups/${this.get('name')}/request_membership`, {
type: "POST"
});
},
}); });
Group.reopenClass({ Group.reopenClass({
@ -216,10 +221,6 @@ Group.reopenClass({
return ajax("/groups/" + name + ".json").then(result => Group.create(result.basic_group)); return ajax("/groups/" + name + ".json").then(result => Group.create(result.basic_group));
}, },
loadOwners(name) {
return ajax('/groups/' + name + '/owners.json').catch(popupAjaxError);
},
loadMembers(name, offset, limit, params) { loadMembers(name, offset, limit, params) {
return ajax('/groups/' + name + '/members.json', { return ajax('/groups/' + name + '/members.json', {
data: _.extend({ data: _.extend({

View File

@ -24,8 +24,13 @@
{{else}} {{else}}
{{d-button action="requestMembership" {{d-button action="requestMembership"
class="group-index-request" class="group-index-request"
disabled=loading
icon="envelope" icon="envelope"
label="groups.request"}} label="groups.request"}}
{{#if loading}}
{{loading-spinner size="small"}}
{{/if}}
{{/if}} {{/if}}
{{else}} {{else}}
{{yield}} {{yield}}

View File

@ -43,9 +43,7 @@
{{/each}} {{/each}}
{{/mobile-nav}} {{/mobile-nav}}
{{group-membership-button model=model {{group-membership-button model=model showLogin='showLogin'}}
createNewMessageViaParams='createNewMessageViaParams'
showLogin='showLogin'}}
</div> </div>
</div> </div>

View File

@ -45,7 +45,6 @@
<td> <td>
{{#group-membership-button model=group {{#group-membership-button model=group
createNewMessageViaParams='createNewMessageViaParams'
showMembershipStatus=true showMembershipStatus=true
groupUserIds=groups.extras.group_user_ids groupUserIds=groups.extras.group_user_ids
showLogin='showLogin'}} showLogin='showLogin'}}

View File

@ -5,7 +5,8 @@ class GroupsController < ApplicationController
:mentionable, :mentionable,
:update, :update,
:messages, :messages,
:histories :histories,
:request_membership
] ]
skip_before_filter :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed] skip_before_filter :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
@ -136,16 +137,6 @@ class GroupsController < ApplicationController
} }
end end
def owners
group = find_group(:group_id)
owners = group.users.where('group_users.owner')
.order("users.last_seen_at DESC")
.limit(5)
render_serialized(owners, GroupUserSerializer)
end
def add_members def add_members
group = Group.find(params[:id]) group = Group.find(params[:id])
group.public ? ensure_logged_in : guardian.ensure_can_edit!(group) group.public ? ensure_logged_in : guardian.ensure_can_edit!(group)
@ -238,7 +229,27 @@ class GroupsController < ApplicationController
else else
render_json_error(group) render_json_error(group)
end end
end
def request_membership
unless current_user.staff?
RateLimiter.new(current_user, "request_group_membership", 1, 1.day).performed!
end
group = find_group(:id)
group_name = group.name
username = current_user.username
post = PostCreator.new(current_user,
title: I18n.t('groups.request_membership_pm.title', group_name: group_name),
raw: I18n.t('groups.request_membership_pm.body', group_name: group_name),
archetype: Archetype.private_message,
target_usernames: username,
target_group_names: group_name,
skip_validations: true
).create!
render json: success_json.merge(relative_url: post.topic.relative_url)
end end
def set_notifications def set_notifications

View File

@ -407,9 +407,6 @@ en:
full_name: 'Full Name' full_name: 'Full Name'
add_members: "Add Members" add_members: "Add Members"
delete_member_confirm: "Remove '%{username}' from the '%{group}' group?" delete_member_confirm: "Remove '%{username}' from the '%{group}' group?"
request_membership_pm:
title: "Membership Request"
body: "I would like to apply for membership in @%{groupName}."
name_placeholder: "Group name, no spaces, same as username rule" name_placeholder: "Group name, no spaces, same as username rule"
public: "Allow users to join/leave the group freely (Requires group to be visible)" public: "Allow users to join/leave the group freely (Requires group to be visible)"
empty: empty:

View File

@ -274,6 +274,9 @@ en:
trust_level_2: "trust_level_2" trust_level_2: "trust_level_2"
trust_level_3: "trust_level_3" trust_level_3: "trust_level_3"
trust_level_4: "trust_level_4" trust_level_4: "trust_level_4"
request_membership_pm:
title: "Membership Request for @%{group_name}"
body: "I would like to apply for membership in @%{group_name}."
education: education:
until_posts: until_posts:

View File

@ -430,7 +430,6 @@ Discourse::Application.routes.draw do
get 'activity' => "groups#show" get 'activity' => "groups#show"
get 'activity/:filter' => "groups#show" get 'activity/:filter' => "groups#show"
get 'members' get 'members'
get 'owners'
get 'posts' get 'posts'
get 'topics' get 'topics'
get 'mentions' get 'mentions'
@ -442,6 +441,7 @@ Discourse::Application.routes.draw do
member do member do
put "members" => "groups#add_members" put "members" => "groups#add_members"
delete "members" => "groups#remove_member" delete "members" => "groups#remove_member"
post "request_membership" => "groups#request_membership"
post "notifications" => "groups#set_notifications" post "notifications" => "groups#set_notifications"
end end
end end

View File

@ -151,26 +151,6 @@ describe "Groups" do
end end
end end
describe 'owners' do
let(:user1) { Fabricate(:user, last_seen_at: Time.zone.now) }
let(:user2) { Fabricate(:user, last_seen_at: Time.zone.now - 1 .day) }
let(:group) { Fabricate(:group, users: [user, user1, user2]) }
it 'should return the right list of owners' do
group.add_owner(user1)
group.add_owner(user2)
xhr :get, "/groups/#{group.name}/owners"
expect(response).to be_success
owners = JSON.parse(response.body)
expect(owners.count).to eq(2)
expect(owners.map { |o| o["id"] }.sort).to eq([user1.id, user2.id])
end
end
describe 'members' do describe 'members' do
let(:user1) do let(:user1) do
Fabricate(:user, Fabricate(:user,
@ -555,4 +535,41 @@ describe "Groups" do
end end
end end
end end
describe "requesting membership for a group" do
let(:new_user) { Fabricate(:user) }
it 'requires the user to log in' do
expect do
xhr :post, "/groups/#{group.name}/request_membership"
end.to raise_error(Discourse::NotLoggedIn)
end
it 'should create the right PM' do
sign_in(user)
xhr :post, "/groups/#{group.name}/request_membership"
expect(response).to be_success
post = Post.last
topic = post.topic
body = JSON.parse(response.body)
expect(body['relative_url']).to eq(topic.relative_url)
expect(post.user).to eq(user)
expect(topic.title).to eq(I18n.t('groups.request_membership_pm.title',
group_name: group.name
))
expect(post.raw).to eq(I18n.t(
'groups.request_membership_pm.body', group_name: group.name
))
expect(topic.archetype).to eq(Archetype.private_message)
expect(topic.allowed_users).to eq([user])
expect(topic.allowed_groups).to eq([group])
end
end
end end