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