diff --git a/CHANGES b/CHANGES index a3bbb8e2f..15c77d2be 100644 --- a/CHANGES +++ b/CHANGES @@ -2019,4 +2019,5 @@ * Fix bug where aliases where displayed incorrectly in SOGo * Add univention as LDAP provider * Bump max_connection for postgres addon to 200 +* mail: Add pagination to mailing list API diff --git a/src/mail.js b/src/mail.js index b34619580..37b6af623 100644 --- a/src/mail.js +++ b/src/mail.js @@ -1165,11 +1165,13 @@ function setAliases(name, domain, aliases, callback) { }); } -function getLists(domain, callback) { +function getLists(domain, page, perPage, callback) { assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof page, 'number'); + assert.strictEqual(typeof perPage, 'number'); assert.strictEqual(typeof callback, 'function'); - mailboxdb.getLists(domain, function (error, result) { + mailboxdb.getLists(domain, page, perPage, function (error, result) { if (error) return callback(error); callback(null, result); diff --git a/src/mailboxdb.js b/src/mailboxdb.js index 4c57bf9d9..1f7c6e6e5 100644 --- a/src/mailboxdb.js +++ b/src/mailboxdb.js @@ -234,11 +234,13 @@ function listAllMailboxes(page, perPage, callback) { }); } -function getLists(domain, callback) { +function getLists(domain, page, perPage, callback) { assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof page, 'number'); + assert.strictEqual(typeof perPage, 'number'); assert.strictEqual(typeof callback, 'function'); - database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE type = ? AND domain = ?', + database.query(`SELECT ${MAILBOX_FIELDS} FROM mailboxes WHERE type = ? AND domain = ? ORDER BY name LIMIT ${(page-1)*perPage},${perPage}`, [ exports.TYPE_LIST, domain ], function (error, results) { if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); diff --git a/src/routes/mail.js b/src/routes/mail.js index 9d8643834..5b40852a0 100644 --- a/src/routes/mail.js +++ b/src/routes/mail.js @@ -248,7 +248,13 @@ function setAliases(req, res, next) { function getLists(req, res, next) { assert.strictEqual(typeof req.params.domain, 'string'); - mail.getLists(req.params.domain, function (error, result) { + const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1; + if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a positive number')); + + const perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25; + if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a positive number')); + + mail.getLists(req.params.domain, page, perPage, function (error, result) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, { lists: result }));