Notification: Set a new Friendship and Accepted Friendship

This commit is contained in:
Paolo Arena
2022-07-26 15:46:39 +02:00
parent b06f1e4ab8
commit 3515914f17
15 changed files with 411 additions and 150 deletions

View File

@@ -30,6 +30,8 @@ const download = require('image-downloader');
// SETTINGS WebPush Configuration
const webpush = require('web-push');
const i18n = require('i18n');
const FILELOG = 'filelog.txt';
const FILEEVENTS = 'logevents.txt';
const FILEMANAGERS = 'logmanagers.txt';
@@ -578,6 +580,7 @@ module.exports = {
'typeid',
'dest',
'descr',
'title',
'openUrl',
'datenotif',
'read',
@@ -852,9 +855,21 @@ module.exports = {
},
getReqByPar(idapp, usernameOrig) {
return {
body: {
idapp: idapp,
},
user: {
username: usernameOrig,
}
};
},
sendNotificationByUsername: async function(idapp, username, cmd, telegram, usernameOrig) {
var {User} = require('../models/user');
const {SendNotif} = require('../models/sendnotif');
const telegrambot = require('../telegram/telegrambot');
@@ -865,7 +880,7 @@ module.exports = {
let userId = user._id;
let lang = user.lang;
let sendnotif = true;
let sendmynotif = true;
let title = this.getNomeAppByIdApp(idapp);
let descr = '';
@@ -880,19 +895,26 @@ module.exports = {
const userrecDest = await User.getUserShortDataByUsername(idapp, usernameOrig);
const req = this.getReqByPar(idapp, usernameOrig);
// CREATE NOTIFICATION IN TABLE SENDNOTIF
SendNotif.createNewNotifToSingleUser(req, null, username, true, shared_consts.TypeNotifs.TYPEDIR_FRIENDS, shared_consts.TypeNotifs.ID_FRIENDS_NEW_REC);
if (userrecDest) {
sendnotif = false; // non lo rimandare 2 volte !
sendmynotif = false; // non lo rimandare 2 volte !
// SEND TELEGRAM NOTIFICATION
telegrambot.askConfirmationUserFriend(idapp, shared_consts.CallFunz.RICHIESTA_AMICIZIA, userrecDest, username, usernameOrig);
}
}
}
if (userId) {
this.sendNotificationToUser(userId, title, descr, openUrl, '', tag,
actions);
// SEND PUSH NOTIFICATION
this.sendNotificationToUser(userId, title, descr, openUrl, '', tag, actions);
}
if (telegram && sendnotif) {
if (telegram && sendmynotif) {
const idtelegram = await User.TelegIdByUsername(idapp, username);
@@ -1105,11 +1127,18 @@ module.exports = {
if (String(userpassed) !== String(userauth)) {
// I'm trying to write something not mine!
this.mylog('userId = ', userpassed, 'req.user._id', userauth);
return {
exit: true,
ret: res.status(404).
send({code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER}),
};
if (!res) {
return {
exit: true,
ret: false,
}
} else {
return {
exit: true,
ret: res.status(404).
send({code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER}),
};
}
} else {
return {exit: false, ret: false};
}
@@ -2754,7 +2783,7 @@ module.exports = {
async loadApps() {
try {
this.MYAPPS = await Site.findAll(0);
console.log('this.MYAPPS', this.MYAPPS);
// console.log('this.MYAPPS', this.MYAPPS);
}catch (e) {
console.error('loadApps', e);
}
@@ -2933,6 +2962,27 @@ module.exports = {
});
},
capitalize(value) {
if (!value) {
return ''
}
const myval = value.toString()
return myval.charAt(0).toUpperCase() + myval.slice(1)
},
firstchars(value, numchars = 200) {
if (!value) {
return ''
}
try {
let mycar = value.substring(0, numchars)
if (value.length > numchars) mycar += '...'
return mycar
} catch (e) {
return value
}
},
removeAtChar(mystr) {
if (mystr && mystr[0] === '@'){
return mystr = mystr.substring(1);
@@ -2961,4 +3011,16 @@ module.exports = {
},
getContentNotifByParams(params, content, typesend) {
if (typesend === shared_consts.TypeSend.TELEGRAM) {
let myhost = this.getHostByIdApp(params.idapp);
if (params.openUrl)
content = content + '\n' + '<a href="' + myhost + params.openUrl + '">' + i18n.__('OPEN PAGE') + '</a>';
}
return content;
}
};

View File

@@ -223,27 +223,36 @@ module.exports = {
try {
if (!recnotif.options) {
recnotif.options = shared_consts.MessageOptions.Notify_ByPushNotification;
recnotif.options = shared_consts.MessageOptions.Notify_ByPushNotification + shared_consts.MessageOptions.Notify_ByBotTelegram;
}
if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification)) {
if (this.checkifSendPushNotification) {
let invia = false;
const params = {
sendreally: true,
typesend: shared_consts.TypeSend.PUSH_NOTIFICATION + shared_consts.TypeSend.TELEGRAM,
title: tools.getNomeAppByIdApp(idapp),
content: recnotif.descr ? recnotif.descr : tools.getContentByTypeMsg(recnotif.typemsg),
openUrl: tools.updateQueryStringParameter(recnotif.openUrl, 'idnotif', recnotif._id),
typemsg: recnotif.typemsg ? recnotif.typemsg : shared_consts.TypeMsg.SEND_TO_USER,
typenotif,
idnotif,
usernameDest: recnotif.usernameDest ? recnotif.usernameDest : recnotif.dest,
};
let params = {
idapp,
sendreally: true,
typesend: 0,
title: tools.getNomeAppByIdApp(idapp),
content: recnotif.descr ? recnotif.descr : tools.getContentByTypeMsg(recnotif.typemsg),
openUrl: tools.updateQueryStringParameter(recnotif.openUrl, 'idnotif', recnotif._id),
typemsg: recnotif.typemsg ? recnotif.typemsg : shared_consts.TypeMsg.SEND_TO_USER,
typenotif,
idnotif,
usernameDest: recnotif.usernameDest ? recnotif.usernameDest : recnotif.dest,
};
ris = await this.SendMsgToParam(idapp, params);
if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByPushNotification) && this.checkifSendPushNotification) {
params.typesend = params.typesend + shared_consts.TypeSend.PUSH_NOTIFICATION;
invia = true;
}
if (tools.isBitActive(recnotif.options, shared_consts.MessageOptions.Notify_ByBotTelegram)) {
params.typesend = params.typesend + shared_consts.TypeSend.TELEGRAM;
invia = true;
}
}
if (invia) {
ris = await this.SendMsgToParam(idapp, params);
}
// Send Msg by EMAIL
@@ -253,9 +262,7 @@ module.exports = {
if (!emaildest)
emaildest = await User.getEmailByUsername(recnotif.dest.idapp, recnotif.dest.username);
console.log('emaildest', emaildest);
await sendemail.sendEmail_Msg(res, user.lang, emaildest, user, idapp, recnotif);
await sendemail.sendEmail_ByNotif(res, user.lang, emaildest, user, idapp, recnotif);
}
return true;
@@ -347,8 +354,9 @@ module.exports = {
if (params.sendreally) {
if (tools.isBitActive(params.typesend, shared_consts.TypeSend.PUSH_NOTIFICATION)) {
risult = tools.sendNotificationToUser(user._id, mytitle, mycontent, params.openUrl, params.openUrl2, params.tag,
params.actions).
const myparam = {...params};
risult = tools.sendNotificationToUser(user._id, mytitle, mycontent, myparam.openUrl, myparam.openUrl2, myparam.tag,
myparam.actions).
then(ris => {
}).
@@ -361,8 +369,8 @@ module.exports = {
const telegid = user.profile.teleg_id;
if (telegid > 0) {
risult = await telegrambot.local_sendMsgTelegramByIdTelegram(idapp, telegid, mycontent);
await tools.snooze(100);
risult = await telegrambot.local_sendMsgTelegramByIdTelegram(idapp, telegid, tools.getContentNotifByParams(params, mycontent, shared_consts.TypeSend.TELEGRAM));
await tools.snooze(50);
}
}
}

View File

@@ -98,6 +98,7 @@ module.exports = {
TABLES_MYBACHECAS: 'mybachecas',
TABLES_MYHOSPS: 'myhosps',
TABLES_MYGOODS: 'mygoods',
TABLES_MYEVENTS: 'myevents',
TABLES_ENABLE_GETREC_BYID: ['mybachecas', 'myhosps', 'myskills', 'mygoods'],
@@ -203,6 +204,7 @@ module.exports = {
MessageOptions: {
Notify_ByEmail: 2,
Notify_ByPushNotification: 4,
Notify_ByBotTelegram: 8,
},
TypeMsg: {
@@ -296,6 +298,7 @@ module.exports = {
TYPEDIR_FRIENDS: 3,
ID_FRIENDS_NEW_REC: 1,
ID_FRIENDS_ACCEPTED: 2,
TYPEDIR_CIRCUITS: 4,