PERF: Add endpoint to check if a group can be mentioned by user.

This commit is contained in:
Guo Xiang Tan 2016-11-25 16:45:15 +08:00
parent 5794f1619d
commit 559918c6c6
9 changed files with 61 additions and 22 deletions

View File

@ -181,7 +181,11 @@ Group.reopenClass({
offset: offset || 0
}
});
}
},
mentionable(name) {
return ajax(`/groups/${name}/mentionable`, { data: { name } });
},
});
export default Group;

View File

@ -21,11 +21,11 @@ export default Discourse.Route.extend({
});
} else if (params.groupname) {
// send a message to a group
Group.find(params.groupname).then(group => {
if (group.mentionable) {
Ember.run.next(() => e.send("createNewMessageViaParams", group.name, params.title, params.body));
Group.mentionable(params.groupname).then(result => {
if (result.mentionable) {
Ember.run.next(() => e.send("createNewMessageViaParams", params.groupname, params.title, params.body));
} 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() {
bootbox.alert(I18n.t("generic_error"));

View File

@ -1,6 +1,6 @@
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]
def show
@ -120,6 +120,16 @@ class GroupsController < ApplicationController
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
group = Group.find(params[:id])
guardian.ensure_can_edit!(group)

View File

@ -169,7 +169,6 @@ class SessionController < ApplicationController
login = params[:login].strip
login = login[1..-1] if login[0] == "@"
if user = User.find_by_username_or_email(login)
# If their password is correct

View File

@ -381,10 +381,6 @@ class Group < ActiveRecord::Base
true
end
def mentionable?(user, group_id)
Group.mentionable(user).where(id: group_id).exists?
end
def staff?
STAFF_GROUPS.include?(self.name.to_sym)
end

View File

@ -1,11 +0,0 @@
class GroupSerializer < BasicGroupSerializer
attributes :mentionable
def mentionable
object.mentionable?(scope.user, object.id)
end
def include_mentionable?
authenticated?
end
end

View File

@ -409,6 +409,7 @@ Discourse::Application.routes.draw do
get 'mentions'
get 'messages'
get 'counts'
get 'mentionable'
member do
put "members" => "groups#add_members"

View File

@ -0,0 +1,4 @@
Fabricator(:email_token) do
user
email { |attrs| attrs[:user].email }
end

View 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