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)
|
2016-05-07 07:23:48 +08:00
|
|
|
return TaskQueueStatusStore.waitForPerformRemote(task).then(() => {
|
2016-03-04 04:37:20 +08:00
|
|
|
return DatabaseStore.findBy(Category, {clientId: category.clientId})
|
2016-05-06 13:30:34 +08:00
|
|
|
.then((updatedCat) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
getSnoozeCategory(accountId, categoryName = SNOOZE_CATEGORY_NAME) {
|
2016-03-15 06:34:16 +08:00
|
|
|
return CategoryStore.whenCategoriesReady(accountId)
|
2016-05-06 13:30:34 +08:00
|
|
|
.then(() => {
|
2016-03-04 04:37:20 +08:00
|
|
|
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 = {}
|
2016-05-06 13:30:34 +08:00
|
|
|
accounts.forEach(({id}) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
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,
|
2016-03-08 10:13:53 +08:00
|
|
|
categoriesToAdd: snooze ? getSnoozeCategory : getInboxCategory,
|
2016-03-04 04:37:20 +08:00
|
|
|
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 (
|
2016-05-06 13:30:34 +08:00
|
|
|
Promise.all(promises).then(() => {
|
2016-03-04 04:37:20 +08:00
|
|
|
return DatabaseStore.modelify(Thread, _.pluck(threads, 'clientId'))
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
moveThreadsToSnooze(threads, snoozeCategoriesByAccountPromise, snoozeDate) {
|
|
|
|
return snoozeCategoriesByAccountPromise
|
2016-05-06 13:30:34 +08:00
|
|
|
.then((snoozeCategoriesByAccountId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
const getSnoozeCategory = (accId) => [snoozeCategoriesByAccountId[accId]]
|
2016-03-11 09:45:50 +08:00
|
|
|
const getInboxCategory = (accId) => [CategoryStore.getInboxCategory(accId)]
|
2016-03-04 04:37:20 +08:00
|
|
|
const description = SnoozeUtils.snoozedUntilMessage(snoozeDate)
|
|
|
|
return SnoozeUtils.moveThreads(
|
|
|
|
threads,
|
|
|
|
{snooze: true, getSnoozeCategory, getInboxCategory, description}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
moveThreadsFromSnooze(threads, snoozeCategoriesByAccountPromise) {
|
|
|
|
return snoozeCategoriesByAccountPromise
|
2016-05-06 13:30:34 +08:00
|
|
|
.then((snoozeCategoriesByAccountId) => {
|
2016-03-08 10:13:53 +08:00
|
|
|
const getSnoozeCategory = (accId) => [snoozeCategoriesByAccountId[accId]]
|
2016-03-11 09:45:50 +08:00
|
|
|
const getInboxCategory = (accId) => [CategoryStore.getInboxCategory(accId)]
|
2016-03-04 04:37:20 +08:00
|
|
|
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
|