2017-08-17 03:45:38 +08:00
|
|
|
import _ from 'underscore';
|
|
|
|
import NylasStore from 'nylas-store';
|
|
|
|
|
|
|
|
import {
|
|
|
|
FeatureUsageStore,
|
|
|
|
SyncbackMetadataTask,
|
|
|
|
Actions,
|
|
|
|
DatabaseStore,
|
|
|
|
Message,
|
|
|
|
CategoryStore,
|
|
|
|
} from 'nylas-exports';
|
|
|
|
|
|
|
|
import SnoozeUtils from './snooze-utils'
|
|
|
|
import {PLUGIN_ID, PLUGIN_NAME} from './snooze-constants';
|
|
|
|
import SnoozeActions from './snooze-actions';
|
|
|
|
|
|
|
|
class SnoozeStore extends NylasStore {
|
|
|
|
|
|
|
|
constructor(pluginId = PLUGIN_ID, pluginName = PLUGIN_NAME) {
|
|
|
|
super();
|
|
|
|
this.pluginId = pluginId;
|
|
|
|
this.pluginName = pluginName;
|
|
|
|
}
|
|
|
|
|
|
|
|
activate() {
|
|
|
|
this.unsubscribers = [
|
|
|
|
SnoozeActions.snoozeThreads.listen(this.onSnoozeThreads),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
this.unsubscribers.forEach(unsub => unsub())
|
|
|
|
}
|
|
|
|
|
|
|
|
recordSnoozeEvent(threads, snoozeDate, label) {
|
|
|
|
try {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:05 +08:00
|
|
|
groupUpdatedThreads = (threads) => {
|
2017-08-17 03:45:38 +08:00
|
|
|
const threadsByAccountId = {}
|
|
|
|
|
|
|
|
threads.forEach((thread) => {
|
|
|
|
const accId = thread.accountId
|
|
|
|
if (!threadsByAccountId[accId]) {
|
2017-08-29 14:50:05 +08:00
|
|
|
threadsByAccountId[accId] = [thread];
|
2017-08-17 03:45:38 +08:00
|
|
|
} else {
|
|
|
|
threadsByAccountId[accId].threads.push(thread);
|
|
|
|
}
|
|
|
|
});
|
2017-08-23 12:49:45 +08:00
|
|
|
return threadsByAccountId;
|
2017-08-17 03:45:38 +08:00
|
|
|
};
|
|
|
|
|
2017-08-29 14:50:05 +08:00
|
|
|
onSnoozeThreads = async (allThreads, snoozeDate, label) => {
|
2017-08-17 03:45:38 +08:00
|
|
|
const lexicon = {
|
|
|
|
displayName: "Snooze",
|
|
|
|
usedUpHeader: "All Snoozes used",
|
2017-08-17 04:31:26 +08:00
|
|
|
iconUrl: "merani://thread-snooze/assets/ic-snooze-modal@2x.png",
|
2017-08-17 03:45:38 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 12:49:45 +08:00
|
|
|
try {
|
|
|
|
// ensure the user is authorized to use this feature
|
|
|
|
await FeatureUsageStore.asyncUseFeature('snooze', {lexicon});
|
|
|
|
|
|
|
|
// log to analytics
|
2017-08-29 14:50:05 +08:00
|
|
|
this.recordSnoozeEvent(allThreads, snoozeDate, label);
|
2017-08-23 12:49:45 +08:00
|
|
|
|
2017-08-29 14:50:05 +08:00
|
|
|
const updatedThreads = await SnoozeUtils.moveThreadsToSnooze(allThreads, snoozeDate);
|
|
|
|
const updatedThreadsByAccountId = this.groupUpdatedThreads(updatedThreads);
|
2017-08-23 12:49:45 +08:00
|
|
|
|
|
|
|
// note we don't wait for this to complete currently
|
2017-08-29 14:50:05 +08:00
|
|
|
Object.values(updatedThreadsByAccountId).map(async (threads) => {
|
2017-08-17 03:45:38 +08:00
|
|
|
// Get messages for those threads and metadata for those.
|
2017-08-23 12:49:45 +08:00
|
|
|
const messages = await DatabaseStore.findAll(Message, {
|
2017-08-29 14:50:05 +08:00
|
|
|
threadId: threads.map(t => t.id),
|
2017-08-17 03:45:38 +08:00
|
|
|
});
|
2017-08-23 12:49:45 +08:00
|
|
|
|
|
|
|
for (const message of messages) {
|
|
|
|
Actions.queueTask(new SyncbackMetadataTask({
|
|
|
|
model: message,
|
|
|
|
accountId: message.accountId,
|
|
|
|
pluginId: this.pluginId,
|
|
|
|
value: {
|
|
|
|
expiration: snoozeDate,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
2017-08-17 03:45:38 +08:00
|
|
|
});
|
2017-08-23 12:49:45 +08:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof FeatureUsageStore.NoProAccessError) {
|
|
|
|
return;
|
2017-08-17 03:45:38 +08:00
|
|
|
}
|
2017-08-29 14:50:05 +08:00
|
|
|
SnoozeUtils.moveThreadsFromSnooze(allThreads);
|
2017-08-17 03:45:38 +08:00
|
|
|
Actions.closePopover();
|
|
|
|
NylasEnv.reportError(error);
|
|
|
|
NylasEnv.showErrorDialog(`Sorry, we were unable to save your snooze settings. ${error.message}`);
|
2017-08-23 12:49:45 +08:00
|
|
|
}
|
2017-08-17 03:45:38 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-23 12:49:45 +08:00
|
|
|
export default new SnoozeStore();
|
|
|
|
|