Mailspring/internal_packages/thread-snooze/lib/snooze-store.es6

104 lines
3.4 KiB
Text
Raw Normal View History

import _ from 'underscore';
import {Actions, NylasAPI, AccountStore, CategoryStore} from 'nylas-exports';
2016-05-06 07:22:44 +08:00
import SnoozeUtils from './snooze-utils'
import {PLUGIN_ID, PLUGIN_NAME} from './snooze-constants';
import SnoozeActions from './snooze-actions';
class SnoozeStore {
constructor(pluginId = PLUGIN_ID, pluginName = PLUGIN_NAME) {
this.pluginId = pluginId
this.pluginName = pluginName
this.accountIds = _.pluck(AccountStore.accounts(), 'id')
2016-05-06 07:22:44 +08:00
this.snoozeCategoriesPromise = SnoozeUtils.getSnoozeCategoriesByAccount(AccountStore.accounts())
}
activate() {
this.unsubscribers = [
AccountStore.listen(this.onAccountsChanged),
SnoozeActions.snoozeThreads.listen(this.onSnoozeThreads),
]
}
recordSnoozeEvent(threads, snoozeDate, label) {
try {
2016-06-08 03:53:05 +08:00
const timeInSec = Math.round(((new Date(snoozeDate)).valueOf() - Date.now()) / 1000);
Actions.recordUserEvent("Threads Snoozed", {
timeInSec: timeInSec,
timeInLog10Sec: Math.log10(timeInSec),
label: label,
numItems: threads.length,
});
} catch (e) {
// Do nothing
}
}
groupUpdatedThreads = (threads, snoozeCategoriesByAccount) => {
2016-05-06 13:30:34 +08:00
const getSnoozeCategory = (accId) => snoozeCategoriesByAccount[accId]
const {getInboxCategory} = CategoryStore
const threadsByAccountId = {}
2016-05-06 13:30:34 +08:00
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-05-06 13:30:34 +08:00
onAccountsChanged = () => {
const nextIds = _.pluck(AccountStore.accounts(), 'id')
const isSameAccountIds = (
this.accountIds.length === nextIds.length &&
this.accountIds.length === _.intersection(this.accountIds, nextIds).length
)
if (!isSameAccountIds) {
this.accountIds = nextIds
this.snoozeCategoriesPromise = SnoozeUtils.getSnoozeCategoriesByAccount(AccountStore.accounts())
}
};
onSnoozeThreads = (threads, snoozeDate, label) => {
this.recordSnoozeEvent(threads, snoozeDate, label)
const accounts = AccountStore.accountsForItems(threads)
2016-05-06 13:30:34 +08:00
const promises = accounts.map((acc) => {
return NylasAPI.authPlugin(this.pluginId, this.pluginName, acc)
})
return Promise.all(promises)
2016-05-06 13:30:34 +08:00
.then(() => {
2016-05-06 07:22:44 +08:00
return SnoozeUtils.moveThreadsToSnooze(threads, this.snoozeCategoriesPromise, snoozeDate)
})
2016-05-06 13:30:34 +08:00
.then((updatedThreads) => {
return this.snoozeCategoriesPromise
.then(snoozeCategories => this.groupUpdatedThreads(updatedThreads, snoozeCategories))
})
2016-05-06 13:30:34 +08:00
.then((updatedThreadsByAccountId) => {
_.each(updatedThreadsByAccountId, (update) => {
const {snoozeCategoryId, returnCategoryId} = update;
Actions.setMetadata(update.threads, this.pluginId, {snoozeDate, snoozeCategoryId, returnCategoryId})
})
})
2016-05-06 13:30:34 +08:00
.catch((error) => {
2016-05-06 07:22:44 +08:00
SnoozeUtils.moveThreadsFromSnooze(threads, this.snoozeCategoriesPromise)
2016-02-25 07:11:03 +08:00
Actions.closePopover();
NylasEnv.reportError(error);
NylasEnv.showErrorDialog(`Sorry, we were unable to save your snooze settings. ${error.message}`);
});
};
deactivate() {
this.unsubscribers.forEach(unsub => unsub())
}
}
export default SnoozeStore;