Friendship Accepted and Refused (notification updated)

This commit is contained in:
Paolo Arena
2022-07-27 20:56:01 +02:00
parent 3515914f17
commit 50c6031789
10 changed files with 151 additions and 49 deletions

View File

@@ -25,15 +25,19 @@ const sendNotifSchema = new Schema({
},
typedir: {
type: Number,
default: 0,
},
typeid: {
type: Number,
default: 0,
},
sender: { // mittente
type: String,
default: '',
},
dest: {
type: String,
default: '',
},
title: {
type: String,
@@ -49,6 +53,7 @@ const sendNotifSchema = new Schema({
},
status: {
type: Number,
default: 0,
},
read: {
type: Boolean,
@@ -142,6 +147,15 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
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);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newdescr = i18n.__('<strong>%s</strong> refused your Friendship', userorig, mydescr);
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
recnotif.openUrl = '/group/' + paramsObj.groupNameDest;
if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_NEW_REC) {
newdescr = i18n.__('<strong>%s</strong> asked you for Friendship', userorig, mydescr);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
newdescr = i18n.__('<strong>%s</strong> accepted your Friendship', userorig, mydescr);
}
}
@@ -230,7 +244,46 @@ sendNotifSchema.statics.saveAndSendNotif = function(myrecnotif, req, res, user)
});
};
sendNotifSchema.statics.saveNotif = function(myrecnotif) {
sendNotifSchema.statics.saveNotif = async function(myrecnotif) {
const SendNotif = this;
let newstatus = 0;
let typeidsearch = 0;
// Controllare se devo modificare un Notif già esistente !
if (myrecnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_FRIENDS) {
typeidsearch = shared_consts.TypeNotifs.ID_FRIENDS_NEW_REC;
if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) {
newstatus = shared_consts.StatusNotifs.STATUS_FRIENDS_ACCEPTED;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newstatus = shared_consts.StatusNotifs.STATUS_FRIENDS_REFUSED;
}
if (newstatus) {
const fields_to_update = {
status: newstatus,
read: true,
};
const query = {
idapp: myrecnotif.idapp,
typedir: myrecnotif.typedir,
typeid: typeidsearch,
dest: myrecnotif.sender,
deleted: false,
status: 0,
};
// Cerca il record e se lo trova lo aggiorna
const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, {
new: false,
returnNewDocument: true
});
if (myrec) {
return myrec._doc;
}
}
}
myrecnotif._id = new ObjectID();
if (!myrecnotif.openUrl) {
@@ -250,6 +303,7 @@ sendNotifSchema.statics.saveNotif = function(myrecnotif) {
console.log(e.message);
return null;
});
};
sendNotifSchema.statics.getDefaultRec = function(req) {
@@ -294,7 +348,7 @@ sendNotifSchema.statics.createNewNotification = async function(req, res, table,
}
};
sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, usernameDest, onlysave, typedir, typeid) {
sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, paramsObj, onlysave, typedir, typeid) {
const SendNotif = this;
try {
@@ -303,7 +357,7 @@ sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, us
myrecnotif.typedir = typedir;
myrecnotif.typeid = typeid;
await SendNotif.sendToSingleUserDest(myrecnotif, req, res, usernameDest, onlysave);
await SendNotif.sendToSingleUserDest(myrecnotif, req, res, paramsObj, onlysave);
return true;
} catch (e) {
@@ -416,14 +470,15 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
};
sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, res, usernameDest, onlysave) {
sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, res, paramsObj, onlysave) {
const SendNotif = this;
try {
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
myrecnotif.dest = usernameDest;
myrecnotif.dest = paramsObj.usernameDest;
myrecnotif.paramsObj = paramsObj;
if (onlysave) {
await SendNotif.saveNotif(myrecnotif);
} else {

View File

@@ -1597,8 +1597,7 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
SendNotif.createNewNotifToSingleUser(req, null, usernameDest, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED);
await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED);
update = {$pull: {'profile.req_friends': {username: {$in: [usernameDest]}}}};
ris = await User.updateOne({idapp, username: usernameOrig}, update);
@@ -1677,6 +1676,10 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD
} else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_FRIEND) {
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_REFUSED);
await this.removeReqFriend(idapp, usernameDest, usernameOrig); // Rimuovo la Richiesta di Amicizia da lui
ris = await this.removeFriend(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
@@ -1737,6 +1740,11 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
};
ris = await User.updateOne({idapp, username: usernameOrig}, update);
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
SendNotif.createNewNotifToSingleUser(req, null, {usernameDest, groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED);
// Elimina la richiesta:
update = {$pull: {req_users: {username: {$in: [usernameOrig]}}}};
ris = await MyGroup.updateOne({idapp, groupname: groupnameDest},