[client-sync] Don't assign duplicate roles in folder fetch

Summary:
Check that there aren't any other categories with a role before we try
to assign it to the current category.

Addresses T7835

Test Plan: manual

Reviewers: juan, evan, mark

Reviewed By: mark

Differential Revision: https://phab.nylas.com/D4298
This commit is contained in:
Halla Moore 2017-03-27 17:34:34 -07:00
parent 2f082f8beb
commit 7079b21b05

View file

@ -3,9 +3,10 @@ const SyncTask = require('./sync-task')
const {localizedCategoryNames} = require('../sync-utils')
const SyncActivity = require('../../shared/sync-activity').default
const GMAIL_ROLES_WITH_FOLDERS = ['all', 'trash', 'spam'];
const assignedRolesCache = new Set();
class FetchFolderListIMAP extends SyncTask {
constructor(...args) {
super(...args)
@ -23,6 +24,19 @@ class FetchFolderListIMAP extends SyncTask {
return Folder;
}
async _roleAlreadyAssigned(role) {
if (assignedRolesCache.has(role)) {
return true
}
const Klass = this._classForMailboxWithRole(role, this._db);
const existing = await Klass.findAll({where: {role: role}})
if (existing.length > 0) {
this.assignedRolesCache.add(role)
return true
}
return false
}
_detectRole(boxName, box) {
return this._roleByAttr(box) || this._roleByName(boxName);
}
@ -95,7 +109,10 @@ class FetchFolderListIMAP extends SyncTask {
let category = categories.find((cat) => cat.name === boxName);
if (!category) {
const role = this._detectRole(boxName, box);
let role = this._detectRole(boxName, box);
if (await this._roleAlreadyAssigned(role)) {
role = null;
}
const Klass = this._classForMailboxWithRole(role, this._db);
const {accountId} = this._db
category = Klass.build({
@ -109,7 +126,7 @@ class FetchFolderListIMAP extends SyncTask {
// if we update the category->role mapping to include more names, we
// need to be able to detect newly added roles on existing categories
const role = this._roleByName(boxName);
if (role) {
if (role && !(await this._roleAlreadyAssigned(role))) {
category.role = role;
await category.save();
}