File size: 2,698 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package contentsafety

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"sync"
)

type AuditEntry struct {
	ContentType string
	Word        string
	Text        string
}

type SubmittedEntry struct {
	ContentType string
	Text        string
}

type Filter struct {
	mu       sync.Mutex
	words    []string
	audit    []AuditEntry
	accepted []SubmittedEntry
}

func NewFilter(words []string) *Filter {
	f := &Filter{}
	f.SetWords(words)
	return f
}

func (f *Filter) SetWords(words []string) {
	clean := make([]string, 0, len(words))
	seen := make(map[string]bool)
	for _, word := range words {
		word = strings.TrimSpace(word)
		key := strings.ToLower(word)
		if word != "" && !seen[key] {
			clean = append(clean, word)
			seen[key] = true
		}
	}
	f.mu.Lock()
	defer f.mu.Unlock()
	f.words = clean
}

func LoadWordsFile(path string) ([]string, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	scanner := bufio.NewScanner(file)
	words := []string{}
	for scanner.Scan() {
		line := strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "\ufeff"))
		if line != "" && !strings.HasPrefix(line, "#") {
			words = append(words, line)
		}
	}
	if err := scanner.Err(); err != nil {
		return nil, err
	}
	return words, nil
}

func NewFilterFromFile(path string) (*Filter, error) {
	words, err := LoadWordsFile(path)
	if err != nil {
		return nil, err
	}
	return NewFilter(words), nil
}

func validType(contentType string) bool {
	return contentType == "question" || contentType == "answer" || contentType == "comment"
}

func (f *Filter) validateLocked(contentType string, text string) error {
	if !validType(contentType) {
		return fmt.Errorf("unsupported content type: %s", contentType)
	}
	lower := strings.ToLower(text)
	for _, word := range f.words {
		if strings.Contains(lower, strings.ToLower(word)) {
			f.audit = append(f.audit, AuditEntry{ContentType: contentType, Word: word, Text: text})
			return fmt.Errorf("sensitive content blocked: %s", word)
		}
	}
	return nil
}

func (f *Filter) Validate(contentType string, text string) error {
	f.mu.Lock()
	defer f.mu.Unlock()
	return f.validateLocked(contentType, text)
}

func (f *Filter) Submit(contentType string, text string) error {
	f.mu.Lock()
	defer f.mu.Unlock()
	if err := f.validateLocked(contentType, text); err != nil {
		return err
	}
	f.accepted = append(f.accepted, SubmittedEntry{ContentType: contentType, Text: text})
	return nil
}

func (f *Filter) AuditLog() []AuditEntry {
	f.mu.Lock()
	defer f.mu.Unlock()
	return append([]AuditEntry(nil), f.audit...)
}

func (f *Filter) Accepted() []SubmittedEntry {
	f.mu.Lock()
	defer f.mu.Unlock()
	return append([]SubmittedEntry(nil), f.accepted...)
}