71 lines
2.7 KiB
JavaScript
71 lines
2.7 KiB
JavaScript
// 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 l’app 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 l’utente 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 };
|