From 2279e37261eef3da7327a771c799088632730f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 16 Oct 2020 23:30:22 -0400 Subject: [PATCH] refactor: deprecate socket.emit('users.search') use api route --- public/src/client/account/blocks.js | 8 ++++-- public/src/client/chats/search.js | 20 ++++++--------- public/src/client/groups/memberlist.js | 2 +- public/src/client/users.js | 14 +++++------ public/src/modules/autocomplete.js | 4 +-- src/api/users.js | 26 ++++++++++++++++++++ src/controllers/admin/users.js | 2 +- src/controllers/users.js | 30 +++-------------------- src/socket.io/user/search.js | 34 +++----------------------- src/views/admin/manage/users.tpl | 2 +- 10 files changed, 58 insertions(+), 84 deletions(-) diff --git a/public/src/client/account/blocks.js b/public/src/client/account/blocks.js index 3e76ceb620a9..d14901d8a3e4 100644 --- a/public/src/client/account/blocks.js +++ b/public/src/client/account/blocks.js @@ -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 () { @@ -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); diff --git a/public/src/client/chats/search.js b/public/src/client/chats/search.js index 61a4d9800aa3..3103a22fab43 100644 --- a/public/src/client/chats/search.js +++ b/public/src/client/chats/search.js @@ -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 () { @@ -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) { diff --git a/public/src/client/groups/memberlist.js b/public/src/client/groups/memberlist.js index c4d02e33e182..4864cb9d602d 100644 --- a/public/src/client/groups/memberlist.js +++ b/public/src/client/groups/memberlist.js @@ -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) { diff --git a/public/src/client/users.js b/public/src/client/users.js index 3ff1a62c68d1..c96b82c2d7b1 100644 --- a/public/src/client/users.js +++ b/public/src/client/users.js @@ -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; @@ -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) { @@ -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); diff --git a/public/src/modules/autocomplete.js b/public/src/modules/autocomplete.js index d8259e6a96b4..656dbe652cdb 100644 --- a/public/src/modules/autocomplete.js +++ b/public/src/modules/autocomplete.js @@ -2,7 +2,7 @@ 'use strict'; -define('autocomplete', function () { +define('autocomplete', ['api'], function (api) { var module = {}; module.user = function (input, params, onselect) { @@ -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); } diff --git a/src/api/users.js b/src/api/users.js index db9aa15dbb10..0a8743068171 100644 --- a/src/api/users.js +++ b/src/api/users.js @@ -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, + }); +}; diff --git a/src/controllers/admin/users.js b/src/controllers/admin/users.js index 773a7c5e109e..00f43015e5e4 100644 --- a/src/controllers/admin/users.js +++ b/src/controllers/admin/users.js @@ -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', }; diff --git a/src/controllers/users.js b/src/controllers/users.js index 3ead28620898..eef5b9c0e5d7 100644 --- a/src/controllers/users.js +++ b/src/controllers/users.js @@ -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; @@ -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; diff --git a/src/socket.io/user/search.js b/src/socket.io/user/search.js index c181fb599d66..8855f82c7bfd 100644 --- a/src/socket.io/user/search.js +++ b/src/socket.io/user/search.js @@ -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; }; }; diff --git a/src/views/admin/manage/users.tpl b/src/views/admin/manage/users.tpl index 3808e960b0e9..0578299721fa 100644 --- a/src/views/admin/manage/users.tpl +++ b/src/views/admin/manage/users.tpl @@ -98,7 +98,7 @@ [[admin/manage/users:users.reputation]] {{{if sort_reputation}}}{{{end}}} [[admin/manage/users:users.flags]] {{{if sort_flags}}}{{{end}}} [[admin/manage/users:users.joined]] {{{if sort_joindate}}}{{{end}}} - [[admin/manage/users:users.last-online]] {{{if sort_online}}}{{{end}}} + [[admin/manage/users:users.last-online]] {{{if sort_online}}}{{{end}}}