refactor: use single function for api code

This commit is contained in:
Barış Soner Uşaklı
2020-10-15 12:12:01 -04:00
parent 25e4a09816
commit 5e2caf19f5
6 changed files with 60 additions and 35 deletions

49
src/api/groups.js Normal file
View File

@@ -0,0 +1,49 @@
'use strict';
const privileges = require('../privileges');
const events = require('../events');
const groups = require('../groups');
const groupsAPI = module.exports;
groupsAPI.create = async function (caller, data) {
if (!caller.uid) {
throw new Error('[[error:no-privileges]]');
} else if (typeof data.name !== 'string' || groups.isPrivilegeGroup(data.name)) {
throw new Error('[[error:invalid-group-name]]');
}
const canCreate = await privileges.global.can('group:create', caller.uid);
if (!canCreate) {
throw new Error('[[error:no-privileges]]');
}
data.ownerUid = caller.uid;
data.system = false;
const groupData = await groups.create(data);
logGroupEvent(caller, 'group-create', {
groupName: data.name,
});
return groupData;
};
// groupsAPI.join = async function (caller, data) {
// // TODO:
// };
// groupsAPI.leave = async function (caller, data) {
// // TODO:
// };
// groupsAPI.delete = async function (caller, data) {
// // TODO:
// };
function logGroupEvent(caller, event, additional) {
events.log({
type: event,
uid: caller.uid,
ip: caller.ip,
...additional,
});
}

5
src/api/index.js Normal file
View File

@@ -0,0 +1,5 @@
'use strict';
module.exports = {
groups: require('./groups'),
};