Skip to content

Instantly share code, notes, and snippets.

@mcav
Created June 10, 2014 23:37
Show Gist options
  • Save mcav/809f322d171be6cc7d86 to your computer and use it in GitHub Desktop.
Save mcav/809f322d171be6cc7d86 to your computer and use it in GitHub Desktop.
/**
* Ensure that local-only folders live in a reasonable place in the
* folder hierarchy by moving them if necessary.
*
* We proactively create local-only folders at the root level before
* we synchronize with the server; if possible, we want these
* folders to reside as siblings to other system-level folders on
* the account. This is called at the end of syncFolderList, after
* we have learned about all existing server folders.
*/
_normalizeFolderHierarchy: function() {
// Find a folder for which we'd like to become a sibling.
var sibling =
this.getFirstFolderWithType('drafts') ||
this.getFirstFolderWithType('sent');
// If for some reason we can't find those folders yet, that's
// okay, we can try this again later.
if (!sibling) {
return;
}
var parent = this.getFolderMetaForFolderId(sibling.parentId);
// NOTE: `parent` may be null if `sibling` is a top-level folder.
var foldersToMove = [this.getFirstFolderWithType('localdrafts'),
this.getFirstFolderWithType('outbox')];
foldersToMove.forEach(function(folder) {
// These folders should always exist, but we double-check here
// for safety. Also, if the folder is already in the right
// place, we're done.
if (!folder || folder.parentId === sibling.parentId) {
return;
}
if (parent) {
folder.path = parent.path + parent.delim + folder.name;
folder.delim = parent.delim;
folder.parentId = parent.id;
} else {
folder.path = folder.name;
folder.delim = '';
folder.parentId = null;
}
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment