feat: add notify icon read
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/engigu/baihu-panel/internal/constant"
|
||||
"github.com/engigu/baihu-panel/internal/database"
|
||||
"github.com/engigu/baihu-panel/internal/eventbus"
|
||||
"github.com/engigu/baihu-panel/internal/logger"
|
||||
"github.com/engigu/baihu-panel/internal/models"
|
||||
"github.com/engigu/baihu-panel/internal/utils"
|
||||
@@ -98,7 +99,7 @@ func (s *NotificationService) SaveChannel(channel NotifyChannel) error {
|
||||
ID: channel.ID,
|
||||
Name: channel.Name,
|
||||
Type: channel.Type,
|
||||
Config: string(configJSON),
|
||||
Config: models.BigText(configJSON),
|
||||
Enabled: channel.Enabled,
|
||||
}
|
||||
return database.DB.Create(notifyWay).Error
|
||||
@@ -108,7 +109,7 @@ func (s *NotificationService) SaveChannel(channel NotifyChannel) error {
|
||||
updates := map[string]interface{}{
|
||||
"name": channel.Name,
|
||||
"type": channel.Type,
|
||||
"config": string(configJSON),
|
||||
"config": models.BigText(configJSON),
|
||||
"enabled": channel.Enabled,
|
||||
}
|
||||
return database.DB.Model(&models.NotifyWay{}).Where("id = ?", channel.ID).Updates(updates).Error
|
||||
@@ -202,12 +203,39 @@ func (s *NotificationService) SendToChannel(channel NotifyChannel, msg *NotifyMe
|
||||
Title: msg.Title,
|
||||
Text: msg.Text,
|
||||
})
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"title": msg.Title,
|
||||
"content": msg.Text,
|
||||
"channel_name": channel.Name,
|
||||
"success": false,
|
||||
"error_msg": "",
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
payload["error_msg"] = err.Error()
|
||||
eventbus.DefaultBus.Publish(eventbus.Event{
|
||||
Type: constant.EventNotifySent,
|
||||
Payload: payload,
|
||||
})
|
||||
return &NotifyResult{Success: false, Error: err.Error()}
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
payload["error_msg"] = result.Error
|
||||
eventbus.DefaultBus.Publish(eventbus.Event{
|
||||
Type: constant.EventNotifySent,
|
||||
Payload: payload,
|
||||
})
|
||||
return &NotifyResult{Success: false, Error: result.Error}
|
||||
}
|
||||
|
||||
payload["success"] = true
|
||||
eventbus.DefaultBus.Publish(eventbus.Event{
|
||||
Type: constant.EventNotifySent,
|
||||
Payload: payload,
|
||||
})
|
||||
|
||||
return &NotifyResult{Success: true}
|
||||
}
|
||||
|
||||
@@ -240,62 +268,91 @@ func (s *NotificationService) SendByChannelID(channelID string, msg *NotifyMessa
|
||||
return s.SendToChannel(ch, msg)
|
||||
}
|
||||
|
||||
// TriggerEvent 触发事件通知(实现 tasks.Notifier 接口)
|
||||
func (s *NotificationService) TriggerEvent(bindingType string, eventType string, dataID string, templateData map[string]interface{}) {
|
||||
var title, text string
|
||||
// SubscribeEvents 注册通知服务自身为事件流的订阅者
|
||||
func (s *NotificationService) SubscribeEvents(bus *eventbus.EventBus) {
|
||||
// 系统事件
|
||||
systemEvents := []string{constant.EventUserLogin, constant.EventBruteForceLogin, constant.EventPasswordChanged}
|
||||
for _, evt := range systemEvents {
|
||||
bus.Subscribe(evt, s.handleEvent(constant.BindingTypeSystem))
|
||||
}
|
||||
|
||||
switch eventType {
|
||||
case constant.EventUserLogin:
|
||||
title = "用户登录通知"
|
||||
text = fmt.Sprintf("用户 %v 在 IP %v 登录成功", templateData["username"], templateData["ip"])
|
||||
case constant.EventBruteForceLogin:
|
||||
title = "系统安全警告"
|
||||
text = fmt.Sprintf("检测到 IP %v 正在尝试暴力破解用户 %v", templateData["ip"], templateData["username"])
|
||||
case constant.EventPasswordChanged:
|
||||
title = "账户安全通知"
|
||||
text = fmt.Sprintf("用户 %v 刚刚修改了密码", templateData["username"])
|
||||
case constant.EventTaskSuccess:
|
||||
title = fmt.Sprintf("任务[%v] 成功", templateData["task_name"])
|
||||
text = fmt.Sprintf("任务 #%v %v\n状态: 成功\n耗时: %vms", templateData["task_id"], templateData["task_name"], templateData["duration"])
|
||||
case constant.EventTaskFailed:
|
||||
title = fmt.Sprintf("任务[%v] 失败", templateData["task_name"])
|
||||
if errStr, ok := templateData["error"]; ok {
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行失败\n错误: %v", templateData["task_id"], templateData["task_name"], errStr)
|
||||
} else {
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行失败\n状态: %v\n耗时: %vms", templateData["task_id"], templateData["task_name"], templateData["status"], templateData["duration"])
|
||||
// 任务事件
|
||||
taskEvents := []string{constant.EventTaskSuccess, constant.EventTaskFailed, constant.EventTaskTimeout}
|
||||
for _, evt := range taskEvents {
|
||||
bus.Subscribe(evt, s.handleEvent(constant.BindingTypeTask))
|
||||
}
|
||||
|
||||
// 通用系统通知
|
||||
bus.Subscribe(constant.EventSystemNotice, s.handleEvent(constant.BindingTypeSystem))
|
||||
}
|
||||
|
||||
func (s *NotificationService) handleEvent(bindingType string) eventbus.Handler {
|
||||
return func(e eventbus.Event) {
|
||||
payload, ok := e.Payload.(map[string]interface{})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
case constant.EventTaskTimeout:
|
||||
title = fmt.Sprintf("任务[%v] 超时", templateData["task_name"])
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行超时\n耗时: %vms", templateData["task_id"], templateData["task_name"], templateData["duration"])
|
||||
default:
|
||||
title = "系统通知"
|
||||
text = "收到未知事件"
|
||||
}
|
||||
|
||||
msg := &NotifyMessage{Title: title, Text: text}
|
||||
|
||||
bindings := s.GetBindingsByEvent(bindingType, eventType, dataID)
|
||||
if len(bindings) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
channels := s.GetChannels()
|
||||
channelMap := make(map[string]NotifyChannel)
|
||||
for _, ch := range channels {
|
||||
channelMap[ch.ID] = ch
|
||||
}
|
||||
|
||||
for _, binding := range bindings {
|
||||
ch, ok := channelMap[binding.WayID]
|
||||
if !ok || !ch.Enabled {
|
||||
continue
|
||||
var dataID string
|
||||
if id, ok := payload["task_id"].(string); ok {
|
||||
dataID = id
|
||||
}
|
||||
go func(channel NotifyChannel) {
|
||||
result := s.SendToChannel(channel, msg)
|
||||
if !result.Success {
|
||||
logger.Warnf("[Notify] 发送事件 %s 到渠道 %s(%s) 失败: %s", eventType, channel.Name, channel.Type, result.Error)
|
||||
|
||||
var title, text string
|
||||
switch e.Type {
|
||||
case constant.EventUserLogin:
|
||||
title = "用户登录通知"
|
||||
text = fmt.Sprintf("用户 %v 在 IP %v 登录成功", payload["username"], payload["ip"])
|
||||
case constant.EventBruteForceLogin:
|
||||
title = "系统安全警告"
|
||||
text = fmt.Sprintf("检测到 IP %v 正在尝试暴力破解用户 %v", payload["ip"], payload["username"])
|
||||
case constant.EventPasswordChanged:
|
||||
title = "账户安全通知"
|
||||
text = fmt.Sprintf("用户 %v 刚刚修改了密码", payload["username"])
|
||||
case constant.EventTaskSuccess:
|
||||
title = fmt.Sprintf("任务[%v] 成功", payload["task_name"])
|
||||
text = fmt.Sprintf("任务 #%v %v\n状态: 成功\n耗时: %vms", payload["task_id"], payload["task_name"], payload["duration"])
|
||||
case constant.EventTaskFailed:
|
||||
title = fmt.Sprintf("任务[%v] 失败", payload["task_name"])
|
||||
if errStr, ok := payload["error"]; ok {
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行失败\n错误: %v", payload["task_id"], payload["task_name"], errStr)
|
||||
} else {
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行失败\n状态: %v\n耗时: %vms", payload["task_id"], payload["task_name"], payload["status"], payload["duration"])
|
||||
}
|
||||
}(ch)
|
||||
case constant.EventTaskTimeout:
|
||||
title = fmt.Sprintf("任务[%v] 超时", payload["task_name"])
|
||||
text = fmt.Sprintf("任务 #%v %v\n执行超时\n耗时: %vms", payload["task_id"], payload["task_name"], payload["duration"])
|
||||
case constant.EventSystemNotice:
|
||||
title, _ = payload["title"].(string)
|
||||
text, _ = payload["content"].(string)
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
msg := &NotifyMessage{Title: title, Text: text}
|
||||
bindings := s.GetBindingsByEvent(bindingType, e.Type, dataID)
|
||||
if len(bindings) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
channels := s.GetChannels()
|
||||
channelMap := make(map[string]NotifyChannel)
|
||||
for _, ch := range channels {
|
||||
channelMap[ch.ID] = ch
|
||||
}
|
||||
|
||||
for _, binding := range bindings {
|
||||
ch, ok := channelMap[binding.WayID]
|
||||
if !ok || !ch.Enabled {
|
||||
continue
|
||||
}
|
||||
go func(channel NotifyChannel) {
|
||||
result := s.SendToChannel(channel, msg)
|
||||
if !result.Success {
|
||||
logger.Warnf("[Notify] 发送事件 %s 到渠道 %s(%s) 失败: %s", e.Type, channel.Name, channel.Type, result.Error)
|
||||
}
|
||||
}(ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user