Files
freeplanet_serverside/src/telegram/handlers/groupHandler.js
Surya Paolo adf1aac10f - aggiornata la grafica della Home di RISO
- Profilo Completition
- Email Verificata
- Invita un Amico (invio di email)
2025-11-15 19:38:55 +01:00

71 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// telegram/handlers/groupHandler.js
const shared_consts = require('../../tools/shared_nodejs');
const tools = require('../../tools/general');
const { User } = require('../../models/user');
const InlineConferma = { RISPOSTA_SI: 'SI_', RISPOSTA_NO: 'NO_' };
/**
* Gestisce conferma/rifiuto a richieste di GRUPPO
* Payload data:
* - action
* - username (mittente originale)
* - userDest (destinatario/utente da aggiungere)
* - groupId (id o path del gruppo)
* - groupname (nome del gruppo)
*/
async function handleGroup({ bot, cl, idapp, msg, data, user, userDest, username_action }) {
let notifyText = '';
// SI → accetta richiesta d'ingresso nel gruppo
if (data.action === InlineConferma.RISPOSTA_SI + shared_consts.CallFunz.RICHIESTA_GRUPPO) {
// Se lapp ha funzioni di persistenza specifiche, usale se esistono
// (non assumo nomi rigidi per non rompere il deploy)
if (typeof User.setGroupCmd === 'function') {
try {
await User.setGroupCmd(idapp, data.userDest || data.username, data.groupId, shared_consts.GROUPCMD.ADDUSERTOGROUP, 1, username_action, { groupname: data.groupname });
} catch (e) {
console.warn('handleGroup:setGroupCmd non eseguito:', e.message);
}
}
// Messaggi di conferma
await cl.sendMsg(msg.chat.id, `${data.userDest || data.username} è stato aggiunto al gruppo ${data.groupname || data.groupId}.`);
// Notifica anche lutente interessato
const targetUsername = data.userDest || data.username;
const teleg_id = await User.TelegIdByUsername(idapp, targetUsername);
if (teleg_id) {
await cl.sendMsg(teleg_id, `👥 Sei stato aggiunto al gruppo: ${data.groupname || data.groupId}`);
}
notifyText = 'Gruppo: aggiunta OK';
return notifyText;
}
// NO → rifiuta/annulla richiesta
if (data.action === InlineConferma.RISPOSTA_NO + shared_consts.CallFunz.RICHIESTA_GRUPPO) {
if (typeof User.setGroupCmd === 'function') {
try {
await User.setGroupCmd(idapp, data.userDest || data.username, data.groupId, shared_consts.GROUPCMD.REMOVEUSERFROMGROUP, 0, username_action, { groupname: data.groupname });
} catch (e) {
console.warn('handleGroup:setGroupCmd non eseguito:', e.message);
}
}
await cl.sendMsg(msg.chat.id, '🚫 Richiesta gruppo rifiutata.');
// Avvisa il richiedente
const targetUsername = data.userDest || data.username;
const teleg_id = await User.TelegIdByUsername(idapp, targetUsername);
if (teleg_id) {
await cl.sendMsg(teleg_id, `❌ La tua richiesta per il gruppo ${data.groupname || data.groupId} è stata rifiutata.`);
}
notifyText = 'Gruppo: rifiutata';
return notifyText;
}
return 'OK';
}
module.exports = { handleGroup };