mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 00:15:46 +01:00
feat: #7964, change all categories at once
allow passing cids to setCategoryWatchState and category.exists
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user