From bc5a4ecf3cfb8b4399c215d151076995660642e0 Mon Sep 17 00:00:00 2001 From: Halla Moore Date: Fri, 24 Jun 2016 16:14:04 -0700 Subject: [PATCH] Add unread query to threads route --- packages/nylas-api/routes/threads.js | 19 +++++++++++++++---- packages/nylas-core/models/account/thread.js | 1 + .../processors/threading.js | 12 ++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/nylas-api/routes/threads.js b/packages/nylas-api/routes/threads.js index 8cd77e62a..159a5353a 100644 --- a/packages/nylas-api/routes/threads.js +++ b/packages/nylas-api/routes/threads.js @@ -10,7 +10,8 @@ module.exports = (server) => { notes: 'Notes go here', tags: ['threads'], validate: { - params: { + query: { + unread: Joi.boolean().allow(''), }, }, response: { @@ -22,9 +23,19 @@ module.exports = (server) => { handler: (request, reply) => { request.getAccountDatabase().then((db) => { const {Thread} = db; - Thread.findAll({limit: 50}).then((threads) => { - reply(Serialization.jsonStringify(threads)); - }) + // the unread value will be '' if the url was just '/threads?unread' + if (request.query.unread || request.query.unread === '') { + Thread.findAll({ + where: {unreadCount: {gt: 0}}, + limit: 50, + }).then((threads) => { + reply(Serialization.jsonStringify(threads)); + }) + } else { + Thread.findAll({limit: 50}).then((threads) => { + reply(Serialization.jsonStringify(threads)); + }) + } }) }, }); diff --git a/packages/nylas-core/models/account/thread.js b/packages/nylas-core/models/account/thread.js index 208bcbcf7..4851d32fb 100644 --- a/packages/nylas-core/models/account/thread.js +++ b/packages/nylas-core/models/account/thread.js @@ -3,6 +3,7 @@ module.exports = (sequelize, Sequelize) => { threadId: Sequelize.STRING, subject: Sequelize.STRING, cleanedSubject: Sequelize.STRING, + unreadCount: Sequelize.INTEGER, }, { classMethods: { associate: ({Message}) => { diff --git a/packages/nylas-message-processor/processors/threading.js b/packages/nylas-message-processor/processors/threading.js index 8f5922b0f..e139207f7 100644 --- a/packages/nylas-message-processor/processors/threading.js +++ b/packages/nylas-message-processor/processors/threading.js @@ -127,6 +127,7 @@ function matchThread({db, accountId, message}) { return Thread.create({ subject: message.subject, cleanedSubject: cleanSubject(message.subject), + unreadCount: 0, }) }) }) @@ -140,6 +141,7 @@ function matchThread({db, accountId, message}) { return Thread.create({ subject: message.subject, cleanedSubject: cleanSubject(message.subject), + unreadCount: 0, }) }) } @@ -154,12 +156,22 @@ function addMessageToThread({db, accountId, message}) { .then((thread) => (thread)) } +function updateThreadProperties({thread, message}) { + // Update the thread's unread count + if (message.unread) { + thread.unreadCount++; + thread.save(); + } +} + + function processMessage({message, accountId}) { return DatabaseConnector.forAccount(accountId) .then((db) => addMessageToThread({db, accountId, message})) .then((thread) => { thread.addMessage(message) message.setThread(thread) + updateThreadProperties({thread, message}) return message }) }