package models import ( "bytes" "log/slog" "time" appbsky "github.com/bluesky-social/indigo/api/bsky" "github.com/bluesky-social/indigo/atproto/syntax" ) type FeedPost struct { ID string `gorm:"primaryKey"` // TODO: Embed, Facets CreatedAt string Labels []FeedPost_Label Langs []FeedPost_Lang Reply *FeedPost_Reply `gorm:"embedded"` Tags []FeedPost_Tag Text string AutoCreatedAt time.Time `gorm:"autoCreateTime"` AutoUpdatedAt time.Time `gorm:"autoUpdateTime"` } type FeedPost_Label struct { FeedPostID string Value string } type FeedPost_Lang struct { FeedPostID string Value string } type FeedPost_Reply struct { Root *StrongRef `gorm:"embedded;embeddedPrefix:reply_root_"` Parent *StrongRef `gorm:"embedded;embeddedPrefix:reply_parent_"` } type FeedPost_Tag struct { FeedPostID string Value string } func NewFeedPost(uri syntax.ATURI, rec []byte) *FeedPost { var out appbsky.FeedPost if err := out.UnmarshalCBOR(bytes.NewReader(rec)); err != nil { slog.Error("could not unmarshal feed post CBOR", "err", err) return nil } post := FeedPost{ ID: string(uri), CreatedAt: out.CreatedAt, Text: out.Text, } if out.Labels != nil && out.Labels.LabelDefs_SelfLabels != nil && out.Labels.LabelDefs_SelfLabels.Values != nil { for _, label := range out.Labels.LabelDefs_SelfLabels.Values { post.Labels = append(post.Labels, FeedPost_Label{ FeedPostID: post.ID, Value: label.Val, }) } } for _, lang := range out.Langs { post.Langs = append(post.Langs, FeedPost_Lang{ FeedPostID: post.ID, Value: lang, }) } if out.Reply != nil { post.Reply = &FeedPost_Reply{ Parent: &StrongRef{ Uri: out.Reply.Parent.Uri, Cid: out.Reply.Parent.Cid, }, Root: &StrongRef{ Uri: out.Reply.Root.Uri, Cid: out.Reply.Root.Cid, }, } } for _, tag := range out.Tags { post.Tags = append(post.Tags, FeedPost_Tag{ FeedPostID: post.ID, Value: tag, }) } return &post }