From 0f64b6e58e9a3658ef2c2907525f9ed098b55585 Mon Sep 17 00:00:00 2001 From: Halla Moore Date: Tue, 28 Jun 2016 15:01:41 -0700 Subject: [PATCH] Add category filtering to threads api endpoint --- packages/nylas-api/routes/threads.js | 33 ++++++++++++++----- .../nylas-core/models/account/category.js | 3 +- .../models/account/thread-category.js | 7 ++++ packages/nylas-core/models/account/thread.js | 3 +- .../processors/threading.js | 6 ++++ 5 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 packages/nylas-core/models/account/thread-category.js diff --git a/packages/nylas-api/routes/threads.js b/packages/nylas-api/routes/threads.js index 7a050965d..65dc91d38 100644 --- a/packages/nylas-api/routes/threads.js +++ b/packages/nylas-api/routes/threads.js @@ -11,14 +11,15 @@ module.exports = (server) => { tags: ['threads'], validate: { query: { - id: Joi.number().integer().min(0), - subject: Joi.string(), - unread: Joi.boolean(), - starred: Joi.boolean(), - startedBefore: Joi.date().timestamp(), - startedAfter: Joi.date().timestamp(), - lastMessageBefore: Joi.date().timestamp(), - lastMessageAfter: Joi.date().timestamp(), + 'id': Joi.number().integer().min(0), + 'subject': Joi.string(), + 'unread': Joi.boolean(), + 'starred': Joi.boolean(), + 'startedBefore': Joi.date().timestamp(), + 'startedAfter': Joi.date().timestamp(), + 'lastMessageBefore': Joi.date().timestamp(), + 'lastMessageAfter': Joi.date().timestamp(), + 'in': Joi.string().allow(Joi.number()), }, }, response: { @@ -29,9 +30,10 @@ module.exports = (server) => { }, handler: (request, reply) => { request.getAccountDatabase().then((db) => { - const {Thread} = db; + const {Thread, Category} = db; const query = request.query; const where = {}; + const include = []; if (query.id) { where.id = query.id; @@ -76,8 +78,21 @@ module.exports = (server) => { } } + // Association queries + if (query.in) { + include.push({ + model: Category, + where: { $or: [ + { id: query.in }, + { name: query.in }, + { role: query.in }, + ]}, + }); + } + Thread.findAll({ where: where, + include: include, limit: 50, }).then((threads) => { reply(Serialization.jsonStringify(threads)); diff --git a/packages/nylas-core/models/account/category.js b/packages/nylas-core/models/account/category.js index 5d2c97408..8178a2f8c 100644 --- a/packages/nylas-core/models/account/category.js +++ b/packages/nylas-core/models/account/category.js @@ -7,8 +7,9 @@ module.exports = (sequelize, Sequelize) => { syncState: JSONType('syncState'), }, { classMethods: { - associate: ({Message}) => { + associate: ({Message, Thread, ThreadCategory}) => { Category.hasMany(Message) + Category.belongsToMany(Thread, {through: ThreadCategory}) }, }, instanceMethods: { diff --git a/packages/nylas-core/models/account/thread-category.js b/packages/nylas-core/models/account/thread-category.js new file mode 100644 index 000000000..6d6c7a501 --- /dev/null +++ b/packages/nylas-core/models/account/thread-category.js @@ -0,0 +1,7 @@ +module.exports = (sequelize, Sequelize) => { + const ThreadCategory = sequelize.define('ThreadCategory', { + role: Sequelize.STRING, + }); + + return ThreadCategory; +}; diff --git a/packages/nylas-core/models/account/thread.js b/packages/nylas-core/models/account/thread.js index dc055a751..95fc2c64c 100644 --- a/packages/nylas-core/models/account/thread.js +++ b/packages/nylas-core/models/account/thread.js @@ -10,7 +10,8 @@ module.exports = (sequelize, Sequelize) => { lastMessageReceivedTimestamp: Sequelize.DATE, }, { classMethods: { - associate: ({Message}) => { + associate: ({Category, Message, ThreadCategory}) => { + Thread.belongsToMany(Category, {through: ThreadCategory}) Thread.hasMany(Message, {as: 'messages'}) }, }, diff --git a/packages/nylas-message-processor/processors/threading.js b/packages/nylas-message-processor/processors/threading.js index 5cee5538b..75d524a4a 100644 --- a/packages/nylas-message-processor/processors/threading.js +++ b/packages/nylas-message-processor/processors/threading.js @@ -170,6 +170,12 @@ function updateThreadProperties({db, thread, message}) { }) thread.lastMessageTimestamp = message.date; + thread.hasCategory(message.CategoryId).then((hasCategory) => { + if (!hasCategory) { + thread.addCategory(message.CategoryId) + } + }); + if (message.unread) { thread.unreadCount++; }