Files
cloudron-box/src/routes/support.js

87 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-12-19 10:54:33 -08:00
'use strict';
exports = module.exports = {
2021-05-01 11:21:09 -07:00
createTicket,
2021-05-01 11:21:09 -07:00
getRemoteSupport,
enableRemoteSupport,
2020-02-05 14:30:56 -08:00
2021-05-01 11:21:09 -07:00
canCreateTicket,
canEnableRemoteSupport
2018-12-19 10:54:33 -08:00
};
var appstore = require('../appstore.js'),
assert = require('assert'),
2019-12-16 14:06:55 -08:00
auditSource = require('../auditsource.js'),
2020-02-04 13:07:26 -08:00
constants = require('../constants.js'),
2018-12-19 10:54:33 -08:00
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2020-02-05 14:30:56 -08:00
settings = require('../settings.js'),
support = require('../support.js'),
2018-12-19 10:54:33 -08:00
_ = require('underscore');
2020-02-05 14:30:56 -08:00
function canCreateTicket(req, res, next) {
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(new HttpError(503, error.message));
if (!supportConfig.submitTickets) return next(new HttpError(405, 'feature disabled by admin'));
next();
});
}
2019-05-07 11:36:08 -07:00
function createTicket(req, res, next) {
2018-12-19 10:54:33 -08:00
assert.strictEqual(typeof req.user, 'object');
2019-10-15 11:48:20 -07:00
const VALID_TYPES = [ 'feedback', 'ticket', 'app_missing', 'app_error', 'upgrade_request', 'email_error' ];
2018-12-19 10:54:33 -08:00
if (typeof req.body.type !== 'string' || !req.body.type) return next(new HttpError(400, 'type must be string'));
if (VALID_TYPES.indexOf(req.body.type) === -1) return next(new HttpError(400, 'unknown type'));
if (typeof req.body.subject !== 'string' || !req.body.subject) return next(new HttpError(400, 'subject must be string'));
if (typeof req.body.description !== 'string' || !req.body.description) return next(new HttpError(400, 'description must be string'));
if (req.body.appId && typeof req.body.appId !== 'string') return next(new HttpError(400, 'appId must be string'));
if (req.body.altEmail && typeof req.body.altEmail !== 'string') return next(new HttpError(400, 'altEmail must be string'));
if (req.body.enableSshSupport && typeof req.body.enableSshSupport !== 'boolean') return next(new HttpError(400, 'enableSshSupport must be a boolean'));
2018-12-19 10:54:33 -08:00
2020-02-05 14:30:56 -08:00
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(new HttpError(503, `Error getting support config: ${error.message}`));
if (supportConfig.email !== constants.SUPPORT_EMAIL) return next(new HttpError(503, 'Sending to non-cloudron email not implemented yet'));
appstore.createTicket(_.extend({ }, req.body, { email: req.user.email, displayName: req.user.displayName }), auditSource.fromRequest(req), function (error, result) {
if (error) return next(new HttpError(503, `Error contacting cloudron.io: ${error.message}. Please email ${constants.SUPPORT_EMAIL}`));
next(new HttpSuccess(201, result));
});
});
}
function canEnableRemoteSupport(req, res, next) {
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(new HttpError(503, error.message));
if (!supportConfig.remoteSupport) return next(new HttpError(405, 'feature disabled by admin'));
2018-12-19 10:54:33 -08:00
2020-02-05 14:30:56 -08:00
next();
2018-12-19 10:54:33 -08:00
});
}
function enableRemoteSupport(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enable !== 'boolean') return next(new HttpError(400, 'enabled is required'));
2019-12-16 14:06:55 -08:00
support.enableRemoteSupport(req.body.enable, auditSource.fromRequest(req), function (error) {
if (error) return next(new HttpError(503, 'Error enabling remote support. Try running "cloudron-support --enable-ssh" on the server'));
next(new HttpSuccess(202, {}));
});
}
function getRemoteSupport(req, res, next) {
support.getRemoteSupport(function (error, status) {
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, status));
});
}