Skip to content

Commit

Permalink
refactor: deprecate socket.emit('users.search') use api route
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 17, 2020
1 parent 083c74e commit 2279e37
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 84 deletions.
8 changes: 6 additions & 2 deletions public/src/client/account/blocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

define('forum/account/blocks', ['forum/account/header'], function (header) {
define('forum/account/blocks', [
'forum/account/header',
'api',
], function (header, api) {
var Blocks = {};

Blocks.init = function () {
Expand All @@ -9,9 +12,10 @@ define('forum/account/blocks', ['forum/account/header'], function (header) {
$('#user-search').on('keyup', function () {
var username = this.value;

socket.emit('user.search', {
api.get('/api/users', {
query: username,
searchBy: 'username',
paginate: false,
}, function (err, data) {
if (err) {
return app.alertError(err.message);
Expand Down
20 changes: 7 additions & 13 deletions public/src/client/chats/search.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';


define('forum/chats/search', ['components'], function (components) {
define('forum/chats/search', ['components', 'api'], function (components, api) {
var search = {};

search.init = function () {
Expand All @@ -19,26 +19,20 @@ define('forum/chats/search', ['components'], function (components) {

function doSearch() {
var username = components.get('chat/search').val();
var chatsListEl = $('[component="chat/search/list"]');

if (!username) {
return chatsListEl.empty();
return $('[component="chat/search/list"]').empty();
}

socket.emit('user.search', {
api.get('/api/users', {
query: username,
searchBy: 'username',
paginate: false,
}, function (err, data) {
if (err) {
return app.alertError(err.message);
}

displayResults(chatsListEl, data);
});
}).then(displayResults)
.catch(app.alertError);
}

function displayResults(chatsListEl, data) {
function displayResults(data) {
var chatsListEl = $('[component="chat/search/list"]');
chatsListEl.empty();

data.users = data.users.filter(function (user) {
Expand Down
2 changes: 1 addition & 1 deletion public/src/client/groups/memberlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ define('forum/groups/memberlist', ['api'], function (api) {
$(this).find('i').toggleClass('invisible');
});
modal.find('input').on('keyup', function () {
socket.emit('user.search', {
api.get('/api/users', {
query: $(this).val(),
paginate: false,
}, function (err, result) {
Expand Down
14 changes: 7 additions & 7 deletions public/src/client/users.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';


define('forum/users', ['translator', 'benchpress'], function (translator, Benchpress) {
define('forum/users', [
'translator', 'benchpress', 'api',
], function (translator, Benchpress, api) {
var Users = {};

var searchTimeoutID = 0;
Expand Down Expand Up @@ -95,12 +97,9 @@ define('forum/users', ['translator', 'benchpress'], function (translator, Benchp


function loadPage(query) {
var qs = decodeURIComponent($.param(query));
$.get(config.relative_path + '/api/users?' + qs, renderSearchResults).fail(function (xhrErr) {
if (xhrErr && xhrErr.responseJSON && xhrErr.responseJSON.error) {
app.alertError(xhrErr.responseJSON.error);
}
});
api.get('/api/users', query)
.then(renderSearchResults)
.catch(app.alertError);
}

function renderSearchResults(data) {
Expand All @@ -112,6 +111,7 @@ define('forum/users', ['translator', 'benchpress'], function (translator, Benchp
data.users = data.users.slice(0, searchResultCount);
}

data.isAdminOrGlobalMod = app.user.isAdmin || app.user.isGlobalMod;
Benchpress.parse('users', 'users', data, function (html) {
translator.translate(html, function (translated) {
translated = $(translated);
Expand Down
4 changes: 2 additions & 2 deletions public/src/modules/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';


define('autocomplete', function () {
define('autocomplete', ['api'], function (api) {
var module = {};

module.user = function (input, params, onselect) {
Expand All @@ -23,7 +23,7 @@ define('autocomplete', function () {
source: function (request, response) {
params.query = request.term;

socket.emit('user.search', params, function (err, result) {
api.get('/api/users', params, function (err, result) {
if (err) {
return app.alertError(err.message);
}
Expand Down
26 changes: 26 additions & 0 deletions src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,29 @@ async function canDeleteUids(uids) {

return true;
}

usersAPI.search = async function (caller, data) {
const [allowed, isPrivileged] = await Promise.all([
privileges.global.can('search:users', caller.uid),
user.isPrivileged(caller.uid),
]);
let filters = data.filters || [];
filters = Array.isArray(filters) ? filters : [filters];
if (!allowed ||
((
data.searchBy === 'ip' ||
data.searchBy === 'email' ||
filters.includes('banned') ||
filters.includes('flagged')
) && !isPrivileged)
) {
throw new Error('[[error:no-privileges]]');
}
return await user.search({
query: data.query,
searchBy: data.searchBy || 'username',
page: data.page || 1,
sortBy: data.sortBy || 'lastonline',
filters: filters,
});
};
2 changes: 1 addition & 1 deletion src/controllers/admin/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function getUsers(req, res) {
postcount: 'users:postcount',
reputation: 'users:reputation',
joindate: 'users:joindate',
online: 'users:online',
lastonline: 'users:online',
flags: 'users:flags',
};

Expand Down
30 changes: 3 additions & 27 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const db = require('../database');
const pagination = require('../pagination');
const privileges = require('../privileges');
const helpers = require('./helpers');
const api = require('../api');

const usersController = module.exports;

Expand All @@ -31,35 +32,10 @@ usersController.index = async function (req, res, next) {
};

usersController.search = async function (req, res) {
const [allowed, isPrivileged] = await Promise.all([
privileges.global.can('search:users', req.uid),
user.isPrivileged(req.uid),
]);
let filters = req.query.filters || [];
filters = Array.isArray(filters) ? filters : [filters];
if (!allowed ||
((
req.query.searchBy === 'ip' ||
req.query.searchBy === 'email' ||
filters.includes('banned') ||
filters.includes('flagged')
) && !isPrivileged)
) {
throw new Error('[[error:no-privileges]]');
}
const [searchData, isAdminOrGlobalMod] = await Promise.all([
user.search({
query: req.query.query,
searchBy: req.query.searchBy || 'username',
page: req.query.page || 1,
sortBy: req.query.sortBy || 'joindate',
filters: filters,
}),
user.isAdminOrGlobalMod(req.uid),
]);
const searchData = await api.users.search(req, req.query);

const section = req.query.section || 'joindate';

searchData.isAdminOrGlobalMod = isAdminOrGlobalMod;
searchData.pagination = pagination.create(req.query.page, searchData.pageCount, req.query);
searchData['section_' + section] = true;
searchData.displayUserSearch = true;
Expand Down
34 changes: 4 additions & 30 deletions src/socket.io/user/search.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
'use strict';

const user = require('../../user');
const pagination = require('../../pagination');
const privileges = require('../../privileges');
const api = require('../../api');
const sockets = require('..');

module.exports = function (SocketUser) {
SocketUser.search = async function (socket, data) {
// TODO: depracate and use usersController.search
sockets.warnDeprecated(socket, 'GET /api/users');
if (!data) {
throw new Error('[[error:invalid-data]]');
}
const [allowed, isPrivileged] = await Promise.all([
privileges.global.can('search:users', socket.uid),
user.isPrivileged(socket.uid),
]);

let filters = data.filters || [];
filters = Array.isArray(filters) ? filters : [filters];
if (!allowed ||
((
data.searchBy === 'ip' ||
data.searchBy === 'email' ||
filters.includes('banned') ||
filters.includes('flagged')
) && !isPrivileged)
) {
throw new Error('[[error:no-privileges]]');
}
const result = await user.search({
query: data.query,
page: data.page,
searchBy: data.searchBy,
sortBy: data.sortBy,
filters: data.filters,
paginate: data.paginate,
uid: socket.uid,
});
const result = api.users.search(socket, data);
result.pagination = pagination.create(data.page, result.pageCount);
result['route_users:' + data.sortBy] = true;
return result;
};
};
2 changes: 1 addition & 1 deletion src/views/admin/manage/users.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<th data-sort="reputation" class="text-right pointer">[[admin/manage/users:users.reputation]] {{{if sort_reputation}}}<i class="fa fa-sort-{{{if reverse}}}down{{{else}}}up{{{end}}}">{{{end}}}</th>
<th data-sort="flags" class="text-right pointer">[[admin/manage/users:users.flags]] {{{if sort_flags}}}<i class="fa fa-sort-{{{if reverse}}}down{{{else}}}up{{{end}}}">{{{end}}}</th>
<th data-sort="joindate" class="pointer">[[admin/manage/users:users.joined]] {{{if sort_joindate}}}<i class="fa fa-sort-{{{if reverse}}}down{{{else}}}up{{{end}}}">{{{end}}}</th>
<th data-sort="online" class="pointer">[[admin/manage/users:users.last-online]] {{{if sort_online}}}<i class="fa fa-sort-{{{if reverse}}}down{{{else}}}up{{{end}}}">{{{end}}}</th>
<th data-sort="lastonline" class="pointer">[[admin/manage/users:users.last-online]] {{{if sort_online}}}<i class="fa fa-sort-{{{if reverse}}}down{{{else}}}up{{{end}}}">{{{end}}}</th>
</tr>
</thead>
<tbody>
Expand Down

0 comments on commit 2279e37

Please sign in to comment.