feat: #7964, change all categories at once

allow passing cids to setCategoryWatchState and category.exists
v1.18.x
Baris Usakli 5 years ago
parent 0da4f7ee8d
commit 485fbd2f14

@ -28,6 +28,7 @@
"reputation": "Reputation",
"bookmarks":"Bookmarks",
"watched_categories": "Watched categories",
"change_all": "Change All",
"watched": "Watched",
"ignored": "Ignored",
"default-category-watch-state": "Default category watch state",

@ -10,6 +10,21 @@ define('forum/account/categories', ['forum/account/header'], function (header) {
ajaxify.data.categories.forEach(function (category) {
handleIgnoreWatch(category.cid);
});
$('[component="category/watch/all"]').find('[component="category/watching"], [component="category/ignoring"], [component="category/notwatching"]').on('click', function () {
var cids = [];
var state = $(this).attr('data-state');
$('[data-parent-cid="0"]').each(function (index, el) {
cids.push($(el).attr('data-cid'));
});
socket.emit('categories.setWatchState', { cid: cids, state: state, uid: ajaxify.data.uid }, function (err, modified_cids) {
if (err) {
return app.alertError(err.message);
}
updateDropdowns(modified_cids, state);
});
});
};
function handleIgnoreWatch(cid) {
@ -22,21 +37,24 @@ define('forum/account/categories', ['forum/account/header'], function (header) {
if (err) {
return app.alertError(err.message);
}
updateDropdowns(modified_cids, state);
modified_cids.forEach(function (cid) {
var category = $('[data-cid="' + cid + '"]');
category.find('[component="category/watching/menu"]').toggleClass('hidden', state !== 'watching');
category.find('[component="category/watching/check"]').toggleClass('fa-check', state === 'watching');
app.alertSuccess('[[category:' + state + '.message]]');
});
});
}
category.find('[component="category/notwatching/menu"]').toggleClass('hidden', state !== 'notwatching');
category.find('[component="category/notwatching/check"]').toggleClass('fa-check', state === 'notwatching');
function updateDropdowns(modified_cids, state) {
modified_cids.forEach(function (cid) {
var category = $('[data-cid="' + cid + '"]');
category.find('[component="category/watching/menu"]').toggleClass('hidden', state !== 'watching');
category.find('[component="category/watching/check"]').toggleClass('fa-check', state === 'watching');
category.find('[component="category/ignoring/menu"]').toggleClass('hidden', state !== 'ignoring');
category.find('[component="category/ignoring/check"]').toggleClass('fa-check', state === 'ignoring');
});
category.find('[component="category/notwatching/menu"]').toggleClass('hidden', state !== 'notwatching');
category.find('[component="category/notwatching/check"]').toggleClass('fa-check', state === 'notwatching');
app.alertSuccess('[[category:' + state + '.message]]');
});
category.find('[component="category/ignoring/menu"]').toggleClass('hidden', state !== 'ignoring');
category.find('[component="category/ignoring/check"]').toggleClass('fa-check', state === 'ignoring');
});
}

@ -23,6 +23,9 @@ require('./update')(Categories);
require('./watch')(Categories);
Categories.exists = async function (cid) {
if (Array.isArray(cid)) {
return await db.exists(cid.map(cid => 'category:' + cid));
}
return await db.exists('category:' + cid);
};

@ -105,8 +105,8 @@ SocketCategories.setWatchState = async function (socket, data) {
if (!data || !data.cid || !data.state) {
throw new Error('[[error:invalid-data]]');
}
return await ignoreOrWatch(async function (uid, cid) {
await user.setCategoryWatchState(uid, cid, categories.watchStates[data.state]);
return await ignoreOrWatch(async function (uid, cids) {
await user.setCategoryWatchState(uid, cids, categories.watchStates[data.state]);
}, socket, data);
};
@ -120,7 +120,7 @@ SocketCategories.ignore = async function (socket, data) {
async function ignoreOrWatch(fn, socket, data) {
let targetUid = socket.uid;
const cids = [parseInt(data.cid, 10)];
const cids = Array.isArray(data.cid) ? data.cid.map(cid => parseInt(cid, 10)) : [parseInt(data.cid, 10)];
if (data.hasOwnProperty('uid')) {
targetUid = data.uid;
}
@ -137,7 +137,7 @@ async function ignoreOrWatch(fn, socket, data) {
}
} while (cat);
await Promise.all(cids.map(cid => fn(targetUid, cid)));
await fn(targetUid, cids);
await topics.pushUnreadCount(targetUid);
return cids;
}

@ -6,7 +6,7 @@ const db = require('../database');
const categories = require('../categories');
module.exports = function (User) {
User.setCategoryWatchState = async function (uid, cid, state) {
User.setCategoryWatchState = async function (uid, cids, state) {
if (!(parseInt(uid, 10) > 0)) {
return;
}
@ -14,11 +14,12 @@ module.exports = function (User) {
if (!isStateValid) {
throw new Error('[[error:invalid-watch-state]]');
}
const exists = await categories.exists(cid);
if (!exists) {
cids = Array.isArray(cids) ? cids : [cids];
const exists = await categories.exists(cids);
if (exists.includes(false)) {
throw new Error('[[error:no-category]]');
}
await db.sortedSetAdd('cid:' + cid + ':uid:watch:state', state, uid);
await db.sortedSetsAdd(cids.map(cid => 'cid:' + cid + ':uid:watch:state'), state, uid);
};
User.getCategoryWatchState = async function (uid) {

Loading…
Cancel
Save