1package validator
2
3import (
4 "fmt"
5 "strings"
6
7 "tangled.sh/tangled.sh/core/appview/db"
8)
9
10func (v *Validator) ValidateIssueComment(comment *db.IssueComment) error {
11 // if comments have parents, only ingest ones that are 1 level deep
12 if comment.ReplyTo != nil {
13 parents, err := db.GetIssueComments(v.db, db.FilterEq("at_uri", *comment.ReplyTo))
14 if err != nil {
15 return fmt.Errorf("failed to fetch parent comment: %w", err)
16 }
17 if len(parents) != 1 {
18 return fmt.Errorf("incorrect number of parent comments returned: %d", len(parents))
19 }
20
21 // depth check
22 parent := parents[0]
23 if parent.ReplyTo != nil {
24 return fmt.Errorf("incorrect depth, this comment is replying at depth >1")
25 }
26 }
27
28 if sb := strings.TrimSpace(v.sanitizer.SanitizeDefault(comment.Body)); sb == "" {
29 return fmt.Errorf("body is empty after HTML sanitization")
30 }
31
32 return nil
33}
34
35func (v *Validator) ValidateIssue(issue *db.Issue) error {
36 if issue.Title == "" {
37 return fmt.Errorf("issue title is empty")
38 }
39
40 if issue.Body == "" {
41 return fmt.Errorf("issue body is empty")
42 }
43
44 if st := strings.TrimSpace(v.sanitizer.SanitizeDescription(issue.Title)); st == "" {
45 return fmt.Errorf("title is empty after HTML sanitization")
46 }
47
48 if sb := strings.TrimSpace(v.sanitizer.SanitizeDefault(issue.Body)); sb == "" {
49 return fmt.Errorf("body is empty after HTML sanitization")
50 }
51
52 return nil
53}