From d0677b4378d2fca9a5735b6abf4633f8c28dad60 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Thu, 2 Jul 2020 13:12:45 +0300 Subject: [PATCH] dont webpush if recipient online --- src/js/Chats.js | 54 ++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/js/Chats.js b/src/js/Chats.js index b4c7be8f..fccaae56 100644 --- a/src/js/Chats.js +++ b/src/js/Chats.js @@ -46,31 +46,7 @@ function showChat(pub) { isTyping = getIsTyping(); chats[pub].msgDraft = $('#new-msg').val(); }); - $(".message-form form").off().on('submit', event => { - event.preventDefault(); - chats[pub].msgDraft = null; - var text = $('#new-msg').val(); - if (!text.length && !chats[pub].attachments) { return; } - chats[pub].setTyping(false); - var msg = {text}; - if (chats[pub].attachments) { - msg.attachments = chats[pub].attachments; - } - if (chats[pub].webPushSubscriptions) { - chats[pub].webPushSubscriptions.slice(0,8).forEach(subscription => { - fetch(notificationServiceUrl, { - method: 'POST', - body: JSON.stringify({subscription, payload: {title:'Message', body: text}}), - headers: { - 'content-type': 'application/json' - } - }); - }); - } - chats[pub].send(msg); - Gallery.closeAttachmentsPreview(); - $('#new-msg').val(''); - }); + $(".message-form form").off().on('submit', e => onMsgFormSubmit(e, pub)); Notifications.changeChatUnseenCount(pub, 0); Profile.addUserToHeader(pub); var msgs = Object.values(chats[pub].messages); @@ -98,6 +74,34 @@ function showChat(pub) { setDeliveredCheckmarks(pub); } +function onMsgFormSubmit(event, pub) { + event.preventDefault(); + chats[pub].msgDraft = null; + var text = $('#new-msg').val(); + if (!text.length && !chats[pub].attachments) { return; } + chats[pub].setTyping(false); + var msg = {text}; + if (chats[pub].attachments) { + msg.attachments = chats[pub].attachments; + } + const shouldWebPush = (pub === Session.getKey().pub) || !(chats[pub].online && chats[pub].online.isOnline); + console.log('shouldWebPush', shouldWebPush); + if (shouldWebPush && chats[pub].webPushSubscriptions) { + chats[pub].webPushSubscriptions.slice(0,8).forEach(subscription => { + fetch(notificationServiceUrl, { + method: 'POST', + body: JSON.stringify({subscription, payload: {title:'Message', body: text}}), + headers: { + 'content-type': 'application/json' + } + }); + }); + } + chats[pub].send(msg); + Gallery.closeAttachmentsPreview(); + $('#new-msg').val(''); +} + var sortChatsByLatest = _.throttle(() => { var sorted = $(".chat-item:not(.new):not(.public-messages)").sort((a, b) => { return ($(b).data('latestTime') || -Infinity) - ($(a).data('latestTime') || -Infinity);