this repo has no description
1package models 2 3import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/bluesky-social/indigo/atproto/syntax" 9 "tangled.org/core/api/tangled" 10) 11 12type Comment struct { 13 Id int64 14 Did syntax.DID 15 Rkey string 16 Subject syntax.ATURI 17 ReplyTo *syntax.ATURI 18 Body string 19 Created time.Time 20 Edited *time.Time 21 Deleted *time.Time 22 Mentions []syntax.DID 23 References []syntax.ATURI 24 PullSubmissionId *int 25} 26 27func (c *Comment) AtUri() syntax.ATURI { 28 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", c.Did, tangled.CommentNSID, c.Rkey)) 29} 30 31func (c *Comment) AsRecord() tangled.Comment { 32 mentions := make([]string, len(c.Mentions)) 33 for i, did := range c.Mentions { 34 mentions[i] = string(did) 35 } 36 references := make([]string, len(c.References)) 37 for i, uri := range c.References { 38 references[i] = string(uri) 39 } 40 var replyTo *string 41 if c.ReplyTo != nil { 42 replyToStr := c.ReplyTo.String() 43 replyTo = &replyToStr 44 } 45 return tangled.Comment{ 46 Subject: c.Subject.String(), 47 Body: c.Body, 48 CreatedAt: c.Created.Format(time.RFC3339), 49 ReplyTo: replyTo, 50 Mentions: mentions, 51 References: references, 52 } 53} 54 55func (c *Comment) IsTopLevel() bool { 56 return c.ReplyTo == nil 57} 58 59func (c *Comment) IsReply() bool { 60 return c.ReplyTo != nil 61} 62 63func (c *Comment) Validate() error { 64 // TODO: sanitize the body and then trim space 65 if sb := strings.TrimSpace(c.Body); sb == "" { 66 return fmt.Errorf("body is empty after HTML sanitization") 67 } 68 69 // if it's for PR, PullSubmissionId should not be nil 70 if c.Subject.Collection().String() == tangled.RepoPullNSID { 71 if c.PullSubmissionId == nil { 72 return fmt.Errorf("PullSubmissionId should not be nil") 73 } 74 } 75 return nil 76} 77 78func CommentFromRecord(did, rkey string, record tangled.Comment) (*Comment, error) { 79 created, err := time.Parse(time.RFC3339, record.CreatedAt) 80 if err != nil { 81 created = time.Now() 82 } 83 84 ownerDid := did 85 86 if _, err = syntax.ParseATURI(record.Subject); err != nil { 87 return nil, err 88 } 89 90 i := record 91 mentions := make([]syntax.DID, len(record.Mentions)) 92 for i, did := range record.Mentions { 93 mentions[i] = syntax.DID(did) 94 } 95 references := make([]syntax.ATURI, len(record.References)) 96 for i, uri := range i.References { 97 references[i] = syntax.ATURI(uri) 98 } 99 var replyTo *syntax.ATURI 100 if record.ReplyTo != nil { 101 replyToAtUri := syntax.ATURI(*record.ReplyTo) 102 replyTo = &replyToAtUri 103 } 104 105 comment := Comment{ 106 Did: syntax.DID(ownerDid), 107 Rkey: rkey, 108 Body: record.Body, 109 Subject: syntax.ATURI(record.Subject), 110 ReplyTo: replyTo, 111 Created: created, 112 Mentions: mentions, 113 References: references, 114 } 115 116 return &comment, nil 117}