mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-01-08 01:04:39 +08:00
aff5505743
- This error was still being thrown for a large number of accounts, despite the fact that the N1-Snoozed category was being created - This error was happening when adding a new account which did not already have a snooze category in the backend: - When the account is added the AccountStore triggers more than once, causing the SnoozeStore to send more than one request to create the same category. Any request after the first one will fail because the category already exists - The fix is not try to create a category unless the accounts have actually changed
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
import _ from 'underscore';
|
|
import {Actions, NylasAPI, AccountStore, CategoryStore} from 'nylas-exports';
|
|
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')
|
|
this.snoozeCategoriesPromise = SnoozeUtils.getSnoozeCategoriesByAccount(AccountStore.accounts())
|
|
}
|
|
|
|
activate() {
|
|
this.unsubscribers = [
|
|
AccountStore.listen(this.onAccountsChanged),
|
|
SnoozeActions.snoozeThreads.listen(this.onSnoozeThreads),
|
|
]
|
|
}
|
|
|
|
recordSnoozeEvent(threads, snoozeDate, label) {
|
|
try {
|
|
const min = Math.round(((new Date(snoozeDate)).valueOf() - Date.now()) / 1000 / 60);
|
|
Actions.recordUserEvent("Snooze Threads", {
|
|
numThreads: threads.length,
|
|
snoozeTime: min,
|
|
buttonType: label,
|
|
});
|
|
} catch (e) {
|
|
// Do nothing
|
|
}
|
|
}
|
|
|
|
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);
|
|
};
|
|
|
|
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)
|
|
const promises = accounts.map((acc) => {
|
|
return NylasAPI.authPlugin(this.pluginId, this.pluginName, acc)
|
|
})
|
|
return Promise.all(promises)
|
|
.then(() => {
|
|
return SnoozeUtils.moveThreadsToSnooze(threads, this.snoozeCategoriesPromise, snoozeDate)
|
|
})
|
|
.then((updatedThreads) => {
|
|
return this.snoozeCategoriesPromise
|
|
.then(snoozeCategories => this.groupUpdatedThreads(updatedThreads, snoozeCategories))
|
|
})
|
|
.then((updatedThreadsByAccountId) => {
|
|
_.each(updatedThreadsByAccountId, (update) => {
|
|
const {snoozeCategoryId, returnCategoryId} = update;
|
|
Actions.setMetadata(update.threads, this.pluginId, {snoozeDate, snoozeCategoryId, returnCategoryId})
|
|
})
|
|
})
|
|
.catch((error) => {
|
|
SnoozeUtils.moveThreadsFromSnooze(threads, this.snoozeCategoriesPromise)
|
|
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;
|