87 lines
3.4 KiB
Go
87 lines
3.4 KiB
Go
package client
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"github.com/topfans/backend/pkg/logger"
|
||
pbNotification "github.com/topfans/backend/pkg/proto/notification"
|
||
)
|
||
|
||
// NotificationClient 通知客户端(v1.5: 替换为真实实现,调用 notificationService.CreateNotification)
|
||
type NotificationClient struct {
|
||
cli pbNotification.NotificationService
|
||
}
|
||
|
||
// NewNotificationClient stub(保留兼容性)
|
||
func NewNotificationClient(_ string, _ interface{}) (*NotificationClient, error) {
|
||
return &NotificationClient{}, nil
|
||
}
|
||
|
||
// NewNotificationClientWithService 用真实 Dubbo client 创建
|
||
func NewNotificationClientWithService(cli pbNotification.NotificationService) *NotificationClient {
|
||
return &NotificationClient{cli: cli}
|
||
}
|
||
|
||
// sendNotification 通用发送方法(spec §8 + §11.2 通知失败仅日志,不影响主流程)
|
||
func (c *NotificationClient) sendNotification(ctx context.Context, userID, starID int64,
|
||
templateCode, title, content string) error {
|
||
if c.cli == nil {
|
||
// fallback: stub 模式(log-only)
|
||
logger.Sugar.Infow("STUB: send notification",
|
||
"user_id", userID, "template", templateCode, "title", title)
|
||
return nil
|
||
}
|
||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||
defer cancel()
|
||
_, err := c.cli.CreateNotification(ctx, &pbNotification.CreateNotificationRequest{
|
||
UserId: userID,
|
||
StarId: starID,
|
||
Type: templateCode,
|
||
Title: title,
|
||
Content: content,
|
||
})
|
||
if err != nil {
|
||
// spec §11.2: 仅日志,不影响主流程
|
||
logger.Sugar.Warnw("send notification failed",
|
||
"template", templateCode, "user_id", userID, "err", err)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// SendReportAutoHidden 发送自动隐藏通知(被举报方)
|
||
func (c *NotificationClient) SendReportAutoHidden(ctx context.Context, userID int64, targetType string, targetID int64) error {
|
||
title, content, tpl := BuildReportAutoHiddenNotice(targetType, targetID)
|
||
return c.sendNotification(ctx, userID, 0, tpl, title, content)
|
||
}
|
||
|
||
// SendReportResolved 发送举报处理结果(举报人)
|
||
func (c *NotificationClient) SendReportResolved(ctx context.Context, reporterID int64, action string, reason string) error {
|
||
title, content, tpl := BuildReportResolved(action, reason)
|
||
return c.sendNotification(ctx, reporterID, 0, tpl, title, content)
|
||
}
|
||
|
||
// SendReportTakedownNotice 发送下架通知(被举报方)
|
||
func (c *NotificationClient) SendReportTakedownNotice(ctx context.Context, userID int64, targetType string, targetID int64, reason string) error {
|
||
title, content, tpl := BuildReportTakedownNotice(targetType, targetID, reason)
|
||
return c.sendNotification(ctx, userID, 0, tpl, title, content)
|
||
}
|
||
|
||
// SendReportBanNotice 发送封禁通知(被举报用户)
|
||
func (c *NotificationClient) SendReportBanNotice(ctx context.Context, userID int64, reason string) error {
|
||
title, content, tpl := BuildReportBanNotice(reason)
|
||
return c.sendNotification(ctx, userID, 0, tpl, title, content)
|
||
}
|
||
|
||
// SendReportWarnNotice 发送警告通知(被举报用户)
|
||
func (c *NotificationClient) SendReportWarnNotice(ctx context.Context, userID int64, reason string) error {
|
||
title, content, tpl := BuildReportWarnNotice(reason)
|
||
return c.sendNotification(ctx, userID, 0, tpl, title, content)
|
||
}
|
||
|
||
// SendFeedbackReplied 发送反馈回复(反馈人)
|
||
func (c *NotificationClient) SendFeedbackReplied(ctx context.Context, userID int64, title string, replyContent string) error {
|
||
titleOut, content, tpl := BuildFeedbackReplied(title, replyContent)
|
||
return c.sendNotification(ctx, userID, 0, tpl, titleOut, content)
|
||
}
|