settings: move branding settings into branding.js
This commit is contained in:
+14
-36
@@ -1,21 +1,23 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
get,
|
||||
set,
|
||||
|
||||
getCloudronAvatar
|
||||
getCloudronName,
|
||||
setCloudronName,
|
||||
getCloudronAvatar,
|
||||
setCloudronAvatar,
|
||||
getFooter,
|
||||
setFooter,
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
branding = require('../branding.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
safe = require('safetydance'),
|
||||
settings = require('../settings.js');
|
||||
safe = require('safetydance');
|
||||
|
||||
async function getFooter(req, res, next) {
|
||||
const [error, footer] = await safe(settings.getFooter());
|
||||
const [error, footer] = await safe(branding.getFooter());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { footer }));
|
||||
@@ -26,7 +28,7 @@ async function setFooter(req, res, next) {
|
||||
|
||||
if (typeof req.body.footer !== 'string') return next(new HttpError(400, 'footer is required'));
|
||||
|
||||
const [error] = await safe(settings.setFooter(req.body.footer));
|
||||
const [error] = await safe(branding.setFooter(req.body.footer));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
@@ -37,14 +39,14 @@ async function setCloudronName(req, res, next) {
|
||||
|
||||
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
|
||||
|
||||
const [error] = await safe(settings.setCloudronName(req.body.name));
|
||||
const [error] = await safe(branding.setCloudronName(req.body.name));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function getCloudronName(req, res, next) {
|
||||
const [error, name] = await safe(settings.getCloudronName());
|
||||
const [error, name] = await safe(branding.getCloudronName());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { name }));
|
||||
@@ -57,14 +59,14 @@ async function setCloudronAvatar(req, res, next) {
|
||||
const avatar = safe.fs.readFileSync(req.files.avatar.path);
|
||||
if (!avatar) return next(500, safe.error.message);
|
||||
|
||||
const [error] = await safe(settings.setCloudronAvatar(avatar));
|
||||
const [error] = await safe(branding.setCloudronAvatar(avatar));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function getCloudronAvatar(req, res, next) {
|
||||
const [error, avatar] = await safe(settings.getCloudronAvatar());
|
||||
const [error, avatar] = await safe(branding.getCloudronAvatar());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
// avoid caching the avatar on the client to see avatar changes immediately
|
||||
@@ -73,27 +75,3 @@ async function getCloudronAvatar(req, res, next) {
|
||||
res.set('Content-Type', 'image/png');
|
||||
res.status(200).send(avatar);
|
||||
}
|
||||
|
||||
async function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.setting, 'string');
|
||||
|
||||
switch (req.params.setting) {
|
||||
case settings.CLOUDRON_AVATAR_KEY: return await getCloudronAvatar(req, res, next);
|
||||
case settings.CLOUDRON_NAME_KEY: return await getCloudronName(req, res, next);
|
||||
case settings.FOOTER_KEY: return await getFooter(req, res, next);
|
||||
|
||||
default: return next(new HttpError(404, 'No such setting'));
|
||||
}
|
||||
}
|
||||
|
||||
async function set(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
switch (req.params.setting) {
|
||||
case settings.CLOUDRON_AVATAR_KEY: return await setCloudronAvatar(req, res, next);
|
||||
case settings.CLOUDRON_NAME_KEY: return await setCloudronName(req, res, next);
|
||||
case settings.FOOTER_KEY: return await setFooter(req, res, next);
|
||||
|
||||
default: return next(new HttpError(404, 'No such branding'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('Branding API', function () {
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.body.name).to.be.ok();
|
||||
expect(response.body.name).to.be('Cloudron');
|
||||
});
|
||||
|
||||
it('cannot set without name', async function () {
|
||||
@@ -49,7 +49,7 @@ describe('Branding API', function () {
|
||||
it('set succeeds', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/branding/cloudron_name`)
|
||||
.query({ access_token: owner.token })
|
||||
.send({ name: name });
|
||||
.send({ name });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user