Files
NodeBB/src/groups/data.js

105 lines
3.6 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');
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
}
2019-07-18 13:01:43 -04:00
const ephemeralIdx = groupNames.reduce(function (memo, cur, idx) {
if (Groups.ephemeralGroups.includes(cur)) {
2017-03-01 22:42:10 +03:00
memo.push(idx);
}
return memo;
}, []);
2019-07-18 13:01:43 -04: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) {
ephemeralIdx.forEach(function (idx) {
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
2019-07-18 13:01:43 -04:00
const results = await plugins.fireHook('filter:groups.get', { groups: groupData });
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) {
await db.setObjectField('group:' + groupName, field, value);
plugins.fireHook('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;
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 || ''));
2018-10-23 08:28:59 -04:00
}
}