mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-22 08:16:09 +08:00
Add timestamp queries to threads route
This commit is contained in:
parent
ab4c8cf32c
commit
2e56d3ced4
|
@ -11,9 +11,13 @@ module.exports = (server) => {
|
||||||
tags: ['threads'],
|
tags: ['threads'],
|
||||||
validate: {
|
validate: {
|
||||||
query: {
|
query: {
|
||||||
|
id: Joi.number().integer().min(0),
|
||||||
unread: Joi.boolean(),
|
unread: Joi.boolean(),
|
||||||
starred: Joi.boolean(),
|
starred: Joi.boolean(),
|
||||||
id: Joi.number().integer().min(0),
|
startedBefore: Joi.date().timestamp(),
|
||||||
|
startedAfter: Joi.date().timestamp(),
|
||||||
|
lastMessageBefore: Joi.date().timestamp(),
|
||||||
|
lastMessageAfter: Joi.date().timestamp(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
response: {
|
response: {
|
||||||
|
@ -25,22 +29,47 @@ module.exports = (server) => {
|
||||||
handler: (request, reply) => {
|
handler: (request, reply) => {
|
||||||
request.getAccountDatabase().then((db) => {
|
request.getAccountDatabase().then((db) => {
|
||||||
const {Thread} = db;
|
const {Thread} = db;
|
||||||
|
const query = request.query;
|
||||||
const where = {};
|
const where = {};
|
||||||
|
|
||||||
if (request.query.id) {
|
if (query.id) {
|
||||||
where.id = request.query.id;
|
where.id = query.id;
|
||||||
}
|
}
|
||||||
if (request.query.unread) {
|
|
||||||
|
// Boolean queries
|
||||||
|
if (query.unread) {
|
||||||
where.unreadCount = {gt: 0};
|
where.unreadCount = {gt: 0};
|
||||||
} else if (request.query.unread !== undefined) {
|
} else if (query.unread !== undefined) {
|
||||||
where.unreadCount = 0;
|
where.unreadCount = 0;
|
||||||
}
|
}
|
||||||
if (request.query.starred) {
|
if (query.starred) {
|
||||||
where.starredCount = {gt: 0};
|
where.starredCount = {gt: 0};
|
||||||
} else if (request.query.starred !== undefined) {
|
} else if (query.starred !== undefined) {
|
||||||
where.starredCount = 0;
|
where.starredCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Thread.findAll({
|
Thread.findAll({
|
||||||
where: where,
|
where: where,
|
||||||
limit: 50,
|
limit: 50,
|
||||||
|
|
|
@ -5,6 +5,9 @@ module.exports = (sequelize, Sequelize) => {
|
||||||
cleanedSubject: Sequelize.STRING,
|
cleanedSubject: Sequelize.STRING,
|
||||||
unreadCount: Sequelize.INTEGER,
|
unreadCount: Sequelize.INTEGER,
|
||||||
starredCount: Sequelize.INTEGER,
|
starredCount: Sequelize.INTEGER,
|
||||||
|
firstMessageTimestamp: Sequelize.DATE,
|
||||||
|
lastMessageTimestamp: Sequelize.DATE,
|
||||||
|
lastMessageReceivedTimestamp: Sequelize.DATE,
|
||||||
}, {
|
}, {
|
||||||
classMethods: {
|
classMethods: {
|
||||||
associate: ({Message}) => {
|
associate: ({Message}) => {
|
||||||
|
|
|
@ -127,6 +127,7 @@ function matchThread({db, accountId, message}) {
|
||||||
return Thread.create({
|
return Thread.create({
|
||||||
subject: message.subject,
|
subject: message.subject,
|
||||||
cleanedSubject: cleanSubject(message.subject),
|
cleanedSubject: cleanSubject(message.subject),
|
||||||
|
firstMessageTimestamp: message.date,
|
||||||
unreadCount: 0,
|
unreadCount: 0,
|
||||||
starredCount: 0,
|
starredCount: 0,
|
||||||
})
|
})
|
||||||
|
@ -142,6 +143,7 @@ function matchThread({db, accountId, message}) {
|
||||||
return Thread.create({
|
return Thread.create({
|
||||||
subject: message.subject,
|
subject: message.subject,
|
||||||
cleanedSubject: cleanSubject(message.subject),
|
cleanedSubject: cleanSubject(message.subject),
|
||||||
|
firstMessageTimestamp: message.date,
|
||||||
unreadCount: 0,
|
unreadCount: 0,
|
||||||
starredCount: 0,
|
starredCount: 0,
|
||||||
})
|
})
|
||||||
|
@ -155,10 +157,19 @@ function addMessageToThread({db, accountId, message}) {
|
||||||
return Thread.find({where: {threadId: message.headers['X-GM-THRID']}})
|
return Thread.find({where: {threadId: message.headers['X-GM-THRID']}})
|
||||||
}
|
}
|
||||||
return matchThread({db, accountId, message})
|
return matchThread({db, accountId, message})
|
||||||
.then((thread) => (thread))
|
.then((thread) => ({db, thread}))
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateThreadProperties({thread, message}) {
|
function updateThreadProperties({db, thread, message}) {
|
||||||
|
const {Category} = db;
|
||||||
|
Category.findById(message.CategoryId).then((category) => {
|
||||||
|
if (category.role !== 'sent') {
|
||||||
|
thread.lastMessageReceivedTimestamp = message.date;
|
||||||
|
thread.save();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
thread.lastMessageTimestamp = message.date;
|
||||||
|
|
||||||
if (message.unread) {
|
if (message.unread) {
|
||||||
thread.unreadCount++;
|
thread.unreadCount++;
|
||||||
}
|
}
|
||||||
|
@ -172,10 +183,10 @@ function updateThreadProperties({thread, message}) {
|
||||||
function processMessage({message, accountId}) {
|
function processMessage({message, accountId}) {
|
||||||
return DatabaseConnector.forAccount(accountId)
|
return DatabaseConnector.forAccount(accountId)
|
||||||
.then((db) => addMessageToThread({db, accountId, message}))
|
.then((db) => addMessageToThread({db, accountId, message}))
|
||||||
.then((thread) => {
|
.then(({db, thread}) => {
|
||||||
thread.addMessage(message)
|
thread.addMessage(message)
|
||||||
message.setThread(thread)
|
message.setThread(thread)
|
||||||
updateThreadProperties({thread, message})
|
updateThreadProperties({db, thread, message})
|
||||||
return message
|
return message
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue