mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-05 03:51:26 +01:00
closes #14030
This commit is contained in:
@@ -158,7 +158,7 @@ categoriesAPI.getTopics = async (caller, data) => {
|
||||
categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
|
||||
let targetUid = caller.uid;
|
||||
let cids = Array.isArray(cid) ? cid : [cid];
|
||||
cids = cids.map(cid => (utils.isNumber(cid) ? parseInt(cid, 10) : cid));
|
||||
cids = cids.map(cid => String(cid));
|
||||
|
||||
if (uid) {
|
||||
targetUid = uid;
|
||||
@@ -170,9 +170,9 @@ categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
|
||||
// filter to subcategories of cid
|
||||
let cat;
|
||||
do {
|
||||
cat = categoryData.find(c => !cids.includes(c.cid) && cids.includes(c.parentCid));
|
||||
cat = categoryData.find(c => !cids.includes(String(c.cid)) && cids.includes(String(c.parentCid)));
|
||||
if (cat) {
|
||||
cids.push(cat.cid);
|
||||
cids.push(String(cat.cid));
|
||||
}
|
||||
} while (cat);
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ searchApi.categories = async (caller, data) => {
|
||||
data.states = (data.states || ['watching', 'tracking', 'notwatching', 'ignoring']).map(
|
||||
state => categories.watchStates[state]
|
||||
);
|
||||
data.parentCid = parseInt(data.parentCid || 0, 10);
|
||||
data.parentCid = String(data.parentCid || 0);
|
||||
|
||||
let cids;
|
||||
if (data.search) {
|
||||
@@ -42,15 +42,15 @@ searchApi.categories = async (caller, data) => {
|
||||
});
|
||||
|
||||
if (Array.isArray(data.selectedCids)) {
|
||||
data.selectedCids = data.selectedCids.map(cid => parseInt(cid, 10));
|
||||
data.selectedCids = data.selectedCids.map(cid => String(cid));
|
||||
}
|
||||
|
||||
let categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass'], data.parentCid);
|
||||
categoriesData = categoriesData.slice(0, 1000);
|
||||
|
||||
categoriesData.forEach((category) => {
|
||||
category.selected = data.selectedCids ? data.selectedCids.includes(category.cid) : false;
|
||||
if (matchedCids.includes(category.cid)) {
|
||||
category.selected = data.selectedCids ? data.selectedCids.includes(String(category.cid)) : false;
|
||||
if (matchedCids.includes(String(category.cid))) {
|
||||
category.match = true;
|
||||
}
|
||||
});
|
||||
@@ -72,7 +72,7 @@ async function findMatchedCids(uid, data) {
|
||||
localOnly: data.localOnly,
|
||||
});
|
||||
|
||||
let matchedCids = result.categories.map(c => c.cid);
|
||||
let matchedCids = result.categories.map(c => String(c.cid));
|
||||
// no need to filter if all 3 states are used
|
||||
const filterByWatchState = !Object.values(categories.watchStates)
|
||||
.every(state => data.states.includes(state));
|
||||
@@ -82,8 +82,12 @@ async function findMatchedCids(uid, data) {
|
||||
matchedCids = matchedCids.filter((cid, index) => data.states.includes(states[index]));
|
||||
}
|
||||
|
||||
const rootCids = _.uniq(_.flatten(await Promise.all(matchedCids.map(categories.getParentCids))));
|
||||
const allChildCids = _.uniq(_.flatten(await Promise.all(matchedCids.map(categories.getChildrenCids))));
|
||||
const rootCids = _.uniq(_.flatten(
|
||||
await Promise.all(matchedCids.map(categories.getParentCids))
|
||||
));
|
||||
const allChildCids = _.uniq(_.flatten(
|
||||
await Promise.all(matchedCids.map(categories.getChildrenCids))
|
||||
));
|
||||
|
||||
return {
|
||||
cids: _.uniq(rootCids.concat(allChildCids).concat(matchedCids)),
|
||||
@@ -145,7 +149,7 @@ searchApi.roomUsers = async (caller, { query, roomId }) => {
|
||||
roomUsers.forEach((user, index) => {
|
||||
if (user) {
|
||||
user.isOwner = isOwners[index];
|
||||
user.canKick = isRoomOwner && (parseInt(user.uid, 10) !== parseInt(caller.uid, 10));
|
||||
user.canKick = isRoomOwner && String(user.uid) !== String(caller.uid);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -104,7 +104,6 @@ Categories.getAllCidsFromSet = async function (key) {
|
||||
}
|
||||
|
||||
cids = await db.getSortedSetRange(key, 0, -1);
|
||||
cids = cids.map(cid => utils.isNumber(cid) ? parseInt(cid, 10) : cid);
|
||||
cache.set(key, cids);
|
||||
return cids.slice();
|
||||
};
|
||||
@@ -261,7 +260,7 @@ Categories.getChildren = async function (cids, uid) {
|
||||
async function getChildrenTree(category, uid) {
|
||||
let childrenCids = await Categories.getChildrenCids(category.cid);
|
||||
childrenCids = await privileges.categories.filterCids('find', childrenCids, uid);
|
||||
childrenCids = childrenCids.filter(cid => parseInt(category.cid, 10) !== parseInt(cid, 10));
|
||||
childrenCids = childrenCids.filter(cid => String(category.cid) !== String(cid));
|
||||
if (!childrenCids.length) {
|
||||
category.children = [];
|
||||
return;
|
||||
@@ -280,7 +279,7 @@ Categories.getParentCids = async function (currentCid) {
|
||||
// eslint-disable-next-line
|
||||
cid = await Categories.getCategoryField(cid, 'parentCid');
|
||||
if (cid) {
|
||||
parents.unshift(cid);
|
||||
parents.unshift(String(cid));
|
||||
}
|
||||
}
|
||||
return parents;
|
||||
@@ -291,12 +290,12 @@ Categories.getChildrenCids = async function (rootCid) {
|
||||
async function recursive(keys) {
|
||||
let childrenCids = await db.getSortedSetRange(keys, 0, -1);
|
||||
|
||||
childrenCids = childrenCids.filter(cid => !allCids.includes(utils.isNumber(cid) ? parseInt(cid, 10) : cid));
|
||||
childrenCids = childrenCids.filter(cid => !allCids.includes(cid));
|
||||
if (!childrenCids.length) {
|
||||
return;
|
||||
}
|
||||
allCids.push(...childrenCids);
|
||||
keys = childrenCids.map(cid => `cid:${cid}:children`);
|
||||
childrenCids.forEach(cid => allCids.push(utils.isNumber(cid) ? parseInt(cid, 10) : cid));
|
||||
await recursive(keys);
|
||||
}
|
||||
const key = `cid:${rootCid}:children`;
|
||||
|
||||
@@ -94,7 +94,7 @@ module.exports = function (Categories) {
|
||||
}
|
||||
|
||||
async function getChildrenCids(cids, uid) {
|
||||
const childrenCids = await Promise.all(cids.map(cid => Categories.getChildrenCids(cid)));
|
||||
const childrenCids = await Promise.all(cids.map(Categories.getChildrenCids));
|
||||
return await privileges.categories.filterCids('find', _.flatten(childrenCids), uid);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -67,8 +67,8 @@ module.exports = function (Categories) {
|
||||
}
|
||||
|
||||
async function updateParent(cid, newParent) {
|
||||
newParent = parseInt(newParent, 10) || 0;
|
||||
if (parseInt(cid, 10) === newParent) {
|
||||
newParent = String(newParent || 0);
|
||||
if (String(cid) === newParent) {
|
||||
throw new Error('[[error:cant-set-self-as-parent]]');
|
||||
}
|
||||
const childrenCids = await Categories.getChildrenCids(cid);
|
||||
@@ -76,7 +76,7 @@ module.exports = function (Categories) {
|
||||
throw new Error('[[error:cant-set-child-as-parent]]');
|
||||
}
|
||||
const categoryData = await Categories.getCategoryFields(cid, ['parentCid', 'order']);
|
||||
const oldParent = categoryData.parentCid;
|
||||
const oldParent = String(categoryData.parentCid);
|
||||
if (oldParent === newParent) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ categoriesController.getAll = async function (req, res) {
|
||||
const rootCid = parseInt(req.query.cid, 10) || 0;
|
||||
const rootChildren = await categories.getAllCidsFromSet(`cid:${rootCid}:children`);
|
||||
async function getRootAndChildren() {
|
||||
const childCids = _.flatten(await Promise.all(rootChildren.map(cid => categories.getChildrenCids(cid))));
|
||||
const childCids = _.flatten(await Promise.all(rootChildren.map(categories.getChildrenCids)));
|
||||
return [rootCid].concat(rootChildren.concat(childCids));
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ async function buildSelectedCategoryLabel(selectedCids) {
|
||||
label = `[[search:categories-x, ${selectedCids.length}]]`;
|
||||
} else if (selectedCids.length === 1 && selectedCids[0] === 'watched') {
|
||||
label = `[[search:categories-watched-categories]]`;
|
||||
} else if (selectedCids.length === 1 && parseInt(selectedCids[0], 10)) {
|
||||
} else if (selectedCids.length === 1 && selectedCids[0]) {
|
||||
const categoryData = await categories.getCategoryData(selectedCids[0]);
|
||||
if (categoryData && categoryData.name) {
|
||||
label = `[[search:categories-x, ${categoryData.name}]]`;
|
||||
|
||||
@@ -40,7 +40,7 @@ module.exports = function (Posts) {
|
||||
};
|
||||
|
||||
async function filterPidsBySingleCid(pids, cid) {
|
||||
const isMembers = await db.isSortedSetMembers(`cid:${parseInt(cid, 10)}:pids`, pids);
|
||||
const isMembers = await db.isSortedSetMembers(`cid:${cid}:pids`, pids);
|
||||
return pids.filter((pid, index) => pid && isMembers[index]);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -412,8 +412,12 @@ async function getChildrenCids(data) {
|
||||
if (!data.searchChildren) {
|
||||
return [];
|
||||
}
|
||||
const childrenCids = await Promise.all(data.categories.map(cid => categories.getChildrenCids(cid)));
|
||||
return await privileges.categories.filterCids('find', _.uniq(_.flatten(childrenCids)), data.uid);
|
||||
const childrenCids = await Promise.all(data.categories.map(categories.getChildrenCids));
|
||||
return await privileges.categories.filterCids(
|
||||
'find',
|
||||
_.uniq(_.flatten(childrenCids)),
|
||||
data.uid
|
||||
);
|
||||
}
|
||||
|
||||
async function getSearchUids(data) {
|
||||
|
||||
@@ -36,7 +36,7 @@ Categories.copyPrivilegesFrom = async function (socket, data) {
|
||||
|
||||
Categories.copyPrivilegesToAllCategories = async function (socket, data) {
|
||||
let cids = await categories.getAllCidsFromSet('categories:cid');
|
||||
cids = cids.filter(cid => parseInt(cid, 10) !== parseInt(data.cid, 10));
|
||||
cids = cids.filter(cid => String(cid) !== String(data.cid));
|
||||
for (const toCid of cids) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await categories.copyPrivilegesFrom(data.cid, toCid, data.group, data.filter);
|
||||
|
||||
@@ -111,7 +111,9 @@ SocketCategories.ignore = async function (socket, data) {
|
||||
|
||||
async function ignoreOrWatch(fn, socket, data) {
|
||||
let targetUid = socket.uid;
|
||||
const cids = Array.isArray(data.cid) ? data.cid.map(cid => parseInt(cid, 10)) : [parseInt(data.cid, 10)];
|
||||
const cids = Array.isArray(data.cid) ?
|
||||
data.cid.map(cid => String(cid)) :
|
||||
[String(data.cid)];
|
||||
if (data.hasOwnProperty('uid')) {
|
||||
targetUid = data.uid;
|
||||
}
|
||||
@@ -122,9 +124,9 @@ async function ignoreOrWatch(fn, socket, data) {
|
||||
// filter to subcategories of cid
|
||||
let cat;
|
||||
do {
|
||||
cat = categoryData.find(c => !cids.includes(c.cid) && cids.includes(c.parentCid));
|
||||
cat = categoryData.find(c => !cids.includes(String(c.cid)) && cids.includes(String(c.parentCid)));
|
||||
if (cat) {
|
||||
cids.push(cat.cid);
|
||||
cids.push(String(cat.cid));
|
||||
}
|
||||
} while (cat);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user