You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB
TypeScript

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/participants/${INSTANCE_NAME}`,
{
params: {
groupJid: groupJid
},
headers: {
apikey: API_KEY
}
}
);
// Check if user is in this group's participants
const participants = response.data?.participants || [];
// Check both JID and phone number formats
const userPhone = userJid.split('@')[0];
if (participants.includes(userJid) || participants.some(p => p.startsWith(userPhone))) {
return true;
}
}
return false;
} catch (error) {
console.error('Error checking community membership:', error);
return false;
}
}