Files
backend/internal/wire/service.go
lafay a69b2026f4
All checks were successful
Build Backend / build (push) Successful in 13m27s
Build Backend / build-docker (push) Successful in 1m29s
feat(report): integrate report handling into application
- Added Report and AdminReport handlers to manage report-related functionalities.
- Updated router and wire generation to include new report services and handlers.
- Enhanced error handling in report and admin report handlers to return appropriate status codes.
- Refactored notification logic in admin report service to utilize a dedicated notification repository and push service.
2026-03-30 03:44:24 +08:00

430 lines
14 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package wire
import (
"time"
"carrot_bbs/internal/cache"
"carrot_bbs/internal/config"
"carrot_bbs/internal/grpc/runner"
"carrot_bbs/internal/pkg/email"
"carrot_bbs/internal/pkg/hook"
"carrot_bbs/internal/pkg/openai"
"carrot_bbs/internal/pkg/redis"
"carrot_bbs/internal/pkg/s3"
"carrot_bbs/internal/pkg/ws"
"carrot_bbs/internal/repository"
"carrot_bbs/internal/service"
"github.com/google/wire"
"go.uber.org/zap"
"gorm.io/gorm"
)
// ServiceSet Service 层 Provider Set
var ServiceSet = wire.NewSet(
// JWT 服务(需要配置参数)
ProvideJWTService,
// 基础服务
ProvideEmailService,
ProvidePostAIService,
// 核心服务(按依赖顺序)
ProvidePushService,
ProvideSystemMessageService,
ProvidePostService,
ProvideCommentService,
ProvideMessageService,
ProvideNotificationService,
ProvideUserService,
ProvideStickerService,
ProvideVoteService,
ProvideChannelService,
ProvideChatService,
ProvideScheduleService,
ProvideScheduleSyncService,
ProvideGroupService,
ProvideUploadService,
ProvideUserActivityService,
ProvideRoleService,
ProvideAdminUserService,
ProvideAdminPostService,
ProvideAdminCommentService,
ProvideAdminGroupService,
ProvideAdminDashboardService,
ProvideQRCodeLoginService,
ProvideHotRankWorker,
ProvideMaterialService,
ProvideCallService,
ProvideReportService,
ProvideAdminReportService,
// 日志服务
ProvideAsyncLogManager,
ProvideOperationLogService,
ProvideLoginLogService,
ProvideDataChangeLogService,
ProvideLogCleanupService,
ProvideLogService,
)
// ProvideJWTService 提供 JWT 服务
func ProvideJWTService(cfg *config.Config) *service.JWTService {
return service.NewJWTService(
cfg.JWT.Secret,
int64(cfg.JWT.AccessTokenExpire.Seconds()),
int64(cfg.JWT.RefreshTokenExpire.Seconds()),
)
}
// ProvideEmailService 提供邮件服务
func ProvideEmailService(client email.Client) service.EmailService {
return service.NewEmailService(client)
}
// ProvidePostAIService 提供 AI 服务
func ProvidePostAIService(client openai.Client) *service.PostAIService {
return service.NewPostAIService(client)
}
// ProvidePushService 提供推送服务
func ProvidePushService(
pushRepo repository.PushRecordRepository,
deviceTokenRepo repository.DeviceTokenRepository,
messageRepo repository.MessageRepository,
wsHub *ws.Hub,
) service.PushService {
return service.NewPushService(pushRepo, deviceTokenRepo, messageRepo, wsHub)
}
// ProvideSystemMessageService 提供系统消息服务
func ProvideSystemMessageService(
notifyRepo repository.SystemNotificationRepository,
pushService service.PushService,
userRepo repository.UserRepository,
postRepo repository.PostRepository,
cacheBackend cache.Cache,
) service.SystemMessageService {
return service.NewSystemMessageService(notifyRepo, pushService, userRepo, postRepo, cacheBackend)
}
func ProvidePostService(
postRepo repository.PostRepository,
systemMessageService service.SystemMessageService,
postAIService *service.PostAIService,
cacheBackend cache.Cache,
txManager repository.TransactionManager,
hookManager *hook.Manager,
_ *hook.ModerationHooks,
_ *hook.BuiltinHooks,
logService *service.LogService,
) service.PostService {
return service.NewPostService(postRepo, systemMessageService, postAIService, cacheBackend, txManager, hookManager, logService)
}
func ProvideCommentService(
commentRepo repository.CommentRepository,
postRepo repository.PostRepository,
systemMessageService service.SystemMessageService,
postAIService *service.PostAIService,
cacheBackend cache.Cache,
hookManager *hook.Manager,
logService *service.LogService,
) *service.CommentService {
return service.NewCommentService(commentRepo, postRepo, systemMessageService, postAIService, cacheBackend, hookManager, logService)
}
// ProvideMessageService 提供消息服务
func ProvideMessageService(
messageRepo repository.MessageRepository,
cacheBackend cache.Cache,
uploadService *service.UploadService,
) *service.MessageService {
return service.NewMessageService(messageRepo, cacheBackend, uploadService)
}
// ProvideNotificationService 提供通知服务
func ProvideNotificationService(
notificationRepo repository.NotificationRepository,
cacheBackend cache.Cache,
) *service.NotificationService {
return service.NewNotificationService(notificationRepo, cacheBackend)
}
// ProvideUserService 提供用户服务
func ProvideUserService(
userRepo repository.UserRepository,
systemMessageService service.SystemMessageService,
emailService service.EmailService,
cacheBackend cache.Cache,
logService *service.LogService,
) service.UserService {
return service.NewUserService(userRepo, systemMessageService, emailService, cacheBackend, logService)
}
// ProvideStickerService 提供表情服务
func ProvideStickerService(
stickerRepo repository.StickerRepository,
) service.StickerService {
return service.NewStickerService(stickerRepo)
}
// ProvideVoteService 提供投票服务
func ProvideVoteService(
voteRepo repository.VoteRepository,
postRepo repository.PostRepository,
cache cache.Cache,
postAIService *service.PostAIService,
systemMessageService service.SystemMessageService,
) *service.VoteService {
return service.NewVoteService(voteRepo, postRepo, cache, postAIService, systemMessageService)
}
func ProvideChannelService(channelRepo repository.ChannelRepository, cacheBackend cache.Cache) service.ChannelService {
return service.NewChannelService(channelRepo, cacheBackend)
}
// ProvideChatService 提供聊天服务
// Note: sensitiveService 传 nil与 main.go 保持一致
func ProvideChatService(
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
wsHub *ws.Hub,
cacheBackend cache.Cache,
uploadService *service.UploadService,
) service.ChatService {
return service.NewChatService(messageRepo, userRepo, nil, wsHub, cacheBackend, uploadService)
}
// ProvideScheduleService 提供日程服务
func ProvideScheduleService(
scheduleRepo repository.ScheduleRepository,
) service.ScheduleService {
return service.NewScheduleService(scheduleRepo)
}
// ProvideScheduleSyncService 提供课表同步服务
func ProvideScheduleSyncService(
taskManager *runner.TaskManager,
scheduleRepo repository.ScheduleRepository,
logger *zap.Logger,
) service.ScheduleSyncService {
return service.NewScheduleSyncService(taskManager, scheduleRepo, logger)
}
// ProvideGroupService 提供群组服务
func ProvideGroupService(
groupRepo repository.GroupRepository,
userRepo repository.UserRepository,
messageRepo repository.MessageRepository,
requestRepo repository.GroupJoinRequestRepository,
notifyRepo repository.SystemNotificationRepository,
wsHub *ws.Hub,
cacheBackend cache.Cache,
) service.GroupService {
return service.NewGroupService(groupRepo, userRepo, messageRepo, requestRepo, notifyRepo, wsHub, cacheBackend)
}
// ProvideUploadService 提供上传服务
func ProvideUploadService(
s3Client *s3.Client,
userService service.UserService,
) *service.UploadService {
return service.NewUploadService(s3Client, userService)
}
// ProvideUserActivityService 提供用户活跃统计服务
func ProvideUserActivityService(repo repository.UserActivityRepository) service.UserActivityService {
return service.NewUserActivityService(repo)
}
// ProvideRoleService 提供角色管理服务
func ProvideRoleService(
roleRepo repository.RoleRepository,
casbinSvc service.CasbinService,
) service.RoleService {
return service.NewRoleService(roleRepo, casbinSvc)
}
// ProvideAdminUserService 提供管理端用户服务
func ProvideAdminUserService(userRepo repository.UserRepository) service.AdminUserService {
return service.NewAdminUserService(userRepo)
}
// ProvideAdminPostService 提供管理端帖子服务
func ProvideAdminPostService(postRepo repository.PostRepository, cacheBackend cache.Cache, logService *service.LogService) service.AdminPostService {
return service.NewAdminPostService(postRepo, cacheBackend, logService)
}
// ProvideAdminCommentService 提供管理端评论服务
func ProvideAdminCommentService(commentRepo repository.CommentRepository, postRepo repository.PostRepository) service.AdminCommentService {
return service.NewAdminCommentService(commentRepo, postRepo)
}
// ProvideAdminGroupService 提供管理端群组服务
func ProvideAdminGroupService(
groupRepo repository.GroupRepository,
userRepo repository.UserRepository,
) service.AdminGroupService {
return service.NewAdminGroupService(groupRepo, userRepo)
}
// ProvideAdminDashboardService 提供管理端仪表盘服务
func ProvideAdminDashboardService(
db *gorm.DB,
userRepo repository.UserRepository,
postRepo repository.PostRepository,
commentRepo repository.CommentRepository,
groupRepo repository.GroupRepository,
activityRepo repository.UserActivityRepository,
) service.AdminDashboardService {
return service.NewAdminDashboardService(db, userRepo, postRepo, commentRepo, groupRepo, activityRepo)
}
// ProvideAsyncLogManager 提供异步日志管理器
func ProvideAsyncLogManager(
operationRepo repository.OperationLogRepository,
loginRepo repository.LoginLogRepository,
dataChangeRepo repository.DataChangeLogRepository,
logger *zap.Logger,
cfg *config.Config,
) *service.AsyncLogManager {
asyncCfg := &service.AsyncLogConfig{
BufferSize: cfg.Log.Async.BufferSize,
BatchSize: cfg.Log.Async.BatchSize,
WorkerCount: cfg.Log.Async.WorkerCount,
EnableFallback: cfg.Log.Async.EnableFallback,
FallbackPath: cfg.Log.Async.FallbackPath,
}
// 解析刷新间隔
if duration, err := parseDuration(cfg.Log.Async.FlushInterval); err == nil {
asyncCfg.FlushInterval = duration
} else {
asyncCfg.FlushInterval = 3 * time.Second
}
return service.NewAsyncLogManager(operationRepo, loginRepo, dataChangeRepo, logger, asyncCfg)
}
// ProvideOperationLogService 提供操作日志服务
func ProvideOperationLogService(
asyncManager *service.AsyncLogManager,
operationRepo repository.OperationLogRepository,
) service.OperationLogService {
return service.NewOperationLogService(asyncManager, operationRepo)
}
// ProvideLoginLogService 提供登录日志服务
func ProvideLoginLogService(
asyncManager *service.AsyncLogManager,
loginRepo repository.LoginLogRepository,
) service.LoginLogService {
return service.NewLoginLogService(asyncManager, loginRepo)
}
// ProvideDataChangeLogService 提供数据变更日志服务
func ProvideDataChangeLogService(
asyncManager *service.AsyncLogManager,
dataChangeRepo repository.DataChangeLogRepository,
) service.DataChangeLogService {
return service.NewDataChangeLogService(asyncManager, dataChangeRepo)
}
// ProvideLogCleanupService 提供日志清理服务
func ProvideLogCleanupService(
operationRepo repository.OperationLogRepository,
loginRepo repository.LoginLogRepository,
dataChangeRepo repository.DataChangeLogRepository,
logger *zap.Logger,
cfg *config.Config,
) service.LogCleanupService {
cleanupCfg := &service.LogCleanupConfig{
Enabled: true,
OperationLog: cfg.Log.Retention.OperationLog,
LoginLog: cfg.Log.Retention.LoginLog,
DataChangeLog: cfg.Log.Retention.DataChange,
CleanupTime: "02:00",
}
return service.NewLogCleanupService(operationRepo, loginRepo, dataChangeRepo, cleanupCfg, logger)
}
// ProvideLogService 提供日志服务组合
func ProvideLogService(
operationLogService service.OperationLogService,
loginLogService service.LoginLogService,
dataChangeLogService service.DataChangeLogService,
) *service.LogService {
return service.NewLogService(operationLogService, loginLogService, dataChangeLogService)
}
// ProvideQRCodeLoginService 提供二维码登录服务
func ProvideHotRankWorker(cfg *config.Config, postRepo repository.PostRepository, cacheBackend cache.Cache) *service.HotRankWorker {
return service.NewHotRankWorker(cfg, postRepo, cacheBackend)
}
func ProvideQRCodeLoginService(
redisClient *redis.Client,
wsHub *ws.Hub,
jwtService *service.JWTService,
userService service.UserService,
activityService service.UserActivityService,
logService *service.LogService,
) *service.QRCodeLoginService {
return service.NewQRCodeLoginService(redisClient, wsHub, jwtService, userService, activityService, logService)
}
// ProvideMaterialService 提供学习资料服务
func ProvideMaterialService(
subjectRepo repository.MaterialSubjectRepository,
fileRepo repository.MaterialFileRepository,
) service.MaterialService {
return service.NewMaterialService(subjectRepo, fileRepo)
}
// parseDuration 解析时长字符串
func parseDuration(s string) (time.Duration, error) {
return time.ParseDuration(s)
}
// ProvideCallService 提供通话服务
func ProvideCallService(
callRepo repository.CallRepository,
wsHub *ws.Hub,
cfg *config.Config,
db *gorm.DB,
) service.CallService {
return service.NewCallService(callRepo, wsHub, cfg, db)
}
// ProvideReportService 提供举报服务
func ProvideReportService(
reportRepo repository.ReportRepository,
postRepo repository.PostRepository,
commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
notifyRepo repository.SystemNotificationRepository,
pushService service.PushService,
txManager repository.TransactionManager,
logService service.OperationLogService,
cfg *config.Config,
) service.ReportService {
return service.NewReportService(reportRepo, postRepo, commentRepo, messageRepo, userRepo, notifyRepo, pushService, txManager, logService, cfg)
}
// ProvideAdminReportService 提供管理端举报服务
func ProvideAdminReportService(
reportRepo repository.ReportRepository,
postRepo repository.PostRepository,
commentRepo repository.CommentRepository,
messageRepo repository.MessageRepository,
userRepo repository.UserRepository,
notifyRepo repository.SystemNotificationRepository,
pushService service.PushService,
txManager repository.TransactionManager,
logService service.OperationLogService,
) service.AdminReportService {
return service.NewAdminReportService(reportRepo, postRepo, commentRepo, messageRepo, userRepo, notifyRepo, pushService, txManager, logService)
}