mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
2a3b9f8a5f
- Add task_type to w_push_events table and GORM models. - Implement OnTaskCompleted callback hook in task executor to avoid circular dependencies. - Implement task listener in push package to trigger notifications on task completion. - Automatically resolve User objects from payload and results. - Enhance UI to select task completed events and preview default templates. - Update Swagger documentation.
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/db"
|
|
)
|
|
|
|
// PushEvent 系统通知事件模型
|
|
type PushEvent struct {
|
|
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
EventKey string `json:"event_key" gorm:"uniqueIndex;size:80;not null"` // 如 admin_login
|
|
Name string `json:"name" gorm:"size:100;not null"` // 如 管理员登录
|
|
TaskType string `json:"task_type" gorm:"size:100;index;not null;default:''"` // 关联的异步任务类型
|
|
Channels []string `json:"channels" gorm:"type:text;serializer:json;not null"` // 推送渠道列表,如 ["lark"]
|
|
Targets []string `json:"targets" gorm:"type:text;serializer:json;not null"` // 推送目标用户/邮箱列表
|
|
Template string `json:"template" gorm:"type:text;not null"` // 消息模板 JSON
|
|
Enabled bool `json:"enabled" gorm:"index;not null;default:false"` // 是否启用
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;index"`
|
|
}
|
|
|
|
// TableName 指定 GORM 表名
|
|
func (PushEvent) TableName() string {
|
|
return "w_push_events"
|
|
}
|
|
|
|
// Validate 基础校验
|
|
func (pe *PushEvent) Validate() error {
|
|
pe.EventKey = strings.TrimSpace(pe.EventKey)
|
|
pe.Name = strings.TrimSpace(pe.Name)
|
|
pe.Template = strings.TrimSpace(pe.Template)
|
|
|
|
if pe.EventKey == "" {
|
|
return errors.New("event key is required")
|
|
}
|
|
if pe.Name == "" {
|
|
return errors.New("event name is required")
|
|
}
|
|
if pe.Template == "" {
|
|
return errors.New("event template is required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetActivePushEventByKey 获取启用的通知事件
|
|
func GetActivePushEventByKey(ctx context.Context, key string) (*PushEvent, error) {
|
|
var event PushEvent
|
|
err := db.DB(ctx).Where("event_key = ? AND enabled = ?", key, true).First(&event).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &event, nil
|
|
}
|