merge maildb.js into mail.js

This commit is contained in:
Girish Ramakrishnan
2021-06-29 14:26:34 -07:00
parent ea430b255b
commit ac484a02f2
13 changed files with 638 additions and 990 deletions

View File

@@ -31,21 +31,22 @@ exports = module.exports = {
getMailboxCount
};
var assert = require('assert'),
const assert = require('assert'),
auditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
mail = require('../mail.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess;
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
function getDomain(req, res, next) {
async function getDomain(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.getDomain(req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(mail.getDomain(req.params.domain));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Mail domain not found'));
next(new HttpSuccess(200, mail.removePrivateFields(result)));
});
next(new HttpSuccess(200, mail.removePrivateFields(result)));
}
function getStatus(req, res, next) {
@@ -61,38 +62,36 @@ function getStatus(req, res, next) {
});
}
function setMailFromValidation(req, res, next) {
async function setMailFromValidation(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
mail.setMailFromValidation(req.params.domain, req.body.enabled, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setMailFromValidation(req.params.domain, req.body.enabled));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
function setCatchAllAddress(req, res, next) {
async function setCatchAllAddress(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (!req.body.addresses) return next(new HttpError(400, 'addresses is required'));
if (!Array.isArray(req.body.addresses)) return next(new HttpError(400, 'addresses must be an array of strings'));
for (var i = 0; i < req.body.addresses.length; i++) {
for (let i = 0; i < req.body.addresses.length; i++) {
if (typeof req.body.addresses[i] !== 'string') return next(new HttpError(400, 'addresses must be an array of strings'));
}
mail.setCatchAllAddress(req.params.domain, req.body.addresses, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setCatchAllAddress(req.params.domain, req.body.addresses));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
function setMailRelay(req, res, next) {
async function setMailRelay(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
@@ -103,24 +102,22 @@ function setMailRelay(req, res, next) {
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'acceptSelfSignedCerts must be a boolean'));
mail.setMailRelay(req.params.domain, req.body, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setMailRelay(req.params.domain, req.body, { skipVerify: false }));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
function setMailEnabled(req, res, next) {
async function setMailEnabled(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
mail.setMailEnabled(req.params.domain, !!req.body.enabled, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setMailEnabled(req.params.domain, !!req.body.enabled, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
function sendTestMail(req, res, next) {
@@ -249,18 +246,17 @@ function setAliases(req, res, next) {
});
}
function setBanner(req, res, next) {
async function setBanner(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.text !== 'string') return res.status(400).send({ message: 'text must be a string' });
if ('html' in req.body && typeof req.body.html !== 'string') return res.status(400).send({ message: 'html must be a string' });
mail.setBanner(req.params.domain, { text: req.body.text, html: req.body.html || null }, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setBanner(req.params.domain, { text: req.body.text, html: req.body.html || null }));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
function getLists(req, res, next) {