dont webpush if recipient online

This commit is contained in:
Martti Malmi 2020-07-02 13:12:45 +03:00
parent 1bb1a948b7
commit d0677b4378

View File

@ -46,31 +46,7 @@ function showChat(pub) {
isTyping = getIsTyping(); isTyping = getIsTyping();
chats[pub].msgDraft = $('#new-msg').val(); chats[pub].msgDraft = $('#new-msg').val();
}); });
$(".message-form form").off().on('submit', event => { $(".message-form form").off().on('submit', e => onMsgFormSubmit(e, 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;
}
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('');
});
Notifications.changeChatUnseenCount(pub, 0); Notifications.changeChatUnseenCount(pub, 0);
Profile.addUserToHeader(pub); Profile.addUserToHeader(pub);
var msgs = Object.values(chats[pub].messages); var msgs = Object.values(chats[pub].messages);
@ -98,6 +74,34 @@ function showChat(pub) {
setDeliveredCheckmarks(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 sortChatsByLatest = _.throttle(() => {
var sorted = $(".chat-item:not(.new):not(.public-messages)").sort((a, b) => { var sorted = $(".chat-item:not(.new):not(.public-messages)").sort((a, b) => {
return ($(b).data('latestTime') || -Infinity) - ($(a).data('latestTime') || -Infinity); return ($(b).data('latestTime') || -Infinity) - ($(a).data('latestTime') || -Infinity);