From 2ab56058885d5dd9e93fd52cf428ca66f50e94d8 Mon Sep 17 00:00:00 2001 From: "brobert (aider)" Date: Sun, 23 Mar 2025 23:27:02 +0100 Subject: [PATCH] feat: add community membership verification and fix message mentions --- src/bot/commands/task.ts | 7 +++++-- src/bot/utils/messaging.ts | 6 +++++- src/utils/communityUtils.ts | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/utils/communityUtils.ts diff --git a/src/bot/commands/task.ts b/src/bot/commands/task.ts index ee4d4dc..65dff65 100644 --- a/src/bot/commands/task.ts +++ b/src/bot/commands/task.ts @@ -2,6 +2,7 @@ import { sendMessage } from '../utils/messaging.ts'; import { createTask, assignTask, completeTask, getPendingTasks } from '../../services/taskService'; import { setRemindersEnabled } from '../../services/userService'; import { normalizeUserIdentifier, extractUserFromJid, formatUserMention } from '../../utils/userUtils'; +import { isUserInCommunity } from '../../utils/communityUtils'; function formatTaskList(tasks: any[]) { if (tasks.length === 0) { @@ -31,9 +32,11 @@ export function handleTaskCommand(body: string, sender: string, groupId: string, console.log('Checking community membership for:', assignedUser); console.log('Linked groups:', Array.from(linkedGroups)); - if (!linkedGroups.has(assignedUser)) { + // Check if user is in any of the linked groups + const userInCommunity = await isUserInCommunity(assignedUser, linkedGroups); + if (!userInCommunity) { sendMessage(sender, `El usuario ${formatUserMention(assignedUser)} no está en la comunidad`); - console.log('User not found in community:', assignedUser); + console.log('User not found in any community groups:', assignedUser); return; } } catch (error) { diff --git a/src/bot/utils/messaging.ts b/src/bot/utils/messaging.ts index a12408e..d3c48ed 100644 --- a/src/bot/utils/messaging.ts +++ b/src/bot/utils/messaging.ts @@ -22,7 +22,11 @@ export async function sendMessage(phone: string, message: string, mentions: stri // Add mentions if provided if (mentions.length > 0) { payload.contextInfo = { - mentionedJid: mentions + mentionedJid: mentions, + participant: mentions[0], // The user being mentioned + quotedMessage: { + conversation: message + } }; } diff --git a/src/utils/communityUtils.ts b/src/utils/communityUtils.ts new file mode 100644 index 0000000..4ddd263 --- /dev/null +++ b/src/utils/communityUtils.ts @@ -0,0 +1,37 @@ +import axios from 'axios'; +import dotenv from 'dotenv'; + +dotenv.config(); + +const API_URL = process.env.API_URL; +const INSTANCE_NAME = process.env.INSTANCE_NAME; +const API_KEY = process.env.API_KEY; + +export async function isUserInCommunity(userJid: string, groupJids: Set): Promise { + try { + // Check each group for the user + for (const groupJid of groupJids) { + const response = await axios.get( + `${API_URL}/group/getParticipants/${INSTANCE_NAME}`, + { + params: { + groupJid: groupJid + }, + headers: { + apikey: API_KEY + } + } + ); + + // Check if user is in this group's participants + const participants = response.data?.participants || []; + if (participants.includes(userJid)) { + return true; + } + } + return false; + } catch (error) { + console.error('Error checking community membership:', error); + return false; + } +}