advanced

package
v1.1.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2026 License: AGPL-3.0 Imports: 36 Imported by: 0

Documentation

Overview

Package advanced provides sophisticated content moderation decision engines with threat analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgeRange

type AgeRange struct {
	Low  int
	High int
}

AgeRange represents an estimated age range

type AudioAnalysis

type AudioAnalysis struct {
	Transcription string
	Language      string
	TextAnalysis  *ContentAnalysis
}

AudioAnalysis represents analysis of audio content

type BoundingBox

type BoundingBox struct {
	Left   float64
	Top    float64
	Width  float64
	Height float64
}

BoundingBox represents the location of a detected element

type CelebrityMatch

type CelebrityMatch struct {
	Name        string
	Confidence  float64
	BoundingBox BoundingBox
	URLs        []string
}

CelebrityMatch represents a detected celebrity in content

type CombinedAnalysis

type CombinedAnalysis struct {
	ImageAnalysis *ImageAnalysis
	TextAnalysis  *ContentAnalysis
	Flags         []string
	RiskLevel     string
}

CombinedAnalysis represents combined image and text analysis results

type ComprehendClientInterface

type ComprehendClientInterface interface {
	DetectDominantLanguage(ctx context.Context, params *comprehend.DetectDominantLanguageInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectDominantLanguageOutput, error)
	DetectSentiment(ctx context.Context, params *comprehend.DetectSentimentInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectSentimentOutput, error)
	DetectPiiEntities(ctx context.Context, params *comprehend.DetectPiiEntitiesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectPiiEntitiesOutput, error)
	DetectEntities(ctx context.Context, params *comprehend.DetectEntitiesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectEntitiesOutput, error)
	DetectKeyPhrases(ctx context.Context, params *comprehend.DetectKeyPhrasesInput, optFns ...func(*comprehend.Options)) (*comprehend.DetectKeyPhrasesOutput, error)
}

ComprehendClientInterface defines the subset of methods we use from the AWS Comprehend client

type ContentAnalysis

type ContentAnalysis struct {
	ContentID      string
	Sentiment      SentimentAnalysis
	Toxicity       ToxicityAnalysis
	PII            []PIIEntity
	Topics         []Topic
	Language       LanguageDetection
	Threats        []ThreatIndicator
	CustomFlags    []CustomFlag
	AnalyzedAt     time.Time
	ProcessingTime time.Duration
}

ContentAnalysis represents the result of text content analysis

type ContentMetadata

type ContentMetadata struct {
	ContentID    string
	AuthorID     string
	AuthorDomain string
	ContentType  ContentType
	Language     string
	Context      string // e.g., "post", "comment", "profile"
	Timestamp    time.Time
	ReplyTo      string // If this is a reply
	Mentions     []string
	Hashtags     []string
	URLs         []string
}

ContentMetadata contains metadata about the content being analyzed

type ContentType

type ContentType string

ContentType represents the type of content being moderated

const (
	// ContentTypeText represents text-based content
	ContentTypeText ContentType = "text"
	// ContentTypeImage represents image content
	ContentTypeImage ContentType = "image"
	// ContentTypeVideo represents video content
	ContentTypeVideo ContentType = "video"
	// ContentTypeAudio represents audio content
	ContentTypeAudio ContentType = "audio"
	// ContentTypeLink represents link content
	ContentTypeLink ContentType = "link"
)

type CostTracker

type CostTracker interface {
	TrackComprehendRequest(operation string, units int)
	TrackTranscribeRequest(jobName string, estimatedMinutes int)
}

CostTracker interface for tracking AWS costs

type CustomFlag

type CustomFlag struct {
	Name       string
	Value      any
	Confidence float64
}

CustomFlag represents a custom moderation flag

type CustomLabel

type CustomLabel struct {
	Name       string
	Confidence float64
	Parents    []string
}

CustomLabel represents a custom detected label

type DecisionEngine

type DecisionEngine struct {
	// contains filtered or unexported fields
}

DecisionEngine makes moderation decisions based on analysis results

func NewDecisionEngine

func NewDecisionEngine(config *ModerationConfig, logger *zap.Logger, reputationScorer *ReputationScorer) *DecisionEngine

NewDecisionEngine creates a new decision engine

func (*DecisionEngine) MakeDecision

func (de *DecisionEngine) MakeDecision(_ context.Context, analysis *ModerationAnalysis) (*ModerationDecision, error)

MakeDecision makes a moderation decision based on all analysis results

type DecisionReason

type DecisionReason struct {
	Type        string // "toxicity", "explicit", "pattern", "reputation", etc.
	Severity    Severity
	Description string
	Evidence    any // Can be various types of evidence
}

DecisionReason explains why a decision was made

type Emotion

