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 (
"carrot_bbs/internal/model"
"time"
)
// ==================== 举报 DTOs ====================

View File

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

View File

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

View File

@@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"time"
"carrot_bbs/internal/dto"
"carrot_bbs/internal/model"
@@ -32,9 +31,10 @@ type adminReportServiceImpl struct {
commentRepo repository.CommentRepository
messageRepo repository.MessageRepository
userRepo repository.UserRepository
systemNotify SystemNotificationService
notifyRepo repository.SystemNotificationRepository
pushService PushService
txManager repository.TransactionManager
logService *LogService
logService OperationLogService
}
// NewAdminReportService 创建管理端举报服务
@@ -44,9 +44,10 @@ func NewAdminReportService(
commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
systemNotify SystemNotificationService,
notifyRepo repository.SystemNotificationRepository,
pushService PushService,
txManager repository.TransactionManager,
logService *LogService,
logService OperationLogService,
) AdminReportService {
return &adminReportServiceImpl{
reportRepo: reportRepo,
@@ -54,7 +55,8 @@ func NewAdminReportService(
commentRepo: commentRepo,
messageRepo: messageRepo,
userRepo: userRepo,
systemNotify: systemNotify,
notifyRepo: notifyRepo,
pushService: pushService,
txManager: txManager,
logService: logService,
}
@@ -190,9 +192,12 @@ func (s *adminReportServiceImpl) HandleReport(ctx context.Context, id string, ac
// 记录操作日志
if s.logService != nil {
s.logService.LogOperation(ctx, "handle_report", "report", id, handledBy, map[string]interface{}{
"action": action,
"result": result,
s.logService.RecordOperation(&model.OperationLog{
UserID: handledBy,
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 {
s.logService.LogOperation(ctx, "batch_handle_reports", "report", "", handledBy, map[string]interface{}{
"ids": ids,
"action": action,
"success_count": response.SuccessCount,
s.logService.RecordOperation(&model.OperationLog{
UserID: handledBy,
Operation: "batch_handle_reports",
TargetType: "report",
TargetID: "",
Module: "report",
})
}
@@ -341,13 +348,15 @@ func (s *adminReportServiceImpl) getTargetContent(ctx context.Context, targetTyp
if err != nil {
return nil, err
}
content.Content = msg.Content
// 从消息段中提取文本内容
contentText := extractTextFromSegments(msg.Segments)
content.Content = contentText
content.Status = string(msg.Status)
content.CreatedAt = dto.FormatTime(msg.CreatedAt)
// 获取发送者信息
if msg.SenderID != "" {
if sender, err := s.userRepo.GetByID(msg.SenderID); err == nil {
content.Author = dto.ConvertUserToResponse(sender)
content.Author = convertUserToBrief(sender)
}
}
}
@@ -439,4 +448,30 @@ func containsID(ids []string, id string) bool {
}
}
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"
"carrot_bbs/internal/config"
"carrot_bbs/internal/dto"
"carrot_bbs/internal/model"
"carrot_bbs/internal/repository"
@@ -28,9 +27,10 @@ type reportServiceImpl struct {
commentRepo repository.CommentRepository
messageRepo repository.MessageRepository
userRepo repository.UserRepository
systemNotify SystemNotificationService
notifyRepo repository.SystemNotificationRepository
pushService PushService
txManager repository.TransactionManager
logService *LogService
logService OperationLogService
config *config.ReportConfig
}
@@ -41,9 +41,10 @@ func NewReportService(
commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
systemNotify SystemNotificationService,
notifyRepo repository.SystemNotificationRepository,
pushService PushService,
txManager repository.TransactionManager,
logService *LogService,
logService OperationLogService,
cfg *config.Config,
) ReportService {
reportConfig := &cfg.Report
@@ -56,7 +57,8 @@ func NewReportService(
commentRepo: commentRepo,
messageRepo: messageRepo,
userRepo: userRepo,
systemNotify: systemNotify,
notifyRepo: notifyRepo,
pushService: pushService,
txManager: txManager,
logService: logService,
config: reportConfig,
@@ -147,10 +149,12 @@ func (s *reportServiceImpl) CreateReport(ctx context.Context, reporterID, target
// 记录操作日志
if s.logService != nil {
s.logService.LogOperation(ctx, "create_report", "report", report.ID, reporterID, map[string]interface{}{
"target_type": targetType,
"target_id": targetID,
"reason": reason,
s.logService.RecordOperation(&model.OperationLog{
UserID: reporterID,
Operation: "create_report",
TargetType: "report",
TargetID: report.ID,
Module: "report",
})
}