From 3767d056ef2b2ba43331e42fa6d629f6c9e5cb2d Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Wed, 3 Jan 2018 23:13:37 -0800 Subject: [PATCH] Fix for sending drafts twice due to back-to-back saves --- app/internal_packages/send-later/lib/main.es6 | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/app/internal_packages/send-later/lib/main.es6 b/app/internal_packages/send-later/lib/main.es6 index 1125e99b5..15f082f68 100644 --- a/app/internal_packages/send-later/lib/main.es6 +++ b/app/internal_packages/send-later/lib/main.es6 @@ -1,7 +1,7 @@ import { ComponentRegistry, DatabaseStore, - SyncbackMetadataTask, + DraftStore, Message, SendActionsStore, Actions, @@ -24,31 +24,25 @@ function handleMetadataExpiration(change) { if (change.type !== 'metadata-expiration' || change.objectClass !== Message.name) { return; } - for (const message of change.objects) { + + change.objects.forEach(async message => { const metadata = message.metadataForPluginId(PLUGIN_ID); if (!metadata || !metadata.expiration || metadata.expiration > new Date()) { - continue; + return; + } + if (!message.draft) { + return; } // clear the metadata - Actions.queueTask( - SyncbackMetadataTask.forSaving({ - model: message, - pluginId: PLUGIN_ID, - value: { - expiration: null, - }, - }) - ); - - if (!message.draft) { - continue; - } + const session = await DraftStore.sessionForClientId(message.headerMessageId); + session.changes.addPluginMetadata(PLUGIN_ID, { expiration: null }); + session.changes.commit(); // send the draft const actionKey = metadata.actionKey || SendActionsStore.DefaultSendActionKey; Actions.sendDraft(message.headerMessageId, { actionKey, delay: 0 }); - } + }); } export function activate() {