- Accepted and Refused Group Entering

- Report a User
This commit is contained in:
paoloar77
2022-08-04 17:30:57 +02:00
parent d902783336
commit 8a587dc715
16 changed files with 529 additions and 266 deletions

View File

@@ -55,6 +55,9 @@ const sendNotifSchema = new Schema({
type: Number,
default: 0,
},
options: {
type: Number,
},
read: {
type: Boolean,
default: false,
@@ -69,6 +72,9 @@ const sendNotifSchema = new Schema({
type: Boolean,
default: false,
},
extrafield: {
type: String,
},
});
@@ -126,6 +132,7 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
let mydescr = '';
let myidrec = '';
let userorig = recnotif.sender;
let tag = '';
try {
if (recnotif.myrectableorig) {
@@ -150,16 +157,56 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
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.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REPORTED) {
if (recnotif.paramsObj.usernameDest === recnotif.paramsObj.username_action) {
newdescr = i18n.__('FRIEND_REPORTED_YOU', recnotif.paramsObj.username_worked);
} else if (recnotif.paramsObj.isAdmin) {
newdescr = i18n.__('FRIEND_REPORTED', recnotif.paramsObj.usernameDest, userorig);
} else {
newdescr = i18n.__('FRIEND_REPORTED_TO_ME', recnotif.paramsObj.username_action, recnotif.paramsObj.username_action);
}
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
recnotif.openUrl = '/grp/' + recnotif.paramsObj.groupNameDest;
recnotif.openUrl = '/grp/' + recnotif.paramsObj.groupnameDest;
if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_NEW_REC) {
newdescr = i18n.__('GROUP_CREATED', userorig, recnotif.paramsObj.groupNameDest);
newdescr = i18n.__('GROUP_CREATED', userorig, recnotif.paramsObj.groupnameDest);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
newdescr = i18n.__('GROUPS_ACCEPTED', userorig, recnotif.paramsObj.groupNameDest);
if (recnotif.paramsObj.isAdmin)
newdescr = i18n.__('ACCETTATO_NOTIFICA_ADMINS', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
else
newdescr = i18n.__('GROUPS_ACCEPTED', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
tag = 'addgroup';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REMOVED) {
if (recnotif.paramsObj.username_action === recnotif.paramsObj.usernameDest && userorig === recnotif.paramsObj.usernameDest)
newdescr = i18n.__('GROUPS_EXIT_USER_TO_ME', recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
else if (userorig === recnotif.paramsObj.usernameDest)
newdescr = i18n.__('GROUPS_REMOVED_TO_ME', recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
else if (userorig === recnotif.paramsObj.username_action)
newdescr = i18n.__('GROUPS_EXIT_USER', userorig, recnotif.paramsObj.groupnameDest);
else
newdescr = i18n.__('GROUPS_REMOVED', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
tag = 'remgroup';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REFUSED) {
newdescr = i18n.__('GROUPS_REFUSED', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
tag = 'addgroup';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER) {
newdescr = i18n.__('GROUP_REQUEST_TO_ENTER', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.singleadmin_username);
tag = 'reqgroups';
// sendnotifPush = false; // non lo rimandare 2 volte !
// telegrambot.askConfirmationUser(idapp, shared_consts.CallFunz.RICHIESTA_GRUPPO, myuser, singleadmin.username, groupname, group._id);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_BLOCK_USER) {
newdescr = i18n.__('RICHIESTA_BLOCCO_GRUPPO', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.singleadmin_username);
tag = 'blockgroups';
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_DELETE_USER) {
newdescr = i18n.__('GRUPPO_ELIMINATO', userorig, recnotif.paramsObj.username_action);
tag = 'deletegroup';
}
}
recnotif.tag = recnotif.tag ? recnotif.tag : tag;
if (!recnotif.descr) {
recnotif.descr = newdescr;
}
@@ -216,95 +263,139 @@ sendNotifSchema.statics.findLastNotifsByUserIdAndIdApp = function(username, idap
};
sendNotifSchema.statics.saveAndSendNotif = function(myrecnotif, req, res, user) {
sendNotifSchema.statics.saveAndSendNotif = async function(myrecnotif, req, res, user) {
const SendNotif = this;
let idapp = req.body.idapp;
const check = tools.checkUserOk(myrecnotif.sender, user ? myrecnotif.sender : req.user.username, res);
if (check.exit) return check.ret;
myrecnotif._id = new ObjectID();
if (!myrecnotif.openUrl) {
// If not exist, then I set openUrl and description
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
const {myrecout, save} = await SendNotif.updateStatusAndDescr(myrecnotif, false);
if (!myrecout)
return null;
if (save) {
await myrecout.save().then((writeresult) => {
let idobj = writeresult._id;
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;
});
});
}).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 myrecnotif.save().then((writeresult) => {
let idobj = writeresult._id;
};
myrecnotif._id = idobj;
sendNotifSchema.statics.updateStatusAndDescr = async function(myrecnotif, onlysave) {
const SendNotif = this;
try {
if (!myrecnotif.openUrl) {
// If not exist, then I set openUrl and description
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
}
let newstatus = 0;
let typeidsearch = 0;
let dest = '';
// 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;
dest = myrecnotif.sender;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newstatus = shared_consts.StatusNotifs.STATUS_FRIENDS_REFUSED;
dest = myrecnotif.sender;
}
} else if (myrecnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_NEW_REC;
if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER;
newstatus = shared_consts.GroupsNotifs.STATUS_GROUPS_ACCEPTED;
dest = myrecnotif.dest;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REFUSED) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER;
newstatus = shared_consts.GroupsNotifs.STATUS_GROUPS_REFUSED;
dest = myrecnotif.dest;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REMOVED) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER;
newstatus = shared_consts.GroupsNotifs.STATUS_GROUPS_REMOVED;
dest = myrecnotif.dest;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_BLOCK_USER) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER;
newstatus = shared_consts.GroupsNotifs.STATUS_GROUPS_BLOCKED;
dest = myrecnotif.dest;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_DELETE_USER) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_REQUEST_TO_ENTER;
newstatus = shared_consts.GroupsNotifs.STATUS_GROUPS_DELETED;
dest = myrecnotif.dest;
}
}
if (newstatus) {
const fields_to_update = {
status: newstatus,
read: true,
descr: myrecnotif.descr,
};
const query = {
idapp: myrecnotif.idapp,
typedir: myrecnotif.typedir,
typeid: typeidsearch,
dest,
deleted: false,
status: 0,
};
// Cerca il record e se lo trova lo aggiorna
const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, {
new: true,
returnNewDocument: true,
});
if (myrec) {
return {myrecout: myrec, save: false};
}
}
myrecnotif._id = new ObjectID();
if (newstatus > 0) {
myrecnotif.status = newstatus;
}
return {myrecout: myrecnotif, save: true};
} catch (e) {
console.error(e);
return {myrecout: null, save: false};
}
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;
});
});
}).catch((e) => {
console.log(e.message);
return null;
});
};
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;
}
} else if (myrecnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_NEW_REC;
if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_ACCEPTED;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REFUSED) {
newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_REFUSED;
}
}
const {myrecout, save} = await SendNotif.updateStatusAndDescr(myrecnotif, true);
if (!myrecout)
return null;
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) {
// If not exist, then I set openUrl and description
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
}
return myrecnotif.save().then((writeresult) => {
return myrecout.save().then((writeresult) => {
let idobj = writeresult._id;
myrecnotif._id = idobj;
return SendNotif.findById(idobj).lean().then(async (recnotif) => {
return recnotif;
});
@@ -334,11 +425,24 @@ sendNotifSchema.statics.getDefaultRec = function(req) {
};
sendNotifSchema.statics.getExtraParam = function(myrecnotif, paramsObj) {
let out = myrecnotif;
//if (myrecnotif._doc) {
//out = myrecnotif._doc
//}
out.paramsObj = paramsObj;
out.options = paramsObj.options ? paramsObj.options : 0;
out.extrafield = paramsObj.groupnameDest ? paramsObj.groupnameDest : '';
return myrecnotif;
};
sendNotifSchema.statics.createNewNotification = async function(req, res, paramsObj, table, rec, typedir, typeid) {
const SendNotif = this;
try {
let myrecnotif = new SendNotif(this.getDefaultRec(req));
myrecnotif = this.getExtraParam(myrecnotif, paramsObj);
myrecnotif.tablerec = table;
if (rec && table) {
@@ -348,7 +452,7 @@ sendNotifSchema.statics.createNewNotification = async function(req, res, paramsO
myrecnotif.typedir = typedir;
myrecnotif.typeid = typeid;
await SendNotif.sendToTheDestinations(myrecnotif, req, res, paramsObj);
await SendNotif.sendToTheDestinations(myrecnotif, req, res);
return true;
} catch (e) {
@@ -362,11 +466,12 @@ sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, pa
try {
let myrecnotif = new SendNotif(this.getDefaultRec(req));
myrecnotif = this.getExtraParam(myrecnotif, paramsObj);
myrecnotif.typedir = typedir;
myrecnotif.typeid = typeid;
await SendNotif.sendToSingleUserDest(myrecnotif, req, res, paramsObj, onlysave);
await SendNotif.sendToSingleUserDest(myrecnotif, req, res, onlysave);
return true;
} catch (e) {
@@ -375,7 +480,7 @@ sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, pa
}
};
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res, paramsObj) {
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res) {
const SendNotif = this;
const {User} = require('../models/user');
@@ -383,8 +488,6 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
const {City} = require('../models/city');
const {Province} = require('../models/province');
myrecnotif.paramsObj = paramsObj;
try {
// Send only to the destination to reach:
@@ -469,12 +572,12 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
}
if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotif.tablerec)) {
if (tools.isBitActive(usernotifprofile.value, shared_consts.GroupsNotifs.STATUS_GROUPS_NEW)) {
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.GroupsNotifs.STATUS_GROUPS_NEW)) {
send = true;
}
}
if (send) {
if (send) {
myrecnotif.dest = user.username;
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
}
@@ -487,19 +590,18 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
};
sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, res, paramsObj, onlysave) {
sendNotifSchema.statics.sendToSingleUserDest = async function(myrecnotif, req, res, onlysave) {
const SendNotif = this;
try {
myrecnotif.paramsObj = paramsObj;
myrecnotif.dest = paramsObj.usernameDest ? paramsObj.usernameDest : '';
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
myrecnotif.dest = myrecnotif.paramsObj && myrecnotif.paramsObj.usernameDest ? myrecnotif.paramsObj.usernameDest : '';
myrecnotif.username_worked = myrecnotif.paramsObj && myrecnotif.paramsObj.username_worked ? myrecnotif.paramsObj.username_worked : myrecnotif.dest;
if (onlysave) {
await SendNotif.saveNotif(myrecnotif);
} else {
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
await SendNotif.saveAndSendNotif(myrecnotif, req, res, null);
}
} catch (e) {