type Emotion struct {
	Type       string // "HAPPY", "SAD", "ANGRY", etc.
	Confidence float64
}

Emotion represents a detected emotion in a face

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine is the main moderation engine implementation

func NewEngine

func NewEngine(
	config *ModerationConfig,
	comprehendClient *comprehend.Client,
	rekognitionClient *rekognition.Client,
	tableName string,
	patternRepo PatternRepository,
	logger *zap.Logger,
	costTracker CostTracker,
	dynamoRM core.DB,
) *Engine

NewEngine creates a new moderation engine

func NewEngineWithMode

func NewEngineWithMode(opts EngineOptions) *Engine

NewEngineWithMode creates a moderation engine with the specified mode

func (*Engine) AnalyzeContent

func (e *Engine) AnalyzeContent(content string, metadata ContentMetadata) (*ContentAnalysis, error)

AnalyzeContent analyzes text content

func (*Engine) AnalyzeContentBatch

func (e *Engine) AnalyzeContentBatch(contents []struct {
	Content  string
	Metadata ContentMetadata
}) ([]*ContentAnalysis, error)

AnalyzeContentBatch analyzes multiple pieces of content in parallel

func (*Engine) AnalyzeImage

func (e *Engine) AnalyzeImage(imageURL string, metadata ContentMetadata) (*ImageAnalysis, error)

AnalyzeImage analyzes image content

func (*Engine) AnalyzeVideo

func (e *Engine) AnalyzeVideo(videoURL string, metadata ContentMetadata) (*VideoAnalysis, error)

AnalyzeVideo analyzes video content using AWS Rekognition Video

func (*Engine) CreatePattern

func (e *Engine) CreatePattern(pattern *ModerationPattern) error

CreatePattern creates a new moderation pattern

func (*Engine) DeletePattern

func (e *Engine) DeletePattern(patternID string) error

DeletePattern deletes a pattern

func (*Engine) GetFalsePositiveRate

func (e *Engine) GetFalsePositiveRate(timeRange TimeRange) (float64, error)

GetFalsePositiveRate calculates the false positive rate

func (*Engine) GetModerationStats

func (e *Engine) GetModerationStats(timeRange TimeRange) (*ModerationStats, error)

GetModerationStats gets moderation statistics

func (*Engine) GetPatterns

func (e *Engine) GetPatterns(filter PatternFilter) ([]*ModerationPattern, error)

GetPatterns retrieves patterns based on filter

func (*Engine) GetReputationScore

func (e *Engine) GetReputationScore(actorID string) (*ReputationScore, error)

GetReputationScore gets a user's reputation score

func (*Engine) GetSharedThreats

func (e *Engine) GetSharedThreats(since time.Time) ([]*ThreatIntel, error)

GetSharedThreats retrieves shared threats

func (*Engine) MakeDecision

func (e *Engine) MakeDecision(analysis *ModerationAnalysis) (*ModerationDecision, error)

MakeDecision makes a moderation decision based on analysis

func (*Engine) ShareThreat

func (e *Engine) ShareThreat(threat *ThreatIntel) error

ShareThreat shares threat intelligence

func (*Engine) UpdatePattern

func (e *Engine) UpdatePattern(patternID string, pattern *ModerationPattern) error

UpdatePattern updates an existing pattern

func (*Engine) UpdateReputation

func (e *Engine) UpdateReputation(actorID string, event ReputationEvent) error

UpdateReputation updates a user's reputation

type EngineOptions

type EngineOptions struct {
	Mode              ModerationMode
	Config            *ModerationConfig
	ComprehendClient  *comprehend.Client
	RekognitionClient *rekognition.Client
	TableName         string
	PatternRepo       PatternRepository
	Logger            *zap.Logger
	CostTracker       CostTracker
	DynamoRM          core.DB
}

EngineOptions contains options for creating a moderation engine

type ExplicitContent

type ExplicitContent struct {
	IsExplicit         bool
	NudityScore        float64
	SuggestiveScore    float64
	ViolenceScore      float64
	VisuallyDisturbing float64
	Confidence         float64
}

ExplicitContent represents explicit content detection

type FaceAnalysis

type FaceAnalysis struct {
	BoundingBox BoundingBox
	Emotions    []Emotion
	AgeRange    AgeRange
	Gender      Gender
	Confidence  float64
}

FaceAnalysis represents analysis of a detected face

type FrameAnalysis

type FrameAnalysis struct {
	Timestamp     time.Duration
	ImageAnalysis ImageAnalysis
}

FrameAnalysis represents analysis of a single video frame

type Gender

type Gender struct {
	Value      string // "Male", "Female"
	Confidence float64
}

Gender represents detected gender information

type GlobalConfig

type GlobalConfig interface {
	GetDisableAWSModeration() bool
	GetDisableComprehend() bool
	GetDisableRekognition() bool
}

