mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-18 03:31:03 +01:00
feat: #12495, add unblock button to users on /blocks
This commit is contained in:
@@ -11,12 +11,19 @@ define('forum/account/blocks', [
|
|||||||
Blocks.init = function () {
|
Blocks.init = function () {
|
||||||
header.init();
|
header.init();
|
||||||
const blockListEl = $('[component="blocks/search/list"]');
|
const blockListEl = $('[component="blocks/search/list"]');
|
||||||
|
const startTypingEl = blockListEl.find('[component="blocks/start-typing"]');
|
||||||
|
const noUsersEl = blockListEl.find('[component="blocks/no-users"]');
|
||||||
|
|
||||||
$('#user-search').on('keyup', function () {
|
$('#user-search').on('keyup', utils.debounce(function () {
|
||||||
const username = this.value;
|
const username = this.value;
|
||||||
|
|
||||||
if (!username) {
|
if (!username) {
|
||||||
return blockListEl.translateHtml('<li><a href="#" class="dropdown-item" role="menuitem">[[admin/menu:search.start-typing]]</a></li>');
|
blockListEl.find('[component="blocks/search/match"]').remove();
|
||||||
|
startTypingEl.removeClass('hidden');
|
||||||
|
noUsersEl.addClass('hidden');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
startTypingEl.addClass('hidden');
|
||||||
api.get('/api/users', {
|
api.get('/api/users', {
|
||||||
query: username,
|
query: username,
|
||||||
searchBy: 'username',
|
searchBy: 'username',
|
||||||
@@ -26,8 +33,10 @@ define('forum/account/blocks', [
|
|||||||
return alerts.error(err);
|
return alerts.error(err);
|
||||||
}
|
}
|
||||||
if (!data.users.length) {
|
if (!data.users.length) {
|
||||||
return blockListEl.translateHtml('<li><a href="#" class="dropdown-item" role="menuitem">[[users:no-users-found]]</a></li>');
|
noUsersEl.removeClass('hidden');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
noUsersEl.addClass('hidden');
|
||||||
// Only show first 10 matches
|
// Only show first 10 matches
|
||||||
if (data.matchCount > 10) {
|
if (data.matchCount > 10) {
|
||||||
data.users.length = 10;
|
data.users.length = 10;
|
||||||
@@ -36,25 +45,36 @@ define('forum/account/blocks', [
|
|||||||
app.parseAndTranslate('account/blocks', 'edit', {
|
app.parseAndTranslate('account/blocks', 'edit', {
|
||||||
edit: data.users,
|
edit: data.users,
|
||||||
}, function (html) {
|
}, function (html) {
|
||||||
$('.block-edit').html(html);
|
blockListEl.find('[component="blocks/search/match"]').remove();
|
||||||
|
html.insertAfter(noUsersEl);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}, 200));
|
||||||
|
|
||||||
|
$('.block-edit').on('click', '[data-action="block"], [data-action="unblock"]', async function () {
|
||||||
|
const uid = parseInt(this.getAttribute('data-uid'), 10);
|
||||||
|
const action = $(this).attr('data-action');
|
||||||
|
const currentBtn = $(this);
|
||||||
|
await performBlock(uid, action);
|
||||||
|
currentBtn.addClass('hidden').siblings('[data-action]').removeClass('hidden');
|
||||||
|
Blocks.refreshList();
|
||||||
});
|
});
|
||||||
|
|
||||||
$('.block-edit').on('click', '[data-action="toggle"]', function () {
|
$('#users-container').on('click', '[data-action="unblock"]', async function () {
|
||||||
const uid = parseInt(this.getAttribute('data-uid'), 10);
|
await performBlock($(this).attr('data-uid'), $(this).attr('data-action'));
|
||||||
socket.emit('user.toggleBlock', {
|
Blocks.refreshList();
|
||||||
blockeeUid: uid,
|
|
||||||
blockerUid: ajaxify.data.uid,
|
|
||||||
}, Blocks.refreshList);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Blocks.refreshList = function (err) {
|
async function performBlock(uid, action) {
|
||||||
if (err) {
|
return socket.emit('user.toggleBlock', {
|
||||||
return alerts.error(err);
|
blockeeUid: uid,
|
||||||
}
|
blockerUid: ajaxify.data.uid,
|
||||||
|
action: action,
|
||||||
|
}).catch(alerts.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
Blocks.refreshList = function () {
|
||||||
$.get(config.relative_path + '/api/' + ajaxify.currentPage)
|
$.get(config.relative_path + '/api/' + ajaxify.currentPage)
|
||||||
.done(function (payload) {
|
.done(function (payload) {
|
||||||
app.parseAndTranslate('account/blocks', 'users', payload, function (html) {
|
app.parseAndTranslate('account/blocks', 'users', payload, function (html) {
|
||||||
|
|||||||
@@ -601,6 +601,7 @@ usersAPI.search = async function (caller, data) {
|
|||||||
throw new Error('[[error:no-privileges]]');
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
return await user.search({
|
return await user.search({
|
||||||
|
uid: caller.uid,
|
||||||
query: data.query,
|
query: data.query,
|
||||||
searchBy: data.searchBy || 'username',
|
searchBy: data.searchBy || 'username',
|
||||||
page: data.page || 1,
|
page: data.page || 1,
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ usersController.index = async function (req, res, next) {
|
|||||||
|
|
||||||
if (req.query.query) {
|
if (req.query.query) {
|
||||||
await usersController.search(req, res, next);
|
await usersController.search(req, res, next);
|
||||||
} else if (sectionToController[section]) {
|
} else if (sectionToController.hasOwnProperty(section) && sectionToController[section]) {
|
||||||
await sectionToController[section](req, res, next);
|
await sectionToController[section](req, res, next);
|
||||||
} else {
|
} else {
|
||||||
await usersController.getUsersSortedByJoinDate(req, res, next);
|
await usersController.getUsersSortedByJoinDate(req, res, next);
|
||||||
|
|||||||
@@ -41,8 +41,16 @@ module.exports = function (SocketUser) {
|
|||||||
|
|
||||||
SocketUser.toggleBlock = async function (socket, data) {
|
SocketUser.toggleBlock = async function (socket, data) {
|
||||||
const isBlocked = await user.blocks.is(data.blockeeUid, data.blockerUid);
|
const isBlocked = await user.blocks.is(data.blockeeUid, data.blockerUid);
|
||||||
await user.blocks.can(socket.uid, data.blockerUid, data.blockeeUid, isBlocked ? 'unblock' : 'block');
|
const { action, blockerUid, blockeeUid } = data;
|
||||||
await user.blocks[isBlocked ? 'remove' : 'add'](data.blockeeUid, data.blockerUid);
|
if (action !== 'block' && action !== 'unblock') {
|
||||||
|
throw new Error('[[error:unknow-block-action]]');
|
||||||
|
}
|
||||||
|
await user.blocks.can(socket.uid, blockerUid, blockeeUid, action);
|
||||||
|
if (data.action === 'block') {
|
||||||
|
await user.blocks.add(blockeeUid, blockerUid);
|
||||||
|
} else if (data.action === 'unblock') {
|
||||||
|
await user.blocks.remove(blockeeUid, blockerUid);
|
||||||
|
}
|
||||||
return !isBlocked;
|
return !isBlocked;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -60,7 +60,19 @@ module.exports = function (User) {
|
|||||||
uids = uids.slice(start, stop);
|
uids = uids.slice(start, stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
const userData = await User.getUsers(uids, uid);
|
const [userData, blocks] = await Promise.all([
|
||||||
|
User.getUsers(uids, uid),
|
||||||
|
User.blocks.list(uid),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (blocks.length) {
|
||||||
|
userData.forEach((user) => {
|
||||||
|
if (user) {
|
||||||
|
user.isBlocked = blocks.includes(user.uid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
searchResult.timing = (process.elapsedTimeSince(startTime) / 1000).toFixed(2);
|
searchResult.timing = (process.elapsedTimeSince(startTime) / 1000).toFixed(2);
|
||||||
searchResult.users = userData.filter(user => user && user.uid > 0);
|
searchResult.users = userData.filter(user => user && user.uid > 0);
|
||||||
return searchResult;
|
return searchResult;
|
||||||
|
|||||||
Reference in New Issue
Block a user