Mailspring/internal_packages/send-later/lib/send-later-store.js
Ben Gotow 4fefec0b52 patch(save): Only save drafts when necessary, avoid sync-engine issues
Summary:
Previously, we have saved drafts back to the user's provider through the sync engine. There are a handful of very serious edge case issues we're working to solve that are creating a bad user experience. (#933, #1175, #1504, #1237)

For now, we're going to change the behavior of N1 to mitagate these issues.

- If you create a draft in N1, we will not sync it to other mail clients while you're working on it.
- If you enable send later, we'll start syncing the draft to the server as before.
- If you created the draft in another client, we'll sync the draft to the server as before.

Fix specs

Test Plan: Run specs

Reviewers: evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D2706
2016-03-10 11:03:38 -08:00

79 lines
2.1 KiB
JavaScript

/** @babel */
import NylasStore from 'nylas-store'
import {
NylasAPI,
Actions,
Message,
DatabaseStore,
SyncbackDraftTask,
} from 'nylas-exports'
import SendLaterActions from './send-later-actions'
import {PLUGIN_ID, PLUGIN_NAME} from './send-later-constants'
class SendLaterStore extends NylasStore {
constructor(pluginId = PLUGIN_ID, pluginName = PLUGIN_NAME) {
super()
this.pluginId = pluginId;
this.pluginName = pluginName;
}
activate() {
this.unsubscribers = [
SendLaterActions.sendLater.listen(this.onSendLater),
SendLaterActions.cancelSendLater.listen(this.onCancelSendLater),
];
}
setMetadata = (draftClientId, metadata)=> {
return DatabaseStore.modelify(Message, [draftClientId])
.then((messages)=> {
const {accountId} = messages[0];
return NylasAPI.authPlugin(this.pluginId, this.pluginName, accountId)
.then(()=> {
Actions.setMetadata(messages, this.pluginId, metadata);
// Important: Do not remove this unless N1 is syncing drafts by default.
Actions.queueTask(new SyncbackDraftTask(draftClientId));
})
.catch((error)=> {
NylasEnv.reportError(error);
NylasEnv.showErrorDialog(`Sorry, we were unable to schedule this message. ${error.message}`);
});
});
};
recordAction(sendLaterDate, dateLabel) {
try {
if (sendLaterDate) {
const min = Math.round(((new Date(sendLaterDate)).valueOf() - Date.now()) / 1000 / 60);
Actions.recordUserEvent("Send Later", {
sendLaterTime: min,
optionLabel: dateLabel,
});
} else {
Actions.recordUserEvent("Send Later Cancel");
}
} catch (e) {
// Do nothing
}
}
onSendLater = (draftClientId, sendLaterDate, dateLabel)=> {
this.recordAction(sendLaterDate, dateLabel)
this.setMetadata(draftClientId, {sendLaterDate});
};
onCancelSendLater = (draftClientId)=> {
this.recordAction(null)
this.setMetadata(draftClientId, {sendLaterDate: null});
};
deactivate = ()=> {
this.unsubscribers.forEach(unsub => unsub());
};
}
export default SendLaterStore