GlobalConfig interface for accessing global configuration

type ImageAnalysis

type ImageAnalysis struct {
	ImageURL       string
	Explicit       ExplicitContent
	Violence       ViolenceDetection
	Text           []TextInImage
	Objects        []ObjectDetection
	Faces          []FaceAnalysis
	Celebrities    []CelebrityMatch
	CustomLabels   []CustomLabel
	AnalyzedAt     time.Time
	ProcessingTime time.Duration
}

ImageAnalysis represents the result of image analysis

type ImageAnalyzer

type ImageAnalyzer struct {
	// contains filtered or unexported fields
}

ImageAnalyzer handles image content analysis using AWS Rekognition

func NewImageAnalyzer

func NewImageAnalyzer(client *rekognition.Client, logger *zap.Logger, config *ModerationConfig, costTracker CostTracker) *ImageAnalyzer

NewImageAnalyzer creates a new image analyzer

func (*ImageAnalyzer) AnalyzeImage

func (ia *ImageAnalyzer) AnalyzeImage(ctx context.Context, imageURL string, _ ContentMetadata) (*ImageAnalysis, error)

AnalyzeImage performs comprehensive image analysis

func (*ImageAnalyzer) AnalyzeImageContent

func (ia *ImageAnalyzer) AnalyzeImageContent(ctx context.Context, imageURL string, textContent string) (*CombinedAnalysis, error)

AnalyzeImageContent performs content-specific analysis

func (*ImageAnalyzer) GetClient

func (ia *ImageAnalyzer) GetClient() *rekognition.Client

GetClient returns the Rekognition client for use by video analyzer

type ImageAnalyzerInterface

type ImageAnalyzerInterface interface {
	AnalyzeImage(ctx context.Context, imageURL string, metadata ContentMetadata) (*ImageAnalysis, error)
}

ImageAnalyzerInterface defines the interface for image analysis

type LanguageDetection

type LanguageDetection struct {
	LanguageCode string // e.g., "en", "es", "fr"
	Confidence   float64
}

LanguageDetection represents detected language

type ModerationAction

type ModerationAction string

ModerationAction represents the action to take

const (
	// ActionAllow represents allowing content to pass through
	ActionAllow ModerationAction = "allow"
	// ActionFlag represents flagging content for review
	ActionFlag ModerationAction = "flag"
	// ActionQuarantine represents quarantining content temporarily
	ActionQuarantine ModerationAction = "quarantine"
	// ActionRemove represents removing content entirely
	ActionRemove ModerationAction = "remove"
	// ActionShadowBan represents shadow banning the content author
	ActionShadowBan ModerationAction = "shadow_ban"
	// ActionReportToAuth represents reporting content to authorities
	ActionReportToAuth ModerationAction = "report_to_authorities"
)

type ModerationAnalysis

type ModerationAnalysis struct {
	ContentMetadata ContentMetadata
	TextAnalysis    *ContentAnalysis
	ImageAnalysis   *ImageAnalysis
	VideoAnalysis   *VideoAnalysis
	PatternMatches  []PatternMatch
	ReputationScore *ReputationScore
	ThreatMatches   []ThreatMatch
}

ModerationAnalysis combines all analysis results

type ModerationConfig

type ModerationConfig struct {
	// Thresholds
	ToxicityThreshold   float64
	ExplicitThreshold   float64
	ViolenceThreshold   float64
	ConfidenceThreshold float64

	// Actions
	AutoRemoveThreshold float64
	QuarantineThreshold float64
	FlagThreshold       float64

	// Reputation
	ReputationDecayRate   float64
	BadActorThreshold     float64
	TrustedActorThreshold float64

	// Performance
	MaxAnalysisTime time.Duration
	EnableCaching   bool
	CacheTTL        time.Duration

	// Features
	EnableTextAnalysis      bool
	EnableImageAnalysis     bool
	EnableVideoAnalysis     bool
	EnablePatternMatching   bool
	EnableReputationScoring bool
	EnableThreatSharing     bool

	// AWS Configuration
	ComprehendRegion  string
	RekognitionRegion string
	S3Bucket          string // Added S3 bucket for storing images

	// Cost controls
	MaxMonthlySpend    float64
	EnableCostTracking bool
}

ModerationConfig contains configuration for the moderation engine

func DefaultModerationConfig

func DefaultModerationConfig() *ModerationConfig

DefaultModerationConfig returns a default configuration that works without AWS

type ModerationDecision

type ModerationDecision struct {
	ContentID       string
	Decision        ModerationAction
	Confidence      float64
	Reasons         []DecisionReason
	RequiresReview  bool
	ReviewPriority  int // 1-10, higher is more urgent
	Recommendations []string
	ExpiresAt       time.Time // For temporary actions
	DecidedAt       time.Time
}

