From e69c2f1aa247bd89ef52601e6aadc5c644d6ff37 Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Tue, 16 Jul 2019 00:52:55 +0000 Subject: [PATCH] notifications: Send message received from desktop app notification reply. Adds a electron_bridge event that takes in message id and reply recived from the notification reply and sends a message. We do this in webapp so desktop doesn't have to depend on narrow and channel modules. We also modify zjunit to reset window.electron_bridge after every run to avoid leaking it. --- frontend_tests/zjsunit/namespace.js | 1 + static/js/notifications.js | 40 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.js index 0fd723f943..59797d2b4a 100644 --- a/frontend_tests/zjsunit/namespace.js +++ b/frontend_tests/zjsunit/namespace.js @@ -41,6 +41,7 @@ exports.restore = function () { delete require.cache[require.resolve(fn)]; }); dependencies = []; + delete global.window.electron_bridge; _.extend(global, old_builtins); old_builtins = {}; }; diff --git a/static/js/notifications.js b/static/js/notifications.js index a814dca01a..838a567be3 100644 --- a/static/js/notifications.js +++ b/static/js/notifications.js @@ -269,6 +269,46 @@ exports.notify_above_composebox = function (note, link_class, link_msg_id, link_ $('#out-of-view-notification').show(); }; +if (window.electron_bridge !== undefined) { + // The code below is for sending a message received from notification reply which + // is often refered to as inline reply feature. This is done so desktop app doesn't + // have to depend on channel.post for setting crsf_token and narrow.by_topic + // to narrow to the message being sent. + window.electron_bridge.send_notification_reply_message_supported = true; + window.electron_bridge.on_event('send_notification_reply_message', function (message_id, reply) { + var message = message_store.get(message_id); + var data = { + type: message.type, + content: reply, + to: message.type === 'private' ? message.reply_to : message.stream, + topic: util.get_message_topic(message), + }; + + function success() { + if (message.type === 'stream') { + narrow.by_topic(message_id, {trigger: 'desktop_notification_reply'}); + } else { + narrow.by_recipient(message_id, {trigger: 'desktop_notification_reply'}); + } + } + + function error(error) { + window.electron_bridge.send_event('send_notification_reply_message_failed', { + data: data, + message_id: message_id, + error: error, + }); + } + + channel.post({ + url: '/json/messages', + data: data, + success: success, + error: error, + }); + }); +} + function process_notification(notification) { var i; var notification_object;