mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 22:16:01 +08:00
PERF: Add endpoint to check if a group can be mentioned by user.
This commit is contained in:
@ -181,7 +181,11 @@ Group.reopenClass({
|
|||||||
offset: offset || 0
|
offset: offset || 0
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
|
|
||||||
|
mentionable(name) {
|
||||||
|
return ajax(`/groups/${name}/mentionable`, { data: { name } });
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default Group;
|
export default Group;
|
||||||
|
@ -21,11 +21,11 @@ export default Discourse.Route.extend({
|
|||||||
});
|
});
|
||||||
} else if (params.groupname) {
|
} else if (params.groupname) {
|
||||||
// send a message to a group
|
// send a message to a group
|
||||||
Group.find(params.groupname).then(group => {
|
Group.mentionable(params.groupname).then(result => {
|
||||||
if (group.mentionable) {
|
if (result.mentionable) {
|
||||||
Ember.run.next(() => e.send("createNewMessageViaParams", group.name, params.title, params.body));
|
Ember.run.next(() => e.send("createNewMessageViaParams", params.groupname, params.title, params.body));
|
||||||
} else {
|
} else {
|
||||||
bootbox.alert(I18n.t("composer.cant_send_pm", { username: group.name }));
|
bootbox.alert(I18n.t("composer.cant_send_pm", { username: params.groupname }));
|
||||||
}
|
}
|
||||||
}).catch(function() {
|
}).catch(function() {
|
||||||
bootbox.alert(I18n.t("generic_error"));
|
bootbox.alert(I18n.t("generic_error"));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
class GroupsController < ApplicationController
|
class GroupsController < ApplicationController
|
||||||
|
|
||||||
before_filter :ensure_logged_in, only: [:set_notifications]
|
before_filter :ensure_logged_in, only: [:set_notifications, :mentionable]
|
||||||
skip_before_filter :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
|
skip_before_filter :preload_json, :check_xhr, only: [:posts_feed, :mentions_feed]
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@ -120,6 +120,16 @@ class GroupsController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def mentionable
|
||||||
|
group = find_group(:name)
|
||||||
|
|
||||||
|
if group
|
||||||
|
render json: { mentionable: Group.mentionable(current_user).where(id: group.id).present? }
|
||||||
|
else
|
||||||
|
raise Discourse::InvalidAccess.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def remove_member
|
def remove_member
|
||||||
group = Group.find(params[:id])
|
group = Group.find(params[:id])
|
||||||
guardian.ensure_can_edit!(group)
|
guardian.ensure_can_edit!(group)
|
||||||
|
@ -169,7 +169,6 @@ class SessionController < ApplicationController
|
|||||||
login = params[:login].strip
|
login = params[:login].strip
|
||||||
login = login[1..-1] if login[0] == "@"
|
login = login[1..-1] if login[0] == "@"
|
||||||
|
|
||||||
|
|
||||||
if user = User.find_by_username_or_email(login)
|
if user = User.find_by_username_or_email(login)
|
||||||
|
|
||||||
# If their password is correct
|
# If their password is correct
|
||||||
|
@ -381,10 +381,6 @@ class Group < ActiveRecord::Base
|
|||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
def mentionable?(user, group_id)
|
|
||||||
Group.mentionable(user).where(id: group_id).exists?
|
|
||||||
end
|
|
||||||
|
|
||||||
def staff?
|
def staff?
|
||||||
STAFF_GROUPS.include?(self.name.to_sym)
|
STAFF_GROUPS.include?(self.name.to_sym)
|
||||||
end
|
end
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
class GroupSerializer < BasicGroupSerializer
|
|
||||||
attributes :mentionable
|
|
||||||
|
|
||||||
def mentionable
|
|
||||||
object.mentionable?(scope.user, object.id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def include_mentionable?
|
|
||||||
authenticated?
|
|
||||||
end
|
|
||||||
end
|
|
@ -409,6 +409,7 @@ Discourse::Application.routes.draw do
|
|||||||
get 'mentions'
|
get 'mentions'
|
||||||
get 'messages'
|
get 'messages'
|
||||||
get 'counts'
|
get 'counts'
|
||||||
|
get 'mentionable'
|
||||||
|
|
||||||
member do
|
member do
|
||||||
put "members" => "groups#add_members"
|
put "members" => "groups#add_members"
|
||||||
|
4
spec/fabricators/email_token_fabricator.rb
Normal file
4
spec/fabricators/email_token_fabricator.rb
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Fabricator(:email_token) do
|
||||||
|
user
|
||||||
|
email { |attrs| attrs[:user].email }
|
||||||
|
end
|
36
spec/integration/groups_spec.rb
Normal file
36
spec/integration/groups_spec.rb
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe "Groups" do
|
||||||
|
describe "checking if a group can be mentioned" do
|
||||||
|
let(:password) { 'somecomplicatedpassword' }
|
||||||
|
let(:email_token) { Fabricate(:email_token, confirmed: true) }
|
||||||
|
let(:user) { email_token.user }
|
||||||
|
let(:group) { Fabricate(:group, name: 'test', users: [user]) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
user.update_attributes!(password: password)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return the right response" do
|
||||||
|
group
|
||||||
|
|
||||||
|
post "/session.json", { login: user.username, password: password }
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
get "/groups/test/mentionable.json", { name: group.name }
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
response_body = JSON.parse(response.body)
|
||||||
|
expect(response_body["mentionable"]).to eq(false)
|
||||||
|
|
||||||
|
group.update_attributes!(alias_level: Group::ALIAS_LEVELS[:everyone])
|
||||||
|
|
||||||
|
get "/groups/test/mentionable.json", { name: group.name }
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
response_body = JSON.parse(response.body)
|
||||||
|
expect(response_body["mentionable"]).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Reference in New Issue
Block a user