ModerationDecision represents the final moderation decision

type ModerationEngine

type ModerationEngine interface {
	// Content analysis
	AnalyzeContent(content string, metadata ContentMetadata) (*ContentAnalysis, error)
	AnalyzeImage(imageURL string, metadata ContentMetadata) (*ImageAnalysis, error)
	AnalyzeVideo(videoURL string, metadata ContentMetadata) (*VideoAnalysis, error)

	// Pattern management
	CreatePattern(pattern *ModerationPattern) error
	UpdatePattern(patternID string, pattern *ModerationPattern) error
	DeletePattern(patternID string) error
	GetPatterns(filter PatternFilter) ([]*ModerationPattern, error)

	// Reputation management
	GetReputationScore(actorID string) (*ReputationScore, error)
	UpdateReputation(actorID string, event ReputationEvent) error

	// Threat intelligence
	ShareThreat(threat *ThreatIntel) error
	GetSharedThreats(since time.Time) ([]*ThreatIntel, error)

	// Decision making
	MakeDecision(analysis *ModerationAnalysis) (*ModerationDecision, error)

	// Reporting
	GetModerationStats(timeRange TimeRange) (*ModerationStats, error)
	GetFalsePositiveRate(timeRange TimeRange) (float64, error)
}

ModerationEngine is the main interface for content moderation

type ModerationError

type ModerationError struct {
	Code    string
	Message string
	Details map[string]any
}

ModerationError represents an error in moderation operations

func (*ModerationError) Error

func (e *ModerationError) Error() string

Error returns the error message for ModerationError

type ModerationEvent

type ModerationEvent struct {
	Type       string
	Confidence float64
	Timestamp  time.Duration
}

ModerationEvent represents a moderation event in the video

type ModerationMetrics

type ModerationMetrics struct {
	// contains filtered or unexported fields
}

ModerationMetrics tracks moderation system performance using DynamORM

func NewModerationMetrics

func NewModerationMetrics(repo repositories.ModerationMetricsRepository, logger *zap.Logger) *ModerationMetrics

NewModerationMetrics creates a new metrics tracker

func (*ModerationMetrics) FlushMetrics

func (mm *ModerationMetrics) FlushMetrics(ctx context.Context) error

FlushMetrics flushes accumulated counters to persistent storage This replaces the background goroutine approach for Lambda compatibility

func (*ModerationMetrics) GetRealtimeStats

func (mm *ModerationMetrics) GetRealtimeStats() *RealtimeStats

GetRealtimeStats returns current real-time statistics

func (*ModerationMetrics) GetStats

func (mm *ModerationMetrics) GetStats(ctx context.Context, timeRange TimeRange) (*ModerationStats, error)

GetStats retrieves moderation statistics for a time range

func (*ModerationMetrics) GetTopPatterns

func (mm *ModerationMetrics) GetTopPatterns(ctx context.Context, limit int) ([]PatternStats, error)

GetTopPatterns returns the most frequently matched patterns

func (*ModerationMetrics) RecordAnalysis

func (mm *ModerationMetrics) RecordAnalysis(ctx context.Context, contentType string, processingTime time.Duration, decision *ModerationDecision)

RecordAnalysis records an analysis event

func (*ModerationMetrics) RecordFalsePositive

func (mm *ModerationMetrics) RecordFalsePositive(ctx context.Context, contentID string, originalDecision *ModerationDecision)

RecordFalsePositive records a false positive

func (*ModerationMetrics) RecordTruePositive

func (mm *ModerationMetrics) RecordTruePositive(_ context.Context, _ string, decision *ModerationDecision)

RecordTruePositive records a true positive (confirmed violation)

type ModerationMode

type ModerationMode string

ModerationMode defines the operation mode for the moderation engine

const (
	// ModeAWS uses AWS services for advanced analysis
	ModeAWS ModerationMode = "aws"

	// ModeBasic uses basic implementations without AWS
	ModeBasic ModerationMode = "basic"

	// ModeHybrid uses AWS when available, falls back to basic
	ModeHybrid ModerationMode = "hybrid"
)

type ModerationPattern

type ModerationPattern struct {
	ID          string
	Name        string
	Description string
	Pattern     string  // Regex or keyword pattern
	Type        string  // "regex", "keyword", "phrase"
	Category    string  // Primary category
	Severity    float64 // 0.0 to 1.0
	Action      ModerationAction
	Flags       []string // Additional flags or categories
	CreatedBy   string
	CreatedAt   time.Time
	UpdatedAt   time.Time
	Active      bool
	HitCount    int64
	LastHit     time.Time
}

ModerationPattern represents a pattern for content matching

type ModerationStats

