Notification: Set a new Friendship and Accepted Friendship
This commit is contained in:
@@ -35,6 +35,9 @@ const sendNotifSchema = new Schema({
|
||||
dest: {
|
||||
type: String,
|
||||
},
|
||||
title: {
|
||||
type: String,
|
||||
},
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
@@ -100,9 +103,11 @@ sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, la
|
||||
{'dest': username},
|
||||
{'datenotif': {$gt: new Date(lastdataread)}},
|
||||
],
|
||||
}).lean().sort({datenotif: -1}).then((arrnotif) => {
|
||||
}).lean().sort({datenotif: -1}).then(async (arrnotif) => {
|
||||
// console.log('arrnotif', arrnotif.length);
|
||||
return arrnotif;
|
||||
|
||||
return this.compileOtherFields(arrnotif);
|
||||
|
||||
}).catch((err) => {
|
||||
console.error('err', err);
|
||||
});
|
||||
@@ -111,19 +116,66 @@ sendNotifSchema.statics.findAllNotifByUsernameIdAndIdApp = function(username, la
|
||||
|
||||
sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
|
||||
|
||||
if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA) {
|
||||
if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) {
|
||||
recnotif.descr = i18n.__('Good: %s');
|
||||
recnotif.openUrl = '/goods';
|
||||
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_SERVICE) {
|
||||
recnotif.descr = i18n.__('Service: %s');
|
||||
recnotif.openUrl = '/services';
|
||||
const numchars = 80;
|
||||
let newdescr = '';
|
||||
let mydescr = '';
|
||||
let myidrec = '';
|
||||
let userorig = recnotif.sender;
|
||||
|
||||
try {
|
||||
if (recnotif.myrectableorig) {
|
||||
myidrec = recnotif.myrectableorig._id;
|
||||
mydescr = recnotif.myrectableorig.descr ? tools.firstchars(recnotif.myrectableorig.descr, numchars) : '';
|
||||
}
|
||||
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_FRIENDS) {
|
||||
recnotif.openUrl = '/friends';
|
||||
|
||||
if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_BACHECA) {
|
||||
if (recnotif.typeid === shared_consts.TypeNotifs.ID_BACHECA_NEW_GOOD) {
|
||||
newdescr = i18n.__('<strong>%s</strong> new Good: %s', userorig, mydescr);
|
||||
recnotif.openUrl = '/mygood/' + myidrec;
|
||||
} 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;
|
||||
}
|
||||
} 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);
|
||||
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) {
|
||||
newdescr = i18n.__('<strong>%s</strong> accepted your Friendship', userorig, mydescr);
|
||||
}
|
||||
}
|
||||
|
||||
if (!recnotif.descr) {
|
||||
recnotif.descr = newdescr;
|
||||
}
|
||||
|
||||
return recnotif;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
sendNotifSchema.statics.compileOtherFields = async function(arrnotif) {
|
||||
|
||||
const {User} = require('../models/user');
|
||||
|
||||
try {
|
||||
// Fill in the image profile of the semyinders !
|
||||
for (const notif of arrnotif) {
|
||||
let myimgprofile = await User.findOne({idapp: notif.idapp, username: notif.sender}, {'profile.img': 1}).lean();
|
||||
if (myimgprofile && myimgprofile.profile.img)
|
||||
notif.myimgsender = 'upload/profile/' + notif.sender + '/' + myimgprofile.profile.img;
|
||||
}
|
||||
|
||||
return arrnotif;
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return arrnotif;
|
||||
}
|
||||
|
||||
return recnotif;
|
||||
};
|
||||
|
||||
sendNotifSchema.statics.findLastNotifsByUserIdAndIdApp = function(username, idapp, limit) {
|
||||
@@ -140,12 +192,8 @@ sendNotifSchema.statics.findLastNotifsByUserIdAndIdApp = function(username, idap
|
||||
{
|
||||
$sort: {datenotif: -1},
|
||||
},
|
||||
]).then((arrnotif) => {
|
||||
// Remove duplicate
|
||||
// Exclude my chat
|
||||
const myarr = arrnotif.filter((ris) => ris._id !== username);
|
||||
// console.table(myarr);
|
||||
return myarr;
|
||||
]).then(async (arrnotif) => {
|
||||
return this.compileOtherFields(arrnotif);
|
||||
|
||||
}).catch((err) => {
|
||||
console.error(err);
|
||||
@@ -160,7 +208,10 @@ sendNotifSchema.statics.saveAndSendNotif = function(myrecnotif, req, res, user)
|
||||
if (check.exit) return check.ret;
|
||||
|
||||
myrecnotif._id = new ObjectID();
|
||||
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
|
||||
if (!myrecnotif.openUrl) {
|
||||
// If not exist, then I set openUrl and description
|
||||
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
|
||||
}
|
||||
|
||||
return myrecnotif.save().then((writeresult) => {
|
||||
let idobj = writeresult._id;
|
||||
@@ -168,9 +219,32 @@ sendNotifSchema.statics.saveAndSendNotif = function(myrecnotif, req, res, user)
|
||||
myrecnotif._id = idobj;
|
||||
|
||||
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;
|
||||
});
|
||||
return await globalTables.sendNotif(myrecnotif.typedir, myrecnotif.typeid, res, idapp, user ? user : req.user, recnotif).
|
||||
then((ris) => {
|
||||
return recnotif;
|
||||
});
|
||||
});
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
sendNotifSchema.statics.saveNotif = function(myrecnotif) {
|
||||
|
||||
myrecnotif._id = new ObjectID();
|
||||
if (!myrecnotif.openUrl) {
|
||||
// If not exist, then I set openUrl and description
|
||||
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
|
||||
}
|
||||
|
||||
return myrecnotif.save().then((writeresult) => {
|
||||
let idobj = writeresult._id;
|
||||
|
||||
myrecnotif._id = idobj;
|
||||
|
||||
return SendNotif.findById(idobj).lean().then(async (recnotif) => {
|
||||
return recnotif;
|
||||
});
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
@@ -191,6 +265,8 @@ sendNotifSchema.statics.getDefaultRec = function(req) {
|
||||
datenotif: new Date(),
|
||||
status: 0,
|
||||
read: false,
|
||||
tablerec: '',
|
||||
idrec: 0,
|
||||
};
|
||||
|
||||
};
|
||||
@@ -202,7 +278,7 @@ sendNotifSchema.statics.createNewNotification = async function(req, res, table,
|
||||
let myrecnotif = new SendNotif(this.getDefaultRec(req));
|
||||
|
||||
myrecnotif.tablerec = table;
|
||||
if (rec) {
|
||||
if (rec && table) {
|
||||
myrecnotif.idrec = rec._id;
|
||||
}
|
||||
|
||||
@@ -218,6 +294,24 @@ sendNotifSchema.statics.createNewNotification = async function(req, res, table,
|
||||
}
|
||||
};
|
||||
|
||||
sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, usernameDest, onlysave, typedir, typeid) {
|
||||
const SendNotif = this;
|
||||
|
||||
try {
|
||||
let myrecnotif = new SendNotif(this.getDefaultRec(req));
|
||||
|
||||
myrecnotif.typedir = typedir;
|
||||
myrecnotif.typeid = typeid;
|
||||
|
||||
await SendNotif.sendToSingleUserDest(myrecnotif, req, res, usernameDest, onlysave);
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error('createNewNotification', e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res) {
|
||||
const SendNotif = this;
|
||||
|
||||
@@ -226,88 +320,119 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
|
||||
const {City} = require('../models/city');
|
||||
const {Province} = require('../models/province');
|
||||
|
||||
// Send only to the destination to reach:
|
||||
const userlist = await User.find({
|
||||
idapp: myrecnotif.idapp,
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, {
|
||||
name: 1,
|
||||
surname: 1,
|
||||
lang: 1,
|
||||
username: 1,
|
||||
'profile.notifs': 1,
|
||||
'profile.notif_idCities': 1,
|
||||
'profile.notif_provinces': 1,
|
||||
'profile.notif_regions': 1,
|
||||
'profile.notif_sectors': 1,
|
||||
'profile.notif_sector_goods': 1,
|
||||
}).lean();
|
||||
try {
|
||||
|
||||
let arrprovinces = [];
|
||||
let arrregions = [];
|
||||
let idSector = 0;
|
||||
// Send only to the destination to reach:
|
||||
const userlist = await User.find({
|
||||
idapp: myrecnotif.idapp,
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, {
|
||||
name: 1,
|
||||
surname: 1,
|
||||
lang: 1,
|
||||
username: 1,
|
||||
'profile.notifs': 1,
|
||||
'profile.notif_idCities': 1,
|
||||
'profile.notif_provinces': 1,
|
||||
'profile.notif_regions': 1,
|
||||
'profile.notif_sectors': 1,
|
||||
'profile.notif_sector_goods': 1,
|
||||
}).lean();
|
||||
|
||||
const mytable = globalTables.getTableByTableName(myrecnotif.tablerec);
|
||||
let arrprovinces = [];
|
||||
let arrregions = [];
|
||||
let idSector = 0;
|
||||
|
||||
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) || shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
|
||||
const mytable = globalTables.getTableByTableName(myrecnotif.tablerec);
|
||||
|
||||
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
|
||||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
|
||||
|
||||
const myrectableorig = await mytable.findOne({_id: myrecnotif.idrec}).lean();
|
||||
if (myrectableorig) {
|
||||
for (const city of myrectableorig.idCity) {
|
||||
arrprovinces.push(await City.getProvinceByIdCity(city));
|
||||
arrregions.push(await City.getRegionByIdCity(city));
|
||||
}
|
||||
|
||||
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
|
||||
idSector = myrectableorig.idSectorGood;
|
||||
} else {
|
||||
idSector = myrectableorig.idSector;
|
||||
}
|
||||
|
||||
const myrec = await mytable.findOne({_id: myrecnotif.idrec}).lean();
|
||||
if (myrec) {
|
||||
for (const city of myrec.idCity) {
|
||||
arrprovinces.push(await City.getProvinceByIdCity(city));
|
||||
arrregions.push(await City.getRegionByIdCity(city));
|
||||
}
|
||||
|
||||
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
|
||||
idSector = myrec.idSectorGood;
|
||||
} else {
|
||||
idSector = myrec.idSector;
|
||||
}
|
||||
|
||||
myrecnotif.myrectableorig = myrectableorig;
|
||||
}
|
||||
|
||||
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
|
||||
|
||||
for (const user of userlist) {
|
||||
|
||||
if (user.profile.notifs) {
|
||||
const usernotifprofile = user.profile.notifs.find((notif) => notif.dir === myrecnotif.typedir);
|
||||
|
||||
let send = false;
|
||||
|
||||
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
|
||||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
|
||||
// Estrai la Città, la Provincia e la regione.
|
||||
|
||||
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_PROVINCE) &&
|
||||
user.profile.notif_provinces && user.profile.notif_provinces.some(r => arrprovinces.indexOf(r) >= 0)) {
|
||||
send = true;
|
||||
}
|
||||
|
||||
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_REGION) &&
|
||||
user.profile.notif_regions && user.profile.notif_regions.some(r => arrregions.indexOf(r) >= 0)) {
|
||||
send = true;
|
||||
}
|
||||
|
||||
if (idSector && usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_SECTOR)) {
|
||||
// Controlla se è del settore selezionato
|
||||
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
|
||||
if (user.profile.notif_sector_goods) {
|
||||
send = send && (user.profile.notif_sector_goods.includes(idSector));
|
||||
}
|
||||
} else if (user.profile.notif_sectors) {
|
||||
send = send && (user.profile.notif_sectors.includes(idSector));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (send) {
|
||||
myrecnotif.dest = user.username;
|
||||
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('sendToTheDestinations', e);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const user of userlist) {
|
||||
};
|
||||
|
||||
if (user.profile.notifs) {
|
||||
const usernotifprofile = user.profile.notifs.find((notif) => notif.dir === myrecnotif.typedir);
|
||||
sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, res, usernameDest, onlysave) {
|
||||
const SendNotif = this;
|
||||
|
||||
let send = false;
|
||||
try {
|
||||
|
||||
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
|
||||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
|
||||
// Estrai la Città, la Provincia e la regione.
|
||||
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
|
||||
|
||||
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_PROVINCE) &&
|
||||
user.profile.notif_provinces && user.profile.notif_provinces.some(r => arrprovinces.indexOf(r) >= 0)) {
|
||||
send = true;
|
||||
}
|
||||
|
||||
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_REGION) &&
|
||||
user.profile.notif_regions && user.profile.notif_regions.some(r => arrregions.indexOf(r) >= 0)) {
|
||||
send = true;
|
||||
}
|
||||
|
||||
if (idSector && usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_SECTOR)) {
|
||||
// Controlla se è del settore selezionato
|
||||
if (myrecnotif.tablerec === shared_consts.TABLES_MYGOODS) {
|
||||
if (user.profile.notif_sector_goods) {
|
||||
send = send && (user.profile.notif_sector_goods.includes(idSector));
|
||||
}
|
||||
} else if (user.profile.notif_sectors) {
|
||||
send = send && (user.profile.notif_sectors.includes(idSector));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (send) {
|
||||
myrecnotif.dest = user.username;
|
||||
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
|
||||
}
|
||||
myrecnotif.dest = usernameDest;
|
||||
if (onlysave) {
|
||||
await SendNotif.saveNotif(myrecnotif);
|
||||
} else {
|
||||
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error('sendToSingleUserDest', e);
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user