Mailspring/internal_packages/thread-snooze/lib/snooze-utils.es6
Juan Tejada d92506d8f1 fix(sync-status): Change old per model status, in favor of per folder
Summary:
Addresses T7275
Previously, we kept track of the sync status of each API model, and the progress
we'd made syncing all available models (e.g. all threads, messages, events, etc)

Given K2's set up, we are now keeping track of sync status per folder, i.e. what
percent of the folder's messages we've synced. This status is now reported from K2
to N1 via folder object deltas, and this commit rewrites the
NylasSyncStatusStore (in ES6) to reflect that.

The new Store keeps the sync state per account, which is the merged state of
per folder sync state, and delta connections state.
We also got rid of `CategoryStore.whenCategoriesReady` in favor of
`whenCategoryListSynced`, which is derived from the fact hat as long as we've
started syncing one folder, we've already synced the entire list of of folders/labels.

There are a couple of TODOs to be addressed in upcoming diffs:
- T7329 Restore the sidebar component to show sync progress, which was previously removed
- T7330 Figure out how to report sync progress per label, specifically, we are interested in knowing how much of the inbox we've synced, which is a label in Gmail. (This might be a non-issue if we sync the inbox very fast, first)

Depends on D3514

Test Plan: Manual

Reviewers: mark, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3515
2016-12-15 11:17:20 -08:00

128 lines
4 KiB
JavaScript

import moment from 'moment';
import _ from 'underscore';
import {
Actions,
Thread,
Category,
DateUtils,
TaskFactory,
AccountStore,
CategoryStore,
DatabaseStore,
SyncbackCategoryTask,
TaskQueueStatusStore,
NylasSyncStatusStore,
} from 'nylas-exports';
import {SNOOZE_CATEGORY_NAME} from './snooze-constants'
const {DATE_FORMAT_SHORT} = DateUtils
const SnoozeUtils = {
snoozedUntilMessage(snoozeDate, now = moment()) {
let message = 'Snoozed'
if (snoozeDate) {
let dateFormat = DATE_FORMAT_SHORT
const date = moment(snoozeDate)
const hourDifference = moment.duration(date.diff(now)).asHours()
if (hourDifference < 24) {
dateFormat = dateFormat.replace('MMM D, ', '');
}
if (date.minutes() === 0) {
dateFormat = dateFormat.replace(':mm', '');
}
message += ` until ${DateUtils.format(date, dateFormat)}`;
}
return message;
},
createSnoozeCategory(accountId, name = SNOOZE_CATEGORY_NAME) {
const category = new Category({
displayName: name,
accountId: accountId,
})
const task = new SyncbackCategoryTask({category})
Actions.queueTask(task)
return TaskQueueStatusStore.waitForPerformRemote(task).then(() => {
return DatabaseStore.findBy(Category, {clientId: category.clientId})
.then((updatedCat) => {
if (updatedCat && updatedCat.isSavedRemotely()) {
return Promise.resolve(updatedCat)
}
return Promise.reject(new Error('Could not create Snooze category'))
})
})
},
getSnoozeCategory(accountId, categoryName = SNOOZE_CATEGORY_NAME) {
return NylasSyncStatusStore.whenCategoryListSynced(accountId)
.then(() => {
const allCategories = CategoryStore.categories(accountId)
const category = _.findWhere(allCategories, {displayName: categoryName})
if (category) {
return Promise.resolve(category);
}
return SnoozeUtils.createSnoozeCategory(accountId, categoryName)
})
},
getSnoozeCategoriesByAccount(accounts = AccountStore.accounts()) {
const snoozeCategoriesByAccountId = {}
accounts.forEach(({id}) => {
if (snoozeCategoriesByAccountId[id] != null) return;
snoozeCategoriesByAccountId[id] = SnoozeUtils.getSnoozeCategory(id)
})
return Promise.props(snoozeCategoriesByAccountId)
},
moveThreads(threads, {snooze, getSnoozeCategory, getInboxCategory, description} = {}) {
const tasks = TaskFactory.tasksForApplyingCategories({
threads,
categoriesToRemove: snooze ? getInboxCategory : getSnoozeCategory,
categoriesToAdd: snooze ? getSnoozeCategory : getInboxCategory,
taskDescription: description,
})
Actions.queueTasks(tasks)
const promises = tasks.map(task => TaskQueueStatusStore.waitForPerformRemote(task))
// Resolve with the updated threads
return (
Promise.all(promises).then(() => {
return DatabaseStore.modelify(Thread, _.pluck(threads, 'clientId'))
})
)
},
moveThreadsToSnooze(threads, snoozeCategoriesByAccountPromise, snoozeDate) {
return snoozeCategoriesByAccountPromise
.then((snoozeCategoriesByAccountId) => {
const getSnoozeCategory = (accId) => [snoozeCategoriesByAccountId[accId]]
const getInboxCategory = (accId) => [CategoryStore.getInboxCategory(accId)]
const description = SnoozeUtils.snoozedUntilMessage(snoozeDate)
return SnoozeUtils.moveThreads(
threads,
{snooze: true, getSnoozeCategory, getInboxCategory, description}
)
})
},
moveThreadsFromSnooze(threads, snoozeCategoriesByAccountPromise) {
return snoozeCategoriesByAccountPromise
.then((snoozeCategoriesByAccountId) => {
const getSnoozeCategory = (accId) => [snoozeCategoriesByAccountId[accId]]
const getInboxCategory = (accId) => [CategoryStore.getInboxCategory(accId)]
const description = 'Unsnoozed';
return SnoozeUtils.moveThreads(
threads,
{snooze: false, getSnoozeCategory, getInboxCategory, description}
)
})
},
}
export default SnoozeUtils