appstore and support: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-18 15:54:53 -07:00
parent 200018a022
commit 03e22170da
16 changed files with 458 additions and 633 deletions

View File

@@ -10,50 +10,47 @@ exports = module.exports = {
getSubscription
};
var appstore = require('../appstore.js'),
const appstore = require('../appstore.js'),
assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess;
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
function getApps(req, res, next) {
appstore.getApps(function (error, apps) {
if (error) return next(BoxError.toHttpError(error));
async function getApps(req, res, next) {
const [error, apps] = await safe(appstore.getApps());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { apps }));
});
next(new HttpSuccess(200, { apps }));
}
function getApp(req, res, next) {
async function getApp(req, res, next) {
assert.strictEqual(typeof req.params.appstoreId, 'string');
appstore.getApp(req.params.appstoreId, function (error, app) {
if (error) return next(BoxError.toHttpError(error));
const [error, app] = await safe(appstore.getApp(req.params.appstoreId));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, app));
});
next(new HttpSuccess(200, app));
}
function getAppVersion(req, res, next) {
async function getAppVersion(req, res, next) {
assert.strictEqual(typeof req.params.appstoreId, 'string');
assert.strictEqual(typeof req.params.versionId, 'string');
appstore.getAppVersion(req.params.appstoreId, req.params.versionId, function (error, manifest) {
if (error) return next(BoxError.toHttpError(error));
const [error, manifest] = await safe(appstore.getAppVersion(req.params.appstoreId, req.params.versionId));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, manifest));
});
next(new HttpSuccess(200, manifest));
}
function createUserToken(req, res, next) {
appstore.getUserToken(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
async function createUserToken(req, res, next) {
const [error, accessToken] = await safe(appstore.createUserToken());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, { accessToken: result }));
});
next(new HttpSuccess(201, { accessToken }));
}
function registerCloudron(req, res, next) {
async function registerCloudron(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.email !== 'string' || !req.body.email) return next(new HttpError(400, 'email must be string'));
@@ -61,19 +58,17 @@ function registerCloudron(req, res, next) {
if ('totpToken' in req.body && typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be string'));
if (typeof req.body.signup !== 'boolean') return next(new HttpError(400, 'signup must be a boolean'));
appstore.registerWithLoginCredentials(req.body, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(appstore.registerWithLoginCredentials(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
}
function getSubscription(req, res, next) {
async function getSubscription(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
appstore.getSubscription(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(appstore.getSubscription());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, result)); // { email, cloudronId, cloudronCreatedAt, plan, current_period_end, canceled_at, cancel_at, status, features }
});
next(new HttpSuccess(200, result)); // { email, cloudronId, cloudronCreatedAt, plan, current_period_end, canceled_at, cancel_at, status, features }
}

View File

@@ -55,7 +55,7 @@ function getCloudronName(req, res, next) {
});
}
function setAppstoreListingConfig(req, res, next) {
async function setAppstoreListingConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
const listingConfig = _.pick(req.body, 'whitelist', 'blacklist');
@@ -73,19 +73,17 @@ function setAppstoreListingConfig(req, res, next) {
if (!listingConfig.blacklist.every(id => typeof id === 'string')) return next(new HttpError(400, 'blacklist must be array of strings'));
}
settings.setAppstoreListingConfig(listingConfig, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(settings.setAppstoreListingConfig(listingConfig));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
next(new HttpSuccess(202, {}));
}
function getAppstoreListingConfig(req, res, next) {
settings.getAppstoreListingConfig(function (error, listingConfig) {
if (error) return next(BoxError.toHttpError(error));
async function getAppstoreListingConfig(req, res, next) {
const [error, listingConfig] = await safe(settings.getAppstoreListingConfig());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, listingConfig));
});
next(new HttpSuccess(200, listingConfig));
}
async function setCloudronAvatar(req, res, next) {

View File

@@ -196,13 +196,12 @@ function getUpdateInfo(req, res, next) {
next(new HttpSuccess(200, { update: updateChecker.getUpdateInfo() }));
}
function checkForUpdates(req, res, next) {
async function checkForUpdates(req, res, next) {
// it can take a while sometimes to get all the app updates one by one
req.clearTimeout();
updateChecker.checkForUpdates({ automatic: false }, function () {
next(new HttpSuccess(200, { update: updateChecker.getUpdateInfo() }));
});
await updateChecker.checkForUpdates({ automatic: false });
next(new HttpSuccess(200, { update: updateChecker.getUpdateInfo() }));
}
function getLogs(req, res, next) {

View File

@@ -26,7 +26,7 @@ const assert = require('assert'),
async function authorize(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const [error, directoryConfig] = await settings.getDirectoryConfig();
const [error, directoryConfig] = await safe(settings.getDirectoryConfig());
if (error) return next(BoxError.toHttpError(error));
if (directoryConfig.lockUserProfiles) return next(new HttpError(403, 'admin has disallowed users from editing profiles'));

View File

@@ -58,12 +58,11 @@ function setTimeZone(req, res, next) {
});
}
function getSupportConfig(req, res, next) {
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(BoxError.toHttpError(error));
async function getSupportConfig(req, res, next) {
const [error, supportConfig] = await settings.getSupportConfig();
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, supportConfig));
});
next(new HttpSuccess(200, supportConfig));
}
function getBackupConfig(req, res, next) {

View File

@@ -10,27 +10,27 @@ exports = module.exports = {
canEnableRemoteSupport
};
var appstore = require('../appstore.js'),
const appstore = require('../appstore.js'),
assert = require('assert'),
auditSource = require('../auditsource.js'),
constants = require('../constants.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance'),
settings = require('../settings.js'),
support = require('../support.js'),
_ = require('underscore');
function canCreateTicket(req, res, next) {
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(new HttpError(503, error.message));
async function canCreateTicket(req, res, next) {
const [error, supportConfig] = await safe(settings.getSupportConfig());
if (error) return next(new HttpError(503, error.message));
if (!supportConfig.submitTickets) return next(new HttpError(405, 'feature disabled by admin'));
if (!supportConfig.submitTickets) return next(new HttpError(405, 'feature disabled by admin'));
next();
});
next();
}
function createTicket(req, res, next) {
async function createTicket(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
const VALID_TYPES = [ 'feedback', 'ticket', 'app_missing', 'app_error', 'upgrade_request', 'email_error' ];
@@ -43,44 +43,39 @@ function createTicket(req, res, next) {
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'));
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'));
const [error, supportConfig] = await safe(settings.getSupportConfig());
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}`));
const [ticketError, result] = await safe(appstore.createTicket(_.extend({ }, req.body, { email: req.user.email, displayName: req.user.displayName }), auditSource.fromRequest(req)));
if (ticketError) return next(new HttpError(503, `Error contacting cloudron.io: ${error.message}. Please email ${constants.SUPPORT_EMAIL}`));
next(new HttpSuccess(201, result));
});
});
next(new HttpSuccess(201, result));
}
function canEnableRemoteSupport(req, res, next) {
settings.getSupportConfig(function (error, supportConfig) {
if (error) return next(new HttpError(503, error.message));
async function canEnableRemoteSupport(req, res, next) {
const [error, supportConfig] = await safe(settings.getSupportConfig());
if (error) return next(new HttpError(503, error.message));
if (!supportConfig.remoteSupport) return next(new HttpError(405, 'feature disabled by admin'));
if (!supportConfig.remoteSupport) return next(new HttpError(405, 'feature disabled by admin'));
next();
});
next();
}
function enableRemoteSupport(req, res, next) {
async 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'));
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'));
const [error] = await safe(support.enableRemoteSupport(req.body.enable, auditSource.fromRequest(req)));
if (error) return next(new HttpError(503, 'Error enabling remote support. Try running "cloudron-support --enable-ssh" on the server'));
next(new HttpSuccess(202, {}));
});
next(new HttpSuccess(202, {}));
}
function getRemoteSupport(req, res, next) {
support.getRemoteSupport(function (error, status) {
if (error) return next(new HttpError(500, error));
async function getRemoteSupport(req, res, next) {
const [error, enabled] = await safe(support.getRemoteSupport());
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, status));
});
next(new HttpSuccess(200, { enabled }));
}