Added a syncback task to mark a thread as read, untested

This commit is contained in:
Halla Moore 2016-06-28 17:38:47 -07:00
parent b867bcb31e
commit f5af9c0cb5
3 changed files with 69 additions and 2 deletions

View file

@ -127,9 +127,26 @@ module.exports = (server) => {
type: "MoveToFolder",
props: {
folderId: request.params.folder_id,
threadId: requres.params.id,
}
threadId: request.params.id,
},
})
},
});
server.route({
method: 'POST',
path: '/threads/{id}/markread',
config: {
description: 'Mark a thread as read.',
tags: ['threads'],
handler: (request, reply) => {
createSyncbackRequest(request, reply, {
type: "MarkThreadAsRead",
props: {
threadId: request.params.id,
},
})
},
},
})
};

View file

@ -228,6 +228,13 @@ class IMAPConnection extends EventEmitter {
)
}
closeBox({expunge = true} = {}) {
if (!this._imap) {
throw new Error(`IMAPConnection::closeBox - You need to call connect() first.`)
}
return this._imap.closeBoxAsync(expunge)
}
getBoxes() {
if (!this._imap) {
throw new Error(`IMAPConnection::getBoxes - You need to call connect() first.`)
@ -235,6 +242,13 @@ class IMAPConnection extends EventEmitter {
return this._imap.getBoxesAsync()
}
addFlags(messageSrc, flags) {
if (!this._imap) {
throw new Error(`IMAPConnection::addFlags - You need to call connect() first.`)
}
return this._imap.addFlagsAsync(messageSrc, flags)
}
runOperation(operation) {
if (!this._imap) {
throw new Error(`IMAPConnection::runOperation - You need to call connect() first.`)

View file

@ -0,0 +1,36 @@
class MarkThreadAsRead {
constructor(account, syncbackRequest) {
this._account = account;
this._syncbackRequest = syncbackRequest;
}
description() {
return `MarkThreadAsRead`;
}
run(db, imap) {
const {Category, Thread} = db;
const thread = Thread.findById(this._syncbackRequest.threadId);
const msgsInCategories = {};
thread.getMessages((messages) => {
for (const message of messages) {
if (!msgsInCategories[messages.CategoryId]) {
msgsInCategories[messages.CategoryId] = [message.messageId];
} else {
msgsInCategories.push(message.messageId);
}
}
for (const categoryId of Object.keys(msgsInCategories)) {
Category.findById(categoryId).then((category) => {
imap.openBox(category, false);
for (const messageId of msgsInCategories[categoryId]) {
imap.addFlags(messageId, 'Seen', (err) => { throw err; });
}
imap.closeBox();
})
}
})
}
}
module.exports = MarkThreadAsRead;