|
|
|
@ -140,7 +140,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
|
|
|
|
|
|
|
|
// Etapa 2: Descubrimiento seguro de grupos (modo 'discover')
|
|
|
|
// Etapa 2: Descubrimiento seguro de grupos (modo 'discover')
|
|
|
|
if (isGroupId(remoteJid)) {
|
|
|
|
if (isGroupId(remoteJid)) {
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
const gatingMode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
const gatingMode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
if (gatingMode === 'discover') {
|
|
|
|
if (gatingMode === 'discover') {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -149,7 +148,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
.get(remoteJid);
|
|
|
|
.get(remoteJid);
|
|
|
|
if (!exists) {
|
|
|
|
if (!exists) {
|
|
|
|
try { await GroupSyncService.ensureGroupLabelAndName(remoteJid); } catch {}
|
|
|
|
try { await GroupSyncService.ensureGroupLabelAndName(remoteJid); } catch {}
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
try { AllowedGroups.upsertPending(remoteJid, (GroupSyncService.activeGroupsCache.get(remoteJid) || null), normalizedSenderId); } catch {}
|
|
|
|
try { AllowedGroups.upsertPending(remoteJid, (GroupSyncService.activeGroupsCache.get(remoteJid) || null), normalizedSenderId); } catch {}
|
|
|
|
try { Metrics.inc('unknown_groups_discovered_total'); } catch {}
|
|
|
|
try { Metrics.inc('unknown_groups_discovered_total'); } catch {}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -168,7 +166,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
} catch {
|
|
|
|
} catch {
|
|
|
|
// Si la tabla no existe por alguna razón, intentar upsert y retornar igualmente
|
|
|
|
// Si la tabla no existe por alguna razón, intentar upsert y retornar igualmente
|
|
|
|
try { await GroupSyncService.ensureGroupLabelAndName(remoteJid); } catch {}
|
|
|
|
try { await GroupSyncService.ensureGroupLabelAndName(remoteJid); } catch {}
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
try { AllowedGroups.upsertPending(remoteJid, (GroupSyncService.activeGroupsCache.get(remoteJid) || null), normalizedSenderId); } catch {}
|
|
|
|
try { AllowedGroups.upsertPending(remoteJid, (GroupSyncService.activeGroupsCache.get(remoteJid) || null), normalizedSenderId); } catch {}
|
|
|
|
try { Metrics.inc('unknown_groups_discovered_total'); } catch {}
|
|
|
|
try { Metrics.inc('unknown_groups_discovered_total'); } catch {}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -189,7 +186,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
|
|
|
|
|
|
|
|
// Etapa 3: Gating en modo 'enforce' — ignorar mensajes de grupos no permitidos
|
|
|
|
// Etapa 3: Gating en modo 'enforce' — ignorar mensajes de grupos no permitidos
|
|
|
|
if (isGroupId(remoteJid)) {
|
|
|
|
if (isGroupId(remoteJid)) {
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
const gatingMode2 = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
const gatingMode2 = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
if (gatingMode2 === 'enforce') {
|
|
|
|
if (gatingMode2 === 'enforce') {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
@ -206,8 +202,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
|
|
|
|
|
|
|
|
// Manejo de comandos de administración (/admin) antes de cualquier otra lógica de grupo
|
|
|
|
// Manejo de comandos de administración (/admin) antes de cualquier otra lógica de grupo
|
|
|
|
if (messageTextTrimmed.startsWith('/admin')) {
|
|
|
|
if (messageTextTrimmed.startsWith('/admin')) {
|
|
|
|
try { AdminService.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
const adminResponses = await AdminService.handle({
|
|
|
|
const adminResponses = await AdminService.handle({
|
|
|
|
sender: normalizedSenderId,
|
|
|
|
sender: normalizedSenderId,
|
|
|
|
groupId: remoteJid,
|
|
|
|
groupId: remoteJid,
|
|
|
|
@ -262,8 +256,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
|| [];
|
|
|
|
|| [];
|
|
|
|
|
|
|
|
|
|
|
|
// Asegurar que CommandService y TaskService usen la misma DB (tests/producción)
|
|
|
|
// Asegurar que CommandService y TaskService usen la misma DB (tests/producción)
|
|
|
|
CommandService.dbInstance = db;
|
|
|
|
|
|
|
|
TaskService.dbInstance = db;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Delegar el manejo del comando
|
|
|
|
// Delegar el manejo del comando
|
|
|
|
const messageId = typeof data?.key?.id === 'string' ? data.key.id : null;
|
|
|
|
const messageId = typeof data?.key?.id === 'string' ? data.key.id : null;
|
|
|
|
@ -299,7 +291,6 @@ export async function handleMessageUpsert(data: any, db: Database): Promise<void
|
|
|
|
if (scope !== 'all' && !isGroup) return;
|
|
|
|
if (scope !== 'all' && !isGroup) return;
|
|
|
|
|
|
|
|
|
|
|
|
// Respetar gating 'enforce'
|
|
|
|
// Respetar gating 'enforce'
|
|
|
|
try { AllowedGroups.dbInstance = db; } catch {}
|
|
|
|
|
|
|
|
const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
const mode = String(process.env.GROUP_GATING_MODE || 'off').toLowerCase();
|
|
|
|
if (mode === 'enforce' && isGroup) {
|
|
|
|
if (mode === 'enforce' && isGroup) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|