2016-02-23 07:48:07 +08:00
|
|
|
|
import _ from 'underscore';
|
2017-02-09 06:13:01 +08:00
|
|
|
|
import {React, FeatureUsageStore, Actions, AccountStore,
|
2017-02-04 11:27:56 +08:00
|
|
|
|
DatabaseStore, Message, CategoryStore} from 'nylas-exports';
|
2017-02-08 04:46:57 +08:00
|
|
|
|
import {FeatureUsedUpModal} from 'nylas-component-kit'
|
2016-05-06 07:22:44 +08:00
|
|
|
|
import SnoozeUtils 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-05-17 06:42:29 +08:00
|
|
|
|
this.accountIds = _.pluck(AccountStore.accounts(), 'id')
|
2016-05-06 07:22:44 +08:00
|
|
|
|
this.snoozeCategoriesPromise = SnoozeUtils.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-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,
|
2016-02-26 04:33:50 +08:00
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
2016-03-04 04:37:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupUpdatedThreads = (threads, snoozeCategoriesByAccount) => {
|
2016-05-06 13:30:34 +08:00
|
|
|
|
const getSnoozeCategory = (accId) => snoozeCategoriesByAccount[accId]
|
2016-03-04 04:37:20 +08:00
|
|
|
|
const {getInboxCategory} = CategoryStore
|
|
|
|
|
const threadsByAccountId = {}
|
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
|
threads.forEach((thread) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
|
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 = () => {
|
2016-05-17 06:42:29 +08:00
|
|
|
|
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())
|
|
|
|
|
}
|
2016-03-15 06:34:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
|
onSnoozeThreads = (threads, snoozeDate, label) => {
|
2017-02-04 11:27:56 +08:00
|
|
|
|
if (!FeatureUsageStore.isUsable("snooze")) {
|
2017-02-08 04:46:57 +08:00
|
|
|
|
const featureData = FeatureUsageStore.featureData("snooze");
|
|
|
|
|
|
|
|
|
|
let headerText = "";
|
|
|
|
|
let rechargeText = ""
|
|
|
|
|
if (!featureData.quota) {
|
|
|
|
|
headerText = "Snooze not yet enabled";
|
|
|
|
|
rechargeText = "Upgrade to Pro to start Snoozing"
|
|
|
|
|
} else {
|
|
|
|
|
headerText = "All Snoozes used";
|
|
|
|
|
let time = "later";
|
|
|
|
|
if (featureData.period === "hourly") {
|
|
|
|
|
time = "next hour"
|
|
|
|
|
} else if (featureData.period === "daily") {
|
|
|
|
|
time = "tomorrow"
|
|
|
|
|
} else if (featureData.period === "weekly") {
|
|
|
|
|
time = "next week"
|
|
|
|
|
} else if (featureData.period === "monthly") {
|
|
|
|
|
time = "next month"
|
|
|
|
|
} else if (featureData.period === "yearly") {
|
|
|
|
|
time = "next year"
|
|
|
|
|
} else if (featureData.period === "unlimited") {
|
|
|
|
|
time = "if you upgrade to Pro"
|
|
|
|
|
}
|
|
|
|
|
rechargeText = `You’ll have ${featureData.quota} more snoozes ${time}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Actions.openModal({
|
|
|
|
|
component: (
|
|
|
|
|
<FeatureUsedUpModal
|
|
|
|
|
modalClass="snooze"
|
|
|
|
|
featureName="Snooze"
|
|
|
|
|
headerText={headerText}
|
|
|
|
|
iconUrl="nylas://thread-snooze/assets/ic-snooze-modal@2x.png"
|
|
|
|
|
rechargeText={rechargeText}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
height: 575,
|
|
|
|
|
width: 412,
|
|
|
|
|
})
|
2017-02-04 11:27:56 +08:00
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
2016-05-07 04:42:09 +08:00
|
|
|
|
this.recordSnoozeEvent(threads, snoozeDate, label)
|
2016-02-26 04:33:50 +08:00
|
|
|
|
|
2017-02-09 06:13:01 +08:00
|
|
|
|
return FeatureUsageStore.useFeature('snooze')
|
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-03-04 04:37:20 +08:00
|
|
|
|
})
|
2016-05-06 13:30:34 +08:00
|
|
|
|
.then((updatedThreads) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
|
return this.snoozeCategoriesPromise
|
|
|
|
|
.then(snoozeCategories => this.groupUpdatedThreads(updatedThreads, snoozeCategories))
|
2016-02-19 02:00:11 +08:00
|
|
|
|
})
|
2016-05-06 13:30:34 +08:00
|
|
|
|
.then((updatedThreadsByAccountId) => {
|
|
|
|
|
_.each(updatedThreadsByAccountId, (update) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
|
const {snoozeCategoryId, returnCategoryId} = update;
|
2017-02-03 04:17:25 +08:00
|
|
|
|
|
|
|
|
|
// Get messages for those threads and metadata for those.
|
|
|
|
|
DatabaseStore.findAll(Message, {threadId: update.threads.map(t => t.id)}).then((messages) => {
|
|
|
|
|
for (const message of messages) {
|
|
|
|
|
const header = message.messageIdHeader;
|
|
|
|
|
const stableId = message.id;
|
|
|
|
|
Actions.setMetadata(message, this.pluginId,
|
2017-02-07 09:22:07 +08:00
|
|
|
|
{expiration: snoozeDate, header, stableId, snoozeCategoryId, returnCategoryId})
|
2017-02-03 04:17:25 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-19 02:00:11 +08:00
|
|
|
|
})
|
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();
|
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;
|