diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 0000000000..55b4f94813 Binary files /dev/null and b/dump.rdb differ diff --git a/src/groups/data.js b/src/groups/data.js index 5ecb9081e0..d8ff309fcc 100644 --- a/src/groups/data.js +++ b/src/groups/data.js @@ -7,6 +7,7 @@ const db = require('../database'); const plugins = require('../plugins'); const utils = require('../utils'); const translator = require('../translator'); +const coverPhoto = require('../coverPhoto'); const intFields = [ 'createtime', 'memberCount', 'hidden', 'system', 'private', @@ -68,35 +69,47 @@ module.exports = function (Groups) { function modifyGroup(group, fields) { if (group) { db.parseIntFields(group, intFields, fields); - escapeGroupData(group); - group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled; - group.labelColor = validator.escape(String(group.labelColor || '#000000')); - group.textColor = validator.escape(String(group.textColor || '#ffffff')); - group.icon = validator.escape(String(group.icon || '')); - group.createtimeISO = utils.toISOString(group.createtime); - group.private = ([null, undefined].includes(group.private)) ? 1 : group.private; - group.memberPostCids = group.memberPostCids || ''; - group.memberPostCidsArray = group.memberPostCids.split(',').map(cid => parseInt(cid, 10)).filter(Boolean); - - 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); - } + setDefaultValues(group); + formatGroupFields(group); + setCoverUrls(group); + } +} +console.log('retaj : refactored code executed'); +function setDefaultValues(group) { + group.userTitleEnabled = ([null, undefined].includes(group.userTitleEnabled)) ? 1 : group.userTitleEnabled; + group.private = ([null, undefined].includes(group.private)) ? 1 : group.private; + group.labelColor = validator.escape(String(group.labelColor || '#000000')); + group.textColor = validator.escape(String(group.textColor || '#ffffff')); + group.icon = validator.escape(String(group.icon || '')); +} - 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); - } +function formatGroupFields(group) { + group.createtimeISO = utils.toISOString(group.createtime); + group.memberPostCids = group.memberPostCids || ''; + group.memberPostCidsArray = group.memberPostCids.split(',').map(cid => parseInt(cid, 10)).filter(Boolean); + group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%')); +} + +function setCoverUrls(group) { + group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url']; + if (group['cover:url']) { + group['cover:url'] = formatUrl(group['cover:url']); + } else { + group['cover:url'] = coverPhoto.getDefaultGroupCover(group.name); + } - group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%')); + if (group['cover:thumb:url']) { + group['cover:thumb:url'] = formatUrl(group['cover:thumb:url']); + } else { + group['cover:thumb:url'] = coverPhoto.getDefaultGroupCover(group.name); } } +function formatUrl(url) { + return url.startsWith('http') ? url : (nconf.get('relative_path') + url); +} + function escapeGroupData(group) { if (group) { group.nameEncoded = encodeURIComponent(group.name);