strette di mano

This commit is contained in:
Surya Paolo
2023-01-06 15:51:48 +01:00
parent f42a39d231
commit d183a07bad
7 changed files with 334 additions and 15 deletions

View File

@@ -379,6 +379,18 @@ const UserSchema = new mongoose.Schema({
username: { type: String },
date: { type: Date },
}], // username
handshake: [
{
_id: false,
username: { type: String },
date: { type: Date },
}], // username
req_handshake: [
{
_id: false,
username: { type: String },
date: { type: Date },
}], // username
mygroups: [
{
_id: false,
@@ -1542,6 +1554,8 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.born_province': 1,
'profile.born_country': 1,
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
email: 1,
date_reg: 1,
'useraport.username': 1,
@@ -1583,6 +1597,8 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.born_province': 1,
'profile.born_country': 1,
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
email: 1,
date_reg: 1,
'useraport.username': 1,
@@ -1625,6 +1641,8 @@ UserSchema.statics.getUserProfileByUsername = async function (
'profile.born_province': 1,
'profile.born_country': 1,
'profile.calc': 1,
'profile.handshake': 1,
'profile.friends': 1,
'mycities': 1,
'comune': 1,
email: 1,
@@ -1757,6 +1775,21 @@ UserSchema.statics.getUsernameFriendsByUsername = async function (
return await this.getArrUsernameFromFieldByUsername(idapp, username, 'profile',
'friends');
};
UserSchema.statics.getUsernameReqHandShakeByUsername = async function (
idapp, username) {
return await this.getArrUsernameFromFieldByUsername(idapp, username, 'profile',
'req_handshake');
};
UserSchema.statics.getUsernameHandShakeByUsername = async function (
idapp, username) {
return await this.getArrUsernameFromFieldByUsername(idapp, username, 'profile',
'handshake');
};
UserSchema.statics.getUsernameGroupsByUsername = async function (
@@ -1781,6 +1814,13 @@ UserSchema.statics.removeFriend = async function (
{ $pull: { 'profile.friends': { username: { $in: [usernameDest] } } } });
};
// Rimuovo l'Amicizia
UserSchema.statics.removeHandShake = async function (
idapp, username, usernameDest) {
return await User.updateOne({ idapp, username },
{ $pull: { 'profile.handshake': { username: { $in: [usernameDest] } } } });
};
// Rimuovo il Gruppo
UserSchema.statics.removeFromMyGroups = async function (
idapp, username, groupnameDest) {
@@ -1813,6 +1853,13 @@ UserSchema.statics.removeReqFriend = async function (
{ $pull: { 'profile.req_friends': { username: { $in: [usernameDest] } } } });
};
// Rimuovo la Stretta di Mano
UserSchema.statics.removeReqHandShake = async function (
idapp, username, usernameDest) {
return await User.updateOne({ idapp, username: username },
{ $pull: { 'profile.req_handshake': { username: { $in: [usernameDest] } } } });
};
UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, usernameDest, cmd, value, disablenotif) {
const { SendNotif } = require('../models/sendnotif');
@@ -1920,6 +1967,87 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
//if (ris) {
ris = await User.getInfoFriendByUsername(idapp, usernameDest);
//}
} else if (cmd === shared_consts.FRIENDSCMD.SETHANDSHAKE) {
// Aggiungo l'Amicizia a me
const foundIfAlreadyFriend = await User.findOne({
idapp,
username: usernameOrig,
'profile.handshake': {
$elemMatch: { username: { $eq: usernameDest } },
},
}, { _id: 1 }).lean();
if (!foundIfAlreadyFriend) {
update = {
$push: {
'profile.handshake': {
username: usernameDest,
date: new Date(),
},
},
};
ris = await User.updateOne({ idapp, username: usernameOrig }, update);
if (!disablenotif) {
// Send a notification to the DESTINATION FRIENDSHIP !
let req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest }, true, shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE,
shared_consts.TypeNotifs.ID_HANDSHAKE_ACCEPTED);
// Send a notification to the SENDER HANDSHAKEHIP !
req = tools.getReqByPar(idapp, usernameDest);
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest: usernameOrig }, true, shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE,
shared_consts.TypeNotifs.ID_HANDSHAKE_ACCEPTED_MY_REQUEST);
}
update = { $pull: { 'profile.req_handshake': { username: { $in: [usernameDest] } } } };
ris = await User.updateOne({ idapp, username: usernameOrig }, update);
if (ris) {
try {
if (!disablenotif) {
const userDest = await User.getRecLangAndIdByUsername(idapp, usernameDest);
const user = await User.getRecLangAndIdByUsername(idapp, usernameOrig);
const msgDest = i18n.__({ phrase: '✅ %s accepted your HandShake request !', locale: user.lang }, usernameOrig);
const msgOrig = i18n.__({ phrase: '✅ You have accepted %s\' HandShake request!', locale: userDest.lang }, usernameDest);
await telegrambot.sendMsgTelegram(idapp, usernameDest, msgDest);
await telegrambot.sendMsgTelegram(idapp, usernameOrig, msgOrig);
}
} catch (e) {
console.error('Notification : ', e);
}
}
}
// Controlla se lui aveva già la mia amicizia
const foundIfAlreadyFriend2 = await User.findOne({
idapp,
username: usernameDest,
'profile.handshake': {
$elemMatch: { username: { $eq: usernameOrig } },
},
}, { _id: 1 }).lean();
if (!foundIfAlreadyFriend2) {
update = {
$push: {
'profile.handshake': {
username: usernameOrig,
date: new Date(),
},
},
};
ris = await User.updateOne({ idapp, username: usernameDest }, update);
this.removeReqHandShake(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da me
this.removeReqHandShake(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da te
}
//if (ris) {
ris = await User.getInfoFriendByUsername(idapp, usernameDest);
//}
} else if (cmd === shared_consts.FRIENDSCMD.REQFRIEND) {
// Aggiungo la richiesta di Amicizia a me
const foundIfAlreadyAskFriend = await User.findOne({
@@ -1946,9 +2074,36 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
// Invia una notifica alla persona
tools.sendNotificationByUsername(idapp, usernameDest, cmd, true, usernameOrig);
}
}
} else if (cmd === shared_consts.FRIENDSCMD.REQHANDSHAKE) {
// Aggiungo la richiesta di Amicizia a me
const foundIfAlreadyAskFriend = await User.findOne({
idapp,
username: usernameDest,
'profile.req_handshake': {
$elemMatch: { username: { $eq: usernameOrig } },
},
}, { _id: 1 }).lean();
if (value) {
if (!foundIfAlreadyAskFriend) {
update = {
$push: {
'profile.req_handshake': {
username: usernameOrig,
date: new Date(),
},
},
};
ris = await User.updateOne({ idapp, username: usernameDest }, update);
}
if (ris) {
// Invia una notifica alla persona
tools.sendNotificationByUsername(idapp, usernameDest, cmd, true, usernameOrig);
}
} else {
if (foundIfAlreadyAskFriend) {
ris = await this.removeReqFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da me
ris = await this.removeReqHandShake(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da me
}
}
@@ -1965,6 +2120,15 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
await this.removeFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da lui
ris = await this.removeFriend(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
} else if (cmd === shared_consts.FRIENDSCMD.REMOVE_FROM_MYHANDSHAKE) {
// Rimuovi anche le eventuali richieste di Amicizia !
await this.removeReqHandShake(idapp, usernameDest, usernameOrig); // Rimuovo la Richiesta di Amicizia da lui
await this.removeReqHandShake(idapp, usernameOrig, usernameDest); // Rimuovo la Richiesta di Amicizia da me
await this.removeHandShake(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da lui
ris = await this.removeHandShake(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
} else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_FRIEND) {
// CREATE NOTIFICATION IN TABLE SENDNOTIF
@@ -1975,6 +2139,16 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
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
} else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_HANDSHAKE) {
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest }, true, shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE,
shared_consts.TypeNotifs.ID_HANDSHAKE_REFUSED);
await this.removeReqHandShake(idapp, usernameDest, usernameOrig); // Rimuovo la Richiesta di Amicizia da lui
ris = await this.removeHandShake(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
} else if (cmd === shared_consts.FRIENDSCMD.BLOCK_USER) {
await this.removeReqFriend(idapp, usernameDest, usernameOrig); // Rimuovo la Richiesta di Amicizia da lui
@@ -2602,6 +2776,8 @@ function getWhatToShow(idapp, username) {
'profile.calc': 1,
email: 1,
date_reg: 1,
'profile.friends': 1,
'profile.handshake': 1,
};
}
@@ -2622,8 +2798,9 @@ function getWhatToShow_Unknown(idapp, username) {
'profile.born_country': 1,
'profile.calc': 1,
date_reg: 1,
};
'profile.handshake': 1,
'profile.friends': 1,
}
}
UserSchema.statics.getWhatToShow_IfFriends = async function (idapp, username) {
@@ -2646,7 +2823,8 @@ UserSchema.statics.getWhatToShow_IfFriends = async function (idapp, username) {
username_who_report: 1,
date_reg: 1,
groups: 1,
friends: 1,
'profile.handshake': 1,
'profile.friends': 1,
};
};
@@ -2693,6 +2871,23 @@ UserSchema.statics.getAskedFriendsByUsername = async function (idapp, username)
};
UserSchema.statics.getAskedHandShakeByUsername = async function (idapp, username) {
const whatToShow_Unknown = getWhatToShow_Unknown(idapp, username);
return await User.find({
idapp,
'profile.req_handshake': {
$elemMatch: { username: { $eq: username } },
},
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown).then((rec) => {
//return rec.map(m => m.username);
});
};
UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
if (!username) {
@@ -2711,6 +2906,10 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
username);
const arrUsernameReqFriends = await User.getUsernameReqFriendsByUsername(
idapp, username);
const arrUsernameHandShake = await User.getUsernameHandShakeByUsername(idapp,
username);
const arrUsernameReqHandShake = await User.getUsernameReqHandShakeByUsername(
idapp, username);
let listFriends = await User.find({
idapp,
@@ -2720,6 +2919,14 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow);
let listHandShake = await User.find({
idapp,
username: { $in: arrUsernameHandShake },
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow);
let listRequestFriends = await User.find({
idapp,
username: { $in: arrUsernameReqFriends },
@@ -2728,6 +2935,14 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
let listRequestHandShake = await User.find({
idapp,
username: { $in: arrUsernameReqHandShake },
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
let listSentRequestFriends = await User.find({
idapp,
'profile.req_friends': {
@@ -2738,6 +2953,16 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
let listSentRequestHandShake = await User.find({
idapp,
'profile.req_handshake': {
$elemMatch: { username: { $eq: username } },
},
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
let listTrusted = await User.find({
idapp, aportador_solidario: username,
'profile.teleg_id': { $gt: 0 },
@@ -2748,9 +2973,12 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
return {
listFriends,
listHandShake,
listRequestFriends,
listRequestHandShake,
listTrusted,
listSentRequestFriends,
listSentRequestHandShake,
};
} catch (e) {
@@ -2759,9 +2987,12 @@ UserSchema.statics.getFriendsByUsername = async function (idapp, username) {
return {
listFriends: [],
listHandShake: [],
listRequestFriends: [],
listRequestHandShake: [],
listTrusted: [],
listSentRequestFriends: [],
listSentRequestHandShake: [],
};
};
@@ -4298,6 +4529,20 @@ UserSchema.statics.addExtraInfo = async function (idapp, recUser) {
? listSentMyRequestFriends
: [];
const listSentMyRequestHandShake = await User.find({
idapp,
'profile.req_handshake': {
$elemMatch: { username: { $eq: recUser.username } },
},
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, { username: 1 }).lean();
recUser._doc.profile.asked_handshake = listSentMyRequestHandShake
? listSentMyRequestHandShake
: [];
const listSentMyRequestGroups = await MyGroup.find({
idapp,
'req_users': {