Mailspring/internal_packages/thread-snooze/lib/snooze-utils.js
Juan Tejada cfbbcd23d3 fix(snooze): Correctly query and create snooze categories per account
Summary:
- Was not properly updating the references to snoozed categories when
  accounts were added or removed
- Update whenCategoriesReady to make sure we listen until category syncing has concluded (Move inside CategoryStore)
- #1676, #1658

Test Plan: - TODO

Reviewers: evan, drew, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2723
2016-03-14 15:36:39 -07:00

128 lines
4 KiB
JavaScript

/** @babel */
import moment from 'moment';
import _ from 'underscore';
import {
Actions,
Thread,
Category,
CategoryStore,
DatabaseStore,
AccountStore,
SyncbackCategoryTask,
TaskQueueStatusStore,
TaskFactory,
DateUtils,
} 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 CategoryStore.whenCategoriesReady(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