refactor: update repository interfaces and improve dependency injection
- Refactored repository structures to use interfaces instead of concrete types, enhancing flexibility and testability. - Updated various repository methods to accept interfaces, allowing for better dependency management. - Modified wire generation to accommodate new repository interfaces, ensuring proper service injection throughout the application. - Enhanced handler methods to utilize DTOs for filtering and data handling, improving code clarity and maintainability.
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
type PostModerationHook struct {
|
||||
name string
|
||||
postAIService PostAIService
|
||||
postRepo *repository.PostRepository
|
||||
postRepo repository.PostRepository
|
||||
strictMode bool
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ type PostAIService interface {
|
||||
ModerateComment(ctx context.Context, content string, images []string) error
|
||||
}
|
||||
|
||||
func NewPostModerationHook(postAIService PostAIService, postRepo *repository.PostRepository, strictMode bool) *PostModerationHook {
|
||||
func NewPostModerationHook(postAIService PostAIService, postRepo repository.PostRepository, strictMode bool) *PostModerationHook {
|
||||
return &PostModerationHook{
|
||||
name: "moderation.post.ai",
|
||||
postAIService: postAIService,
|
||||
@@ -107,11 +107,11 @@ func (h *PostModerationHook) Execute(ctx *HookContext) error {
|
||||
type CommentModerationHook struct {
|
||||
name string
|
||||
postAIService PostAIService
|
||||
commentRepo *repository.CommentRepository
|
||||
commentRepo repository.CommentRepository
|
||||
strictMode bool
|
||||
}
|
||||
|
||||
func NewCommentModerationHook(postAIService PostAIService, commentRepo *repository.CommentRepository, strictMode bool) *CommentModerationHook {
|
||||
func NewCommentModerationHook(postAIService PostAIService, commentRepo repository.CommentRepository, strictMode bool) *CommentModerationHook {
|
||||
return &CommentModerationHook{
|
||||
name: "moderation.comment.ai",
|
||||
postAIService: postAIService,
|
||||
@@ -300,8 +300,8 @@ func (h *SensitiveWordHook) executeComment(ctx *HookContext) error {
|
||||
type ModerationHooks struct {
|
||||
postAIService PostAIService
|
||||
sensitiveService SensitiveService
|
||||
postRepo *repository.PostRepository
|
||||
commentRepo *repository.CommentRepository
|
||||
postRepo repository.PostRepository
|
||||
commentRepo repository.CommentRepository
|
||||
strictMode bool
|
||||
replaceStr string
|
||||
}
|
||||
@@ -309,8 +309,8 @@ type ModerationHooks struct {
|
||||
func NewModerationHooks(
|
||||
postAIService PostAIService,
|
||||
sensitiveService SensitiveService,
|
||||
postRepo *repository.PostRepository,
|
||||
commentRepo *repository.CommentRepository,
|
||||
postRepo repository.PostRepository,
|
||||
commentRepo repository.CommentRepository,
|
||||
strictMode bool,
|
||||
replaceStr string,
|
||||
) *ModerationHooks {
|
||||
|
||||
@@ -69,14 +69,46 @@ func (c *clientImpl) ModeratePost(ctx context.Context, title, content string, im
|
||||
if !c.IsEnabled() {
|
||||
return true, "", nil
|
||||
}
|
||||
return c.moderateContentInBatches(ctx, fmt.Sprintf("帖子标题:%s\n帖子内容:%s", title, content), images)
|
||||
|
||||
// 构建帖子提示词,处理标题或内容为空的情况
|
||||
var prompt strings.Builder
|
||||
if strings.TrimSpace(title) != "" {
|
||||
prompt.WriteString("帖子标题:" + title)
|
||||
}
|
||||
if strings.TrimSpace(content) != "" {
|
||||
if prompt.Len() > 0 {
|
||||
prompt.WriteString("\n")
|
||||
}
|
||||
prompt.WriteString("帖子内容:" + content)
|
||||
}
|
||||
// 如果标题和内容都为空但有图片,说明是纯图片帖子
|
||||
if prompt.Len() == 0 && len(normalizeImageURLs(images)) > 0 {
|
||||
prompt.WriteString("帖子内容:[仅包含图片]")
|
||||
}
|
||||
if prompt.Len() == 0 {
|
||||
prompt.WriteString("帖子内容:[空]")
|
||||
}
|
||||
|
||||
return c.moderateContentInBatches(ctx, prompt.String(), images)
|
||||
}
|
||||
|
||||
func (c *clientImpl) ModerateComment(ctx context.Context, content string, images []string) (bool, string, error) {
|
||||
if !c.IsEnabled() {
|
||||
return true, "", nil
|
||||
}
|
||||
return c.moderateContentInBatches(ctx, fmt.Sprintf("评论内容:%s", content), images)
|
||||
|
||||
// 构建评论提示词,处理纯图片评论的情况
|
||||
var prompt strings.Builder
|
||||
if strings.TrimSpace(content) != "" {
|
||||
prompt.WriteString("评论内容:" + content)
|
||||
} else if len(normalizeImageURLs(images)) > 0 {
|
||||
// 评论只有图片没有文字,说明是纯图片评论
|
||||
prompt.WriteString("评论内容:[仅包含图片]")
|
||||
} else {
|
||||
prompt.WriteString("评论内容:[空]")
|
||||
}
|
||||
|
||||
return c.moderateContentInBatches(ctx, prompt.String(), images)
|
||||
}
|
||||
|
||||
func (c *clientImpl) moderateContentInBatches(ctx context.Context, contentPrompt string, images []string) (bool, string, error) {
|
||||
|
||||
Reference in New Issue
Block a user