diff --git a/lib/api/mailboxes.js b/lib/api/mailboxes.js index 0c3216a7..323ebe7e 100644 --- a/lib/api/mailboxes.js +++ b/lib/api/mailboxes.js @@ -34,6 +34,7 @@ module.exports = (db, server, mailboxHandler) => { * @apiParam {String} user Users unique ID * @apiParam {Boolean} [specialUse=false] Should the response include only folders with specialUse flag set. * @apiParam {Boolean} [counters=false] Should the response include counters (total + unseen). Counters come with some overhead. + * @apiParam {Boolean} [sizes=false] Should the response include mailbox size in bytes. Size numbers come with a lot of overhead as an aggregated query is ran. * * @apiSuccess {Boolean} success Indicates successful response * @apiSuccess {Object[]} results List of user mailboxes @@ -107,6 +108,10 @@ module.exports = (db, server, mailboxHandler) => { .truthy(['Y', 'true', 'yes', 'on', '1', 1]) .falsy(['N', 'false', 'no', 'off', '0', 0, '']) .default(false), + sizes: Joi.boolean() + .truthy(['Y', 'true', 'yes', 'on', '1', 1]) + .falsy(['N', 'false', 'no', 'off', '0', 0, '']) + .default(false), sess: Joi.string().max(255), ip: Joi.string().ip({ version: ['ipv4', 'ipv6'], @@ -138,6 +143,9 @@ module.exports = (db, server, mailboxHandler) => { let user = new ObjectID(result.value.user); let counters = result.value.counters; + let sizes = result.value.sizes; + + let sizeValues = false; let userData; try { @@ -166,6 +174,37 @@ module.exports = (db, server, mailboxHandler) => { return next(); } + 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 + } + } + let mailboxes; try { mailboxes = await db.database @@ -224,6 +263,15 @@ module.exports = (db, server, mailboxHandler) => { subscribed: mailboxData.subscribed }; + if (sizeValues) { + for (let sizeValue of sizeValues) { + if (mailboxData._id.equals(sizeValue._id)) { + response.size = sizeValue.mailboxSize; + break; + } + } + } + if (!counters) { responses.push(response); continue;