Fix: Todo Multi refresh ...
fix some promises problem
This commit is contained in:
@@ -1,11 +1,25 @@
|
||||
var os = require("os");
|
||||
|
||||
require('../models/subscribers');
|
||||
|
||||
var Url = require('url-parse');
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
// SETTINGS WebPush Configuration
|
||||
const webpush = require('web-push');
|
||||
|
||||
const subject = process.env.URLBASE_APP1; //'mailto:' + process.env.EMAIL_FROM
|
||||
const publicVapidKey = process.env.PUBLIC_VAPI_KEY;
|
||||
const privateVapidKey = process.env.PRIVATE_VAPI_KEY;
|
||||
webpush.setVapidDetails('mailto:' + process.env.EMAIL_FROM, publicVapidKey, privateVapidKey);
|
||||
|
||||
if (process.env.GCM_API_KEY !== "")
|
||||
webpush.setGCMAPIKey(process.env.GCM_API_KEY);
|
||||
|
||||
webpush.setVapidDetails(subject, publicVapidKey, privateVapidKey);
|
||||
console.log('setVapidDetails... config...');
|
||||
|
||||
|
||||
module.exports = {
|
||||
getHostname: function () {
|
||||
@@ -45,8 +59,120 @@ module.exports = {
|
||||
|
||||
console.log('sendBackNotif:', subscription, payload);
|
||||
// Pass object into sendNotification
|
||||
webpush.sendNotification(subscription, JSON.stringify(payload)).catch(err => console.error(err));
|
||||
webpush.sendNotification(subscription, JSON.stringify(payload)).catch(err => console.error(err))
|
||||
.catch(err => {
|
||||
if (err.statusCode === 410) {
|
||||
// Gone: is not valid anymore (Expired probably!), so I have to delete from my db
|
||||
return Subscription.findOneAndRemove( { _id:subscription._id } )
|
||||
} else {
|
||||
console.log('Subscription is no longer valid: ', err);
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
sendNotificationToUser: function (userId, title, content, openUrl, tag) {
|
||||
|
||||
let payload = {
|
||||
title: title,
|
||||
message: content,
|
||||
url: openUrl,
|
||||
tag,
|
||||
// ttl: req.body.ttl,
|
||||
// icon: req.body.icon,
|
||||
// image: req.body.image,
|
||||
// badge: req.body.badge,
|
||||
// tag: req.body.tag
|
||||
};
|
||||
|
||||
return Subscription.find({ userId }, (err, subscriptions) => {
|
||||
if (err) {
|
||||
console.error(`Error occurred while getting subscriptions`);
|
||||
res.status(500).json({
|
||||
error: 'Technical error occurred'
|
||||
});
|
||||
return false;
|
||||
} else {
|
||||
let conta = 0
|
||||
let parallelSubscriptionCalls = subscriptions.map((subscription) => {
|
||||
const trovati = subscriptions.length
|
||||
return new Promise((resolve, reject) => {
|
||||
const pushSubscription = {
|
||||
endpoint: subscription.endpoint,
|
||||
keys: {
|
||||
p256dh: subscription.keys.p256dh,
|
||||
auth: subscription.keys.auth
|
||||
}
|
||||
};
|
||||
|
||||
conta++;
|
||||
|
||||
|
||||
const parse = require('url-parse');
|
||||
const parsedUrl = parse(subscription.endpoint);
|
||||
const audience = parsedUrl.protocol + '//' + parsedUrl.hostname;
|
||||
|
||||
const vapidHeaders = webpush.getVapidHeaders(
|
||||
audience,
|
||||
process.env.URLBASE_APP1,
|
||||
process.env.PUBLIC_VAPI_KEY,
|
||||
process.env.PRIVATE_VAPI_KEY,
|
||||
'aes128gcm'
|
||||
);
|
||||
|
||||
const pushOptions = {
|
||||
vapidDetails: {
|
||||
subject: process.env.URLBASE_APP1,
|
||||
privateKey: process.env.PRIVATE_VAPI_KEY,
|
||||
publicKey: process.env.PUBLIC_VAPI_KEY,
|
||||
},
|
||||
TTL: payload.ttl,
|
||||
headers: vapidHeaders
|
||||
};
|
||||
|
||||
console.log('************ INVIO WEBPUSH.SENDNOTIFICATION N° ', conta, '/', trovati, 'A', subscription.browser);
|
||||
console.log('vapidDetails', pushOptions.vapidDetails);
|
||||
|
||||
payload.title = process.env.URLBASE_APP1 + ' Msg n° ' + conta +'/' + trovati;
|
||||
// payload.message += subscription.browser ;
|
||||
|
||||
const pushPayload = JSON.stringify(payload);
|
||||
|
||||
console.log('A1) SUBS: pushSubscription', pushSubscription);
|
||||
console.log('A2) OPZIONI: pushOptions', pushOptions);
|
||||
console.log('A3) MSG_TO_SEND: pushPayload', pushPayload);
|
||||
|
||||
webpush.sendNotification(
|
||||
pushSubscription,
|
||||
pushPayload,
|
||||
// pushOptions
|
||||
).then((value) => {
|
||||
resolve({
|
||||
status: true,
|
||||
endpoint: subscription.endpoint,
|
||||
data: value
|
||||
});
|
||||
}).catch((err) => {
|
||||
reject({
|
||||
status: false,
|
||||
endpoint: subscription.endpoint,
|
||||
data: err
|
||||
});
|
||||
});
|
||||
}).catch(error => {
|
||||
console.log('ERROR: sendNotificationToUser', error)
|
||||
});
|
||||
});
|
||||
// q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
|
||||
// console.info(pushResults);
|
||||
// });
|
||||
// res.json({
|
||||
// data: 'Push triggered'
|
||||
// });
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
// Test
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user