feat: enforce tenant content moderation
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package moderation
|
||||
|
||||
import "strings"
|
||||
|
||||
var visibleTextKeys = map[string]bool{
|
||||
"txt": true, "text": true, "title": true, "caption": true,
|
||||
"description": true, "file_name": true, "filename": true, "name": true,
|
||||
}
|
||||
|
||||
func ExtractText(content any) string {
|
||||
parts := make([]string, 0, 4)
|
||||
var walk func(any, int, bool)
|
||||
walk = func(value any, depth int, includeString bool) {
|
||||
if depth > 6 || value == nil {
|
||||
return
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case string:
|
||||
if text := strings.TrimSpace(typed); includeString && text != "" {
|
||||
parts = append(parts, text)
|
||||
}
|
||||
case map[string]any:
|
||||
for key, item := range typed {
|
||||
walk(item, depth+1, visibleTextKeys[strings.ToLower(strings.TrimSpace(key))])
|
||||
}
|
||||
case []any:
|
||||
for _, item := range typed {
|
||||
walk(item, depth+1, includeString)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(content, 0, true)
|
||||
return strings.TrimSpace(strings.Join(parts, "\n"))
|
||||
}
|
||||
Reference in New Issue
Block a user