mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-25 16:11:19 +01:00
* feat: wip categories pagination * feat: add subCategoriesPerPage setting * feat: add load more sub categories button to category page * fix: openapi spec * feat: show sub categories left on category page hide button when no more categories left * breaking: rename categories to allCategories on /search categories contains the search results * fix: spec * refactor: remove cidsPerPage * fix: tests * feat: use component for subcategories * fix: prevent negative subCategoriesLeft * feat: new category filter/search WIP * feat: remove categories from /tag * fix: dont load all categories when showing move modal * feat: allow adding custom categories to list * breaking: dont load entire category tree on post queue removed unused code add hooks to filter/selector add options to filter/selector * feat: make selector modal work again * feat: replace old search module * fix: topic move selector * feat: dont load all categories on create category modal * fix: fix more categorySelectors * feat: dont load entire category tree on group details page * feat: dont load all categories on home page and user settings page * feat: add pagination to /user/:userslug/categories * fix: update schemas * fix: more tests * fix: test * feat: flags page, dont return entire category tree * fix: flag test * feat: categories manage page dont load all categories allow changing root category clear caches properly * fix: spec * feat: admins&mods page dont load all categories * fix: spec * fix: dont load all children when opening dropdown * fix: on search results dont return all children * refactor: pass all options, rename options.cids to options.selectedCids * fix: #9266 * fix: index 0 * fix: spec * feat: #9265, add setObjectBulk * refactor: shoter updateOrder * feat: selectors on categories/category * fix: tests and search filter * fix: category update test * feat: pagination on acp categories page show order in set order modal * fix: allow drag&drop on pages > 1 in /admin/manage/categories * fix: teasers for deep nested categories fix sub category display on /category page * fix: spec * refactor: use eslint-disable-next-line * refactor: shorter
206 lines
5.2 KiB
JavaScript
206 lines
5.2 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function (module) {
|
|
const helpers = require('./helpers');
|
|
|
|
const cache = require('../cache').create('redis');
|
|
|
|
module.objectCache = cache;
|
|
|
|
module.setObject = async function (key, data) {
|
|
if (!key || !data) {
|
|
return;
|
|
}
|
|
|
|
if (data.hasOwnProperty('')) {
|
|
delete data[''];
|
|
}
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
if (data[key] === undefined || data[key] === null) {
|
|
delete data[key];
|
|
}
|
|
});
|
|
|
|
if (!Object.keys(data).length) {
|
|
return;
|
|
}
|
|
if (Array.isArray(key)) {
|
|
const batch = module.client.batch();
|
|
key.forEach(k => batch.hmset(k, data));
|
|
await helpers.execBatch(batch);
|
|
} else {
|
|
await module.client.async.hmset(key, data);
|
|
}
|
|
|
|
cache.del(key);
|
|
};
|
|
|
|
module.setObjectBulk = async function (keys, data) {
|
|
if (!keys.length || !data.length) {
|
|
return;
|
|
}
|
|
const batch = module.client.batch();
|
|
keys.forEach((k, i) => batch.hmset(k, data[i]));
|
|
await helpers.execBatch(batch);
|
|
cache.del(keys);
|
|
};
|
|
|
|
module.setObjectField = async function (key, field, value) {
|
|
if (!field) {
|
|
return;
|
|
}
|
|
if (Array.isArray(key)) {
|
|
const batch = module.client.batch();
|
|
key.forEach(k => batch.hset(k, field, value));
|
|
await helpers.execBatch(batch);
|
|
} else {
|
|
await module.client.async.hset(key, field, value);
|
|
}
|
|
|
|
cache.del(key);
|
|
};
|
|
|
|
module.getObject = async function (key) {
|
|
if (!key) {
|
|
return null;
|
|
}
|
|
|
|
const data = await module.getObjectsFields([key], []);
|
|
return data && data.length ? data[0] : null;
|
|
};
|
|
|
|
module.getObjects = async function (keys) {
|
|
return await module.getObjectsFields(keys, []);
|
|
};
|
|
|
|
module.getObjectField = async function (key, field) {
|
|
if (!key) {
|
|
return null;
|
|
}
|
|
const cachedData = {};
|
|
cache.getUnCachedKeys([key], cachedData);
|
|
if (cachedData[key]) {
|
|
return cachedData[key].hasOwnProperty(field) ? cachedData[key][field] : null;
|
|
}
|
|
return await module.client.async.hget(key, String(field));
|
|
};
|
|
|
|
module.getObjectFields = async function (key, fields) {
|
|
if (!key) {
|
|
return null;
|
|
}
|
|
const results = await module.getObjectsFields([key], fields);
|
|
return results ? results[0] : null;
|
|
};
|
|
|
|
module.getObjectsFields = async function (keys, fields) {
|
|
if (!Array.isArray(keys) || !keys.length) {
|
|
return [];
|
|
}
|
|
if (!Array.isArray(fields)) {
|
|
return keys.map(function () { return {}; });
|
|
}
|
|
const cachedData = {};
|
|
const unCachedKeys = cache.getUnCachedKeys(keys, cachedData);
|
|
|
|
let data = [];
|
|
if (unCachedKeys.length > 1) {
|
|
const batch = module.client.batch();
|
|
unCachedKeys.forEach(k => batch.hgetall(k));
|
|
data = await helpers.execBatch(batch);
|
|
} else if (unCachedKeys.length === 1) {
|
|
data = [await module.client.async.hgetall(unCachedKeys[0])];
|
|
}
|
|
|
|
unCachedKeys.forEach(function (key, i) {
|
|
cachedData[key] = data[i] || null;
|
|
cache.set(key, cachedData[key]);
|
|
});
|
|
|
|
if (!fields.length) {
|
|
return keys.map(key => (cachedData[key] ? { ...cachedData[key] } : null));
|
|
}
|
|
return keys.map(function (key) {
|
|
const item = cachedData[key] || {};
|
|
const result = {};
|
|
fields.forEach((field) => {
|
|
result[field] = item[field] !== undefined ? item[field] : null;
|
|
});
|
|
return result;
|
|
});
|
|
};
|
|
|
|
module.getObjectKeys = async function (key) {
|
|
return await module.client.async.hkeys(key);
|
|
};
|
|
|
|
module.getObjectValues = async function (key) {
|
|
return await module.client.async.hvals(key);
|
|
};
|
|
|
|
module.isObjectField = async function (key, field) {
|
|
const exists = await module.client.async.hexists(key, field);
|
|
return exists === 1;
|
|
};
|
|
|
|
module.isObjectFields = async function (key, fields) {
|
|
const batch = module.client.batch();
|
|
fields.forEach(f => batch.hexists(String(key), String(f)));
|
|
const results = await helpers.execBatch(batch);
|
|
return Array.isArray(results) ? helpers.resultsToBool(results) : null;
|
|
};
|
|
|
|
module.deleteObjectField = async function (key, field) {
|
|
if (key === undefined || key === null || field === undefined || field === null) {
|
|
return;
|
|
}
|
|
await module.client.async.hdel(key, field);
|
|
cache.del(key);
|
|
};
|
|
|
|
module.deleteObjectFields = async function (key, fields) {
|
|
if (!key || (Array.isArray(key) && !key.length) || !Array.isArray(fields) || !fields.length) {
|
|
return;
|
|
}
|
|
fields = fields.filter(Boolean);
|
|
if (!fields.length) {
|
|
return;
|
|
}
|
|
if (Array.isArray(key)) {
|
|
const batch = module.client.batch();
|
|
key.forEach(k => batch.hdel(k, fields));
|
|
await helpers.execBatch(batch);
|
|
} else {
|
|
await module.client.async.hdel(key, fields);
|
|
}
|
|
|
|
cache.del(key);
|
|
};
|
|
|
|
module.incrObjectField = async function (key, field) {
|
|
return await module.incrObjectFieldBy(key, field, 1);
|
|
};
|
|
|
|
module.decrObjectField = async function (key, field) {
|
|
return await module.incrObjectFieldBy(key, field, -1);
|
|
};
|
|
|
|
module.incrObjectFieldBy = async function (key, field, value) {
|
|
value = parseInt(value, 10);
|
|
if (!key || isNaN(value)) {
|
|
return null;
|
|
}
|
|
let result;
|
|
if (Array.isArray(key)) {
|
|
var batch = module.client.batch();
|
|
key.forEach(k => batch.hincrby(k, field, value));
|
|
result = await helpers.execBatch(batch);
|
|
} else {
|
|
result = await module.client.async.hincrby(key, field, value);
|
|
}
|
|
cache.del(key);
|
|
return Array.isArray(result) ? result.map(value => parseInt(value, 10)) : parseInt(result, 10);
|
|
};
|
|
};
|