Add starred query to threads route

This commit is contained in:
Halla Moore 2016-06-27 10:15:05 -07:00
parent bc5a4ecf3c
commit 621f8b5c2b
3 changed files with 27 additions and 15 deletions

View file

@ -11,7 +11,8 @@ module.exports = (server) => {
tags: ['threads'],
validate: {
query: {
unread: Joi.boolean().allow(''),
unread: Joi.boolean(),
starred: Joi.boolean(),
},
},
response: {
@ -23,19 +24,25 @@ module.exports = (server) => {
handler: (request, reply) => {
request.getAccountDatabase().then((db) => {
const {Thread} = db;
// 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));
})
const where = {};
if (request.query.unread) {
where.unreadCount = {gt: 0};
} else if (request.query.unread !== undefined) {
where.unreadCount = 0;
}
if (request.query.starred) {
where.starredCount = {gt: 0};
} else if (request.query.starred !== undefined) {
where.starredCount = 0;
}
Thread.findAll({
where: where,
limit: 50,
}).then((threads) => {
reply(Serialization.jsonStringify(threads));
})
})
},
});

View file

@ -4,6 +4,7 @@ module.exports = (sequelize, Sequelize) => {
subject: Sequelize.STRING,
cleanedSubject: Sequelize.STRING,
unreadCount: Sequelize.INTEGER,
starredCount: Sequelize.INTEGER,
}, {
classMethods: {
associate: ({Message}) => {

View file

@ -128,6 +128,7 @@ function matchThread({db, accountId, message}) {
subject: message.subject,
cleanedSubject: cleanSubject(message.subject),
unreadCount: 0,
starredCount: 0,
})
})
})
@ -142,6 +143,7 @@ function matchThread({db, accountId, message}) {
subject: message.subject,
cleanedSubject: cleanSubject(message.subject),
unreadCount: 0,
starredCount: 0,
})
})
}
@ -157,11 +159,13 @@ function addMessageToThread({db, accountId, message}) {
}
function updateThreadProperties({thread, message}) {
// Update the thread's unread count
if (message.unread) {
thread.unreadCount++;
thread.save();
}
if (message.starred) {
thread.starredCount++;
}
thread.save();
}