feat: 添加举报功能支持
- 新增 Report 数据模型、DTO、Repository、Service 层 - 实现用户端举报 API (POST /api/v1/reports) - 实现管理端举报管理 API (列表、详情、处理、批量处理) - 添加举报自动隐藏机制(达到阈值自动隐藏内容) - 集成系统通知(举报处理结果通知) - 更新路由和 Wire 依赖注入配置 Made-with: Cursor
This commit is contained in:
442
internal/service/admin_report_service.go
Normal file
442
internal/service/admin_report_service.go
Normal file
@@ -0,0 +1,442 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/repository"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// AdminReportService 管理端举报服务接口
|
||||
type AdminReportService interface {
|
||||
// GetReportList 获取举报列表
|
||||
GetReportList(ctx context.Context, query dto.AdminReportListQuery) ([]dto.AdminReportListResponse, int64, error)
|
||||
// GetReportDetail 获取举报详情
|
||||
GetReportDetail(ctx context.Context, id string) (*dto.AdminReportDetailResponse, error)
|
||||
// HandleReport 处理举报
|
||||
HandleReport(ctx context.Context, id string, action, result, handledBy string) error
|
||||
// BatchHandle 批量处理举报
|
||||
BatchHandle(ctx context.Context, ids []string, action, result, handledBy string) (*dto.AdminBatchHandleResponse, error)
|
||||
}
|
||||
|
||||
// adminReportServiceImpl 管理端举报服务实现
|
||||
type adminReportServiceImpl struct {
|
||||
reportRepo repository.ReportRepository
|
||||
postRepo repository.PostRepository
|
||||
commentRepo repository.CommentRepository
|
||||
messageRepo repository.MessageRepository
|
||||
userRepo repository.UserRepository
|
||||
systemNotify SystemNotificationService
|
||||
txManager repository.TransactionManager
|
||||
logService *LogService
|
||||
}
|
||||
|
||||
// NewAdminReportService 创建管理端举报服务
|
||||
func NewAdminReportService(
|
||||
reportRepo repository.ReportRepository,
|
||||
postRepo repository.PostRepository,
|
||||
commentRepo repository.CommentRepository,
|
||||
messageRepo repository.MessageRepository,
|
||||
userRepo repository.UserRepository,
|
||||
systemNotify SystemNotificationService,
|
||||
txManager repository.TransactionManager,
|
||||
logService *LogService,
|
||||
) AdminReportService {
|
||||
return &adminReportServiceImpl{
|
||||
reportRepo: reportRepo,
|
||||
postRepo: postRepo,
|
||||
commentRepo: commentRepo,
|
||||
messageRepo: messageRepo,
|
||||
userRepo: userRepo,
|
||||
systemNotify: systemNotify,
|
||||
txManager: txManager,
|
||||
logService: logService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetReportList 获取举报列表
|
||||
func (s *adminReportServiceImpl) GetReportList(ctx context.Context, query dto.AdminReportListQuery) ([]dto.AdminReportListResponse, int64, error) {
|
||||
// 设置默认分页参数
|
||||
if query.Page <= 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.PageSize <= 0 {
|
||||
query.PageSize = 20
|
||||
}
|
||||
|
||||
// 查询举报列表
|
||||
reports, total, err := s.reportRepo.List(query)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get report list", zap.Error(err))
|
||||
return nil, 0, errors.New("failed to get report list")
|
||||
}
|
||||
|
||||
// 收集用户ID
|
||||
userIDs := make(map[string]bool)
|
||||
for _, report := range reports {
|
||||
userIDs[report.ReporterID] = true
|
||||
if report.HandledBy != nil {
|
||||
userIDs[*report.HandledBy] = true
|
||||
}
|
||||
}
|
||||
|
||||
// 批量获取用户信息
|
||||
users, err := s.getUsersByIDs(ctx, userIDs)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get users", zap.Error(err))
|
||||
}
|
||||
|
||||
// 转换响应
|
||||
responses := make([]dto.AdminReportListResponse, len(reports))
|
||||
for i, report := range reports {
|
||||
var reporter, handler *model.User
|
||||
if user, ok := users[report.ReporterID]; ok {
|
||||
reporter = user
|
||||
}
|
||||
if report.HandledBy != nil {
|
||||
if user, ok := users[*report.HandledBy]; ok {
|
||||
handler = user
|
||||
}
|
||||
}
|
||||
responses[i] = *dto.ConvertReportToAdminListResponse(report, reporter, handler)
|
||||
}
|
||||
|
||||
return responses, total, nil
|
||||
}
|
||||
|
||||
// GetReportDetail 获取举报详情
|
||||
func (s *adminReportServiceImpl) GetReportDetail(ctx context.Context, id string) (*dto.AdminReportDetailResponse, error) {
|
||||
// 查询举报
|
||||
report, err := s.reportRepo.FindByID(id)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get report", zap.Error(err), zap.String("id", id))
|
||||
return nil, errors.New("report not found")
|
||||
}
|
||||
|
||||
// 获取举报人信息
|
||||
var reporter *model.User
|
||||
if user, err := s.userRepo.GetByID(report.ReporterID); err == nil {
|
||||
reporter = user
|
||||
}
|
||||
|
||||
// 获取处理人信息
|
||||
var handler *model.User
|
||||
if report.HandledBy != nil {
|
||||
if user, err := s.userRepo.GetByID(*report.HandledBy); err == nil {
|
||||
handler = user
|
||||
}
|
||||
}
|
||||
|
||||
// 获取被举报内容详情
|
||||
targetContent, err := s.getTargetContent(ctx, report.TargetType, report.TargetID)
|
||||
if err != nil {
|
||||
zap.L().Warn("failed to get target content", zap.Error(err))
|
||||
}
|
||||
|
||||
return dto.ConvertReportToAdminDetailResponse(report, reporter, handler, targetContent), nil
|
||||
}
|
||||
|
||||
// HandleReport 处理举报
|
||||
func (s *adminReportServiceImpl) HandleReport(ctx context.Context, id string, action, result, handledBy string) error {
|
||||
// 查询举报
|
||||
report, err := s.reportRepo.FindByID(id)
|
||||
if err != nil {
|
||||
return errors.New("report not found")
|
||||
}
|
||||
|
||||
// 检查状态
|
||||
if report.Status != model.ReportStatusPending && report.Status != model.ReportStatusProcessing {
|
||||
return errors.New("report has already been handled")
|
||||
}
|
||||
|
||||
// 确定新状态
|
||||
var newStatus model.ReportStatus
|
||||
if action == "approve" {
|
||||
newStatus = model.ReportStatusResolved
|
||||
} else {
|
||||
newStatus = model.ReportStatusRejected
|
||||
}
|
||||
|
||||
// 使用事务处理
|
||||
err = s.txManager.RunInTransaction(ctx, func(ctx context.Context) error {
|
||||
// 更新举报状态
|
||||
if err := s.reportRepo.UpdateStatusWithContext(ctx, id, newStatus, handledBy, result); err != nil {
|
||||
return fmt.Errorf("failed to update report status: %w", err)
|
||||
}
|
||||
|
||||
// 如果确认违规,删除内容
|
||||
if action == "approve" {
|
||||
if err := s.deleteTargetContent(ctx, report.TargetType, report.TargetID); err != nil {
|
||||
zap.L().Error("failed to delete target content", zap.Error(err))
|
||||
// 不阻止举报处理
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 发送通知给举报人
|
||||
s.notifyReporter(ctx, report, action, result)
|
||||
|
||||
// 记录操作日志
|
||||
if s.logService != nil {
|
||||
s.logService.LogOperation(ctx, "handle_report", "report", id, handledBy, map[string]interface{}{
|
||||
"action": action,
|
||||
"result": result,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchHandle 批量处理举报
|
||||
func (s *adminReportServiceImpl) BatchHandle(ctx context.Context, ids []string, action, result, handledBy string) (*dto.AdminBatchHandleResponse, error) {
|
||||
// 确定新状态
|
||||
var newStatus model.ReportStatus
|
||||
if action == "approve" {
|
||||
newStatus = model.ReportStatusResolved
|
||||
} else {
|
||||
newStatus = model.ReportStatusRejected
|
||||
}
|
||||
|
||||
response := &dto.AdminBatchHandleResponse{
|
||||
FailedIDs: []string{},
|
||||
}
|
||||
|
||||
// 使用事务处理
|
||||
err := s.txManager.RunInTransaction(ctx, func(ctx context.Context) error {
|
||||
// 获取举报列表
|
||||
reports, err := s.reportRepo.GetByIDsWithContext(ctx, ids)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get reports: %w", err)
|
||||
}
|
||||
|
||||
// 过滤出可以处理的举报
|
||||
var validIDs []string
|
||||
for _, report := range reports {
|
||||
if report.Status == model.ReportStatusPending || report.Status == model.ReportStatusProcessing {
|
||||
validIDs = append(validIDs, report.ID)
|
||||
} else {
|
||||
response.FailedIDs = append(response.FailedIDs, report.ID)
|
||||
response.FailedCount++
|
||||
}
|
||||
}
|
||||
|
||||
if len(validIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 批量更新状态
|
||||
affected, err := s.reportRepo.BatchUpdateStatusWithContext(ctx, validIDs, newStatus, handledBy, result)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to batch update reports: %w", err)
|
||||
}
|
||||
response.SuccessCount = int(affected)
|
||||
|
||||
// 如果确认违规,批量删除内容
|
||||
if action == "approve" {
|
||||
for _, report := range reports {
|
||||
if containsID(validIDs, report.ID) {
|
||||
if err := s.deleteTargetContent(ctx, report.TargetType, report.TargetID); err != nil {
|
||||
zap.L().Error("failed to delete target content",
|
||||
zap.Error(err),
|
||||
zap.String("target_type", string(report.TargetType)),
|
||||
zap.String("target_id", report.TargetID),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 发送通知给举报人
|
||||
for _, report := range reports {
|
||||
if containsID(validIDs, report.ID) {
|
||||
s.notifyReporter(ctx, report, action, result)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
if s.logService != nil {
|
||||
s.logService.LogOperation(ctx, "batch_handle_reports", "report", "", handledBy, map[string]interface{}{
|
||||
"ids": ids,
|
||||
"action": action,
|
||||
"success_count": response.SuccessCount,
|
||||
})
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// getUsersByIDs 批量获取用户
|
||||
func (s *adminReportServiceImpl) getUsersByIDs(ctx context.Context, userIDs map[string]bool) (map[string]*model.User, error) {
|
||||
users := make(map[string]*model.User)
|
||||
for id := range userIDs {
|
||||
user, err := s.userRepo.GetByID(id)
|
||||
if err == nil {
|
||||
users[id] = user
|
||||
}
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// getTargetContent 获取被举报内容详情
|
||||
func (s *adminReportServiceImpl) getTargetContent(ctx context.Context, targetType model.ReportTargetType, targetID string) (*dto.ReportTargetContent, error) {
|
||||
content := &dto.ReportTargetContent{
|
||||
Type: string(targetType),
|
||||
ID: targetID,
|
||||
}
|
||||
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
post, err := s.postRepo.GetByIDForAdmin(targetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content.Title = post.Title
|
||||
content.Content = post.Content
|
||||
content.Status = string(post.Status)
|
||||
content.CreatedAt = dto.FormatTime(post.CreatedAt)
|
||||
if post.User != nil {
|
||||
content.Author = dto.ConvertUserToResponse(post.User)
|
||||
}
|
||||
if len(post.Images) > 0 {
|
||||
images := make([]string, len(post.Images))
|
||||
for i, img := range post.Images {
|
||||
images[i] = img.URL
|
||||
}
|
||||
content.Images = images
|
||||
}
|
||||
|
||||
case model.ReportTargetTypeComment:
|
||||
comment, err := s.commentRepo.GetByID(targetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content.Content = comment.Content
|
||||
content.Status = string(comment.Status)
|
||||
content.CreatedAt = dto.FormatTime(comment.CreatedAt)
|
||||
if comment.User != nil {
|
||||
content.Author = dto.ConvertUserToResponse(comment.User)
|
||||
}
|
||||
|
||||
case model.ReportTargetTypeMessage:
|
||||
msg, err := s.messageRepo.GetMessageByID(targetID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content.Content = msg.Content
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// deleteTargetContent 删除目标内容
|
||||
func (s *adminReportServiceImpl) deleteTargetContent(ctx context.Context, targetType model.ReportTargetType, targetID string) error {
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
return s.postRepo.Delete(targetID)
|
||||
case model.ReportTargetTypeComment:
|
||||
return s.commentRepo.Delete(targetID)
|
||||
case model.ReportTargetTypeMessage:
|
||||
return s.messageRepo.UpdateMessageStatus(targetID, model.MessageStatusDeleted)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// notifyReporter 通知举报人
|
||||
func (s *adminReportServiceImpl) notifyReporter(ctx context.Context, report *model.Report, action, result string) {
|
||||
if s.systemNotify == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取举报人信息
|
||||
reporter, err := s.userRepo.GetByID(report.ReporterID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var title, content string
|
||||
var notifyType model.SystemNotificationType
|
||||
|
||||
if action == "approve" {
|
||||
notifyType = model.SysNotifyReportResolved
|
||||
title = "举报处理结果"
|
||||
content = fmt.Sprintf("您举报的%s已确认违规,内容已被删除。", s.getTargetTypeName(report.TargetType))
|
||||
if result != "" {
|
||||
content += fmt.Sprintf(" 处理说明:%s", result)
|
||||
}
|
||||
} else {
|
||||
notifyType = model.SysNotifyReportRejected
|
||||
title = "举报处理结果"
|
||||
content = fmt.Sprintf("您举报的%s经审核未发现违规,举报已驳回。", s.getTargetTypeName(report.TargetType))
|
||||
if result != "" {
|
||||
content += fmt.Sprintf(" 处理说明:%s", result)
|
||||
}
|
||||
}
|
||||
|
||||
// 发送通知
|
||||
_, err = s.systemNotify.CreateNotification(ctx, &model.SystemNotification{
|
||||
ReceiverID: report.ReporterID,
|
||||
Type: notifyType,
|
||||
Title: title,
|
||||
Content: content,
|
||||
ExtraData: &model.SystemNotificationExtra{
|
||||
ActorIDStr: report.ReporterID,
|
||||
ActorName: reporter.Nickname,
|
||||
TargetID: report.TargetID,
|
||||
TargetType: string(report.TargetType),
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
zap.L().Error("failed to send report notification", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// getTargetTypeName 获取目标类型名称
|
||||
func (s *adminReportServiceImpl) getTargetTypeName(targetType model.ReportTargetType) string {
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
return "帖子"
|
||||
case model.ReportTargetTypeComment:
|
||||
return "评论"
|
||||
case model.ReportTargetTypeMessage:
|
||||
return "消息"
|
||||
}
|
||||
return "内容"
|
||||
}
|
||||
|
||||
// containsID 检查ID是否在列表中
|
||||
func containsID(ids []string, id string) bool {
|
||||
for _, v := range ids {
|
||||
if v == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
213
internal/service/report_service.go
Normal file
213
internal/service/report_service.go
Normal file
@@ -0,0 +1,213 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"carrot_bbs/internal/config"
|
||||
"carrot_bbs/internal/dto"
|
||||
"carrot_bbs/internal/model"
|
||||
"carrot_bbs/internal/repository"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// ReportService 举报服务接口(用户端)
|
||||
type ReportService interface {
|
||||
// CreateReport 创建举报
|
||||
CreateReport(ctx context.Context, reporterID, targetType, targetID, reason, description string) (*model.Report, error)
|
||||
// HasUserReported 检查用户是否已举报
|
||||
HasUserReported(ctx context.Context, reporterID, targetType, targetID string) (bool, error)
|
||||
}
|
||||
|
||||
// reportServiceImpl 举报服务实现
|
||||
type reportServiceImpl struct {
|
||||
reportRepo repository.ReportRepository
|
||||
postRepo repository.PostRepository
|
||||
commentRepo repository.CommentRepository
|
||||
messageRepo repository.MessageRepository
|
||||
userRepo repository.UserRepository
|
||||
systemNotify SystemNotificationService
|
||||
txManager repository.TransactionManager
|
||||
logService *LogService
|
||||
config *config.ReportConfig
|
||||
}
|
||||
|
||||
// NewReportService 创建举报服务
|
||||
func NewReportService(
|
||||
reportRepo repository.ReportRepository,
|
||||
postRepo repository.PostRepository,
|
||||
commentRepo repository.CommentRepository,
|
||||
messageRepo repository.MessageRepository,
|
||||
userRepo repository.UserRepository,
|
||||
systemNotify SystemNotificationService,
|
||||
txManager repository.TransactionManager,
|
||||
logService *LogService,
|
||||
cfg *config.Config,
|
||||
) ReportService {
|
||||
reportConfig := &cfg.Report
|
||||
if reportConfig.AutoHideThreshold == 0 {
|
||||
reportConfig.AutoHideThreshold = 3
|
||||
}
|
||||
return &reportServiceImpl{
|
||||
reportRepo: reportRepo,
|
||||
postRepo: postRepo,
|
||||
commentRepo: commentRepo,
|
||||
messageRepo: messageRepo,
|
||||
userRepo: userRepo,
|
||||
systemNotify: systemNotify,
|
||||
txManager: txManager,
|
||||
logService: logService,
|
||||
config: reportConfig,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateReport 创建举报
|
||||
func (s *reportServiceImpl) CreateReport(ctx context.Context, reporterID, targetType, targetID, reason, description string) (*model.Report, error) {
|
||||
// 验证举报类型
|
||||
reportTargetType := model.ReportTargetType(targetType)
|
||||
if reportTargetType != model.ReportTargetTypePost &&
|
||||
reportTargetType != model.ReportTargetTypeComment &&
|
||||
reportTargetType != model.ReportTargetTypeMessage {
|
||||
return nil, errors.New("invalid target type")
|
||||
}
|
||||
|
||||
// 验证举报原因
|
||||
reportReason := model.ReportReason(reason)
|
||||
if reportReason != model.ReportReasonSpam &&
|
||||
reportReason != model.ReportReasonInappropriate &&
|
||||
reportReason != model.ReportReasonHarassment &&
|
||||
reportReason != model.ReportReasonMisinformation &&
|
||||
reportReason != model.ReportReasonOther {
|
||||
return nil, errors.New("invalid reason")
|
||||
}
|
||||
|
||||
// 检查目标内容是否存在
|
||||
if err := s.validateTargetExists(ctx, reportTargetType, targetID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 检查用户是否已举报过
|
||||
hasReported, err := s.reportRepo.HasUserReported(reporterID, reportTargetType, targetID)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to check if user has reported", zap.Error(err))
|
||||
return nil, errors.New("failed to check report status")
|
||||
}
|
||||
if hasReported {
|
||||
return nil, errors.New("you have already reported this content")
|
||||
}
|
||||
|
||||
// 创建举报
|
||||
report := &model.Report{
|
||||
ReporterID: reporterID,
|
||||
TargetType: reportTargetType,
|
||||
TargetID: targetID,
|
||||
Reason: reportReason,
|
||||
Description: description,
|
||||
Status: model.ReportStatusPending,
|
||||
}
|
||||
|
||||
// 使用事务处理
|
||||
var createdReport *model.Report
|
||||
err = s.txManager.RunInTransaction(ctx, func(ctx context.Context) error {
|
||||
// 创建举报记录
|
||||
if err := s.reportRepo.CreateWithContext(ctx, report); err != nil {
|
||||
return fmt.Errorf("failed to create report: %w", err)
|
||||
}
|
||||
|
||||
createdReport = report
|
||||
|
||||
// 检查举报次数是否达到阈值
|
||||
count, err := s.reportRepo.GetReportCount(reportTargetType, targetID)
|
||||
if err != nil {
|
||||
zap.L().Error("failed to get report count", zap.Error(err))
|
||||
return nil // 不阻止举报创建
|
||||
}
|
||||
|
||||
// 达到阈值,自动隐藏内容
|
||||
if int(count) >= s.config.AutoHideThreshold {
|
||||
if err := s.hideTargetContent(ctx, reportTargetType, targetID); err != nil {
|
||||
zap.L().Error("failed to hide target content", zap.Error(err))
|
||||
return nil // 不阻止举报创建
|
||||
}
|
||||
zap.L().Info("content auto-hidden due to report threshold",
|
||||
zap.String("target_type", targetType),
|
||||
zap.String("target_id", targetID),
|
||||
zap.Int64("report_count", count),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
return createdReport, nil
|
||||
}
|
||||
|
||||
// HasUserReported 检查用户是否已举报
|
||||
func (s *reportServiceImpl) HasUserReported(ctx context.Context, reporterID, targetType, targetID string) (bool, error) {
|
||||
return s.reportRepo.HasUserReported(reporterID, model.ReportTargetType(targetType), targetID)
|
||||
}
|
||||
|
||||
// validateTargetExists 验证目标内容是否存在
|
||||
func (s *reportServiceImpl) validateTargetExists(ctx context.Context, targetType model.ReportTargetType, targetID string) error {
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
post, err := s.postRepo.GetByID(targetID)
|
||||
if err != nil || post == nil {
|
||||
return errors.New("post not found")
|
||||
}
|
||||
case model.ReportTargetTypeComment:
|
||||
comment, err := s.commentRepo.GetByID(targetID)
|
||||
if err != nil || comment == nil {
|
||||
return errors.New("comment not found")
|
||||
}
|
||||
case model.ReportTargetTypeMessage:
|
||||
_, err := s.messageRepo.GetMessageByID(targetID)
|
||||
if err != nil {
|
||||
return errors.New("message not found")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// hideTargetContent 隐藏目标内容
|
||||
func (s *reportServiceImpl) hideTargetContent(ctx context.Context, targetType model.ReportTargetType, targetID string) error {
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
post, err := s.postRepo.GetByID(targetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if post != nil && post.Status != model.PostStatusDeleted {
|
||||
post.Status = model.PostStatusDeleted
|
||||
return s.postRepo.Update(post)
|
||||
}
|
||||
case model.ReportTargetTypeComment:
|
||||
comment, err := s.commentRepo.GetByID(targetID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if comment != nil && comment.Status != model.CommentStatusDeleted {
|
||||
comment.Status = model.CommentStatusDeleted
|
||||
return s.commentRepo.Update(comment)
|
||||
}
|
||||
case model.ReportTargetTypeMessage:
|
||||
// 消息隐藏通过更新状态实现
|
||||
return s.messageRepo.UpdateMessageStatus(targetID, model.MessageStatusDeleted)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user