- Refused User

- Report User
- Unblock User
- refresh tables when an action (setFriends and setGroups) occurred.
- fix duplicate call of loadsite
This commit is contained in:
paoloar77
2022-08-08 16:35:32 +02:00
parent e433d3db8c
commit fc8e8d8034
7 changed files with 182 additions and 59 deletions

View File

@@ -5,7 +5,6 @@ const tools = require('../tools/general');
const shared_consts = require('../tools/shared_nodejs');
mongoose.Promise = global.Promise;
mongoose.level = 'F';
@@ -99,6 +98,12 @@ const MyGroupSchema = new Schema({
username: {type: String},
date: {type: Date},
}], // username
refused_users: [
{
_id: false,
username: {type: String},
date: {type: Date},
}], // username
deleted: {
type: Boolean,
default: false,
@@ -112,7 +117,7 @@ MyGroupSchema.statics.getFieldsForSearch = function() {
MyGroupSchema.statics.executeQueryTable = function(idapp, params, user) {
params.fieldsearch = this.getFieldsForSearch();
const { User } = require('./user');
const {User} = require('./user');
if (params.options) {
if (tools.isBitActive(params.options, shared_consts.OPTIONS_SEARCH_USER_ONLY_FULL_WORDS)) {
@@ -144,8 +149,6 @@ MyGroupSchema.pre('save', async function(next) {
next();
});
MyGroupSchema.statics.findAllIdApp = async function(idapp) {
const myfind = {idapp};
@@ -171,7 +174,23 @@ MyGroupSchema.statics.removeReqGroup = async function(idapp, username, groupname
{$pull: {req_users: {username: {$in: [username]}}}});
};
MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
// Aggiungi agli utenti Rifiutati del Gruppo
MyGroupSchema.statics.refuseReqGroup = async function(idapp, username, groupnameDest) {
return MyGroup.updateOne({idapp, groupname: groupnameDest},
{
$push:
{
refused_users: {
username,
date: new Date(),
},
},
});
};
MyGroupSchema.statics.getWhatToShow = function(idapp, username) {
// FOR ME, PERMIT ALL
return {
groupname: 1,
@@ -189,11 +208,12 @@ MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
admins: 1,
blocked: 1,
req_users: 1,
refused_users: 1,
};
}
};
MyGroupSchema.statics.getWhatToShow_Unknown = function (idapp, username) {
MyGroupSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
return {
groupname: 1,
title: 1,
@@ -204,7 +224,7 @@ MyGroupSchema.statics.getWhatToShow_Unknown = function (idapp, username) {
idCity: 1,
note: 1,
};
}
};
MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
idapp, groupname, field) {
@@ -227,13 +247,6 @@ MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
};
MyGroupSchema.statics.getUsernameReqGroupsByGroupname = async function(
idapp, groupname) {
return this.getArrUsernameFromFieldByGroupname(idapp, groupname, 'req_users');
};
MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname) {
const whatToShow = this.getWhatToShow(idapp, groupname);
@@ -252,8 +265,6 @@ MyGroupSchema.statics.deleteGroup = async function(idapp, usernameOrig, groupnam
return MyGroup.findOneAndRemove({idapp, groupname});
};
MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req) {
try {
@@ -300,11 +311,21 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req)
{deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown);
let listRefusedGroups = await MyGroup.find({
idapp,
'refused_users': {
$elemMatch: {username: {$eq: username}},
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown);
return {
listUsersGroup,
listgroups,
//listRequestUsersGroup,
listSentRequestGroups,
listRefusedGroups,
mygroups: req.user.profile.mygroups,
};
@@ -317,6 +338,7 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req)
listRequestUsersGroup: [],
listTrusted: [],
listSentRequestGroups: [],
listRefusedGroups: [],
};
};

View File

@@ -177,6 +177,15 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
} else {
newdescr = i18n.__('FRIEND_REPORTED_TO_ME', recnotif.paramsObj.username_action, recnotif.paramsObj.username_action);
}
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_UNBLOCKED) {
tag = 'unblockeduser';
if (recnotif.paramsObj.usernameDest === recnotif.paramsObj.username_action) {
newdescr = i18n.__('FRIEND_UNBLOCKED_YOU', recnotif.paramsObj.username_worked);
} else if (recnotif.paramsObj.isAdmin) {
newdescr = i18n.__('FRIEND_UNBLOCKED', recnotif.paramsObj.usernameDest, userorig);
} else {
newdescr = i18n.__('FRIEND_UNBLOCKED_TO_ME', recnotif.paramsObj.username_action, recnotif.paramsObj.username_action);
}
}
} else if (recnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
tag = 'group';
@@ -184,9 +193,9 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_NEW_REC) {
newdescr = i18n.__('GROUP_CREATED', userorig, recnotif.paramsObj.groupnameDest);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
if (recnotif.paramsObj.isAdmin)
if (recnotif.paramsObj.isAdmin) {
newdescr = i18n.__('ACCETTATO_NOTIFICA_ADMINS', userorig, recnotif.paramsObj.groupnameDest, recnotif.paramsObj.username_action);
else
} 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) {
@@ -215,6 +224,11 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
newdescr = i18n.__('GRUPPO_ELIMINATO', userorig, recnotif.paramsObj.username_action);
tag = 'deletegroup';
}
if (recnotif.paramsObj.isAdmin) {
// If is a Admin, then click to see the user's profile
recnotif.openUrl = '/my/' + userorig;
}
}
recnotif.tag = recnotif.tag ? recnotif.tag : tag;
@@ -289,7 +303,9 @@ sendNotifSchema.statics.saveAndSendNotif = async function(myrecnotif, req, res,
// console.log('myrecout._id', myrecout._id.toString());
if (save) {
await myrecout.save().then(async (res) => {
let res = null;
try {
res = await myrecout.save();
if (res) {
const idobj = res._id;
@@ -299,10 +315,11 @@ sendNotifSchema.statics.saveAndSendNotif = async function(myrecnotif, req, res,
return await globalTables.sendNotif(myrecread.typedir, myrecread.typeid, res, idapp, user ? user : req.user, myrecread);
}
}).catch((e) => {
} catch(e) {
console.log(e.message);
return null;
});
}
} else {
return await globalTables.sendNotif(myrecout.typedir, myrecout.typeid, res, idapp, user ? user : req.user, myrecout);
}
@@ -492,7 +509,7 @@ sendNotifSchema.statics.createNewNotifToSingleUser = async function(req, res, pa
}
};
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req, res) {
sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotifpass, req, res) {
const SendNotif = this;
const {User} = require('../models/user');
@@ -504,7 +521,7 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
// Send only to the destination to reach:
const userlist = await User.find({
idapp: myrecnotif.idapp,
idapp: myrecnotifpass.idapp,
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
@@ -525,39 +542,40 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
let arrregions = [];
let idSector = 0;
const mytable = globalTables.getTableByTableName(myrecnotif.tablerec);
const mytable = globalTables.getTableByTableName(myrecnotifpass.tablerec);
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotifpass.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotifpass.tablerec)) {
const myrectableorig = await mytable.findOne({_id: myrecnotif.idrec}).lean();
const myrectableorig = await mytable.findOne({_id: myrecnotifpass.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) {
if (myrecnotifpass.tablerec === shared_consts.TABLES_MYGOODS) {
idSector = myrectableorig.idSectorGood;
} else {
idSector = myrectableorig.idSector;
}
}
myrecnotif.myrectableorig = myrectableorig;
myrecnotifpass.myrectableorig = myrectableorig;
}
myrecnotif = this.getDescrAndLinkByRecNotif(myrecnotif);
myrecnotifpass = this.getDescrAndLinkByRecNotif(myrecnotifpass);
delete myrecnotifpass._doc['_id'];
for (const user of userlist) {
if (user.profile.notifs) {
const usernotifprofile = user.profile.notifs.find((notif) => notif.dir === myrecnotif.typedir);
if (user.profile && user.profile.notifs) {
const usernotifprofile = user.profile.notifs.find((notif) => notif.dir === myrecnotifpass.typedir);
let send = false;
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotif.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotif.tablerec)) {
if (shared_consts.TABLES_ADV_NOTIFICATION.includes(myrecnotifpass.tablerec) ||
shared_consts.TABLES_EVENTS_NOTIFICATION.includes(myrecnotifpass.tablerec)) {
// Estrai la Città, la Provincia e la regione.
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.UsersNotif.NEW_ADV_PROVINCE) &&
@@ -572,7 +590,7 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
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 (myrecnotifpass.tablerec === shared_consts.TABLES_MYGOODS) {
if (user.profile.notif_sector_goods) {
send = send && (user.profile.notif_sector_goods.includes(idSector));
}
@@ -583,13 +601,14 @@ sendNotifSchema.statics.sendToTheDestinations = async function(myrecnotif, req,
}
if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotif.tablerec)) {
if (shared_consts.TABLES_GROUPS_NOTIFICATION.includes(myrecnotifpass.tablerec)) {
if (usernotifprofile && tools.isBitActive(usernotifprofile.value, shared_consts.GroupsNotifs.STATUS_GROUPS_NEW)) {
send = true;
}
}
if (send) {
let myrecnotif = new SendNotif(myrecnotifpass);
myrecnotif.dest = user.username;
await SendNotif.saveAndSendNotif(myrecnotif, req, res, user);
}

View File

@@ -1777,6 +1777,36 @@ UserSchema.statics.setFriendsCmd = async function(req, idapp, usernameOrig, user
shared_consts.TypeNotifs.ID_FRIENDS_REPORTED);
}
}
} else if (cmd === shared_consts.FRIENDSCMD.UNBLOCK_USER) {
username_worked = usernameDest;
// Sblocco la persona
ris = await User.updateOne({idapp, username: username_worked}, {
$set: {
reported: false,
},
});
if (ris) {
// Send a notification to the DESTINATION!
await SendNotif.createNewNotifToSingleUser(req, null, {username_worked, usernameDest, username_action}, false, shared_consts.TypeNotifs.TYPEDIR_FRIENDS,
shared_consts.TypeNotifs.ID_FRIENDS_UNBLOCKED);
// Send a notification to the SENDER !
// Hai segnalato %s da %s per comportamenti non idonei.
await SendNotif.createNewNotifToSingleUser(req, null, {username_worked, usernameDest: username_action, username_action}, false,
shared_consts.TypeNotifs.TYPEDIR_FRIENDS,
shared_consts.TypeNotifs.ID_FRIENDS_UNBLOCKED);
if (usernameOrig !== telegrambot.ADMIN_USER_SERVER) {
// Send a notification to the Admin
await SendNotif.createNewNotifToSingleUser(req, null, {username_worked, usernameDest: telegrambot.ADMIN_USER_SERVER, username_action, isAdmin: true}, false,
shared_consts.TypeNotifs.TYPEDIR_FRIENDS,
shared_consts.TypeNotifs.ID_FRIENDS_UNBLOCKED);
}
}
}
} catch (e) {
@@ -1832,8 +1862,11 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
// Elimina la richiesta:
update = {$pull: {req_users: {username: {$in: [usernameOrig]}}}};
ris = await MyGroup.updateOne({idapp, groupname: groupnameDest},
update);
await MyGroup.updateOne({idapp, groupname: groupnameDest}, update);
// Elimina eventualmente se era bloccato:
update = {$pull: {refused_users: {username: {$in: [usernameOrig]}}}};
await MyGroup.updateOne({idapp, groupname: groupnameDest}, update);
} else {
ris = false;
}
@@ -1892,6 +1925,8 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
} else if (cmd === shared_consts.GROUPSCMD.REFUSE_REQ_GROUP) {
ris = await MyGroup.refuseReqGroup(idapp, usernameOrig, groupnameDest); // Aggiungi alla lista utenti Rifiutati
ris = await MyGroup.removeReqGroup(idapp, usernameOrig, groupnameDest); // Rimuovo la richiesta di entrare nel gruppo
console.log('ris', ris);
@@ -3443,6 +3478,20 @@ UserSchema.statics.addExtraInfo = async function(idapp, recUser) {
? listSentMyRequestGroups
: [];
const listRefusedGroups = await MyGroup.find({
idapp,
'refused_users': {
$elemMatch: {username: {$eq: recUser.username}},
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, MyGroup.getWhatToShow_Unknown()).lean();
recUser._doc.profile.refused_groups = listRefusedGroups
? listRefusedGroups
: [];
const listManageGroups = await MyGroup.find({
idapp,
'admins': {