an app.bsky.* indexer
1package models
2
3import (
4 "bytes"
5 "log/slog"
6 "time"
7
8 appbsky "github.com/bluesky-social/indigo/api/bsky"
9 "github.com/bluesky-social/indigo/atproto/syntax"
10)
11
12type FeedPost struct {
13 ID string `gorm:"primaryKey"`
14
15 // TODO: Embed, Facets
16
17 CreatedAt string
18 Labels []FeedPost_Label
19 Langs []FeedPost_Lang
20 Reply *FeedPost_Reply `gorm:"embedded"`
21 Tags []FeedPost_Tag
22 Text string
23
24 AutoCreatedAt time.Time `gorm:"autoCreateTime"`
25 AutoUpdatedAt time.Time `gorm:"autoUpdateTime"`
26}
27
28type FeedPost_Label struct {
29 FeedPostID string
30 Value string
31}
32
33type FeedPost_Lang struct {
34 FeedPostID string
35 Value string
36}
37
38type FeedPost_Reply struct {
39 Root *StrongRef `gorm:"embedded;embeddedPrefix:reply_root_"`
40 Parent *StrongRef `gorm:"embedded;embeddedPrefix:reply_parent_"`
41}
42
43type FeedPost_Tag struct {
44 FeedPostID string
45 Value string
46}
47
48func NewFeedPost(uri syntax.ATURI, rec []byte) *FeedPost {
49 var out appbsky.FeedPost
50 if err := out.UnmarshalCBOR(bytes.NewReader(rec)); err != nil {
51 slog.Error("could not unmarshal feed post CBOR", "err", err)
52 return nil
53 }
54
55 post := FeedPost{
56 ID: string(uri),
57 CreatedAt: out.CreatedAt,
58 Text: out.Text,
59 }
60
61 if out.Labels != nil && out.Labels.LabelDefs_SelfLabels != nil && out.Labels.LabelDefs_SelfLabels.Values != nil {
62 for _, label := range out.Labels.LabelDefs_SelfLabels.Values {
63 post.Labels = append(post.Labels, FeedPost_Label{
64 FeedPostID: post.ID,
65 Value: label.Val,
66 })
67 }
68 }
69
70 for _, lang := range out.Langs {
71 post.Langs = append(post.Langs, FeedPost_Lang{
72 FeedPostID: post.ID,
73 Value: lang,
74 })
75 }
76
77 if out.Reply != nil {
78 post.Reply = &FeedPost_Reply{
79 Parent: &StrongRef{
80 Uri: out.Reply.Parent.Uri,
81 Cid: out.Reply.Parent.Cid,
82 },
83 Root: &StrongRef{
84 Uri: out.Reply.Root.Uri,
85 Cid: out.Reply.Root.Cid,
86 },
87 }
88 }
89
90 for _, tag := range out.Tags {
91 post.Tags = append(post.Tags, FeedPost_Tag{
92 FeedPostID: post.ID,
93 Value: tag,
94 })
95 }
96
97 return &post
98}