Richieste d'Amicizia: aggiunto data di richiesta e di amicizia

This commit is contained in:
paoloar77
2022-01-15 12:25:57 +01:00
parent e5a0ecb6ce
commit 0d96cf72b6

View File

@@ -138,6 +138,9 @@ const UserSchema = new mongoose.Schema({
verified_by_aportador: { verified_by_aportador: {
type: Boolean, type: Boolean,
}, },
trust_modified: {
type: Date,
},
aportador_iniziale: { aportador_iniziale: {
type: String, type: String,
}, },
@@ -311,10 +314,19 @@ const UserSchema = new mongoose.Schema({
description: {type: String}, description: {type: String},
rating: {type: Number}, rating: {type: Number},
}], }],
friends: [], // username friends: [
req_friends: [], // username {
_id: false,
username: {type: String},
date: {type: Date},
}], // username
req_friends: [
{
_id: false,
username: {type: String},
date: {type: Date},
}], // username
}, },
}) })
; ;
@@ -1203,23 +1215,50 @@ UserSchema.statics.getUserProfileByUsername = async function(idapp, username) {
}; };
UserSchema.statics.getUsernameFriendsByUsername = async function( UserSchema.statics.getArrUsernameFromFieldByUsername = async function(
idapp, username) { idapp, username, field, subfield) {
return User.findOne({ const myobj = {};
myobj[field + '.' + subfield] = 1;
let arrrec = await User.findOne({
idapp, 'username': username, idapp, 'username': username,
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
}, {'profile.friends': 1}).then((rec) => rec ? rec._doc.profile.friends : []); }, myobj).then((ris) => ris ? ris._doc[field][subfield] : []);
if (arrrec.length > 0) {
return arrrec.map(m => m.username);
}
return [];
}; };
UserSchema.statics.getUsernameReqFriendsByUsername = async function( UserSchema.statics.getUsernameReqFriendsByUsername = async function(
idapp, username) { idapp, username) {
return User.findOne({ return this.getArrUsernameFromFieldByUsername(idapp, username, 'profile',
idapp, 'username': username, 'req_friends');
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
}, {'profile.req_friends': 1}). };
then((rec) => rec ? rec._doc.profile.req_friends : []);
UserSchema.statics.getUsernameFriendsByUsername = async function(
idapp, username) {
return this.getArrUsernameFromFieldByUsername(idapp, username, 'profile',
'friends');
};
// Rimuovo l'Amicizia
UserSchema.statics.removeFriend = async function(idapp, username, usernameDest) {
return User.updateOne({idapp, username},
{$pull: {'profile.friends': {username: {$in: [usernameDest]}}}});
};
// Rimuovo la Richiesta di Amicizia
UserSchema.statics.removeReqFriend = async function(idapp, username, usernameDest) {
return User.updateOne({idapp, username: username},
{$pull: {'profile.req_friends': {username: {$in: [usernameDest]}}}});
}; };
UserSchema.statics.setFriendsCmd = async function( UserSchema.statics.setFriendsCmd = async function(
@@ -1227,13 +1266,16 @@ UserSchema.statics.setFriendsCmd = async function(
let ris = null; let ris = null;
let update = {}; let update = {};
try {
if (cmd === shared_consts.FRIENDSCMD.SETTRUST) { if (cmd === shared_consts.FRIENDSCMD.SETTRUST) {
// Aggiungi alle amicizie // Aggiungi alle amicizie
await this.setFriendsCmd(idapp, usernameOrig, usernameDest, shared_consts.FRIENDSCMD.SETFRIEND, value); await this.setFriendsCmd(idapp, usernameOrig, usernameDest,
shared_consts.FRIENDSCMD.SETFRIEND, value);
return User.updateOne({idapp, username: usernameDest}, return User.updateOne({idapp, username: usernameDest},
{$set: {verified_by_aportador: value}}, {new: false}); {$set: {verified_by_aportador: value, trust_modified: new Date()}},
{new: false});
} else if (cmd === shared_consts.FRIENDSCMD.SETFRIEND) { } else if (cmd === shared_consts.FRIENDSCMD.SETFRIEND) {
// Aggiungo l'Amicizia a me // Aggiungo l'Amicizia a me
@@ -1241,15 +1283,22 @@ UserSchema.statics.setFriendsCmd = async function(
idapp, idapp,
username: usernameOrig, username: usernameOrig,
'profile.friends': { 'profile.friends': {
$elemMatch: {$eq: usernameDest}, $elemMatch: {$eq: {username: usernameDest}},
}, },
}); });
if (!foundIfAlreadyFriend) { if (!foundIfAlreadyFriend) {
update = {$push: {'profile.friends': [usernameDest]}}; update = {
$push: {
'profile.friends': {
username: usernameDest,
date: new Date(),
},
},
};
ris = await User.updateOne({idapp, username: usernameOrig}, update); ris = await User.updateOne({idapp, username: usernameOrig}, update);
update = {$pullAll: {'profile.req_friends': [usernameDest]}}; update = {$pull: {'profile.req_friends': {username: {$in: [usernameDest]}}}};
ris = await User.updateOne({idapp, username: usernameOrig}, update); ris = await User.updateOne({idapp, username: usernameOrig}, update);
} }
@@ -1258,16 +1307,22 @@ UserSchema.statics.setFriendsCmd = async function(
idapp, idapp,
username: usernameDest, username: usernameDest,
'profile.friends': { 'profile.friends': {
$elemMatch: {$eq: usernameOrig}, $elemMatch: {$eq: {username: usernameOrig}},
}, },
}); });
if (!foundIfAlreadyFriend2) { if (!foundIfAlreadyFriend2) {
update = {$push: {'profile.friends': [usernameOrig]}}; update = {
$push: {
'profile.friends': {
username: usernameOrig,
date: new Date(),
},
},
};
ris = await User.updateOne({idapp, username: usernameDest}, update); ris = await User.updateOne({idapp, username: usernameDest}, update);
update = {$pullAll: {'profile.req_friends': [usernameOrig]}}; this.removeReqFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da me
ris = await User.updateOne({idapp, username: usernameDest}, update);
} }
if (ris) { if (ris) {
ris = await User.getInfoFriendByUsername(idapp, usernameDest); ris = await User.getInfoFriendByUsername(idapp, usernameDest);
@@ -1278,13 +1333,20 @@ UserSchema.statics.setFriendsCmd = async function(
idapp, idapp,
username: usernameDest, username: usernameDest,
'profile.req_friends': { 'profile.req_friends': {
$elemMatch: {$eq: usernameOrig}, $elemMatch: {$eq: {username: usernameOrig}},
}, },
}); });
if (value) { if (value) {
if (!foundIfAlreadyAskFriend) { if (!foundIfAlreadyAskFriend) {
update = {$push: {'profile.req_friends': [usernameOrig]}}; update = {
$push: {
'profile.req_friends': {
username: usernameOrig,
date: new Date(),
},
},
};
ris = await User.updateOne({idapp, username: usernameDest}, update); ris = await User.updateOne({idapp, username: usernameDest}, update);
} }
if (ris) { if (ris) {
@@ -1293,8 +1355,7 @@ UserSchema.statics.setFriendsCmd = async function(
} }
} else { } else {
if (foundIfAlreadyAskFriend) { if (foundIfAlreadyAskFriend) {
update = {$pullAll: {'profile.req_friends': [usernameOrig]}}; ris = await this.removeFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da me
ris = await User.updateOne({idapp, username: usernameDest}, update);
} }
} }
@@ -1303,28 +1364,19 @@ UserSchema.statics.setFriendsCmd = async function(
} }
} else if (cmd === shared_consts.FRIENDSCMD.REMOVE_FROM_MYFRIENDS) { } else if (cmd === shared_consts.FRIENDSCMD.REMOVE_FROM_MYFRIENDS) {
// Rimuovo l'Amicizia da lui
await User.updateOne({idapp, username: usernameDest}, await this.removeFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da lui
{$pullAll: {'profile.friends': [usernameOrig]}}); ris = await this.removeFriend(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
// Rimuovo l'Amicizia da me
ris = await User.updateOne({idapp, username: usernameOrig},
{$pullAll: {'profile.friends': [usernameDest]}});
} else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_FRIEND) { } else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_FRIEND) {
// Rimuovo la Richiesta di Amicizia da lui await this.removeReqFriend(idapp, usernameDest, usernameOrig); // Rimuovo la Richiesta di Amicizia da lui
await User.updateOne({idapp, username: usernameDest}, ris = await this.removeFriend(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
{$pullAll: {'profile.req_friends': [usernameOrig]}});
// Rimuovo l'Amicizia da me
ris = await User.updateOne({idapp, username: usernameOrig},
{$pullAll: {'profile.friends': [usernameDest]}});
} else if (cmd === shared_consts.FRIENDSCMD.BLOCK_USER) { } else if (cmd === shared_consts.FRIENDSCMD.BLOCK_USER) {
// Rimuovo l'Amicizia da lui
await User.updateOne({idapp, username: usernameDest}, await this.removeFriend(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da lui
{$pullAll: {'profile.friends': [usernameOrig]}}); await this.removeFriend(idapp, usernameOrig, usernameDest); // Rimuovo l'Amicizia da me
// Rimuovo l'Amicizia da me
await User.updateOne({idapp, username: usernameOrig},
{$pullAll: {'profile.friends': [usernameDest]}});
// Blocco la persona // Blocco la persona
ris = await User.updateOne({idapp, username: usernameDest}, { ris = await User.updateOne({idapp, username: usernameDest}, {
@@ -1336,6 +1388,9 @@ UserSchema.statics.setFriendsCmd = async function(
}, },
}); });
} }
} catch (e) {
console.error('Error: ', e);
}
return ris; return ris;
}; };
@@ -1416,7 +1471,7 @@ UserSchema.statics.getAskedFriendsByUsername = async function(idapp, username) {
return User.find({ return User.find({
idapp, idapp,
'profile.req_friends': { 'profile.req_friends': {
$elemMatch: {$eq: username}, $elemMatch: {$eq: {username: username}},
}, },
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}], $or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown).then((rec) => { }, whatToShow_Unknown).then((rec) => {
@@ -1455,7 +1510,7 @@ UserSchema.statics.getFriendsByUsername = async function(idapp, username) {
let listSentRequestFriends = await User.find({ let listSentRequestFriends = await User.find({
idapp, idapp,
'profile.req_friends': { 'profile.req_friends': {
$elemMatch: {$eq: username}, $elemMatch: {$eq: {username: username}},
}, },
$or: [ $or: [
{deleted: {$exists: false}}, {deleted: {$exists: false}},
@@ -1469,7 +1524,6 @@ UserSchema.statics.getFriendsByUsername = async function(idapp, username) {
{deleted: {$exists: true, $eq: false}}], {deleted: {$exists: true, $eq: false}}],
}, whatToShow); }, whatToShow);
return { return {
listFriends, listFriends,
listRequestFriends, listRequestFriends,
@@ -1487,7 +1541,7 @@ UserSchema.statics.getFriendsByUsername = async function(idapp, username) {
listTrusted: [], listTrusted: [],
listSentRequestFriends: [], listSentRequestFriends: [],
} };
}; };
UserSchema.statics.getAportadorSolidarioByUsername = async function( UserSchema.statics.getAportadorSolidarioByUsername = async function(