Friends Notification

Starting Group Create Notification...
This commit is contained in:
Paolo Arena
2022-07-28 21:47:23 +02:00
parent 50c6031789
commit 7cda5dbb8b
8 changed files with 85 additions and 46 deletions

View File

@@ -145,7 +145,8 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = function(recnotif) {
recnotif.openUrl = '/my/' + userorig;
if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_NEW_REC) {
newdescr = i18n.__('<strong>%s</strong> asked you for Friendship', userorig, mydescr);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) {
} else if ((recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED) ||
(recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED_MY_REQUEST)) {
newdescr = i18n.__('<strong>%s</strong> accepted your Friendship', userorig, mydescr);
} else if (recnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newdescr = i18n.__('<strong>%s</strong> refused your Friendship', userorig, mydescr);
@@ -258,30 +259,38 @@ sendNotifSchema.statics.saveNotif = async function(myrecnotif) {
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_FRIENDS_REFUSED) {
newstatus = shared_consts.StatusNotifs.STATUS_FRIENDS_REFUSED;
}
if (newstatus) {
const fields_to_update = {
status: newstatus,
read: true,
};
} else if (myrecnotif.typedir === shared_consts.TypeNotifs.TYPEDIR_GROUPS) {
typeidsearch = shared_consts.TypeNotifs.ID_GROUP_NEW_REC;
if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_ACCEPTED) {
newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_ACCEPTED;
} else if (myrecnotif.typeid === shared_consts.TypeNotifs.ID_GROUP_REFUSED) {
newstatus = shared_consts.StatusNotifs.STATUS_GROUPS_REFUSED;
}
}
const query = {
idapp: myrecnotif.idapp,
typedir: myrecnotif.typedir,
typeid: typeidsearch,
dest: myrecnotif.sender,
deleted: false,
status: 0,
};
if (newstatus) {
const fields_to_update = {
status: newstatus,
read: true,
};
// Cerca il record e se lo trova lo aggiorna
const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, {
new: false,
returnNewDocument: true
});
const query = {
idapp: myrecnotif.idapp,
typedir: myrecnotif.typedir,
typeid: typeidsearch,
dest: myrecnotif.sender,
deleted: false,
status: 0,
};
if (myrec) {
return myrec._doc;
}
// Cerca il record e se lo trova lo aggiorna
const myrec = await SendNotif.findOneAndUpdate(query, {$set: fields_to_update}, {
new: false,
returnNewDocument: true,
});
if (myrec) {
return myrec._doc;
}
}

View File

@@ -17,6 +17,8 @@ const {MyGroup} = require('../models/mygroup');
const {ObjectID} = require('mongodb');
const i18n = require('i18n');
const shared_consts = require('../tools/shared_nodejs');
mongoose.Promise = global.Promise;
@@ -476,7 +478,7 @@ UserSchema.statics.canHavePower = function(perm) {
try {
let consentito = false;
if (User.isAdmin(perm) || User.isManager(perm) ||
User.isEditor(perm) || User.isTutor(perm)) {
User.isEditor(perm) || User.isFacilitatore(perm)) {
consentito = true;
}
@@ -531,7 +533,7 @@ UserSchema.statics.isDepartment = function(perm) {
}
};
UserSchema.statics.isTutor = function(perm) {
UserSchema.statics.isFacilitatore = function(perm) {
try {
return ((perm & shared_consts.Permissions.Facilitatore) ===
shared_consts.Permissions.Facilitatore);
@@ -1561,6 +1563,8 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD
const {SendNotif} = require('../models/sendnotif');
const telegrambot = require('../telegram/telegrambot');
let ris = null;
let update = {};
try {
@@ -1595,12 +1599,31 @@ UserSchema.statics.setFriendsCmd = async function(idapp, usernameOrig, usernameD
};
ris = await User.updateOne({idapp, username: usernameOrig}, update);
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
// Send a notification to the DESTINATION FRIENDSHIP !
let req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED);
// Send a notification to the SENDER FRIENDSHIP !
req = tools.getReqByPar(idapp, usernameOrig);
await SendNotif.createNewNotifToSingleUser(req, null, {usernameDest}, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_ACCEPTED_MY_REQUEST);
update = {$pull: {'profile.req_friends': {username: {$in: [usernameDest]}}}};
ris = await User.updateOne({idapp, username: usernameOrig}, update);
if (ris) {
try {
const userDest = await User.getRecLangAndIdByUsername(idapp, usernameDest);
const user = await User.getRecLangAndIdByUsername(idapp, usernameOrig);
const msgOrig = i18n.__({phrase: '✅ %s accepted your Friendship request !', locale: user.lang}, usernameOrig)
const msgDest = i18n.__({phrase: "✅ You have accepted %s' Friendship request!", locale: userDest.lang}, usernameDest);
await telegrambot.sendMsgTelegram(idapp, usernameDest, msgOrig);
await telegrambot.sendMsgTelegram(idapp, usernameOrig, msgDest);
}catch (e) {
console.error('Notification : ', e);
}
}
}
// Controlla se lui aveva già la mia amicizia
@@ -1723,6 +1746,8 @@ UserSchema.statics.ifAlreadyInGroup = async function(idapp, usernameOrig, groupn
UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameDest, cmd, value, username_action) {
const {SendNotif} = require('../models/sendnotif');
let ris = null;
let update = {};
try {
@@ -1743,7 +1768,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools.getReqByPar(idapp, usernameOrig);
SendNotif.createNewNotifToSingleUser(req, null, {usernameDest, groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED);
SendNotif.create(req, null, {usernameDest:'', groupnameDest}, true, shared_consts.TypeNotifs.TYPEDIR_GROUPS, shared_consts.TypeNotifs.ID_GROUP_ACCEPTED);
// Elimina la richiesta:
update = {$pull: {req_users: {username: {$in: [usernameOrig]}}}};