From fc8e8d8034072f85639a04a9c5e3c32bc891c937 Mon Sep 17 00:00:00 2001 From: paoloar77 Date: Mon, 8 Aug 2022 16:35:32 +0200 Subject: [PATCH] - Refused User - Report User - Unblock User - refresh tables when an action (setFriends and setGroups) occurred. - fix duplicate call of loadsite --- src/server/locales/it.json | 5 ++- src/server/models/mygroup.js | 58 +++++++++++++++++++--------- src/server/models/sendnotif.js | 59 ++++++++++++++++++---------- src/server/models/user.js | 53 ++++++++++++++++++++++++- src/server/router/sendmsg_router.js | 3 +- src/server/tools/general.js | 60 +++++++++++++++++++++-------- src/server/tools/shared_nodejs.js | 3 ++ 7 files changed, 182 insertions(+), 59 deletions(-) diff --git a/src/server/locales/it.json b/src/server/locales/it.json index d380845..c4462cf 100644 --- a/src/server/locales/it.json +++ b/src/server/locales/it.json @@ -25,5 +25,8 @@ "GRUPPO_ELIMINATO": "Il gruppo %s è stato eliminato da parte di %s", "FRIEND_REPORTED_TO_ME": "Sei stato segnalato da %s per comportamenti non idonei. Contatta %s per chiarimenti", "FRIEND_REPORTED": "E' stato segnalato %s da %s per comportamenti non idonei.", - "FRIEND_REPORTED_YOU": "Hai segnalato %s per comportamenti non idonei." + "FRIEND_REPORTED_YOU": "Hai segnalato %s per comportamenti non idonei.", + "FRIEND_UNBLOCKED_TO_ME": "Sei stato riattivato da %s", + "FRIEND_UNBLOCKED": "E' stato riattivato %s da %s.", + "FRIEND_UNBLOCKED_YOU": "Hai riattivato %s." } diff --git a/src/server/models/mygroup.js b/src/server/models/mygroup.js index 7233886..d9b9c5f 100755 --- a/src/server/models/mygroup.js +++ b/src/server/models/mygroup.js @@ -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: [], }; }; diff --git a/src/server/models/sendnotif.js b/src/server/models/sendnotif.js index 9437a6f..b407a02 100755 --- a/src/server/models/sendnotif.js +++ b/src/server/models/sendnotif.js @@ -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); } diff --git a/src/server/models/user.js b/src/server/models/user.js index 0f13324..3189bc7 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -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': { diff --git a/src/server/router/sendmsg_router.js b/src/server/router/sendmsg_router.js index 313051e..9dd492f 100755 --- a/src/server/router/sendmsg_router.js +++ b/src/server/router/sendmsg_router.js @@ -50,7 +50,8 @@ router.post('/', authenticate, (req, res) => { recmsg.typedir = shared_consts.TypeNotifs.TYPEDIR_EVENTS; recmsg.typeid = shared_consts.TypeNotifs.ID_EVENTS_REMOVE_REC; - return await SendNotif.saveAndSendNotif(recmyevent, req, res).then((out) => { + // ##Todo !! DA SISTEMARE !!! + return await SendNotif.saveAndSendNotif(recmsg, req, res).then((out) => { if (out) return res.send({ code: server_constants.RIS_CODE_OK, msg: '', id: recmsg._id }); else diff --git a/src/server/tools/general.js b/src/server/tools/general.js index 56b4a90..7fe519c 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -937,7 +937,7 @@ module.exports = { }, sendNotifGroupByUsername: async function( - cmd, typeid_pass, idapp, usernameOrig, usernameDest, username_action, groupname, myrecgroup, isAdmin, username_worked) { + cmd, idapp, usernameOrig, usernameDest, username_action, groupname, myrecgroup, isAdmin, username_worked) { const {SendNotif} = require('../models/sendnotif'); const {User} = require('../models/user'); @@ -991,9 +991,6 @@ module.exports = { typeid = shared_consts.TypeNotifs.ID_GROUP_DELETE_USER; } - if (typeid_pass) { - typeid = typeid_pass; - } } if (sendnotif && typeid > 0) { @@ -1023,7 +1020,7 @@ module.exports = { if (usernameOrig === singleadmin.username) giainviato = true; - await this.sendNotifGroupByUsername(cmd, 0, idapp, usernameOrig, singleadmin.username, username_action, groupname, group, true); + await this.sendNotifGroupByUsername(cmd, idapp, usernameOrig, singleadmin.username, username_action, groupname, group, true); } } catch (e) { console.error('sendNotificationByGroupname', e); @@ -1032,9 +1029,11 @@ module.exports = { if (!giainviato && (cmd !== shared_consts.GROUPSCMD.REQGROUP)) { // SEND TO THE USER THE NOTIFICATION - await this.sendNotifGroupByUsername(cmd, value, idapp, usernameOrig, usernameOrig, username_action, groupname, false); + await this.sendNotifGroupByUsername(cmd, idapp, usernameOrig, usernameOrig, username_action, groupname, false); } + + } catch (e) { console.error('e', e); } @@ -1652,17 +1651,6 @@ module.exports = { numrowend = 1; if (params.querytype === shared_consts.QUERYTYPE_MYGROUP) { - // query.push({'$addFields': {'myid': {'$toObjectId': params.myid}}}); - /*query.push({ - $match: { - $and: [ - {idapp}, - {'_id': '$myid'}, - ], - }, - }); - - */ query.push({ $match: { $expr: { @@ -1700,6 +1688,44 @@ module.exports = { }, ); + } else if (params.querytype === shared_consts.QUERYTYPE_REFUSED_USER_GRP) { + query.push({ + $match: { + $expr: { + $eq: ['$_id', params.myid], + }, + }, + }); + query.push({$project: {refused_users: 1}}); + const qa1 = this.getLookup( + { + lk_tab: 'users', + lk_LF: 'refused_users.username', + lk_FF: 'username', + lk_as: 'user', + }, 0, { + 'user.idapp': 1, + 'user.username': 1, + 'user.profile.img': 1, + 'user.profile.qualifica': 1, + }); + if (qa1) query = [...query, ...qa1]; + query.push({$unwind: '$user'}); + query.push({ + $match: { + $and: [ + {'user.idapp': idapp}, + ], + }, + }); + + query.push({ + $replaceRoot: { + newRoot: '$user', + }, + }, + ); + } if (newvers) { diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 3502612..32b00be 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -5,6 +5,7 @@ module.exports = { }, QUERYTYPE_MYGROUP: 1, + QUERYTYPE_REFUSED_USER_GRP: 2, ALL_SAW_AND_ACCEPTED: 3, // --------------------- @@ -46,6 +47,7 @@ module.exports = { REFUSE_REQ_FRIEND: 145, CANCEL_REQ_FRIEND: 146, BLOCK_USER: 155, + UNBLOCK_USER: 156, REPORT_USER: 158, FIND_PEOPLE: 166, }, @@ -321,6 +323,7 @@ module.exports = { ID_FRIENDS_REFUSED: 4, ID_FRIENDS_ACCEPTED_MY_REQUEST: 8, ID_FRIENDS_REPORTED: 16, + ID_FRIENDS_UNBLOCKED: 32, TYPEDIR_GROUPS: 4, ID_GROUP_NEW_REC: 1,