Add various syncback tasks for messages

This commit is contained in:
Halla Moore 2016-06-29 18:52:58 -07:00
parent 80d80eb306
commit fd7570f6c8
8 changed files with 176 additions and 0 deletions

View file

@ -1,5 +1,6 @@
const Joi = require('joi'); const Joi = require('joi');
const Serialization = require('../serialization'); const Serialization = require('../serialization');
const {createSyncbackRequest} = require('../route-helpers');
module.exports = (server) => { module.exports = (server) => {
@ -80,4 +81,66 @@ module.exports = (server) => {
}) })
}, },
}) })
server.route({
method: 'PUT',
path: '/messages/{id}',
config: {
description: 'Update a message',
tags: ['messages'],
validate: {
params: {
id: Joi.string(),
payload: {
folder_id: Joi.string(),
},
},
},
response: {
schema: Serialization.jsonSchema('SyncbackRequest'),
},
},
handler: (request, reply) => {
const payload = request.payload
if (payload.folder_id) {
createSyncbackRequest(request, reply, {
type: "MoveMessageToFolder",
props: {
folderId: request.payload.folder_id,
messageId: request.params.id,
},
})
}
if (payload.unread === false) {
createSyncbackRequest(request, reply, {
type: "MarkMessageAsRead",
props: {
messageId: request.params.id,
},
})
} else if (payload.unread === true) {
createSyncbackRequest(request, reply, {
type: "MarkMessageAsUnread",
props: {
messageId: request.params.id,
},
})
}
if (payload.starred === false) {
createSyncbackRequest(request, reply, {
type: "UnstarMessage",
props: {
messageId: request.params.id,
},
})
} else if (payload.starred === true) {
createSyncbackRequest(request, reply, {
type: "StarMessage",
props: {
messageId: request.params.id,
},
})
}
},
});
}; };

View file

@ -16,6 +16,16 @@ class SyncbackTaskFactory {
Task = require('./syncback_tasks/star-thread.imap'); break; Task = require('./syncback_tasks/star-thread.imap'); break;
case "UnstarThread": case "UnstarThread":
Task = require('./syncback_tasks/unstar-thread.imap'); break; Task = require('./syncback_tasks/unstar-thread.imap'); break;
case "MoveMessageToFolder":
Task = require('./syncback_tasks/move-message-to-folder.imap'); break;
case "MarkMessageAsRead":
Task = require('./syncback_tasks/mark-message-as-read.imap'); break;
case "MarkMessageAsUnread":
Task = require('./syncback_tasks/mark-message-as-unread.imap'); break;
case "StarMessage":
Task = require('./syncback_tasks/star-message.imap'); break;
case "UnstarMessage":
Task = require('./syncback_tasks/unstar-message.imap'); break;
default: default:
throw new Error(`Invalid Task Type: ${syncbackRequest.type}`) throw new Error(`Invalid Task Type: ${syncbackRequest.type}`)
} }

View file

@ -0,0 +1,18 @@
const SyncbackTask = require('./syncback-task')
const TaskHelpers = require('./task-helpers')
class MarkMessageAsReadIMAP extends SyncbackTask {
description() {
return `MarkMessageAsRead`;
}
run(db, imap) {
const messageId = this.syncbackRequestObject().props.messageId
return TaskHelpers.openMessageBox({messageId, db, imap})
.then(({box, message}) => {
return box.addFlags(message.categoryImapUID, 'SEEN')
})
}
}
module.exports = MarkMessageAsReadIMAP;

View file

@ -0,0 +1,18 @@
const SyncbackTask = require('./syncback-task')
const TaskHelpers = require('./task-helpers')
class MarkMessageAsUnreadIMAP extends SyncbackTask {
description() {
return `MarkMessageAsUnread`;
}
run(db, imap) {
const messageId = this.syncbackRequestObject().props.messageId
return TaskHelpers.openMessageBox({messageId, db, imap})
.then(({box, message}) => {
return box.delFlags(message.categoryImapUID, 'SEEN')
})
}
}
module.exports = MarkMessageAsUnreadIMAP;

View file

@ -0,0 +1,21 @@
const SyncbackTask = require('./syncback-task')
const TaskHelpers = require('./task-helpers')
class MoveMessageToFolderIMAP extends SyncbackTask {
description() {
return `MoveMessageToFolder`;
}
run(db, imap) {
const messageId = this.syncbackRequestObject().props.messageId
const toFolderId = this.syncbackRequestObject().props.folderId
return TaskHelpers.openMessageBox({messageId, db, imap})
.then(({box, message}) => {
return db.Category.findById(toFolderId).then((newCategory) => {
return box.moveFromBox(message.categoryImapUID, newCategory.name)
})
})
}
}
module.exports = MoveMessageToFolderIMAP

View file

@ -0,0 +1,18 @@
const SyncbackTask = require('./syncback-task')
const TaskHelpers = require('./task-helpers')
class StarMessageIMAP extends SyncbackTask {
description() {
return `StarMessage`;
}
run(db, imap) {
const messageId = this.syncbackRequestObject().props.messageId
return TaskHelpers.openMessageBox({messageId, db, imap})
.then(({box, message}) => {
return box.addFlags(message.categoryImapUID, 'FLAGGED')
})
}
}
module.exports = StarMessageIMAP;

View file

@ -23,5 +23,15 @@ const TaskHelpers = {
) )
}) })
}, },
openMessageBox: function openMessageBox({messageId, db, imap}) {
return db.Message.findById(messageId).then((message) => {
return db.Category.findById(message.categoryId).then((category) => {
return imap.openBox(category.name).then((box) => {
return Promise.resolve({box, message})
})
})
})
},
} }
module.exports = TaskHelpers module.exports = TaskHelpers

View file

@ -0,0 +1,18 @@
const SyncbackTask = require('./syncback-task')
const TaskHelpers = require('./task-helpers')
class UnstarMessageIMAP extends SyncbackTask {
description() {
return `UnstarMessage`;
}
run(db, imap) {
const messageId = this.syncbackRequestObject().props.messageId
return TaskHelpers.openMessageBox({messageId, db, imap})
.then(({box, message}) => {
return box.delFlags(message.categoryImapUID, 'FLAGGED')
})
}
}
module.exports = UnstarMessageIMAP;