type ModerationStats struct {
	TimeRange         TimeRange
	TotalAnalyzed     int64
	ActionCounts      map[ModerationAction]int64
	CategoryCounts    map[string]int64
	SeverityCounts    map[Severity]int64
	AverageConfidence float64
	FalsePositives    int64
	TruePositives     int64
	ResponseTime      time.Duration
}

ModerationStats contains moderation statistics

type NoOpImageAnalyzer

type NoOpImageAnalyzer struct {
	// contains filtered or unexported fields
}

NoOpImageAnalyzer provides no-op image analysis without AWS dependencies It returns neutral/safe results to avoid blocking content when AWS is disabled

func NewNoOpImageAnalyzer

func NewNoOpImageAnalyzer(logger *zap.Logger, config *ModerationConfig) *NoOpImageAnalyzer

NewNoOpImageAnalyzer creates a no-op image analyzer that doesn't require AWS

func (*NoOpImageAnalyzer) AnalyzeImage

func (n *NoOpImageAnalyzer) AnalyzeImage(_ context.Context, imageURL string, metadata ContentMetadata) (*ImageAnalysis, error)

AnalyzeImage performs no-op image analysis, returning neutral/safe results

type NoOpTextAnalyzer

type NoOpTextAnalyzer struct {
	// contains filtered or unexported fields
}

NoOpTextAnalyzer provides basic text analysis without AWS dependencies It returns neutral/safe results to avoid blocking content when AWS is disabled

func NewNoOpTextAnalyzer

func NewNoOpTextAnalyzer(logger *zap.Logger, config *ModerationConfig) *NoOpTextAnalyzer

NewNoOpTextAnalyzer creates a no-op text analyzer that doesn't require AWS

func (*NoOpTextAnalyzer) AnalyzeText

func (n *NoOpTextAnalyzer) AnalyzeText(_ context.Context, text string, metadata ContentMetadata) (*ContentAnalysis, error)

AnalyzeText performs no-op text analysis, returning neutral/safe results

type NoOpVideoAnalyzer

type NoOpVideoAnalyzer struct {
	// contains filtered or unexported fields
}

NoOpVideoAnalyzer provides no-op video analysis without AWS dependencies It returns neutral/safe results to avoid blocking content when AWS is disabled

func NewNoOpVideoAnalyzer

func NewNoOpVideoAnalyzer(logger *zap.Logger, config *ModerationConfig) *NoOpVideoAnalyzer

NewNoOpVideoAnalyzer creates a no-op video analyzer that doesn't require AWS

func (*NoOpVideoAnalyzer) AnalyzeVideo

func (n *NoOpVideoAnalyzer) AnalyzeVideo(_ context.Context, videoURL string, metadata ContentMetadata) (*VideoAnalysis, error)

AnalyzeVideo performs no-op video analysis, returning neutral/safe results

type ObjectDetection

type ObjectDetection struct {
	Name        string
	Confidence  float64
	BoundingBox BoundingBox
	Parents     []string
}

ObjectDetection represents an object detected in an image

type PIIEntity

type PIIEntity struct {
	Type       string // e.g., "EMAIL", "PHONE", "SSN", "CREDIT_CARD"
	Text       string
	BeginIndex int
	EndIndex   int
	Confidence float64
}

PIIEntity represents personally identifiable information

type PatternFilter

type PatternFilter struct {
	Category    string  // Single category filter
	Type        string  // Pattern type filter
	Active      *bool   // Active status filter
	MinSeverity float64 // Minimum severity filter
	Limit       int     // Result limit
	CreatedBy   string  // Filter by creator
}

PatternFilter for filtering patterns

type PatternMatch

type PatternMatch struct {
	PatternID   string
	PatternName string
	MatchText   string
	Location    string // Where in content
	Confidence  float64
}

PatternMatch represents a matched moderation pattern

type PatternMatcher

type PatternMatcher struct {
	// contains filtered or unexported fields
}

PatternMatcher handles pattern-based content matching

func NewPatternMatcher

func NewPatternMatcher(repository PatternRepository, logger *zap.Logger) *PatternMatcher

NewPatternMatcher creates a new pattern matcher Pattern updates should be triggered by DynamoDB stream events when patterns are modified

func (*PatternMatcher) CreatePattern

func (pm *PatternMatcher) CreatePattern(ctx context.Context, pattern *ModerationPattern) error

CreatePattern creates a new moderation pattern

func (*PatternMatcher) DeletePattern

func (pm *PatternMatcher) DeletePattern(ctx context.Context, patternID string) error

DeletePattern deletes a pattern (soft delete by marking inactive)

func (*PatternMatcher) GetPatterns

func (pm *PatternMatcher) GetPatterns(ctx context.Context, filter PatternFilter) ([]*ModerationPattern, error)

