Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139package models
import ( "fmt" "strings" "time"
"github.com/bluesky-social/indigo/atproto/syntax" "github.com/whyrusleeping/cbor-gen" "tangled.org/core/api/tangled")
type Comment struct { Id int64 Did syntax.DID Collection syntax.NSID Rkey string Subject syntax.ATURI ReplyTo *syntax.ATURI Body string Created time.Time Edited *time.Time Deleted *time.Time Mentions []syntax.DID References []syntax.ATURI PullSubmissionId *int}
func (c *Comment) AtUri() syntax.ATURI { return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", c.Did, c.Collection, c.Rkey))}
func (c *Comment) AsRecord() typegen.CBORMarshaler { mentions := make([]string, len(c.Mentions)) for i, did := range c.Mentions { mentions[i] = string(did) } references := make([]string, len(c.References)) for i, uri := range c.References { references[i] = string(uri) } var replyTo *string if c.ReplyTo != nil { replyToStr := c.ReplyTo.String() replyTo = &replyToStr } switch c.Collection { case tangled.RepoIssueCommentNSID: return &tangled.RepoIssueComment{ Issue: c.Subject.String(), Body: c.Body, CreatedAt: c.Created.Format(time.RFC3339), ReplyTo: replyTo, Mentions: mentions, References: references, } case tangled.RepoPullCommentNSID: return &tangled.RepoPullComment{ Pull: c.Subject.String(), Body: c.Body, CreatedAt: c.Created.Format(time.RFC3339), Mentions: mentions, References: references, } default: // default to CommentNSID return &tangled.Comment{ Subject: c.Subject.String(), Body: c.Body, CreatedAt: c.Created.Format(time.RFC3339), ReplyTo: replyTo, Mentions: mentions, References: references, } }}
func (c *Comment) IsTopLevel() bool { return c.ReplyTo == nil}
func (c *Comment) IsReply() bool { return c.ReplyTo != nil}
func (c *Comment) Validate() error { // TODO: sanitize the body and then trim space if sb := strings.TrimSpace(c.Body); sb == "" { return fmt.Errorf("body is empty after HTML sanitization") }
// if it's for PR, PullSubmissionId should not be nil if c.Subject.Collection().String() == tangled.RepoPullNSID { if c.PullSubmissionId == nil { return fmt.Errorf("PullSubmissionId should not be nil") } } return nil}
func CommentFromRecord(did syntax.DID, rkey syntax.RecordKey, record tangled.Comment) (*Comment, error) { created, err := time.Parse(time.RFC3339, record.CreatedAt) if err != nil { created = time.Now() }
if _, err = syntax.ParseATURI(record.Subject); err != nil { return nil, err }
i := record mentions := make([]syntax.DID, len(record.Mentions)) for i, did := range record.Mentions { mentions[i] = syntax.DID(did) } references := make([]syntax.ATURI, len(record.References)) for i, uri := range i.References { references[i] = syntax.ATURI(uri) } var replyTo *syntax.ATURI if record.ReplyTo != nil { replyToAtUri := syntax.ATURI(*record.ReplyTo) replyTo = &replyToAtUri }
comment := Comment{ Did: did, Collection: tangled.CommentNSID, Rkey: rkey.String(), Body: record.Body, Subject: syntax.ATURI(record.Subject), ReplyTo: replyTo, Created: created, Mentions: mentions, References: references, }
return &comment, nil}