feat: #7964, change all categories at once

allow passing cids to setCategoryWatchState and category.exists
This commit is contained in:
Baris Usakli
2019-10-11 15:25:41 -04:00
parent 0da4f7ee8d
commit 485fbd2f14
5 changed files with 43 additions and 20 deletions

View File

@@ -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) {