GetPatterns retrieves patterns based on filter

func (*PatternMatcher) MatchContent

func (pm *PatternMatcher) MatchContent(_ context.Context, content string, _ ContentMetadata) ([]PatternMatch, error)

MatchContent checks content against all active patterns

func (*PatternMatcher) RefreshPatterns

func (pm *PatternMatcher) RefreshPatterns(ctx context.Context) error

RefreshPatterns refreshes the pattern cache from DynamoDB This method should be called in response to DynamoDB stream events when patterns are modified. In a serverless architecture, the stream-router Lambda should process DynamoDB stream events and invoke this method when pattern records (PK=PATTERN#*) are created, updated, or deleted.

Event-driven pattern refresh ensures: - No wasted compute from polling - Immediate cache updates when patterns change - Compatibility with Lambda execution limits - Efficient resource utilization in serverless environments

func (*PatternMatcher) UpdatePattern

func (pm *PatternMatcher) UpdatePattern(ctx context.Context, patternID string, updates *ModerationPattern) error

UpdatePattern updates an existing pattern

type PatternRepository

type PatternRepository interface {
	CreatePattern(ctx context.Context, pattern *ModerationPattern) error
	UpdatePattern(ctx context.Context, patternID string, pattern *ModerationPattern) error
	DeletePattern(ctx context.Context, patternID string) error
	GetPattern(ctx context.Context, patternID string) (*ModerationPattern, error)
	GetPatterns(ctx context.Context, filter PatternFilter) ([]*ModerationPattern, error)
	IncrementHitCount(ctx context.Context, patternID string) error
	LoadActivePatterns(ctx context.Context) ([]*ModerationPattern, error)
}

PatternRepository defines the interface for pattern operations

func NewPatternRepositoryAdapter

func NewPatternRepositoryAdapter(repo *repositories.PatternRepository) PatternRepository

NewPatternRepositoryAdapter adapts repositories.PatternRepository to the advanced PatternRepository interface.

type PatternStats

type PatternStats struct {
	PatternID   string
	PatternName string
	HitCount    int64
	LastHit     time.Time
}

PatternStats represents pattern matching statistics

type RealtimeStats

type RealtimeStats struct {
	Uptime          time.Duration
	TotalAnalyzed   int64
	AnalysisRate    float64 // per second
	AllowRate       float64
	FlagRate        float64
	RemoveRate      float64
	QuarantineRate  float64
	AvgResponseTime time.Duration
	P95ResponseTime time.Duration
}

RealtimeStats represents current real-time statistics

type RekognitionCostTracker

type RekognitionCostTracker interface {
	TrackRekognitionRequest(operation string, imageCount int)
}

RekognitionCostTracker interface for tracking AWS Rekognition costs

type ReputationEvent

type ReputationEvent struct {
	EventType   string // "violation", "false_positive", "good_content", "user_report"
	Severity    Severity
	Description string
	Timestamp   time.Time
}

ReputationEvent represents an event that affects reputation

type ReputationFactor

type ReputationFactor struct {
	Factor      string  // e.g., "content_violations", "user_reports", "false_positives"
	Impact      float64 // Positive or negative impact on score
	Description string
}

ReputationFactor represents a factor affecting reputation

type ReputationHistoryItem

type ReputationHistoryItem struct {
	Timestamp   time.Time
	EventType   string
	Severity    Severity
	Description string
	Impact      float64
}

ReputationHistoryItem represents a reputation event in history.

type ReputationScore

type ReputationScore struct {
	ActorID            string
	Score              float64 // 0.0 to 100.0
	Level              string  // "trusted", "normal", "suspicious", "bad_actor"
	ViolationCount     int
	FalsePositiveCount int
	ContentCount       int
	LastViolation      time.Time
	Factors            []ReputationFactor
	UpdatedAt          time.Time
}

ReputationScore represents an actor's reputation

type ReputationScorer

type ReputationScorer struct {
	// contains filtered or unexported fields
}

ReputationScorer manages user reputation scoring.

It is intentionally TableTheory-backed: Lesser does not use direct DynamoDB SDK calls.

func NewReputationScorer

func NewReputationScorer(db core.DB, logger *zap.Logger, config *ModerationConfig) *ReputationScorer

NewReputationScorer creates a new reputation scorer.

func (*ReputationScorer) CalculateReputationImpact

func (rs *ReputationScorer) CalculateReputationImpact(decision *ModerationDecision) float64

CalculateReputationImpact calculates the reputation impact of a moderation decision.

func (*ReputationScorer) GetActorsByReputation

func (rs *ReputationScorer) GetActorsByReputation(ctx context.Context, minScore, maxScore float64, limit int) ([]*ReputationScore, error)

GetActorsByReputation retrieves actors within a reputation range.

func (*ReputationScorer) GetReputationHistory

func (rs *ReputationScorer) GetReputationHistory(ctx context.Context, actorID string, limit int) ([]ReputationHistoryItem, error)

GetReputationHistory retrieves reputation event history.

func (*ReputationScorer) GetReputationScore

func (rs *ReputationScorer) GetReputationScore(ctx context.Context, actorID string) (*ReputationScore, error)

GetReputationScore retrieves or calculates a user's reputation score.

func (*ReputationScorer) UpdateReputation

func (rs *ReputationScorer) UpdateReputation(ctx context.Context, actorID string, event ReputationEvent) error

UpdateReputation updates a user's reputation based on an event.

type SentimentAnalysis

type SentimentAnalysis struct {
	Sentiment  string  // POSITIVE, NEGATIVE, NEUTRAL, MIXED
	Positive   float64 // 0.0 to 1.0
	Negative   float64
	Neutral    float64
	Mixed      float64
	Confidence float64
}

SentimentAnalysis contains sentiment analysis results

type Severity

type Severity string

Severity represents the severity level of a moderation issue

const (
	// SeverityLow represents low severity issues
	SeverityLow Severity = "low"
	// SeverityMedium represents medium severity issues
	SeverityMedium Severity = "medium"
	// SeverityHigh represents high severity issues
	SeverityHigh Severity = "high"
	// SeverityCritical represents critical severity issues
	SeverityCritical Severity = "critical"
)

type Signal

type Signal struct {
	Type       string
	Severity   Severity
	Score      float64
	Confidence float64
	Evidence   any
}

Signal represents a moderation signal

type TextAnalyzer

type TextAnalyzer struct {
	// contains filtered or unexported fields
}

TextAnalyzer handles text content analysis using AWS Comprehend

func NewTextAnalyzer

func NewTextAnalyzer(client *comprehend.Client, logger *zap.Logger, config *ModerationConfig, costTracker CostTracker) *TextAnalyzer

NewTextAnalyzer creates a new text analyzer

func (*TextAnalyzer) AnalyzeText

func (ta *TextAnalyzer) AnalyzeText(ctx context.Context, text string, metadata ContentMetadata) (*ContentAnalysis, error)

AnalyzeText performs comprehensive text analysis

type TextAnalyzerInterface

type TextAnalyzerInterface interface {
	AnalyzeText(ctx context.Context, text string, metadata ContentMetadata) (*ContentAnalysis, error)
}

TextAnalyzerInterface defines the interface for text analysis

type TextInImage

type TextInImage struct {
	Text        string
	Confidence  float64
	BoundingBox BoundingBox
}

TextInImage represents text detected in an image

type ThreatIndicator

type ThreatIndicator struct {
	Type        string // e.g., "VIOLENCE", "SELF_HARM", "TERRORISM"
	Severity    Severity
	Confidence  float64
	Evidence    []string
	ActionItems []string
}

ThreatIndicator represents a potential threat

type ThreatIntel

type ThreatIntel struct {
	ID           string
	ThreatType   string
	Indicators   []string // Hashes, patterns, domains, etc.
	Severity     Severity
	Description  string
	SourceDomain string
	FirstSeen    time.Time
	LastSeen     time.Time
	HitCount     int64
	Confidence   float64
	TTL          time.Duration
}

ThreatIntel represents shared threat intelligence

type ThreatIntelRepository

type ThreatIntelRepository interface {
	ShareThreat(ctx context.Context, threat *repositories.ThreatIntel) error
	GetSharedThreats(ctx context.Context, since time.Time) ([]*repositories.ThreatIntel, error)
	GetThreatsByType(ctx context.Context, threatType string, limit int) ([]*repositories.ThreatIntel, error)
	UpdateThreatConfidence(ctx context.Context, threatID string, newConfidence float64) error
	IncrementHitCount(ctx context.Context, threatID string) error
	LoadActiveThreats(ctx context.Context) ([]*repositories.ThreatIntel, error)
	GetThreatByID(ctx context.Context, threatID string) (*repositories.ThreatIntel, error)
	GetIndicatorThreat(ctx context.Context, indicator string) (string, error)
}

ThreatIntelRepository interface for dependency injection

type ThreatIntelligence

type ThreatIntelligence struct {
	// contains filtered or unexported fields
}

ThreatIntelligence manages cross-instance threat sharing

func NewThreatIntelligence

func NewThreatIntelligence(repo ThreatIntelRepository, logger *zap.Logger) *ThreatIntelligence

NewThreatIntelligence creates a new threat intelligence component

func (*ThreatIntelligence) CheckContent

func (ti *ThreatIntelligence) CheckContent(_ context.Context, content string, metadata ContentMetadata) ([]ThreatMatch, error)

CheckContent checks content against known threats

func (*ThreatIntelligence) GetSharedThreats

func (ti *ThreatIntelligence) GetSharedThreats(ctx context.Context, since time.Time) ([]*ThreatIntel, error)

GetSharedThreats retrieves threats shared since a given time

func (*ThreatIntelligence) GetThreatsByType

func (ti *ThreatIntelligence) GetThreatsByType(ctx context.Context, threatType string, limit int) ([]*ThreatIntel, error)

GetThreatsByType retrieves threats of a specific type

func (*ThreatIntelligence) RefreshThreats

func (ti *ThreatIntelligence) RefreshThreats(ctx context.Context) error

RefreshThreats loads the latest threats from the database into the cache. This method should be called on-demand, typically triggered by EventBridge scheduled events in a serverless environment.

Example usage in a Lambda function:

func handler(ctx context.Context, event events.CloudWatchEvent) error {
    return threatIntel.RefreshThreats(ctx)
}

func (*ThreatIntelligence) ShareThreat

func (ti *ThreatIntelligence) ShareThreat(ctx context.Context, threat *ThreatIntel) error

ShareThreat shares a new threat with the network

func (*ThreatIntelligence) UpdateThreatConfidence

func (ti *ThreatIntelligence) UpdateThreatConfidence(ctx context.Context, threatID string, newConfidence float64) error

UpdateThreatConfidence updates the confidence score of a threat

type ThreatMatch

type ThreatMatch struct {
	ThreatID   string
	ThreatType string
	Indicator  string
	Confidence float64
}

ThreatMatch represents a matched threat

type TimeRange

type TimeRange struct {
	Start time.Time
	End   time.Time
}

TimeRange represents a time range for queries

type Topic

type Topic struct {
	Name     string
	Score    float64
	Category string
}

Topic represents a detected topic

type ToxicCategory

type ToxicCategory struct {
	Category   string // e.g., "PROFANITY", "HATE_SPEECH", "THREAT"
	Score      float64
	Confidence float64
}

ToxicCategory represents a category of toxic content

type ToxicityAnalysis

type ToxicityAnalysis struct {
	IsToxic        bool
	ToxicityScore  float64 // 0.0 to 1.0
	Categories     []ToxicCategory
	TargetedGroups []string
	Confidence     float64
}

ToxicityAnalysis contains toxicity detection results

type TranscriptionResult

type TranscriptionResult struct {
	Transcription string
	Language      string
	Confidence    float64
	JobName       string
	Duration      time.Duration
}

TranscriptionResult contains the result of audio transcription

type TranscriptionService

type TranscriptionService struct {
	// contains filtered or unexported fields
}

TranscriptionService handles audio transcription using AWS Transcribe

func NewTranscriptionService

func NewTranscriptionService(
	transcribeClient *transcribe.Client,
	s3Client *s3.Client,
	logger *zap.Logger,
	outputBucket string,
	costTracker CostTracker,
) *TranscriptionService

NewTranscriptionService creates a new transcription service

func (*TranscriptionService) TranscribeAudio

func (ts *TranscriptionService) TranscribeAudio(ctx context.Context, s3URI string) (*TranscriptionResult, error)

TranscribeAudio transcribes audio from S3 URI and returns the transcription

type VideoAnalysis

type VideoAnalysis struct {
	VideoURL       string
	Frames         []FrameAnalysis
	Audio          AudioAnalysis
	Duration       time.Duration
	AnalyzedAt     time.Time
	ProcessingTime time.Duration
}

VideoAnalysis represents the result of video analysis

type VideoAnalyzer

type VideoAnalyzer struct {
	// contains filtered or unexported fields
}

VideoAnalyzer handles video content analysis using AWS Rekognition Video

func NewVideoAnalyzer

func NewVideoAnalyzer(client *rekognition.Client, logger *zap.Logger, config *ModerationConfig, costTracker CostTracker) *VideoAnalyzer

NewVideoAnalyzer creates a new video analyzer

func (*VideoAnalyzer) AnalyzeVideo

func (va *VideoAnalyzer) AnalyzeVideo(ctx context.Context, videoURL string, _ ContentMetadata) (*VideoAnalysis, error)

AnalyzeVideo performs comprehensive video analysis with frame sampling and audio processing

type VideoAnalyzerInterface

type VideoAnalyzerInterface interface {
	AnalyzeVideo(ctx context.Context, videoURL string, metadata ContentMetadata) (*VideoAnalysis, error)
}

VideoAnalyzerInterface defines the interface for video analysis

type ViolenceDetection

type ViolenceDetection struct {
	HasViolence     bool
	WeaponsDetected []string
	BloodScore      float64
	ViolenceScore   float64
	Confidence      float64
}

ViolenceDetection represents violence detection results

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL