Stringi la mano (fase prima)

This commit is contained in:
Surya Paolo
2023-01-08 19:20:02 +01:00
parent 399153f477
commit 331c2e6780
5 changed files with 144 additions and 28 deletions

View File

@@ -1982,12 +1982,12 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
ris = await User.getInfoFriendByUsername(idapp, usernameDest);
//}
} else if (cmd === shared_consts.FRIENDSCMD.SETHANDSHAKE) {
// Aggiungo l'Amicizia a me
// Aggiungo la Stretta di mano a lui
const foundIfAlreadyFriend = await User.findOne({
idapp,
username: usernameOrig,
username: usernameDest,
'profile.handshake': {
$elemMatch: { username: { $eq: usernameDest } },
$elemMatch: { username: { $eq: usernameOrig } },
},
}, { _id: 1 }).lean();
@@ -1995,29 +1995,20 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
update = {
$push: {
'profile.handshake': {
username: usernameDest,
username: usernameOrig,
date: new Date(),
},
},
};
ris = await User.updateOne({ idapp, username: usernameOrig }, update);
ris = await User.updateOne({ idapp, username: usernameDest }, update);
if (!disablenotif) {
// Send a notification to the DESTINATION HANDSHAKE !
let req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest }, true, shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE,
shared_consts.TypeNotifs.ID_HANDSHAKE_ACCEPTED_MY_REQUEST);
// 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);
}
update = { $pull: { 'profile.req_handshake': { username: { $in: [usernameDest] } } } };
ris = await User.updateOne({ idapp, username: usernameOrig }, update);
if (ris) {
try {
if (!disablenotif) {
@@ -2136,12 +2127,7 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
} 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
ris = await this.removeHandShake(idapp, usernameDest, usernameOrig); // Rimuovo l'Amicizia da lui
} else if (cmd === shared_consts.FRIENDSCMD.CANCEL_REQ_FRIEND) {
@@ -2150,9 +2136,6 @@ UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, use
await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest }, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS,
shared_consts.TypeNotifs.ID_FRIENDS_REFUSED);
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
@@ -3946,6 +3929,18 @@ UserSchema.statics.getDiffusoriUsers = async function (idapp) {
};
UserSchema.statics.getBestStretteDiManoUsers = async function (idapp) {
const User = this;
const lastn = 10;
return await User.aggregate(User.getQueryUsersStretteDiMano(idapp)).then(ris => {
// console.table(ris);
return ris;
});
};
UserSchema.statics.checkUser = async function (idapp, username) {
const User = this;
@@ -4152,6 +4147,102 @@ UserSchema.statics.getQueryUsersDiffusori = function (idapp) {
return query;
};
UserSchema.statics.getQueryUsersStretteDiMano = function (idapp) {
const query = [
{
$match: {
idapp,
$or: [
{
deleted: {
$exists: false,
},
},
{
deleted: {
$exists: true,
$eq: false,
},
},
],
},
},
{
$group: {
_id: "$profile.handshake",
count: {
$sum: 1,
},
},
},
{
$match: { "count": { $gte: 2 } }
},
{
$sort: {
count: -1,
},
},
{ $limit: 20 },
{
$lookup: {
from: "users",
let: {
username: "$_id",
idapp,
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$$username",
"$username",
],
},
{
$eq: [
"$$idapp",
"$idapp",
],
},
],
},
},
},
],
as: "user",
},
},
{ $unwind: "$user" },
{
$replaceRoot: {
newRoot: {
$mergeObjects: ["$user", "$$ROOT"],
},
},
},
{
$project: {
_id: 0,
count: 1,
aportador_solidario: 1,
username: 1,
name: 1,
surname: 1,
lasttimeonline: 1,
idapp: 1,
"profile.img": 1,
'profile.handshake': 1,
},
},
];
return query;
};
UserSchema.statics.getUsersRegWeekly = function (idapp, nrec) {
const query = [