Added some filter queries for Messages

This commit is contained in:
Halla Moore 2016-06-30 14:22:23 -07:00
parent d0666540b9
commit 6225f09018

View file

@ -13,6 +13,13 @@ module.exports = (server) => {
tags: ['messages'], tags: ['messages'],
validate: { validate: {
query: { query: {
unread: Joi.boolean(),
starred: Joi.boolean(),
subject: Joi.string(),
thread_id: Joi.number().integer().min(0),
received_before: Joi.date(),
received_after: Joi.date(),
filename: Joi.string(),
limit: Joi.number().integer().min(1).max(2000).default(100), limit: Joi.number().integer().min(1).max(2000).default(100),
offset: Joi.number().integer().min(0).default(0), offset: Joi.number().integer().min(0).default(0),
}, },
@ -25,11 +32,45 @@ module.exports = (server) => {
}, },
handler: (request, reply) => { handler: (request, reply) => {
request.getAccountDatabase().then((db) => { request.getAccountDatabase().then((db) => {
const {Message, Folder, Label} = db; const {Message, Folder, Label, File} = db;
const query = request.query;
const where = {};
const include = [{model: Folder}, {model: Label}];
if (query.unread != null) {
where.unread = query.unread;
}
if (query.starred != null) {
where.starred = query.starred;
}
if (query.subject) {
where.subject = query.subject;
}
if (query.thread_id != null) {
where.threadId = query.threadId;
}
if (query.received_before) {
where.date = {lt: query.received_before};
}
if (query.received_after) {
if (where.date) {
where.date.gt = query.received_after;
} else {
where.date = {gt: query.received_after};
}
}
if (query.filename) {
include.push({
model: File,
where: {filename: query.filename},
})
}
Message.findAll({ Message.findAll({
limit: request.query.limit, where: where,
offset: request.query.offset, limit: query.limit,
include: [{model: Folder}, {model: Label}], offset: query.offset,
include: include,
}).then((messages) => { }).then((messages) => {
reply(Serialization.jsonStringify(messages)); reply(Serialization.jsonStringify(messages));
}) })