2016-06-19 18:02:32 +08:00
|
|
|
const Joi = require('joi');
|
|
|
|
const Serialization = require('../serialization');
|
|
|
|
|
|
|
|
module.exports = (server) => {
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/threads',
|
|
|
|
config: {
|
|
|
|
description: 'Returns threads',
|
|
|
|
notes: 'Notes go here',
|
|
|
|
tags: ['threads'],
|
|
|
|
validate: {
|
2016-06-25 07:14:04 +08:00
|
|
|
query: {
|
2016-06-28 03:30:28 +08:00
|
|
|
id: Joi.number().integer().min(0),
|
2016-06-28 01:15:05 +08:00
|
|
|
unread: Joi.boolean(),
|
|
|
|
starred: Joi.boolean(),
|
2016-06-28 03:30:28 +08:00
|
|
|
startedBefore: Joi.date().timestamp(),
|
|
|
|
startedAfter: Joi.date().timestamp(),
|
|
|
|
lastMessageBefore: Joi.date().timestamp(),
|
|
|
|
lastMessageAfter: Joi.date().timestamp(),
|
2016-06-19 18:02:32 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: Joi.array().items(
|
2016-06-24 06:46:52 +08:00
|
|
|
Serialization.jsonSchema('Thread')
|
2016-06-19 18:02:32 +08:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
handler: (request, reply) => {
|
|
|
|
request.getAccountDatabase().then((db) => {
|
|
|
|
const {Thread} = db;
|
2016-06-28 03:30:28 +08:00
|
|
|
const query = request.query;
|
2016-06-28 01:15:05 +08:00
|
|
|
const where = {};
|
|
|
|
|
2016-06-28 03:30:28 +08:00
|
|
|
if (query.id) {
|
|
|
|
where.id = query.id;
|
2016-06-28 01:35:34 +08:00
|
|
|
}
|
2016-06-28 03:30:28 +08:00
|
|
|
|
|
|
|
// Boolean queries
|
|
|
|
if (query.unread) {
|
2016-06-28 01:15:05 +08:00
|
|
|
where.unreadCount = {gt: 0};
|
2016-06-28 03:30:28 +08:00
|
|
|
} else if (query.unread !== undefined) {
|
2016-06-28 01:15:05 +08:00
|
|
|
where.unreadCount = 0;
|
|
|
|
}
|
2016-06-28 03:30:28 +08:00
|
|
|
if (query.starred) {
|
2016-06-28 01:15:05 +08:00
|
|
|
where.starredCount = {gt: 0};
|
2016-06-28 03:30:28 +08:00
|
|
|
} else if (query.starred !== undefined) {
|
2016-06-28 01:15:05 +08:00
|
|
|
where.starredCount = 0;
|
2016-06-25 07:14:04 +08:00
|
|
|
}
|
2016-06-28 01:15:05 +08:00
|
|
|
|
2016-06-28 03:30:28 +08:00
|
|
|
// Timestamp queries
|
|
|
|
if (query.lastMessageBefore) {
|
|
|
|
where.lastMessageReceivedTimestamp = {lt: query.lastMessageBefore};
|
|
|
|
}
|
|
|
|
if (query.lastMessageAfter) {
|
|
|
|
if (where.lastMessageReceivedTimestamp) {
|
|
|
|
where.lastMessageReceivedTimestamp.gt = query.lastMessageAfter;
|
|
|
|
} else {
|
|
|
|
where.lastMessageReceivedTimestamp = {gt: query.lastMessageAfter};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (query.startedBefore) {
|
|
|
|
where.firstMessageTimestamp = {lt: query.startedBefore};
|
|
|
|
}
|
|
|
|
if (query.startedAfter) {
|
|
|
|
if (where.firstMessageTimestamp) {
|
|
|
|
where.firstMessageTimestamp.gt = query.startedAfter;
|
|
|
|
} else {
|
|
|
|
where.firstMessageTimestamp = {gt: query.startedAfter};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 01:15:05 +08:00
|
|
|
Thread.findAll({
|
|
|
|
where: where,
|
|
|
|
limit: 50,
|
|
|
|
}).then((threads) => {
|
|
|
|
reply(Serialization.jsonStringify(threads));
|
|
|
|
})
|
2016-06-19 18:02:32 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
});
|
2016-06-24 06:46:52 +08:00
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'PUT',
|
|
|
|
path: '/threads/${id}',
|
|
|
|
config: {
|
|
|
|
description: 'Update a thread',
|
|
|
|
notes: 'Can move between folders',
|
|
|
|
tags: ['threads'],
|
|
|
|
validate: {
|
|
|
|
params: {
|
|
|
|
payload: {
|
|
|
|
folder_id: Joi.string(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
schema: Joi.array().items(
|
|
|
|
Serialization.jsonSchema('Thread')
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
handler: (request, reply) => {
|
|
|
|
request.getAccountDatabase().then((db) => {
|
|
|
|
db.SyncbackRequest.create({
|
|
|
|
type: "MoveToFolder",
|
|
|
|
props: {
|
|
|
|
folderId: request.params.folder_id,
|
|
|
|
threadId: request.params.id,
|
|
|
|
},
|
|
|
|
}).then((syncbackRequest) => {
|
|
|
|
reply(Serialization.jsonStringify(syncbackRequest))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
});
|
2016-06-19 18:02:32 +08:00
|
|
|
};
|