TgRouting/deamon/groupFilter.js

30 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// groupFilter.js
// Есть ли хотя бы одна общая группа (userGroups и otherGroups — массивы объектов с .id)
export function hasCommonGroup(userGroups, otherGroups) {
if (!Array.isArray(userGroups) || !Array.isArray(otherGroups)) return false
const userGroupIds = userGroups.map(g => g.id)
return otherGroups.some(g => userGroupIds.includes(g.id))
}
// Строго фильтрует: возвращает только ботов, у которых есть общая группа с userGroups,
// и только те команды внутри бота, которые разрешены этим группам.
export function filterBotsAndCommandsByUserGroups(allBots, userGroups) {
console.log('allBots');
console.dir(allBots, { depth: null, colors: true });
return allBots
.filter(bot => {
const botGroups = bot.groups || []
// Строго: если у бота есть группы — только с пересечением, иначе вообще не показываем
return botGroups.length > 0 && hasCommonGroup(userGroups, botGroups)
})
.map(bot => {
// Оставляем только доступные команды для этих групп
const filteredCommands = (bot.commands || []).filter(cmd =>
cmd.groupIds?.length > 0 && hasCommonGroup(userGroups, cmd.groupIds)
)
return { ...bot, commands: filteredCommands }
})
}