feat: add community membership verification and fix message mentions

main
brobert (aider) 3 months ago
parent c27446934a
commit 2ab5605888

@ -2,6 +2,7 @@ import { sendMessage } from '../utils/messaging.ts';
import { createTask, assignTask, completeTask, getPendingTasks } from '../../services/taskService'; import { createTask, assignTask, completeTask, getPendingTasks } from '../../services/taskService';
import { setRemindersEnabled } from '../../services/userService'; import { setRemindersEnabled } from '../../services/userService';
import { normalizeUserIdentifier, extractUserFromJid, formatUserMention } from '../../utils/userUtils'; import { normalizeUserIdentifier, extractUserFromJid, formatUserMention } from '../../utils/userUtils';
import { isUserInCommunity } from '../../utils/communityUtils';
function formatTaskList(tasks: any[]) { function formatTaskList(tasks: any[]) {
if (tasks.length === 0) { 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('Checking community membership for:', assignedUser);
console.log('Linked groups:', Array.from(linkedGroups)); 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`); 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; return;
} }
} catch (error) { } catch (error) {

@ -22,7 +22,11 @@ export async function sendMessage(phone: string, message: string, mentions: stri
// Add mentions if provided // Add mentions if provided
if (mentions.length > 0) { if (mentions.length > 0) {
payload.contextInfo = { payload.contextInfo = {
mentionedJid: mentions mentionedJid: mentions,
participant: mentions[0], // The user being mentioned
quotedMessage: {
conversation: message
}
}; };
} }

@ -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<string>): Promise<boolean> {
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;
}
}
Loading…
Cancel
Save