refactor(server): decouple services and improve architecture
- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion. - Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects. - Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management. - Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering. - Improve database initialization by moving it from `internal/model` to `internal/database`. - Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool. - Enhance error handling and security by implementing fail-fast checks for encryption key length during startup. - Clean up unused code, including the `avatar` package and several unused DTOs.
This commit is contained in:
@@ -171,21 +171,31 @@ func (e *BioModerationReviewError) IsReview() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
type PostAIService struct {
|
||||
// PostAIService 帖子 AI 审核服务接口
|
||||
type PostAIService interface {
|
||||
IsEnabled() bool
|
||||
ModeratePost(ctx context.Context, title, content string, images []string) error
|
||||
ModerateComment(ctx context.Context, content string, images []string) error
|
||||
ModerateImage(ctx context.Context, imageURL string) error
|
||||
ModerateBio(ctx context.Context, bio string) error
|
||||
}
|
||||
|
||||
// postAIService 帖子 AI 审核服务实现
|
||||
type postAIService struct {
|
||||
openAIClient openai.Client
|
||||
tencentClient tencent.Client
|
||||
strictMode bool
|
||||
}
|
||||
|
||||
func NewPostAIService(openAIClient openai.Client, tencentClient tencent.Client, strictMode bool) *PostAIService {
|
||||
return &PostAIService{
|
||||
func NewPostAIService(openAIClient openai.Client, tencentClient tencent.Client, strictMode bool) PostAIService {
|
||||
return &postAIService{
|
||||
openAIClient: openAIClient,
|
||||
tencentClient: tencentClient,
|
||||
strictMode: strictMode,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostAIService) IsEnabled() bool {
|
||||
func (s *postAIService) IsEnabled() bool {
|
||||
if s == nil {
|
||||
return false
|
||||
}
|
||||
@@ -193,15 +203,15 @@ func (s *PostAIService) IsEnabled() bool {
|
||||
(s.tencentClient != nil && s.tencentClient.IsEnabled())
|
||||
}
|
||||
|
||||
func (s *PostAIService) isOpenAIEnabled() bool {
|
||||
func (s *postAIService) isOpenAIEnabled() bool {
|
||||
return s != nil && s.openAIClient != nil && s.openAIClient.IsEnabled()
|
||||
}
|
||||
|
||||
func (s *PostAIService) isTencentEnabled() bool {
|
||||
func (s *postAIService) isTencentEnabled() bool {
|
||||
return s != nil && s.tencentClient != nil && s.tencentClient.IsEnabled()
|
||||
}
|
||||
|
||||
func (s *PostAIService) ModeratePost(ctx context.Context, title, content string, images []string) error {
|
||||
func (s *postAIService) ModeratePost(ctx context.Context, title, content string, images []string) error {
|
||||
if !s.IsEnabled() {
|
||||
return nil
|
||||
}
|
||||
@@ -234,7 +244,7 @@ func (s *PostAIService) ModeratePost(ctx context.Context, title, content string,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PostAIService) ModerateComment(ctx context.Context, content string, images []string) error {
|
||||
func (s *postAIService) ModerateComment(ctx context.Context, content string, images []string) error {
|
||||
if !s.IsEnabled() {
|
||||
return nil
|
||||
}
|
||||
@@ -267,7 +277,7 @@ func (s *PostAIService) ModerateComment(ctx context.Context, content string, ima
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PostAIService) ModerateImage(ctx context.Context, imageURL string) error {
|
||||
func (s *postAIService) ModerateImage(ctx context.Context, imageURL string) error {
|
||||
if !s.isOpenAIEnabled() {
|
||||
return nil
|
||||
}
|
||||
@@ -293,7 +303,7 @@ func (s *PostAIService) ModerateImage(ctx context.Context, imageURL string) erro
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostAIService) ModerateBio(ctx context.Context, bio string) error {
|
||||
func (s *postAIService) ModerateBio(ctx context.Context, bio string) error {
|
||||
if !s.IsEnabled() {
|
||||
return nil
|
||||
}
|
||||
@@ -326,7 +336,7 @@ func (s *PostAIService) ModerateBio(ctx context.Context, bio string) error {
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PostAIService) moderateTextWithTencent(
|
||||
func (s *postAIService) moderateTextWithTencent(
|
||||
ctx context.Context,
|
||||
text string,
|
||||
rejectFactory func(string) error,
|
||||
|
||||
Reference in New Issue
Block a user