2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
2016-02-26 02:37:03 +08:00
|
|
|
import moment from 'moment';
|
2016-02-19 02:00:11 +08:00
|
|
|
import _ from 'underscore';
|
|
|
|
import {
|
|
|
|
Actions,
|
|
|
|
Thread,
|
|
|
|
Category,
|
|
|
|
CategoryStore,
|
|
|
|
DatabaseStore,
|
|
|
|
AccountStore,
|
|
|
|
SyncbackCategoryTask,
|
|
|
|
TaskQueueStatusStore,
|
|
|
|
TaskFactory,
|
2016-02-26 02:37:03 +08:00
|
|
|
DateUtils,
|
2016-02-19 02:00:11 +08:00
|
|
|
} from 'nylas-exports';
|
2016-03-07 08:55:28 +08:00
|
|
|
import {SNOOZE_CATEGORY_NAME} from './snooze-constants'
|
|
|
|
|
|
|
|
const {DATE_FORMAT_SHORT} = DateUtils
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-02-26 05:06:01 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
const SnoozeUtils = {
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
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()
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
if (hourDifference < 24) {
|
|
|
|
dateFormat = dateFormat.replace('MMM D, ', '');
|
|
|
|
}
|
|
|
|
if (date.minutes() === 0) {
|
|
|
|
dateFormat = dateFormat.replace(':mm', '');
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
message += ` until ${DateUtils.format(date, dateFormat)}`;
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
},
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
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)
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
2016-03-04 04:37:20 +08:00
|
|
|
return Promise.reject(new Error('Could not create Snooze category'))
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
|
|
|
})
|
2016-03-04 04:37:20 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
whenCategoriesReady() {
|
|
|
|
const categoriesReady = ()=> CategoryStore.categories().length > 0
|
|
|
|
if (!categoriesReady()) {
|
|
|
|
return new Promise((resolve)=> {
|
|
|
|
const unsubscribe = CategoryStore.listen(()=> {
|
|
|
|
if (categoriesReady()) {
|
|
|
|
unsubscribe()
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
2016-03-04 04:37:20 +08:00
|
|
|
return Promise.resolve()
|
|
|
|
},
|
|
|
|
|
|
|
|
getSnoozeCategory(accountId, categoryName = SNOOZE_CATEGORY_NAME) {
|
|
|
|
return SnoozeUtils.whenCategoriesReady()
|
|
|
|
.then(()=> {
|
|
|
|
const allCategories = CategoryStore.categories(accountId)
|
|
|
|
const category = _.findWhere(allCategories, {displayName: categoryName})
|
|
|
|
if (category) {
|
|
|
|
return Promise.resolve(category);
|
2016-02-23 07:48:07 +08:00
|
|
|
}
|
2016-03-04 04:37:20 +08:00
|
|
|
return SnoozeUtils.createSnoozeCategory(accountId, categoryName)
|
2016-02-23 07:48:07 +08:00
|
|
|
})
|
2016-03-04 04:37:20 +08:00
|
|
|
},
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
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,
|
|
|
|
categoryToAdd: snooze ? getSnoozeCategory : getInboxCategory,
|
|
|
|
taskDescription: description,
|
|
|
|
})
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
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} = CategoryStore
|
|
|
|
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} = CategoryStore
|
|
|
|
const description = 'Unsnoozed';
|
|
|
|
return SnoozeUtils.moveThreads(
|
|
|
|
threads,
|
|
|
|
{snooze: false, getSnoozeCategory, getInboxCategory, description}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
export default SnoozeUtils
|