Version: 0.9.68.dev.260504
后端: 1. 阶段 3 notification 服务边界落地,新增 `cmd/notification`、`services/notification`、`gateway/notification`、`shared/contracts/notification` 和 notification port,按 userauth 同款最小手搓 zrpc 样板收口 2. notification outbox consumer、relay 和 retry loop 迁入独立服务入口,处理 `notification.feishu.requested`,gateway 改为通过 zrpc client 调用 notification 3. 清退旧单体 notification DAO/model/service/provider/runner 和 `service/events/notification_feishu.go`,旧实现不再作为活跃编译路径 4. 修复 outbox 路由归属、dispatch 启动扫描、Kafka topic 探测/投递超时、sending 租约恢复、毒消息 MarkDead 错误回传和 RPC timeout 边界 5. 同步调整 active-scheduler 触发通知事件、核心 outbox handler、MySQL 迁移边界和 notification 配置 文档: 1. 更新微服务迁移计划,将阶段 3 notification 标记为已完成,并明确下一阶段从 active-scheduler 开始
This commit is contained in:
158
backend/services/notification/sv/channel.go
Normal file
158
backend/services/notification/sv/channel.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package sv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/respond"
|
||||
notificationfeishu "github.com/LoveLosita/smartflow/backend/services/notification/internal/feishu"
|
||||
notificationmodel "github.com/LoveLosita/smartflow/backend/services/notification/model"
|
||||
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
channelTestStatusSuccess = "success"
|
||||
channelTestStatusFailed = "failed"
|
||||
)
|
||||
|
||||
// GetFeishuWebhook 查询当前用户的飞书 Webhook 触发器配置。
|
||||
func (s *Service) GetFeishuWebhook(ctx context.Context, userID int) (contracts.ChannelResponse, error) {
|
||||
if userID <= 0 {
|
||||
return contracts.ChannelResponse{}, respond.ErrUnauthorized
|
||||
}
|
||||
row, err := s.channelStore.GetUserNotificationChannel(ctx, userID, notificationmodel.ChannelFeishuWebhook)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return contracts.ChannelResponse{
|
||||
Channel: notificationmodel.ChannelFeishuWebhook,
|
||||
AuthType: notificationmodel.AuthTypeNone,
|
||||
Configured: false,
|
||||
}, nil
|
||||
}
|
||||
return contracts.ChannelResponse{}, err
|
||||
}
|
||||
return responseFromChannel(row), nil
|
||||
}
|
||||
|
||||
// SaveFeishuWebhook 幂等保存当前用户的飞书 Webhook 触发器配置。
|
||||
func (s *Service) SaveFeishuWebhook(ctx context.Context, userID int, req contracts.SaveFeishuWebhookRequest) (contracts.ChannelResponse, error) {
|
||||
if userID <= 0 {
|
||||
return contracts.ChannelResponse{}, respond.ErrUnauthorized
|
||||
}
|
||||
webhookURL := strings.TrimSpace(req.WebhookURL)
|
||||
if webhookURL == "" {
|
||||
return contracts.ChannelResponse{}, respond.MissingParam
|
||||
}
|
||||
if err := notificationfeishu.ValidateWebhookURL(webhookURL); err != nil {
|
||||
return contracts.ChannelResponse{}, respond.WrongParamType
|
||||
}
|
||||
authType := normalizeAuthType(req.AuthType)
|
||||
bearerToken := strings.TrimSpace(req.BearerToken)
|
||||
if authType == notificationmodel.AuthTypeBearer && bearerToken == "" {
|
||||
return contracts.ChannelResponse{}, respond.MissingParam
|
||||
}
|
||||
row := ¬ificationmodel.UserNotificationChannel{
|
||||
UserID: userID,
|
||||
Channel: notificationmodel.ChannelFeishuWebhook,
|
||||
Enabled: req.Enabled,
|
||||
WebhookURL: webhookURL,
|
||||
AuthType: authType,
|
||||
BearerToken: bearerToken,
|
||||
}
|
||||
if err := s.channelStore.UpsertUserNotificationChannel(ctx, row); err != nil {
|
||||
return contracts.ChannelResponse{}, err
|
||||
}
|
||||
return s.GetFeishuWebhook(ctx, userID)
|
||||
}
|
||||
|
||||
// DeleteFeishuWebhook 删除当前用户的飞书 Webhook 触发器配置。
|
||||
func (s *Service) DeleteFeishuWebhook(ctx context.Context, userID int) error {
|
||||
if userID <= 0 {
|
||||
return respond.ErrUnauthorized
|
||||
}
|
||||
return s.channelStore.DeleteUserNotificationChannel(ctx, userID, notificationmodel.ChannelFeishuWebhook)
|
||||
}
|
||||
|
||||
// TestFeishuWebhook 发送一条最小业务 JSON 到当前用户配置的飞书 Webhook。
|
||||
func (s *Service) TestFeishuWebhook(ctx context.Context, userID int) (contracts.TestResult, error) {
|
||||
if userID <= 0 {
|
||||
return contracts.TestResult{}, respond.ErrUnauthorized
|
||||
}
|
||||
now := s.options.Now()
|
||||
traceID := "trace_feishu_webhook_test"
|
||||
sendResult, sendErr := s.provider.Send(ctx, notificationfeishu.SendRequest{
|
||||
NotificationID: 0,
|
||||
UserID: userID,
|
||||
TriggerID: "ast_test_webhook",
|
||||
PreviewID: "asp_test_webhook",
|
||||
TriggerType: "manual_test",
|
||||
TargetType: "notification_channel",
|
||||
TargetID: 0,
|
||||
TargetURL: "/assistant/00000000-0000-0000-0000-000000000000",
|
||||
MessageText: "这是一条 SmartFlow 飞书 Webhook 测试消息。",
|
||||
TraceID: traceID,
|
||||
AttemptCount: 1,
|
||||
})
|
||||
if sendErr != nil {
|
||||
return contracts.TestResult{}, sendErr
|
||||
}
|
||||
|
||||
status := channelTestStatusFailed
|
||||
testErr := strings.TrimSpace(sendResult.ErrorMessage)
|
||||
if sendResult.Outcome == notificationfeishu.SendOutcomeSuccess {
|
||||
status = channelTestStatusSuccess
|
||||
testErr = ""
|
||||
}
|
||||
if sendResult.Outcome == notificationfeishu.SendOutcomeSkipped && testErr == "" {
|
||||
testErr = "飞书 webhook 未配置或未启用"
|
||||
}
|
||||
if err := s.channelStore.UpdateUserNotificationChannelTestResult(ctx, userID, notificationmodel.ChannelFeishuWebhook, status, testErr, now); err != nil {
|
||||
return contracts.TestResult{}, err
|
||||
}
|
||||
channel, err := s.GetFeishuWebhook(ctx, userID)
|
||||
if err != nil {
|
||||
return contracts.TestResult{}, err
|
||||
}
|
||||
return contracts.TestResult{
|
||||
Channel: channel,
|
||||
Status: status,
|
||||
Outcome: string(sendResult.Outcome),
|
||||
Message: testErr,
|
||||
TraceID: traceID,
|
||||
SentAt: now,
|
||||
Skipped: sendResult.Outcome == notificationfeishu.SendOutcomeSkipped,
|
||||
Provider: notificationfeishu.Channel,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func responseFromChannel(row *notificationmodel.UserNotificationChannel) contracts.ChannelResponse {
|
||||
if row == nil {
|
||||
return contracts.ChannelResponse{
|
||||
Channel: notificationmodel.ChannelFeishuWebhook,
|
||||
AuthType: notificationmodel.AuthTypeNone,
|
||||
Configured: false,
|
||||
}
|
||||
}
|
||||
return contracts.ChannelResponse{
|
||||
Channel: row.Channel,
|
||||
Enabled: row.Enabled,
|
||||
Configured: strings.TrimSpace(row.WebhookURL) != "",
|
||||
WebhookURLMask: notificationfeishu.MaskWebhookURL(row.WebhookURL),
|
||||
AuthType: normalizeAuthType(row.AuthType),
|
||||
HasBearerToken: strings.TrimSpace(row.BearerToken) != "",
|
||||
LastTestStatus: row.LastTestStatus,
|
||||
LastTestError: row.LastTestError,
|
||||
LastTestAt: row.LastTestAt,
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeAuthType(authType string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(authType)) {
|
||||
case notificationmodel.AuthTypeBearer:
|
||||
return notificationmodel.AuthTypeBearer
|
||||
default:
|
||||
return notificationmodel.AuthTypeNone
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user