Files
NodeBB/src/groups/data.js

109 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-03-01 22:42:10 +03:00
'use strict';
2019-07-18 13:01:43 -04:00
const validator = require('validator');
const nconf = require('nconf');
2017-03-01 22:42:10 +03:00
2019-07-18 13:01:43 -04:00
const db = require('../database');
const plugins = require('../plugins');
const utils = require('../utils');
2020-11-18 23:24:51 -05:00
const translator = require('../translator');
2017-03-01 22:42:10 +03:00
2018-10-23 08:28:59 -04:00
const intFields = [
'createtime', 'memberCount', 'hidden', 'system', 'private',
'userTitleEnabled', 'disableJoinRequests', 'disableLeave',
2018-10-23 08:28:59 -04:00
];
2017-03-01 22:42:10 +03:00
module.exports = function (Groups) {
2019-07-18 13:01:43 -04:00
Groups.getGroupsFields = async function (groupNames, fields) {
2017-03-01 22:42:10 +03:00
if (!Array.isArray(groupNames) || !groupNames.length) {
2019-07-18 13:01:43 -04:00
return [];
2017-03-01 22:42:10 +03:00
}
2021-02-04 00:01:39 -07:00
const ephemeralIdx = groupNames.reduce((memo, cur, idx) => {
if (Groups.ephemeralGroups.includes(cur)) {
2017-03-01 22:42:10 +03:00
memo.push(idx);
}
return memo;
}, []);
2021-02-03 23:59:08 -07:00
const keys = groupNames.map(groupName => `group:${groupName}`);
2020-06-19 12:03:33 -04:00
const groupData = await (fields.length ? db.getObjectsFields(keys, fields) : db.getObjects(keys));
2019-07-18 13:01:43 -04:00
if (ephemeralIdx.length) {
2021-02-04 00:01:39 -07:00
ephemeralIdx.forEach((idx) => {
2019-07-18 13:01:43 -04:00
groupData[idx] = Groups.getEphemeralGroup(groupNames[idx]);
});
}
2017-03-01 22:42:10 +03:00
2019-07-18 13:01:43 -04:00
groupData.forEach(group => modifyGroup(group, fields));
2017-03-01 22:42:10 +03:00
const results = await plugins.hooks.fire('filter:groups.get', { groups: groupData });
2019-07-18 13:01:43 -04:00
return results.groups;
2017-03-01 22:42:10 +03:00
};
2019-07-18 13:01:43 -04:00
Groups.getGroupsData = async function (groupNames) {
return await Groups.getGroupsFields(groupNames, []);
2018-10-23 08:28:59 -04:00
};
2019-07-18 13:01:43 -04:00
Groups.getGroupData = async function (groupName) {
const groupsData = await Groups.getGroupsData([groupName]);
return Array.isArray(groupsData) && groupsData[0] ? groupsData[0] : null;
2020-01-31 22:56:55 -05:00
};
Groups.getGroupField = async function (groupName, field) {
const groupData = await Groups.getGroupFields(groupName, [field]);
return groupData ? groupData[field] : null;
2017-03-01 22:42:10 +03:00
};
2019-07-18 13:01:43 -04:00
Groups.getGroupFields = async function (groupName, fields) {
const groups = await Groups.getGroupsFields([groupName], fields);
return groups ? groups[0] : null;
2017-03-01 22:42:10 +03:00
};
2019-07-18 13:01:43 -04:00
Groups.setGroupField = async function (groupName, field, value) {
2021-02-03 23:59:08 -07:00
await db.setObjectField(`group:${groupName}`, field, value);
plugins.hooks.fire('action:group.set', { field: field, value: value, type: 'set' });
2017-03-01 22:42:10 +03:00
};
};
2018-10-23 08:28:59 -04:00
2018-10-25 19:58:01 -04:00
function modifyGroup(group, fields) {
2018-10-23 08:28:59 -04:00
if (group) {
2018-10-25 19:58:01 -04:00
db.parseIntFields(group, intFields, fields);
2018-10-23 08:28:59 -04:00
escapeGroupData(group);
group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled;
group.labelColor = validator.escape(String(group.labelColor || '#000000'));
2019-03-09 12:35:36 -05:00
group.textColor = validator.escape(String(group.textColor || '#ffffff'));
2018-10-23 08:28:59 -04:00
group.icon = validator.escape(String(group.icon || ''));
group.createtimeISO = utils.toISOString(group.createtime);
group.private = ([null, undefined].includes(group.private)) ? 1 : group.private;
Categories refactor (#9257) * 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
2021-02-07 15:09:52 -05:00
group.memberPostCids = group.memberPostCids || '';
group.memberPostCidsArray = group.memberPostCids.split(',').map(cid => parseInt(cid, 10)).filter(Boolean);
2018-10-23 08:28:59 -04:00
group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url'];
if (group['cover:url']) {
group['cover:url'] = group['cover:url'].startsWith('http') ? group['cover:url'] : (nconf.get('relative_path') + group['cover:url']);
} else {
group['cover:url'] = require('../coverPhoto').getDefaultGroupCover(group.name);
}
if (group['cover:thumb:url']) {
group['cover:thumb:url'] = group['cover:thumb:url'].startsWith('http') ? group['cover:thumb:url'] : (nconf.get('relative_path') + group['cover:thumb:url']);
} else {
group['cover:thumb:url'] = require('../coverPhoto').getDefaultGroupCover(group.name);
}
2018-10-23 08:28:59 -04:00
group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%'));
}
}
function escapeGroupData(group) {
if (group) {
group.nameEncoded = encodeURIComponent(group.name);
group.displayName = validator.escape(String(group.name));
group.description = validator.escape(String(group.description || ''));
group.userTitle = validator.escape(String(group.userTitle || ''));
2020-11-18 23:24:51 -05:00
group.userTitleEscaped = translator.escape(group.userTitle);
2018-10-23 08:28:59 -04:00
}
}