mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-06-22 19:32:02 +02:00
feat: #7743 groups/cover,create,data
This commit is contained in:
@@ -1,80 +1,60 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const path = require('path');
|
||||||
var path = require('path');
|
|
||||||
|
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var image = require('../image');
|
const image = require('../image');
|
||||||
var file = require('../file');
|
const file = require('../file');
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.updateCoverPosition = function (groupName, position, callback) {
|
Groups.updateCoverPosition = async function (groupName, position) {
|
||||||
if (!groupName) {
|
if (!groupName) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
Groups.setGroupField(groupName, 'cover:position', position, callback);
|
await Groups.setGroupField(groupName, 'cover:position', position);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.updateCover = function (uid, data, callback) {
|
Groups.updateCover = async function (uid, data) {
|
||||||
// Position only? That's fine
|
let tempPath = data.file ? data.file : '';
|
||||||
if (!data.imageData && !data.file && data.position) {
|
try {
|
||||||
return Groups.updateCoverPosition(data.groupName, data.position, callback);
|
// Position only? That's fine
|
||||||
}
|
if (!data.imageData && !data.file && data.position) {
|
||||||
|
return await Groups.updateCoverPosition(data.groupName, data.position);
|
||||||
|
}
|
||||||
|
if (!tempPath) {
|
||||||
|
tempPath = await image.writeImageDataToTempFile(data.imageData);
|
||||||
|
}
|
||||||
|
const filename = 'groupCover-' + data.groupName + path.extname(tempPath);
|
||||||
|
const uploadData = await image.uploadImage(filename, 'files', {
|
||||||
|
path: tempPath,
|
||||||
|
uid: uid,
|
||||||
|
name: 'groupCover',
|
||||||
|
});
|
||||||
|
const url = uploadData.url;
|
||||||
|
await Groups.setGroupField(data.groupName, 'cover:url', url);
|
||||||
|
|
||||||
var tempPath = data.file ? data.file : '';
|
await image.resizeImage({
|
||||||
var url;
|
path: tempPath,
|
||||||
|
width: 358,
|
||||||
|
});
|
||||||
|
const thumbUploadData = await image.uploadImage('groupCoverThumb-' + data.groupName + path.extname(tempPath), 'files', {
|
||||||
|
path: tempPath,
|
||||||
|
uid: uid,
|
||||||
|
name: 'groupCover',
|
||||||
|
});
|
||||||
|
await Groups.setGroupField(data.groupName, 'cover:thumb:url', thumbUploadData.url);
|
||||||
|
|
||||||
async.waterfall([
|
if (data.position) {
|
||||||
function (next) {
|
await Groups.updateCoverPosition(data.groupName, data.position);
|
||||||
if (tempPath) {
|
}
|
||||||
return next(null, tempPath);
|
|
||||||
}
|
|
||||||
image.writeImageDataToTempFile(data.imageData, next);
|
|
||||||
},
|
|
||||||
function (_tempPath, next) {
|
|
||||||
tempPath = _tempPath;
|
|
||||||
|
|
||||||
const filename = 'groupCover-' + data.groupName + path.extname(tempPath);
|
return { url: url };
|
||||||
image.uploadImage(filename, 'files', {
|
} finally {
|
||||||
path: tempPath,
|
|
||||||
uid: uid,
|
|
||||||
name: 'groupCover',
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (uploadData, next) {
|
|
||||||
url = uploadData.url;
|
|
||||||
Groups.setGroupField(data.groupName, 'cover:url', url, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
image.resizeImage({
|
|
||||||
path: tempPath,
|
|
||||||
width: 358,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
image.uploadImage('groupCoverThumb-' + data.groupName + path.extname(tempPath), 'files', {
|
|
||||||
path: tempPath,
|
|
||||||
uid: uid,
|
|
||||||
name: 'groupCover',
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (uploadData, next) {
|
|
||||||
Groups.setGroupField(data.groupName, 'cover:thumb:url', uploadData.url, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
if (data.position) {
|
|
||||||
Groups.updateCoverPosition(data.groupName, data.position, next);
|
|
||||||
} else {
|
|
||||||
next(null);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
file.delete(tempPath);
|
file.delete(tempPath);
|
||||||
callback(err, { url: url });
|
}
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.removeCover = function (data, callback) {
|
Groups.removeCover = async function (data) {
|
||||||
db.deleteObjectFields('group:' + data.groupName, ['cover:url', 'cover:thumb:url', 'cover:position'], callback);
|
await db.deleteObjectFields('group:' + data.groupName, ['cover:url', 'cover:thumb:url', 'cover:position']);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,79 +1,67 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const meta = require('../meta');
|
||||||
var meta = require('../meta');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
const utils = require('../utils');
|
||||||
var utils = require('../utils');
|
const db = require('../database');
|
||||||
var db = require('../database');
|
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.create = function (data, callback) {
|
Groups.create = async function (data) {
|
||||||
var isSystem = isSystemGroup(data);
|
const isSystem = isSystemGroup(data);
|
||||||
var groupData;
|
const timestamp = data.timestamp || Date.now();
|
||||||
var timestamp = data.timestamp || Date.now();
|
let disableJoinRequests = parseInt(data.disableJoinRequests, 10) === 1 ? 1 : 0;
|
||||||
var disableJoinRequests = parseInt(data.disableJoinRequests, 10) === 1 ? 1 : 0;
|
|
||||||
if (data.name === 'administrators') {
|
if (data.name === 'administrators') {
|
||||||
disableJoinRequests = 1;
|
disableJoinRequests = 1;
|
||||||
}
|
}
|
||||||
var isHidden = parseInt(data.hidden, 10) === 1;
|
const isHidden = parseInt(data.hidden, 10) === 1;
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
validateGroupName(data.name, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
meta.userOrGroupExists(data.name, next);
|
|
||||||
},
|
|
||||||
function (exists, next) {
|
|
||||||
if (exists) {
|
|
||||||
return next(new Error('[[error:group-already-exists]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var memberCount = data.hasOwnProperty('ownerUid') ? 1 : 0;
|
validateGroupName(data.name);
|
||||||
var isPrivate = data.hasOwnProperty('private') ? parseInt(data.private, 10) : 1;
|
|
||||||
var slug = utils.slugify(data.name);
|
|
||||||
groupData = {
|
|
||||||
name: data.name,
|
|
||||||
slug: slug,
|
|
||||||
createtime: timestamp,
|
|
||||||
userTitle: data.userTitle || data.name,
|
|
||||||
userTitleEnabled: parseInt(data.userTitleEnabled, 10) === 1 ? 1 : 0,
|
|
||||||
description: data.description || '',
|
|
||||||
memberCount: memberCount,
|
|
||||||
hidden: isHidden ? 1 : 0,
|
|
||||||
system: isSystem ? 1 : 0,
|
|
||||||
private: isPrivate,
|
|
||||||
disableJoinRequests: disableJoinRequests,
|
|
||||||
};
|
|
||||||
plugins.fireHook('filter:group.create', { group: groupData, data: data }, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
var tasks = [
|
|
||||||
async.apply(db.sortedSetAdd, 'groups:createtime', groupData.createtime, groupData.name),
|
|
||||||
async.apply(db.setObject, 'group:' + groupData.name, groupData),
|
|
||||||
];
|
|
||||||
|
|
||||||
if (data.hasOwnProperty('ownerUid')) {
|
const exists = await meta.userOrGroupExists(data.name);
|
||||||
tasks.push(async.apply(db.setAdd, 'group:' + groupData.name + ':owners', data.ownerUid));
|
if (exists) {
|
||||||
tasks.push(async.apply(db.sortedSetAdd, 'group:' + groupData.name + ':members', timestamp, data.ownerUid));
|
throw new Error('[[error:group-already-exists]]');
|
||||||
|
}
|
||||||
|
|
||||||
groupData.ownerUid = data.ownerUid;
|
const memberCount = data.hasOwnProperty('ownerUid') ? 1 : 0;
|
||||||
}
|
const isPrivate = data.hasOwnProperty('private') ? parseInt(data.private, 10) : 1;
|
||||||
|
const groupData = {
|
||||||
|
name: data.name,
|
||||||
|
slug: utils.slugify(data.name),
|
||||||
|
createtime: timestamp,
|
||||||
|
userTitle: data.userTitle || data.name,
|
||||||
|
userTitleEnabled: parseInt(data.userTitleEnabled, 10) === 1 ? 1 : 0,
|
||||||
|
description: data.description || '',
|
||||||
|
memberCount: memberCount,
|
||||||
|
hidden: isHidden ? 1 : 0,
|
||||||
|
system: isSystem ? 1 : 0,
|
||||||
|
private: isPrivate,
|
||||||
|
disableJoinRequests: disableJoinRequests,
|
||||||
|
};
|
||||||
|
|
||||||
if (!isHidden && !isSystem) {
|
plugins.fireHook('filter:group.create', { group: groupData, data: data });
|
||||||
tasks.push(async.apply(db.sortedSetAdd, 'groups:visible:createtime', timestamp, groupData.name));
|
|
||||||
tasks.push(async.apply(db.sortedSetAdd, 'groups:visible:memberCount', groupData.memberCount, groupData.name));
|
|
||||||
tasks.push(async.apply(db.sortedSetAdd, 'groups:visible:name', 0, groupData.name.toLowerCase() + ':' + groupData.name));
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.push(async.apply(db.setObjectField, 'groupslug:groupname', groupData.slug, groupData.name));
|
await db.sortedSetAdd('groups:createtime', groupData.createtime, groupData.name);
|
||||||
|
await db.setObject('group:' + groupData.name, groupData);
|
||||||
|
|
||||||
async.series(tasks, next);
|
if (data.hasOwnProperty('ownerUid')) {
|
||||||
},
|
await db.setAdd('group:' + groupData.name + ':owners', data.ownerUid);
|
||||||
function (results, next) {
|
await db.sortedSetAdd('group:' + groupData.name + ':members', timestamp, data.ownerUid);
|
||||||
plugins.fireHook('action:group.create', { group: groupData });
|
|
||||||
next(null, groupData);
|
groupData.ownerUid = data.ownerUid;
|
||||||
},
|
}
|
||||||
], callback);
|
|
||||||
|
if (!isHidden && !isSystem) {
|
||||||
|
await db.sortedSetAddBulk([
|
||||||
|
['groups:visible:createtime', timestamp, groupData.name],
|
||||||
|
['groups:visible:memberCount', groupData.memberCount, groupData.name],
|
||||||
|
['groups:visible:name', 0, groupData.name.toLowerCase() + ':' + groupData.name],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.setObjectField('groupslug:groupname', groupData.slug, groupData.name);
|
||||||
|
|
||||||
|
plugins.fireHook('action:group.create', { group: groupData });
|
||||||
|
return groupData;
|
||||||
};
|
};
|
||||||
|
|
||||||
function isSystemGroup(data) {
|
function isSystemGroup(data) {
|
||||||
@@ -82,23 +70,21 @@ module.exports = function (Groups) {
|
|||||||
Groups.isPrivilegeGroup(data.name);
|
Groups.isPrivilegeGroup(data.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateGroupName(name, callback) {
|
function validateGroupName(name) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return callback(new Error('[[error:group-name-too-short]]'));
|
throw new Error('[[error:group-name-too-short]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Groups.isPrivilegeGroup(name) && name.length > meta.config.maximumGroupNameLength) {
|
if (!Groups.isPrivilegeGroup(name) && name.length > meta.config.maximumGroupNameLength) {
|
||||||
return callback(new Error('[[error:group-name-too-long]]'));
|
throw new Error('[[error:group-name-too-long]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name === 'guests' || (!Groups.isPrivilegeGroup(name) && name.includes(':'))) {
|
if (name === 'guests' || (!Groups.isPrivilegeGroup(name) && name.includes(':'))) {
|
||||||
return callback(new Error('[[error:invalid-group-name]]'));
|
throw new Error('[[error:invalid-group-name]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.includes('/') || !utils.slugify(name)) {
|
if (name.includes('/') || !utils.slugify(name)) {
|
||||||
return callback(new Error('[[error:invalid-group-name]]'));
|
throw new Error('[[error:invalid-group-name]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
callback();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const validator = require('validator');
|
||||||
var validator = require('validator');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
|
||||||
|
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var utils = require('../utils');
|
const utils = require('../utils');
|
||||||
|
|
||||||
const intFields = [
|
const intFields = [
|
||||||
'createtime', 'memberCount', 'hidden', 'system', 'private',
|
'createtime', 'memberCount', 'hidden', 'system', 'private',
|
||||||
@@ -14,70 +13,54 @@ const intFields = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
module.exports = function (Groups) {
|
module.exports = function (Groups) {
|
||||||
Groups.getGroupsFields = function (groupNames, fields, callback) {
|
Groups.getGroupsFields = async function (groupNames, fields) {
|
||||||
if (!Array.isArray(groupNames) || !groupNames.length) {
|
if (!Array.isArray(groupNames) || !groupNames.length) {
|
||||||
return callback(null, []);
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var ephemeralIdx = groupNames.reduce(function (memo, cur, idx) {
|
const ephemeralIdx = groupNames.reduce(function (memo, cur, idx) {
|
||||||
if (Groups.ephemeralGroups.includes(cur)) {
|
if (Groups.ephemeralGroups.includes(cur)) {
|
||||||
memo.push(idx);
|
memo.push(idx);
|
||||||
}
|
}
|
||||||
return memo;
|
return memo;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
async.waterfall([
|
let groupData;
|
||||||
function (next) {
|
const keys = groupNames.map(groupName => 'group:' + groupName);
|
||||||
const keys = groupNames.map(groupName => 'group:' + groupName);
|
if (fields.length) {
|
||||||
if (fields.length) {
|
groupData = await db.getObjectsFields(keys, fields);
|
||||||
db.getObjectsFields(keys, fields, next);
|
} else {
|
||||||
} else {
|
groupData = await db.getObjects(keys);
|
||||||
db.getObjects(keys, next);
|
}
|
||||||
}
|
if (ephemeralIdx.length) {
|
||||||
},
|
ephemeralIdx.forEach(function (idx) {
|
||||||
function (groupData, next) {
|
groupData[idx] = Groups.getEphemeralGroup(groupNames[idx]);
|
||||||
if (ephemeralIdx.length) {
|
});
|
||||||
ephemeralIdx.forEach(function (idx) {
|
}
|
||||||
groupData[idx] = Groups.getEphemeralGroup(groupNames[idx]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
groupData.forEach(group => modifyGroup(group, fields));
|
groupData.forEach(group => modifyGroup(group, fields));
|
||||||
|
|
||||||
plugins.fireHook('filter:groups.get', { groups: groupData }, next);
|
const results = await plugins.fireHook('filter:groups.get', { groups: groupData });
|
||||||
},
|
return results.groups;
|
||||||
function (results, next) {
|
|
||||||
next(null, results.groups);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getGroupsData = function (groupNames, callback) {
|
Groups.getGroupsData = async function (groupNames) {
|
||||||
Groups.getGroupsFields(groupNames, [], callback);
|
return await Groups.getGroupsFields(groupNames, []);
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getGroupData = function (groupName, callback) {
|
Groups.getGroupData = async function (groupName) {
|
||||||
Groups.getGroupsData([groupName], function (err, groupsData) {
|
const groupsData = await Groups.getGroupsData([groupName]);
|
||||||
callback(err, Array.isArray(groupsData) && groupsData[0] ? groupsData[0] : null);
|
return Array.isArray(groupsData) && groupsData[0] ? groupsData[0] : null;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.getGroupFields = function (groupName, fields, callback) {
|
Groups.getGroupFields = async function (groupName, fields) {
|
||||||
Groups.getGroupsFields([groupName], fields, function (err, groups) {
|
const groups = await Groups.getGroupsFields([groupName], fields);
|
||||||
callback(err, groups ? groups[0] : null);
|
return groups ? groups[0] : null;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Groups.setGroupField = function (groupName, field, value, callback) {
|
Groups.setGroupField = async function (groupName, field, value) {
|
||||||
async.waterfall([
|
await db.setObjectField('group:' + groupName, field, value);
|
||||||
function (next) {
|
plugins.fireHook('action:group.set', { field: field, value: value, type: 'set' });
|
||||||
db.setObjectField('group:' + groupName, field, value, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('action:group.set', { field: field, value: value, type: 'set' });
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user