Allow parameters for group and username filters on directory (#5815)

This commit is contained in:
Joe Buhlig 2018-05-16 09:20:17 -05:00 committed by Régis Hanol
parent 489e7f220d
commit 3cd4c82c49
5 changed files with 59 additions and 9 deletions

View File

@ -2,11 +2,13 @@ import debounce from 'discourse/lib/debounce';
export default Ember.Controller.extend({
application: Ember.inject.controller(),
queryParams: ["period", "order", "asc", "name"],
queryParams: ["period", "order", "asc", "name", "group", "exclude_usernames"],
period: "weekly",
order: "likes_received",
asc: null,
name: "",
group: null,
exclude_usernames: null,
showTimeRead: Ember.computed.equal("period", "all"),

View File

@ -3,7 +3,9 @@ export default Discourse.Route.extend({
period: { refreshModel: true },
order: { refreshModel: true },
asc: { refreshModel: true },
name: { refreshModel: true, replace: true }
name: { refreshModel: true, replace: true },
group: { refreshModel: true },
exclude_usernames: { refreshModel: true }
},
refreshQueryWithoutTransition: true,
@ -18,7 +20,9 @@ export default Discourse.Route.extend({
period: "weekly",
order: "likes_received",
asc: null,
name: ""
name: "",
group: null,
exclude_usernames: null
});
}
},

View File

@ -10,6 +10,14 @@ class DirectoryItemsController < ApplicationController
result = DirectoryItem.where(period_type: period_type).includes(:user)
if params[:group]
result = result.includes(user: :groups).where(users: {groups: {name: params[:group]}})
end
if params[:exclude_usernames]
result = result.references(:user).where.not(users: {username: params[:exclude_usernames].split(",")})
end
order = params[:order] || DirectoryItem.headings.first
if DirectoryItem.headings.include?(order.to_sym)
dir = params[:asc] ? 'ASC' : 'DESC'

View File

@ -2,6 +2,10 @@ require 'rails_helper'
describe DirectoryItemsController do
let!(:user) { Fabricate(:user) }
let!(:evil_trout) { Fabricate(:evil_trout) }
let!(:walter_white) { Fabricate(:walter_white) }
let!(:stage_user) { Fabricate(:staged, username: 'stage_user') }
let!(:group) { Fabricate(:group, users: [evil_trout,stage_user]) }
it "requires a `period` param" do
get '/directory_items.json'
@ -28,10 +32,6 @@ describe DirectoryItemsController do
context "with data" do
before do
Fabricate(:evil_trout)
Fabricate(:walter_white)
Fabricate(:staged, username: 'stage_user')
DirectoryItem.refresh!
end
@ -77,5 +77,29 @@ describe DirectoryItemsController do
expect(json['total_rows_directory_items']).to eq(1)
expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
end
it "excludes users by username" do
get '/directory_items.json', params: { period: 'all', exclude_usernames: "stage_user,eviltrout" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json).to be_present
expect(json['directory_items'].length).to eq(2)
expect(json['total_rows_directory_items']).to eq(2)
expect(json['directory_items'][0]['user']['username']).to eq(walter_white.username) | eq(user.username)
expect(json['directory_items'][1]['user']['username']).to eq(walter_white.username) | eq(user.username)
end
it "filters users by group" do
get '/directory_items.json', params: { period: 'all', group: group.name }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json).to be_present
expect(json['directory_items'].length).to eq(2)
expect(json['total_rows_directory_items']).to eq(2)
expect(json['directory_items'][0]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
expect(json['directory_items'][1]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
end
end
end

View File

@ -2,12 +2,24 @@ import { acceptance } from "helpers/qunit-helpers";
acceptance("User Directory");
QUnit.test("Visit Page", async assert => {
await visit("/users");
await visit("/u");
assert.ok($('body.users-page').length, "has the body class");
assert.ok(exists('.directory table tr'), "has a list of users");
});
QUnit.test("Visit All Time", async assert => {
await visit("/users?period=all");
await visit("/u?period=all");
assert.ok(exists('.time-read'), "has time read column");
});
QUnit.test("Visit Without Usernames", async assert => {
await visit("/u?exclude_usernames=system");
assert.ok($('body.users-page').length, "has the body class");
assert.ok(exists('.directory table tr'), "has a list of users");
});
QUnit.test("Visit With Group Filter", async assert => {
await visit("/u?group=trust_level_0");
assert.ok($('body.users-page').length, "has the body class");
assert.ok(exists('.directory table tr'), "has a list of users");
});