Break Gmail mailboxes into folders and labels

This commit is contained in:
Ben Gotow 2016-06-28 18:31:36 -07:00
parent 7180ebbb70
commit 0189e3e77d
3 changed files with 21 additions and 12 deletions

View file

@ -13,6 +13,7 @@ function jsonSchema(modelName) {
if (modelName === 'Account') {
return Joi.object().keys({
id: Joi.number(),
object: Joi.string(),
email_address: Joi.string(),
connection_settings: Joi.object(),
sync_policy: Joi.object(),
@ -22,6 +23,7 @@ function jsonSchema(modelName) {
if (modelName === 'Category') {
return Joi.object().keys({
id: Joi.number(),
object: Joi.string(),
name: Joi.string().allow(null),
display_name: Joi.string(),
})

View file

@ -1,8 +1,23 @@
const {Provider} = require('nylas-core');
const GMAIL_FOLDERS = ['[Gmail]/All Mail', '[Gmail]/Trash', '[Gmail]/Spam'];
class FetchCategoryList {
constructor(provider) {
this._provider = provider;
}
description() {
return `FetchCategoryList`;
}
_typeForMailbox(boxName) {
if (this._provider === Provider.Gmail) {
return GMAIL_FOLDERS.includes(boxName) ? 'folder' : 'label';
}
return 'folder';
}
_roleForMailbox(boxName, box) {
for (const attrib of (box.attribs || [])) {
const role = {
@ -53,6 +68,7 @@ class FetchCategoryList {
if (!category) {
category = Category.build({
name: boxName,
type: this._typeForMailbox(boxName, box),
role: this._roleForMailbox(boxName, box),
});
created.push(category);

View file

@ -133,21 +133,12 @@ class SyncWorker {
const {Category} = this._db;
const {folderSyncOptions} = this._account.syncPolicy;
return Category.findAll().then((categories) => {
return Category.findAll({where: {type: 'folder'}}).then((categories) => {
const priority = ['inbox', 'all', 'drafts', 'sent', 'spam', 'trash'].reverse();
let categoriesToSync = categories.sort((a, b) =>
const categoriesToSync = categories.sort((a, b) =>
(priority.indexOf(a.role) - priority.indexOf(b.role)) * -1
)
if (this._account.provider === Provider.Gmail) {
categoriesToSync = categoriesToSync.filter(cat =>
['[Gmail]/All Mail', '[Gmail]/Trash', '[Gmail]/Spam'].includes(cat.name)
)
if (categoriesToSync.length !== 3) {
throw new Error(`Account is missing a core Gmail folder: ${categoriesToSync.join(',')}`)
}
}
return Promise.all(categoriesToSync.map((cat) =>
this._conn.runOperation(new FetchMessagesInCategory(cat, folderSyncOptions))
))
@ -155,7 +146,7 @@ class SyncWorker {
}
performSync() {
return this._conn.runOperation(new FetchCategoryList())
return this._conn.runOperation(new FetchCategoryList(this._account.provider))
.then(() => this.syncbackMessageActions())
.then(() => this.syncAllCategories())
}