2026-03-09 21:28:58 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"carrot_bbs/internal/pkg/openai"
|
2026-03-17 00:47:17 +08:00
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2026-03-09 21:28:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type PostModerationRejectedError struct {
|
|
|
|
|
Reason string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *PostModerationRejectedError) Error() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "post rejected by moderation"
|
|
|
|
|
}
|
|
|
|
|
return "post rejected by moderation: " + e.Reason
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *PostModerationRejectedError) UserMessage() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "内容未通过审核,请修改后重试"
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(e.Reason)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 04:23:24 +08:00
|
|
|
type PostModerationReviewError struct {
|
|
|
|
|
Reason string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *PostModerationReviewError) Error() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "post needs manual review"
|
|
|
|
|
}
|
|
|
|
|
return "post needs manual review: " + e.Reason
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *PostModerationReviewError) UserMessage() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "内容需要人工复审,请耐心等待"
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(e.Reason)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *PostModerationReviewError) IsReview() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 21:28:58 +08:00
|
|
|
type CommentModerationRejectedError struct {
|
|
|
|
|
Reason string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *CommentModerationRejectedError) Error() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "comment rejected by moderation"
|
|
|
|
|
}
|
|
|
|
|
return "comment rejected by moderation: " + e.Reason
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *CommentModerationRejectedError) UserMessage() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "评论未通过审核,请修改后重试"
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(e.Reason)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 04:23:24 +08:00
|
|
|
type CommentModerationReviewError struct {
|
|
|
|
|
Reason string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *CommentModerationReviewError) Error() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "comment needs manual review"
|
|
|
|
|
}
|
|
|
|
|
return "comment needs manual review: " + e.Reason
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *CommentModerationReviewError) UserMessage() string {
|
|
|
|
|
if strings.TrimSpace(e.Reason) == "" {
|
|
|
|
|
return "评论需要人工复审,请耐心等待"
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimSpace(e.Reason)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *CommentModerationReviewError) IsReview() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 21:28:58 +08:00
|
|
|
type PostAIService struct {
|
|
|
|
|
openAIClient openai.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPostAIService(openAIClient openai.Client) *PostAIService {
|
|
|
|
|
return &PostAIService{
|
|
|
|
|
openAIClient: openAIClient,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *PostAIService) IsEnabled() bool {
|
|
|
|
|
return s != nil && s.openAIClient != nil && s.openAIClient.IsEnabled()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *PostAIService) ModeratePost(ctx context.Context, title, content string, images []string) error {
|
|
|
|
|
if !s.IsEnabled() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 04:23:24 +08:00
|
|
|
resp, err := s.openAIClient.ModeratePost(ctx, title, content, images)
|
2026-03-09 21:28:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if s.openAIClient.Config().StrictModeration {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-17 00:47:17 +08:00
|
|
|
zap.L().Warn("AI moderation failed, fallback allow",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
)
|
2026-03-09 21:28:58 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-11 04:23:24 +08:00
|
|
|
|
|
|
|
|
switch resp.Result {
|
|
|
|
|
case openai.ModerationResultBlock:
|
|
|
|
|
return &PostModerationRejectedError{Reason: resp.Reason}
|
|
|
|
|
case openai.ModerationResultReview:
|
|
|
|
|
return &PostModerationReviewError{Reason: resp.Reason}
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
2026-03-09 21:28:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *PostAIService) ModerateComment(ctx context.Context, content string, images []string) error {
|
|
|
|
|
if !s.IsEnabled() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 04:23:24 +08:00
|
|
|
resp, err := s.openAIClient.ModerateComment(ctx, content, images)
|
2026-03-09 21:28:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if s.openAIClient.Config().StrictModeration {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-17 00:47:17 +08:00
|
|
|
zap.L().Warn("AI comment moderation failed, fallback allow",
|
|
|
|
|
zap.Error(err),
|
|
|
|
|
)
|
2026-03-09 21:28:58 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-04-11 04:23:24 +08:00
|
|
|
|
|
|
|
|
switch resp.Result {
|
|
|
|
|
case openai.ModerationResultBlock:
|
|
|
|
|
return &CommentModerationRejectedError{Reason: resp.Reason}
|
|
|
|
|
case openai.ModerationResultReview:
|
|
|
|
|
return &CommentModerationReviewError{Reason: resp.Reason}
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
2026-03-09 21:28:58 +08:00
|
|
|
}
|
|
|
|
|
}
|