2017-09-07 07:19:48 +08:00
|
|
|
import {
|
|
|
|
Thread,
|
|
|
|
Message,
|
|
|
|
Actions,
|
|
|
|
DatabaseStore,
|
|
|
|
FeatureUsageStore,
|
|
|
|
SyncbackMetadataTask,
|
|
|
|
} from 'nylas-exports'
|
|
|
|
|
2017-09-26 06:44:20 +08:00
|
|
|
import {PLUGIN_ID} from './send-reminders-constants'
|
2017-09-07 07:19:48 +08:00
|
|
|
|
2017-09-26 06:44:20 +08:00
|
|
|
const FEATURE_LEXICON = {
|
|
|
|
usedUpHeader: "All Reminders Used",
|
|
|
|
usagePhrase: "add reminders to",
|
|
|
|
iconUrl: "mailspring://send-reminders/assets/ic-send-reminders-modal@2x.png",
|
|
|
|
};
|
2017-09-07 07:19:48 +08:00
|
|
|
|
2017-09-26 06:44:20 +08:00
|
|
|
export function reminderDateFor(draftOrThread) {
|
|
|
|
return ((draftOrThread && draftOrThread.metadataForPluginId(PLUGIN_ID)) || {}).expiration;
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 06:44:20 +08:00
|
|
|
async function incrementMetadataUse(model, expiration) {
|
|
|
|
if (reminderDateFor(model)) {
|
|
|
|
return true;
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
2017-09-26 06:44:20 +08:00
|
|
|
try {
|
|
|
|
await FeatureUsageStore.asyncUseFeature(PLUGIN_ID, FEATURE_LEXICON)
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof FeatureUsageStore.NoProAccessError) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
2017-09-26 06:44:20 +08:00
|
|
|
if (expiration) {
|
|
|
|
const seconds = Math.round(((new Date(expiration)).getTime() - Date.now()) / 1000)
|
|
|
|
Actions.recordUserEvent("Set Reminder", {
|
|
|
|
seconds: seconds,
|
|
|
|
secondsLog10: Math.log10(seconds),
|
|
|
|
});
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
2017-09-26 06:44:20 +08:00
|
|
|
return true;
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:50:34 +08:00
|
|
|
function assertMetadataShape(value) {
|
|
|
|
const t = Object.assign({}, value);
|
|
|
|
if (t.expiration && !(t.expiration instanceof Date)) {
|
|
|
|
throw new Error(`"expiration" should always be absent or a date. Received ${t.expiration}`);
|
|
|
|
}
|
|
|
|
if (t.lastReplyTimestamp && !(t.lastReplyTimestamp < Date.now() / 100)) {
|
|
|
|
throw new Error(`"lastReplyTimestamp" should always be a unix timestamp in seconds. Received ${t.lastReplyTimestamp}`);
|
|
|
|
}
|
|
|
|
delete t.expiration;
|
|
|
|
delete t.shouldNotify;
|
|
|
|
delete t.sentHeaderMessageId;
|
|
|
|
delete t.lastReplyTimestamp;
|
|
|
|
if (Object.keys(t).length > 0) {
|
|
|
|
throw new Error(`Unexpected keys in metadata: ${Object.keys(t)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateReminderMetadata(thread, metadataValue) {
|
|
|
|
assertMetadataShape(metadataValue);
|
|
|
|
|
|
|
|
if (!await incrementMetadataUse(thread, metadataValue.expiration)) {
|
2017-09-07 07:19:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Actions.queueTask(new SyncbackMetadataTask({
|
2017-09-26 06:44:20 +08:00
|
|
|
model: thread,
|
|
|
|
accountId: thread.accountId,
|
2017-09-07 07:19:48 +08:00
|
|
|
pluginId: PLUGIN_ID,
|
2017-09-26 14:50:34 +08:00
|
|
|
value: metadataValue,
|
2017-09-07 07:19:48 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-09-26 14:50:34 +08:00
|
|
|
export async function updateDraftReminderMetadata(draftSession, metadataValue) {
|
|
|
|
assertMetadataShape(metadataValue);
|
|
|
|
|
|
|
|
if (!await incrementMetadataUse(draftSession.draft(), metadataValue.expiration)) {
|
2017-09-26 06:44:20 +08:00
|
|
|
return;
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
2017-09-26 06:44:20 +08:00
|
|
|
draftSession.changes.add({pristine: false})
|
2017-09-26 14:50:34 +08:00
|
|
|
draftSession.changes.addPluginMetadata(PLUGIN_ID, metadataValue);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:50:34 +08:00
|
|
|
export async function transferReminderMetadataFromDraftToThread({accountId, headerMessageId}) {
|
|
|
|
const message = await DatabaseStore.findBy(Message, {accountId, headerMessageId})
|
2017-09-26 06:44:20 +08:00
|
|
|
if (!message) {
|
|
|
|
throw new Error("SendReminders: Could not find message to update")
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 06:44:20 +08:00
|
|
|
const metadata = message.metadataForPluginId(PLUGIN_ID) || {}
|
|
|
|
if (!metadata || !metadata.expiration) {
|
|
|
|
return;
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
2017-09-26 06:44:20 +08:00
|
|
|
|
|
|
|
const thread = await DatabaseStore.find(Thread, message.threadId);
|
|
|
|
updateReminderMetadata(thread, {
|
|
|
|
expiration: metadata.expiration,
|
|
|
|
sentHeaderMessageId: metadata.sentHeaderMessageId,
|
|
|
|
lastReplyTimestamp: new Date(thread.lastMessageReceivedTimestamp).getTime() / 1000,
|
|
|
|
shouldNotify: false,
|
|
|
|
});
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|