2016-02-19 02:00:11 +08:00
|
|
|
/** @babel */
|
2016-02-23 07:48:07 +08:00
|
|
|
import _ from 'underscore';
|
2016-03-04 04:37:20 +08:00
|
|
|
import {Actions, NylasAPI, AccountStore, CategoryStore} from 'nylas-exports';
|
|
|
|
import {
|
|
|
|
moveThreadsToSnooze,
|
|
|
|
moveThreadsFromSnooze,
|
|
|
|
getSnoozeCategoriesByAccount,
|
|
|
|
} from './snooze-utils';
|
2016-02-19 02:00:11 +08:00
|
|
|
import {PLUGIN_ID, PLUGIN_NAME} from './snooze-constants';
|
|
|
|
import SnoozeActions from './snooze-actions';
|
|
|
|
|
|
|
|
|
|
|
|
class SnoozeStore {
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
constructor(pluginId = PLUGIN_ID, pluginName = PLUGIN_NAME) {
|
2016-02-19 02:00:11 +08:00
|
|
|
this.pluginId = pluginId
|
2016-03-04 04:37:20 +08:00
|
|
|
this.pluginName = pluginName
|
2016-03-15 06:34:16 +08:00
|
|
|
this.snoozeCategoriesPromise = getSnoozeCategoriesByAccount(AccountStore.accounts())
|
2016-03-04 04:37:20 +08:00
|
|
|
}
|
2016-02-19 02:00:11 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
activate() {
|
2016-03-15 06:34:16 +08:00
|
|
|
this.unsubscribers = [
|
|
|
|
AccountStore.listen(this.onAccountsChanged),
|
|
|
|
SnoozeActions.snoozeThreads.listen(this.onSnoozeThreads),
|
|
|
|
]
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
recordSnoozeEvent(threads, snoozeDate, label) {
|
2016-02-26 04:33:50 +08:00
|
|
|
try {
|
2016-02-26 06:01:00 +08:00
|
|
|
const min = Math.round(((new Date(snoozeDate)).valueOf() - Date.now()) / 1000 / 60);
|
2016-02-26 04:33:50 +08:00
|
|
|
Actions.recordUserEvent("Snooze Threads", {
|
|
|
|
numThreads: threads.length,
|
2016-02-26 06:01:00 +08:00
|
|
|
snoozeTime: min,
|
|
|
|
buttonType: label,
|
2016-02-26 04:33:50 +08:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2016-03-04 04:37:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
groupUpdatedThreads = (threads, snoozeCategoriesByAccount) => {
|
|
|
|
const getSnoozeCategory = (accId)=> snoozeCategoriesByAccount[accId]
|
|
|
|
const {getInboxCategory} = CategoryStore
|
|
|
|
const threadsByAccountId = {}
|
|
|
|
|
|
|
|
threads.forEach((thread)=> {
|
|
|
|
const accId = thread.accountId
|
|
|
|
if (!threadsByAccountId[accId]) {
|
|
|
|
threadsByAccountId[accId] = {
|
|
|
|
threads: [thread],
|
|
|
|
snoozeCategoryId: getSnoozeCategory(accId).serverId,
|
|
|
|
returnCategoryId: getInboxCategory(accId).serverId,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
threadsByAccountId[accId].threads.push(thread);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Promise.resolve(threadsByAccountId);
|
|
|
|
};
|
|
|
|
|
2016-03-15 06:34:16 +08:00
|
|
|
onAccountsChanged = ()=> {
|
|
|
|
this.snoozeCategoriesPromise = getSnoozeCategoriesByAccount(AccountStore.accounts())
|
|
|
|
};
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
onSnoozeThreads = (threads, snoozeDate, label) => {
|
|
|
|
this.recordSnoozeEvent(threads, label)
|
2016-02-26 04:33:50 +08:00
|
|
|
|
2016-02-19 02:00:11 +08:00
|
|
|
const accounts = AccountStore.accountsForItems(threads)
|
|
|
|
const promises = accounts.map((acc)=> {
|
2016-03-04 04:37:20 +08:00
|
|
|
return NylasAPI.authPlugin(this.pluginId, this.pluginName, acc)
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
2016-03-04 04:37:20 +08:00
|
|
|
return Promise.all(promises)
|
2016-02-19 02:00:11 +08:00
|
|
|
.then(()=> {
|
2016-03-04 04:37:20 +08:00
|
|
|
return moveThreadsToSnooze(threads, this.snoozeCategoriesPromise, snoozeDate)
|
|
|
|
})
|
|
|
|
.then((updatedThreads)=> {
|
|
|
|
return this.snoozeCategoriesPromise
|
|
|
|
.then(snoozeCategories => this.groupUpdatedThreads(updatedThreads, snoozeCategories))
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
2016-02-23 07:48:07 +08:00
|
|
|
.then((updatedThreadsByAccountId)=> {
|
|
|
|
_.each(updatedThreadsByAccountId, (update)=> {
|
2016-03-04 04:37:20 +08:00
|
|
|
const {snoozeCategoryId, returnCategoryId} = update;
|
|
|
|
Actions.setMetadata(update.threads, this.pluginId, {snoozeDate, snoozeCategoryId, returnCategoryId})
|
2016-02-23 07:48:07 +08:00
|
|
|
})
|
2016-02-19 02:00:11 +08:00
|
|
|
})
|
|
|
|
.catch((error)=> {
|
2016-03-04 04:37:20 +08:00
|
|
|
moveThreadsFromSnooze(threads, this.snoozeCategoriesPromise)
|
2016-02-25 07:11:03 +08:00
|
|
|
Actions.closePopover();
|
2016-02-24 09:51:10 +08:00
|
|
|
NylasEnv.reportError(error);
|
|
|
|
NylasEnv.showErrorDialog(`Sorry, we were unable to save your snooze settings. ${error.message}`);
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
deactivate() {
|
2016-03-15 06:34:16 +08:00
|
|
|
this.unsubscribers.forEach(unsub => unsub())
|
2016-02-19 02:00:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SnoozeStore;
|