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