2017-07-26 16:52:55 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Joi = require('joi');
|
2021-08-30 16:18:42 +08:00
|
|
|
const ObjectId = require('mongodb').ObjectId;
|
2017-07-26 16:52:55 +08:00
|
|
|
const imapTools = require('../../imap-core/lib/imap-tools');
|
|
|
|
const tools = require('../tools');
|
2018-08-30 17:24:21 +08:00
|
|
|
const roles = require('../roles');
|
|
|
|
const util = require('util');
|
2020-07-20 01:51:06 +08:00
|
|
|
const { sessSchema, sessIPSchema, booleanSchema } = require('../schemas');
|
2017-07-26 16:52:55 +08:00
|
|
|
|
|
|
|
module.exports = (db, server, mailboxHandler) => {
|
2018-08-30 17:24:21 +08:00
|
|
|
const getMailboxCounter = util.promisify(tools.getMailboxCounter);
|
2018-08-30 17:47:31 +08:00
|
|
|
const updateMailbox = util.promisify(mailboxHandler.update.bind(mailboxHandler));
|
|
|
|
const deleteMailbox = util.promisify(mailboxHandler.del.bind(mailboxHandler));
|
2018-08-30 17:42:06 +08:00
|
|
|
const createMailbox = util.promisify((...args) => {
|
|
|
|
let callback = args.pop();
|
|
|
|
mailboxHandler.create(...args, (err, status, id) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, { status, id });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-30 17:24:21 +08:00
|
|
|
server.get(
|
|
|
|
'/users/:user/mailboxes',
|
2022-11-24 18:26:10 +08:00
|
|
|
tools.responseWrapper(async (req, res) => {
|
2018-08-30 17:24:21 +08:00
|
|
|
res.charSet('utf-8');
|
|
|
|
|
|
|
|
const schema = Joi.object().keys({
|
2020-07-02 18:08:16 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
2020-07-20 01:51:06 +08:00
|
|
|
specialUse: booleanSchema.default(false),
|
|
|
|
showHidden: booleanSchema.default(false),
|
|
|
|
counters: booleanSchema.default(false),
|
|
|
|
sizes: booleanSchema.default(false),
|
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-08-30 17:24:21 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:24:21 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-08-30 17:24:21 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).readOwn('mailboxes'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).readAny('mailboxes'));
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:18:42 +08:00
|
|
|
let user = new ObjectId(result.value.user);
|
2018-08-30 17:24:21 +08:00
|
|
|
let counters = result.value.counters;
|
2018-11-24 03:39:42 +08:00
|
|
|
let sizes = result.value.sizes;
|
|
|
|
|
|
|
|
let sizeValues = false;
|
2018-08-30 17:24:21 +08:00
|
|
|
|
|
|
|
let userData;
|
|
|
|
try {
|
|
|
|
userData = await db.users.collection('users').findOne(
|
|
|
|
{
|
|
|
|
_id: user
|
|
|
|
},
|
|
|
|
{
|
|
|
|
projection: {
|
|
|
|
address: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:24:21 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
code: 'InternalDatabaseError'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!userData) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(404);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:24:21 +08:00
|
|
|
error: 'This user does not exist',
|
|
|
|
code: 'UserNotFound'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-24 03:39:42 +08:00
|
|
|
if (sizes) {
|
|
|
|
try {
|
|
|
|
sizeValues = await db.database
|
|
|
|
.collection('messages')
|
|
|
|
.aggregate([
|
|
|
|
{
|
|
|
|
$match: {
|
|
|
|
user
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
mailbox: '$mailbox',
|
|
|
|
size: '$size'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
_id: '$mailbox',
|
|
|
|
mailboxSize: {
|
|
|
|
$sum: '$size'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
])
|
|
|
|
.toArray();
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:24:21 +08:00
|
|
|
let mailboxes;
|
|
|
|
try {
|
|
|
|
mailboxes = await db.database
|
2017-11-28 17:51:45 +08:00
|
|
|
.collection('mailboxes')
|
|
|
|
.find({
|
|
|
|
user
|
|
|
|
})
|
2018-08-30 17:24:21 +08:00
|
|
|
.toArray();
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:24:21 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
code: 'InternalDatabaseError'
|
|
|
|
});
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:24:21 +08:00
|
|
|
if (!mailboxes) {
|
|
|
|
mailboxes = [];
|
|
|
|
}
|
|
|
|
|
2018-09-10 19:13:14 +08:00
|
|
|
if (result.value.specialUse) {
|
|
|
|
mailboxes = mailboxes.filter(mailboxData => mailboxData.path === 'INBOX' || mailboxData.specialUse);
|
|
|
|
}
|
2018-08-30 17:24:21 +08:00
|
|
|
|
2020-07-02 18:08:16 +08:00
|
|
|
if (!result.value.showHidden) {
|
|
|
|
mailboxes = mailboxes.filter(mailboxData => !mailboxData.hidden);
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:26:38 +08:00
|
|
|
mailboxes = mailboxes
|
|
|
|
.map(mailboxData => mailboxData)
|
|
|
|
.sort((a, b) => {
|
|
|
|
if (a.path === 'INBOX') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (b.path === 'INBOX') {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (a.path.indexOf('INBOX/') === 0 && b.path.indexOf('INBOX/') !== 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.path.indexOf('INBOX/') !== 0 && b.path.indexOf('INBOX/') === 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (a.subscribed !== b.subscribed) {
|
|
|
|
return (a.subscribed ? 0 : 1) - (b.subscribed ? 0 : 1);
|
|
|
|
}
|
|
|
|
return a.path.localeCompare(b.path);
|
|
|
|
});
|
2018-08-30 17:24:21 +08:00
|
|
|
|
|
|
|
let responses = [];
|
|
|
|
|
2019-07-31 16:26:38 +08:00
|
|
|
let counterOps = [];
|
|
|
|
|
2018-08-30 17:24:21 +08:00
|
|
|
for (let mailboxData of mailboxes) {
|
|
|
|
let path = mailboxData.path.split('/');
|
|
|
|
let name = path.pop();
|
|
|
|
|
|
|
|
let response = {
|
2021-01-07 15:41:48 +08:00
|
|
|
id: mailboxData._id.toString(),
|
2018-08-30 17:24:21 +08:00
|
|
|
name,
|
|
|
|
path: mailboxData.path,
|
|
|
|
specialUse: mailboxData.specialUse,
|
|
|
|
modifyIndex: mailboxData.modifyIndex,
|
2020-07-02 18:08:16 +08:00
|
|
|
subscribed: mailboxData.subscribed,
|
2023-08-17 14:50:34 +08:00
|
|
|
hidden: !!mailboxData.hidden
|
2018-08-30 17:24:21 +08:00
|
|
|
};
|
|
|
|
|
2021-03-26 17:22:50 +08:00
|
|
|
if (mailboxData.retention) {
|
|
|
|
response.retention = mailboxData.retention;
|
|
|
|
}
|
|
|
|
|
2018-11-24 03:39:42 +08:00
|
|
|
if (sizeValues) {
|
|
|
|
for (let sizeValue of sizeValues) {
|
|
|
|
if (mailboxData._id.equals(sizeValue._id)) {
|
|
|
|
response.size = sizeValue.mailboxSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:24:21 +08:00
|
|
|
if (!counters) {
|
|
|
|
responses.push(response);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let total, unseen;
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2019-07-31 16:32:41 +08:00
|
|
|
counterOps.push(
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
total = await getMailboxCounter(db, mailboxData._id, false);
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
response.total = total;
|
|
|
|
})()
|
|
|
|
);
|
2018-08-30 17:24:21 +08:00
|
|
|
|
2019-07-31 16:32:41 +08:00
|
|
|
counterOps.push(
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
unseen = await getMailboxCounter(db, mailboxData._id, 'unseen');
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
response.unseen = unseen;
|
|
|
|
})()
|
|
|
|
);
|
2018-08-30 17:24:21 +08:00
|
|
|
|
|
|
|
responses.push(response);
|
2017-11-28 17:51:45 +08:00
|
|
|
}
|
2018-08-30 17:24:21 +08:00
|
|
|
|
2019-07-31 16:26:38 +08:00
|
|
|
if (counterOps.length) {
|
|
|
|
await Promise.all(counterOps);
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:24:21 +08:00
|
|
|
success: true,
|
|
|
|
results: responses
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
server.post(
|
|
|
|
'/users/:user/mailboxes',
|
2022-11-24 18:26:10 +08:00
|
|
|
tools.responseWrapper(async (req, res) => {
|
2018-08-30 17:42:06 +08:00
|
|
|
res.charSet('utf-8');
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
const schema = Joi.object().keys({
|
2020-07-02 18:08:16 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
2018-08-30 17:42:06 +08:00
|
|
|
path: Joi.string()
|
2018-10-19 00:07:13 +08:00
|
|
|
.regex(/\/{2,}|\/$/, { invert: true })
|
2018-08-30 17:42:06 +08:00
|
|
|
.required(),
|
2020-07-20 01:51:06 +08:00
|
|
|
hidden: booleanSchema.default(false),
|
2018-09-07 14:58:05 +08:00
|
|
|
retention: Joi.number().min(0),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2018-08-30 17:42:06 +08:00
|
|
|
});
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-08-30 17:42:06 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-08-30 17:42:06 +08:00
|
|
|
});
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).createOwn('mailboxes'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).createAny('mailboxes'));
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2021-08-30 16:18:42 +08:00
|
|
|
let user = new ObjectId(result.value.user);
|
2018-08-30 17:42:06 +08:00
|
|
|
let path = imapTools.normalizeMailbox(result.value.path);
|
|
|
|
let retention = result.value.retention;
|
|
|
|
|
|
|
|
let opts = {
|
2020-07-02 18:23:06 +08:00
|
|
|
subscribed: true,
|
|
|
|
hidden: !!result.value.hidden
|
2018-08-30 17:42:06 +08:00
|
|
|
};
|
2020-07-02 18:23:06 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
if (retention) {
|
|
|
|
opts.retention = retention;
|
|
|
|
}
|
|
|
|
|
|
|
|
let status, id;
|
|
|
|
try {
|
|
|
|
let data = await createMailbox(user, path, opts);
|
|
|
|
status = data.status;
|
|
|
|
id = data.id;
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500); // TODO: use response code specific status
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: err.message,
|
|
|
|
code: err.code
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof status === 'string') {
|
2022-11-24 18:26:10 +08:00
|
|
|
res.status(500);
|
|
|
|
return res.json({
|
2017-07-26 16:52:55 +08:00
|
|
|
error: 'Mailbox creation failed with code ' + status
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2017-07-26 16:52:55 +08:00
|
|
|
success: !!status,
|
|
|
|
id
|
|
|
|
});
|
2018-08-30 17:42:06 +08:00
|
|
|
})
|
|
|
|
);
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
server.get(
|
|
|
|
'/users/:user/mailboxes/:mailbox',
|
2022-11-24 18:26:10 +08:00
|
|
|
tools.responseWrapper(async (req, res) => {
|
2018-08-30 17:42:06 +08:00
|
|
|
res.charSet('utf-8');
|
|
|
|
const schema = Joi.object().keys({
|
2020-07-02 18:08:16 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).allow('resolve').required(),
|
2020-07-16 16:15:04 +08:00
|
|
|
path: Joi.string().regex(/\/{2,}|\/$/, { invert: true }),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2018-08-30 17:42:06 +08:00
|
|
|
});
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-08-30 17:42:06 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-08-30 17:42:06 +08:00
|
|
|
});
|
|
|
|
}
|
2018-01-25 03:04:53 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).readOwn('mailboxes'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).readAny('mailboxes'));
|
|
|
|
}
|
2018-01-25 03:04:53 +08:00
|
|
|
|
2021-08-30 16:18:42 +08:00
|
|
|
let user = new ObjectId(result.value.user);
|
|
|
|
let mailbox = result.value.mailbox !== 'resolve' ? new ObjectId(result.value.mailbox) : 'resolve';
|
2018-08-30 17:42:06 +08:00
|
|
|
|
|
|
|
let userData;
|
|
|
|
|
|
|
|
try {
|
|
|
|
userData = await db.users.collection('users').findOne(
|
|
|
|
{
|
|
|
|
_id: user
|
|
|
|
},
|
|
|
|
{
|
|
|
|
projection: {
|
|
|
|
address: true
|
|
|
|
}
|
2018-01-25 03:04:53 +08:00
|
|
|
}
|
2018-08-30 17:42:06 +08:00
|
|
|
);
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
code: 'InternalDatabaseError'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!userData) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(404);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
error: 'This user does not exist',
|
|
|
|
code: 'UserNotFound'
|
|
|
|
});
|
|
|
|
}
|
2018-01-25 03:04:53 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
let mailboxQuery = {
|
|
|
|
_id: mailbox,
|
|
|
|
user
|
|
|
|
};
|
2017-11-28 17:51:45 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
if (mailbox === 'resolve') {
|
|
|
|
mailboxQuery = {
|
2020-07-16 16:15:04 +08:00
|
|
|
path: result.value.path,
|
2018-08-30 17:42:06 +08:00
|
|
|
user
|
|
|
|
};
|
|
|
|
}
|
2017-11-28 17:51:45 +08:00
|
|
|
|
2018-08-30 17:42:06 +08:00
|
|
|
let mailboxData;
|
|
|
|
try {
|
|
|
|
mailboxData = await db.database.collection('mailboxes').findOne(mailboxQuery);
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
error: 'MongoDB Error: ' + err.message,
|
|
|
|
code: 'InternalDatabaseError'
|
2018-01-25 03:04:53 +08:00
|
|
|
});
|
2017-11-28 17:51:45 +08:00
|
|
|
}
|
2018-08-30 17:42:06 +08:00
|
|
|
if (!mailboxData) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(404);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 18:14:13 +08:00
|
|
|
error: 'This mailbox does not exist',
|
|
|
|
code: 'NoSuchMailbox'
|
2018-08-30 17:42:06 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
mailbox = mailboxData._id;
|
|
|
|
|
|
|
|
let path = mailboxData.path.split('/');
|
|
|
|
let name = path.pop();
|
|
|
|
|
|
|
|
let total, unseen;
|
|
|
|
|
|
|
|
try {
|
|
|
|
total = await getMailboxCounter(db, mailboxData._id, false);
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
unseen = await getMailboxCounter(db, mailboxData._id, 'unseen');
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:42:06 +08:00
|
|
|
success: true,
|
|
|
|
id: mailbox,
|
|
|
|
name,
|
|
|
|
path: mailboxData.path,
|
|
|
|
specialUse: mailboxData.specialUse,
|
|
|
|
modifyIndex: mailboxData.modifyIndex,
|
|
|
|
subscribed: mailboxData.subscribed,
|
2020-07-02 18:08:16 +08:00
|
|
|
hidden: !!mailboxData.hidden,
|
2018-08-30 17:42:06 +08:00
|
|
|
total,
|
|
|
|
unseen
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
server.put(
|
|
|
|
'/users/:user/mailboxes/:mailbox',
|
2022-11-24 18:26:10 +08:00
|
|
|
tools.responseWrapper(async (req, res) => {
|
2018-08-30 17:47:31 +08:00
|
|
|
res.charSet('utf-8');
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
const schema = Joi.object().keys({
|
2020-07-02 18:08:16 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).required(),
|
2018-10-19 00:07:13 +08:00
|
|
|
path: Joi.string().regex(/\/{2,}|\/$/, { invert: true }),
|
2020-07-02 18:08:16 +08:00
|
|
|
retention: Joi.number().empty('').min(0),
|
2020-07-20 01:51:06 +08:00
|
|
|
subscribed: booleanSchema,
|
|
|
|
hidden: booleanSchema,
|
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2018-08-30 17:47:31 +08:00
|
|
|
});
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-08-30 17:47:31 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-08-30 17:47:31 +08:00
|
|
|
});
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).updateOwn('mailboxes'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).updateAny('mailboxes'));
|
2017-07-26 16:52:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-30 16:18:42 +08:00
|
|
|
let user = new ObjectId(result.value.user);
|
|
|
|
let mailbox = new ObjectId(result.value.mailbox);
|
2018-08-30 17:47:31 +08:00
|
|
|
|
|
|
|
let updates = {};
|
|
|
|
let update = false;
|
|
|
|
Object.keys(result.value || {}).forEach(key => {
|
|
|
|
if (!['user', 'mailbox'].includes(key)) {
|
|
|
|
updates[key] = result.value[key];
|
|
|
|
update = true;
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
if (!update) {
|
2022-11-24 18:26:10 +08:00
|
|
|
res.status(400);
|
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: 'Nothing was changed'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let status;
|
|
|
|
try {
|
|
|
|
status = await updateMailbox(user, mailbox, updates);
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500); // TODO: use response code specific status
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: err.message,
|
|
|
|
code: err.code
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof status === 'string') {
|
2022-11-24 18:26:10 +08:00
|
|
|
res.status(500);
|
|
|
|
return res.json({
|
2017-07-26 16:52:55 +08:00
|
|
|
error: 'Mailbox update failed with code ' + status
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2017-07-26 16:52:55 +08:00
|
|
|
success: true
|
|
|
|
});
|
2018-08-30 17:47:31 +08:00
|
|
|
})
|
|
|
|
);
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
server.del(
|
|
|
|
'/users/:user/mailboxes/:mailbox',
|
2022-11-24 18:26:10 +08:00
|
|
|
tools.responseWrapper(async (req, res) => {
|
2018-08-30 17:47:31 +08:00
|
|
|
res.charSet('utf-8');
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
const schema = Joi.object().keys({
|
2020-07-02 18:08:16 +08:00
|
|
|
user: Joi.string().hex().lowercase().length(24).required(),
|
|
|
|
mailbox: Joi.string().hex().lowercase().length(24).required(),
|
2020-07-20 01:51:06 +08:00
|
|
|
sess: sessSchema,
|
|
|
|
ip: sessIPSchema
|
2018-08-30 17:47:31 +08:00
|
|
|
});
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2020-07-20 01:51:06 +08:00
|
|
|
const result = schema.validate(req.params, {
|
2018-08-30 17:47:31 +08:00
|
|
|
abortEarly: false,
|
|
|
|
convert: true
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
if (result.error) {
|
2019-07-31 21:05:59 +08:00
|
|
|
res.status(400);
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: result.error.message,
|
2020-07-16 16:31:58 +08:00
|
|
|
code: 'InputValidationError',
|
|
|
|
details: tools.validationErrors(result)
|
2018-08-30 17:47:31 +08:00
|
|
|
});
|
|
|
|
}
|
2017-07-26 16:52:55 +08:00
|
|
|
|
2018-08-30 17:47:31 +08:00
|
|
|
// permissions check
|
|
|
|
if (req.user && req.user === result.value.user) {
|
|
|
|
req.validate(roles.can(req.role).deleteOwn('mailboxes'));
|
|
|
|
} else {
|
|
|
|
req.validate(roles.can(req.role).deleteAny('mailboxes'));
|
|
|
|
}
|
|
|
|
|
2021-08-30 16:18:42 +08:00
|
|
|
let user = new ObjectId(result.value.user);
|
|
|
|
let mailbox = new ObjectId(result.value.mailbox);
|
2018-08-30 17:47:31 +08:00
|
|
|
|
|
|
|
let status;
|
|
|
|
try {
|
|
|
|
status = await deleteMailbox(user, mailbox);
|
|
|
|
} catch (err) {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500); // TODO: use response code specific status
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: err.message,
|
|
|
|
code: err.code
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof status === 'string') {
|
2021-05-20 19:47:20 +08:00
|
|
|
res.status(500); // TODO: use response code specific status
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2018-08-30 17:47:31 +08:00
|
|
|
error: 'Mailbox deletion failed with code ' + status,
|
|
|
|
code: status
|
2017-07-26 16:52:55 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:26:10 +08:00
|
|
|
return res.json({
|
2017-07-26 16:52:55 +08:00
|
|
|
success: true
|
|
|
|
});
|
2018-08-30 17:47:31 +08:00
|
|
|
})
|
|
|
|
);
|
2017-07-26 16:52:55 +08:00
|
|
|
};
|