/
Alcatraz
/
Wagomon
Обзор
Документация
Войти
/
Alcatraz
/
Wagomon
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/lib/server/notifyChannels.js
96 строк
3 KB
saitgalineu
vkr
26 май 2026, 10:08
26 май 2026, 10:08
f9b2919
Код
Авторство
О чём код?
import { db } from './db.js'; import { isEventVisibleForUser } from './alertLevels.js'; import * as telegramChannel from './notify/telegramChannel.js'; import * as mattermostChannel from './notify/mattermostChannel.js'; export async function getChannelConfig() { try { const { rows } = await db.query( ` SELECT telegram_enabled, telegram_token, mattermost_enabled, mattermost_url, mattermost_token, mattermost_action_url FROM notification_channel_settings WHERE id = 1 ` ); return rows[0] || { telegram_enabled: true, telegram_token: null, mattermost_enabled: false, mattermost_url: null, mattermost_token: null, mattermost_action_url: null }; } catch (err) { const msg = err?.message ?? String(err); console.warn('getChannelConfig: failed to read notification_channel_settings, fallback to notification_channels', msg); const { rows } = await db.query( ` SELECT telegram_enabled, NULL::text AS telegram_token, mattermost_enabled, mattermost_url, mattermost_token, NULL::text AS mattermost_action_url FROM notification_channels ORDER BY id DESC LIMIT 1 ` ); return rows[0] || { telegram_enabled: true, telegram_token: null, mattermost_enabled: false, mattermost_url: null, mattermost_token: null, mattermost_action_url: null }; } } export async function notifyChannels(events) { if (!events || events.length === 0) return; const config = await getChannelConfig(); console.warn('notifyChannels config', { telegram_enabled: !!config.telegram_enabled, mattermost_enabled: !!config.mattermost_enabled, has_mattermost_url: !!config.mattermost_url }); const { rows: users } = await db.query( 'SELECT id, alert_status FROM users WHERE telegram_id IS NOT NULL' ); for (const event of events) { console.warn('[poller] notify event', { sensorType: event?.sensorType, sensorId: event?.sensorId, controllerId: event?.controllerId, alertStatus: event?.alertStatus, messagePreview: String(event?.message || '').slice(0, 160) }); const eventAlertStatus = event.alertStatus || 'INFO'; const eligibleUsers = users.filter((user) => { const userAlertStatus = user.alert_status || 'INFO'; return isEventVisibleForUser(eventAlertStatus, userAlertStatus); }); console.warn('[poller] eligible users', { count: eligibleUsers.length }); if (config.telegram_enabled) { await telegramChannel.send(event, { users: eligibleUsers }); } await mattermostChannel.send(event, { config }); } }