Notification with Service Workers now is working!

When a Notification arrives, it save into the IndexDb,
then in Vue.js call a polling to check in the db if there is a different record count.
If is different then call a get to update the notification.
This commit is contained in:
paoloar77
2022-08-07 02:01:35 +02:00
parent 8a587dc715
commit e433d3db8c
8 changed files with 76 additions and 41 deletions

View File

@@ -55,7 +55,7 @@ const sendNotifSchema = new Schema({
type: Number,
default: 0,
},
options: {
typesend: {
type: Number,
},
read: {
@@ -75,6 +75,12 @@ const sendNotifSchema = new Schema({
extrafield: {
type: String,
},
tag: {
type: String,
},
options: {
type: Array,
},
});
@@ -144,20 +150,26 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) {
newdescr = i18n.__('<strong>%s</strong> new Good: %s', userorig, mydescr);
recnotif.openUrl = '/mygood/' + myidrec;
tag = 'newgood';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_SERVICE) {
newdescr = i18n.__('<strong>%s</strong> new Service: %s', userorig, mydescr);
recnotif.openUrl = '/myservice/' + myidrec;
tag = 'newservice';
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_FRIENDS) {
recnotif.openUrl = '/my/' + userorig;
if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_NEW_REC) {
newdescr = i18n.__('<strong>%s</strong> asked you for Friendship', userorig, mydescr);
tag = 'newfriend';
} else if ((recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) ||
(recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED_MY_REQUEST)) {
newdescr = i18n.__('<strong>%s</strong> accepted your Friendship', userorig, mydescr);
tag = 'acceptedfriend';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newdescr = i18n.__('<strong>%s</strong> refused your Friendship', userorig, mydescr);
tag = 'refusedfriend';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REPORTED) {
tag = 'reportuser';
if (recnotif.paramsObj.usernameDest === recnotif.paramsObj.username_action) {
newdescr = i18n.__('FRIEND_REPORTED_YOU', recnotif.paramsObj.username_worked);
} else if (recnotif.paramsObj.isAdmin) {
@@ -167,6 +179,7 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
}
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
tag = 'group';
recnotif.openUrl = '/grp/' + recnotif.paramsObj.groupnameDest;
if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_NEW_REC) {
newdescr = i18n.__('GROUP_CREATED', userorig, recnotif.paramsObj.groupnameDest);
@@ -202,7 +215,6 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
newdescr = i18n.__('GRUPPO_ELIMINATO', userorig, recnotif.paramsObj.username_action);
tag = 'deletegroup';
}
}
recnotif.tag = recnotif.tag ? recnotif.tag : tag;
@@ -274,26 +286,25 @@ sendNotifSchema.statics.saveAndSendNotif = async function(myrecnotif, req, res,
if (!myrecout)
return null;
if (save) {
await myrecout.save().then((writeresult) => {
let idobj = writeresult._id;
// console.log('myrecout._id', myrecout._id.toString());
return SendNotif.findById(idobj).lean().then(async (recnotif) => {
return await globalTables.sendNotif(myrecnotif.typedir, myrecnotif.typeid, res, idapp, user ? user : req.user, recnotif).
then((ris) => {
return recnotif;
});
});
if (save) {
await myrecout.save().then(async (res) => {
if (res) {
const idobj = res._id;
const myrecread = await SendNotif.findById(idobj).lean();
// console.log('myrecread._id', myrecread._id.toString());
return await globalTables.sendNotif(myrecread.typedir, myrecread.typeid, res, idapp, user ? user : req.user, myrecread);
}
}).catch((e) => {
console.log(e.message);
return null;
});
} else {
return await globalTables.sendNotif(myrecout.typedir, myrecout.typeid, res, idapp, user ? user : req.user, myrecout).
then((ris) => {
return myrecout;
});
return await globalTables.sendNotif(myrecout.typedir, myrecout.typeid, res, idapp, user ? user : req.user, myrecout);
}
};
@@ -373,7 +384,7 @@ sendNotifSchema.statics.updateStatusAndDescr = async function(myrecnotif, onlysa
}
}
myrecnotif._id = new ObjectID();
// myrecnotif._id = new ObjectID();
if (newstatus > 0) {
myrecnotif.status = newstatus;
}
@@ -431,7 +442,8 @@ sendNotifSchema.statics.getExtraParam = function(myrecnotif, paramsObj) {
//out = myrecnotif._doc
//}
out.paramsObj = paramsObj;
out.options = paramsObj.options ? paramsObj.options : 0;
out.options = paramsObj.options ? paramsObj.options : [];
out.typesend = paramsObj.typesend ? paramsObj.typesend : 0;
out.extrafield = paramsObj.groupnameDest ? paramsObj.groupnameDest : '';
return myrecnotif;