refactor(report): update import paths and enhance logging functionality
Some checks failed
Build Backend / build (push) Failing after 7m21s
Build Backend / build-docker (push) Has been skipped

- Removed unused time imports from report DTO and service files.
- Updated import paths for response handling in admin and general report handlers.
- Refactored logging methods in admin and report services to use a unified operation logging approach.
- Introduced helper functions for extracting text from message segments and converting user models to brief report information.
This commit is contained in:
lafay
2026-03-30 02:11:12 +08:00
parent 5f7b02ee8e
commit 7fa49155dd
5 changed files with 66 additions and 28 deletions

View File

@@ -2,7 +2,6 @@ package dto
import ( import (
"carrot_bbs/internal/model" "carrot_bbs/internal/model"
"time"
) )
// ==================== 举报 DTOs ==================== // ==================== 举报 DTOs ====================

View File

@@ -2,7 +2,7 @@ package handler
import ( import (
"carrot_bbs/internal/dto" "carrot_bbs/internal/dto"
"carrot_bbs/internal/response" "carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/service" "carrot_bbs/internal/service"
"strconv" "strconv"

View File

@@ -2,7 +2,7 @@ package handler
import ( import (
"carrot_bbs/internal/dto" "carrot_bbs/internal/dto"
"carrot_bbs/internal/response" "carrot_bbs/internal/pkg/response"
"carrot_bbs/internal/service" "carrot_bbs/internal/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"time"
"carrot_bbs/internal/dto" "carrot_bbs/internal/dto"
"carrot_bbs/internal/model" "carrot_bbs/internal/model"
@@ -32,9 +31,10 @@ type adminReportServiceImpl struct {
commentRepo repository.CommentRepository commentRepo repository.CommentRepository
messageRepo repository.MessageRepository messageRepo repository.MessageRepository
userRepo repository.UserRepository userRepo repository.UserRepository
systemNotify SystemNotificationService notifyRepo repository.SystemNotificationRepository
pushService PushService
txManager repository.TransactionManager txManager repository.TransactionManager
logService *LogService logService OperationLogService
} }
// NewAdminReportService 创建管理端举报服务 // NewAdminReportService 创建管理端举报服务
@@ -44,9 +44,10 @@ func NewAdminReportService(
commentRepo repository.CommentRepository, commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository, messageRepo repository.MessageRepository,
userRepo repository.UserRepository, userRepo repository.UserRepository,
systemNotify SystemNotificationService, notifyRepo repository.SystemNotificationRepository,
pushService PushService,
txManager repository.TransactionManager, txManager repository.TransactionManager,
logService *LogService, logService OperationLogService,
) AdminReportService { ) AdminReportService {
return &adminReportServiceImpl{ return &adminReportServiceImpl{
reportRepo: reportRepo, reportRepo: reportRepo,
@@ -54,7 +55,8 @@ func NewAdminReportService(
commentRepo: commentRepo, commentRepo: commentRepo,
messageRepo: messageRepo, messageRepo: messageRepo,
userRepo: userRepo, userRepo: userRepo,
systemNotify: systemNotify, notifyRepo: notifyRepo,
pushService: pushService,
txManager: txManager, txManager: txManager,
logService: logService, logService: logService,
} }
@@ -190,9 +192,12 @@ func (s *adminReportServiceImpl) HandleReport(ctx context.Context, id string, ac
// 记录操作日志 // 记录操作日志
if s.logService != nil { if s.logService != nil {
s.logService.LogOperation(ctx, "handle_report", "report", id, handledBy, map[string]interface{}{ s.logService.RecordOperation(&model.OperationLog{
"action": action, UserID: handledBy,
"result": result, Operation: "handle_report",
TargetType: "report",
TargetID: id,
Module: "report",
}) })
} }
@@ -274,10 +279,12 @@ func (s *adminReportServiceImpl) BatchHandle(ctx context.Context, ids []string,
// 记录操作日志 // 记录操作日志
if s.logService != nil { if s.logService != nil {
s.logService.LogOperation(ctx, "batch_handle_reports", "report", "", handledBy, map[string]interface{}{ s.logService.RecordOperation(&model.OperationLog{
"ids": ids, UserID: handledBy,
"action": action, Operation: "batch_handle_reports",
"success_count": response.SuccessCount, TargetType: "report",
TargetID: "",
Module: "report",
}) })
} }
@@ -341,13 +348,15 @@ func (s *adminReportServiceImpl) getTargetContent(ctx context.Context, targetTyp
if err != nil { if err != nil {
return nil, err return nil, err
} }
content.Content = msg.Content // 从消息段中提取文本内容
contentText := extractTextFromSegments(msg.Segments)
content.Content = contentText
content.Status = string(msg.Status) content.Status = string(msg.Status)
content.CreatedAt = dto.FormatTime(msg.CreatedAt) content.CreatedAt = dto.FormatTime(msg.CreatedAt)
// 获取发送者信息 // 获取发送者信息
if msg.SenderID != "" { if msg.SenderID != "" {
if sender, err := s.userRepo.GetByID(msg.SenderID); err == nil { if sender, err := s.userRepo.GetByID(msg.SenderID); err == nil {
content.Author = dto.ConvertUserToResponse(sender) content.Author = convertUserToBrief(sender)
} }
} }
} }
@@ -440,3 +449,29 @@ func containsID(ids []string, id string) bool {
} }
return false return false
} }
// extractTextFromSegments 从消息段中提取文本内容
func extractTextFromSegments(segments model.MessageSegments) string {
var text string
for _, seg := range segments {
if seg.Type == "text" {
if content, ok := seg.Data["text"].(string); ok {
text += content
}
}
}
return text
}
// convertUserToBrief 将用户模型转换为举报用的简要信息
func convertUserToBrief(user *model.User) *dto.ReportUserBrief {
if user == nil {
return nil
}
return &dto.ReportUserBrief{
ID: user.ID,
Username: user.Username,
Nickname: user.Nickname,
Avatar: user.Avatar,
}
}

View File

@@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"carrot_bbs/internal/config" "carrot_bbs/internal/config"
"carrot_bbs/internal/dto"
"carrot_bbs/internal/model" "carrot_bbs/internal/model"
"carrot_bbs/internal/repository" "carrot_bbs/internal/repository"
@@ -28,9 +27,10 @@ type reportServiceImpl struct {
commentRepo repository.CommentRepository commentRepo repository.CommentRepository
messageRepo repository.MessageRepository messageRepo repository.MessageRepository
userRepo repository.UserRepository userRepo repository.UserRepository
systemNotify SystemNotificationService notifyRepo repository.SystemNotificationRepository
pushService PushService
txManager repository.TransactionManager txManager repository.TransactionManager
logService *LogService logService OperationLogService
config *config.ReportConfig config *config.ReportConfig
} }
@@ -41,9 +41,10 @@ func NewReportService(
commentRepo repository.CommentRepository, commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository, messageRepo repository.MessageRepository,
userRepo repository.UserRepository, userRepo repository.UserRepository,
systemNotify SystemNotificationService, notifyRepo repository.SystemNotificationRepository,
pushService PushService,
txManager repository.TransactionManager, txManager repository.TransactionManager,
logService *LogService, logService OperationLogService,
cfg *config.Config, cfg *config.Config,
) ReportService { ) ReportService {
reportConfig := &cfg.Report reportConfig := &cfg.Report
@@ -56,7 +57,8 @@ func NewReportService(
commentRepo: commentRepo, commentRepo: commentRepo,
messageRepo: messageRepo, messageRepo: messageRepo,
userRepo: userRepo, userRepo: userRepo,
systemNotify: systemNotify, notifyRepo: notifyRepo,
pushService: pushService,
txManager: txManager, txManager: txManager,
logService: logService, logService: logService,
config: reportConfig, config: reportConfig,
@@ -147,10 +149,12 @@ func (s *reportServiceImpl) CreateReport(ctx context.Context, reporterID, target
// 记录操作日志 // 记录操作日志
if s.logService != nil { if s.logService != nil {
s.logService.LogOperation(ctx, "create_report", "report", report.ID, reporterID, map[string]interface{}{ s.logService.RecordOperation(&model.OperationLog{
"target_type": targetType, UserID: reporterID,
"target_id": targetID, Operation: "create_report",
"reason": reason, TargetType: "report",
TargetID: report.ID,
Module: "report",
}) })
} }