Files
backend/internal/dto/notification_converter.go
lan cf36b1350d refactor: introduce Wire dependency injection and interface-based architecture
- Add Google Wire for compile-time dependency injection
- Replace concrete service types with interfaces across handlers
- Remove global state (DB, cache) in favor of constructor injection
- Split monolithic files into focused modules:
  - config: separate files for each config domain
  - dto: converters split by domain (user, post, message, group)
  - cache: separate metrics.go and redis_cache.go
- Introduce unified apperrors package with string-based error codes
- Add transaction support with context-aware repository methods
- Upgrade golang.org/x/crypto from 0.17.0 to 0.26.0
2026-03-13 09:38:18 +08:00

129 lines
4.2 KiB
Go

package dto
import (
"carrot_bbs/internal/model"
"strconv"
)
// ==================== Notification 转换 ====================
// ConvertNotificationToResponse 将Notification转换为NotificationResponse
func ConvertNotificationToResponse(notification *model.Notification) *NotificationResponse {
if notification == nil {
return nil
}
return &NotificationResponse{
ID: notification.ID,
UserID: notification.UserID,
Type: string(notification.Type),
Title: notification.Title,
Content: notification.Content,
Data: notification.Data,
IsRead: notification.IsRead,
CreatedAt: FormatTime(notification.CreatedAt),
}
}
// ConvertNotificationsToResponse 将Notification列表转换为响应列表
func ConvertNotificationsToResponse(notifications []*model.Notification) []*NotificationResponse {
result := make([]*NotificationResponse, 0, len(notifications))
for _, n := range notifications {
result = append(result, ConvertNotificationToResponse(n))
}
return result
}
// ==================== SystemMessage 转换 ====================
// SystemMessageToResponse 将Message转换为SystemMessageResponse
func SystemMessageToResponse(msg *model.Message) *SystemMessageResponse {
if msg == nil {
return nil
}
// 从 segments 中提取文本内容
content := ExtractTextContentFromModel(msg.Segments)
resp := &SystemMessageResponse{
ID: msg.ID,
SenderID: msg.SenderID,
ReceiverID: "", // 系统消息的接收者需要从上下文获取
Content: content,
Category: string(msg.Category),
SystemType: string(msg.SystemType),
CreatedAt: msg.CreatedAt,
}
if msg.ExtraData != nil {
resp.ExtraData = map[string]interface{}{
"actor_id": msg.ExtraData.ActorID,
"actor_name": msg.ExtraData.ActorName,
"avatar_url": msg.ExtraData.AvatarURL,
"target_id": msg.ExtraData.TargetID,
"target_type": msg.ExtraData.TargetType,
"action_url": msg.ExtraData.ActionURL,
"action_time": msg.ExtraData.ActionTime,
}
}
return resp
}
// SystemMessagesToResponse 将Message列表转换为SystemMessageResponse列表
func SystemMessagesToResponse(messages []*model.Message) []*SystemMessageResponse {
result := make([]*SystemMessageResponse, 0, len(messages))
for _, msg := range messages {
result = append(result, SystemMessageToResponse(msg))
}
return result
}
// SystemNotificationToResponse 将SystemNotification转换为SystemMessageResponse
func SystemNotificationToResponse(n *model.SystemNotification) *SystemMessageResponse {
if n == nil {
return nil
}
resp := &SystemMessageResponse{
ID: strconv.FormatInt(n.ID, 10),
SenderID: model.SystemSenderIDStr, // 系统发送者
ReceiverID: n.ReceiverID,
Content: n.Content,
Category: "notification",
SystemType: string(n.Type),
IsRead: n.IsRead,
CreatedAt: n.CreatedAt,
}
if n.ExtraData != nil {
resp.ExtraData = map[string]interface{}{
"actor_id": n.ExtraData.ActorID,
"actor_id_str": n.ExtraData.ActorIDStr,
"actor_name": n.ExtraData.ActorName,
"avatar_url": n.ExtraData.AvatarURL,
"target_id": n.ExtraData.TargetID,
"target_title": n.ExtraData.TargetTitle,
"target_type": n.ExtraData.TargetType,
"action_url": n.ExtraData.ActionURL,
"action_time": n.ExtraData.ActionTime,
"group_id": n.ExtraData.GroupID,
"group_name": n.ExtraData.GroupName,
"group_avatar": n.ExtraData.GroupAvatar,
"group_description": n.ExtraData.GroupDescription,
"flag": n.ExtraData.Flag,
"request_type": n.ExtraData.RequestType,
"request_status": n.ExtraData.RequestStatus,
"reason": n.ExtraData.Reason,
"target_user_id": n.ExtraData.TargetUserID,
"target_user_name": n.ExtraData.TargetUserName,
"target_user_avatar": n.ExtraData.TargetUserAvatar,
}
}
return resp
}
// SystemNotificationsToResponse 将SystemNotification列表转换为SystemMessageResponse列表
func SystemNotificationsToResponse(notifications []*model.SystemNotification) []*SystemMessageResponse {
result := make([]*SystemMessageResponse, 0, len(notifications))
for _, n := range notifications {
result = append(result, SystemNotificationToResponse(n))
}
return result
}