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
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 FeedPostID string
40 Parent StrongRef `gorm:"embedded;embeddedPrefix:parent_"`
41 Root StrongRef `gorm:"embedded;embeddedPrefix:root_"`
42}
43
44type FeedPost_Tag struct {
45 FeedPostID string
46 Value string
47}
48
49func NewFeedPost(uri syntax.ATURI, rec []byte) *FeedPost {
50 var out appbsky.FeedPost
51 if err := out.UnmarshalCBOR(bytes.NewReader(rec)); err != nil {
52 slog.Error("could not unmarshal feed post CBOR", "err", err)
53 return nil
54 }
55
56 post := FeedPost{
57 ID: string(uri),
58 CreatedAt: out.CreatedAt,
59 Text: out.Text,
60 }
61
62 if out.Labels != nil && out.Labels.LabelDefs_SelfLabels != nil && out.Labels.LabelDefs_SelfLabels.Values != nil {
63 for _, label := range out.Labels.LabelDefs_SelfLabels.Values {
64 post.Labels = append(post.Labels, FeedPost_Label{
65 FeedPostID: post.ID,
66 Value: label.Val,
67 })
68 }
69 }
70
71 for _, lang := range out.Langs {
72 post.Langs = append(post.Langs, FeedPost_Lang{
73 FeedPostID: post.ID,
74 Value: lang,
75 })
76 }
77
78 if out.Reply != nil {
79 post.Reply = FeedPost_Reply{
80 FeedPostID: post.ID,
81 Parent: StrongRef{
82 Uri: out.Reply.Parent.Uri,
83 Cid: out.Reply.Parent.Cid,
84 },
85 Root: StrongRef{
86 Uri: out.Reply.Root.Uri,
87 Cid: out.Reply.Root.Cid,
88 },
89 }
90 }
91
92 for _, tag := range out.Tags {
93 post.Tags = append(post.Tags, FeedPost_Tag{
94 FeedPostID: post.ID,
95 Value: tag,
96 })
97 }
98
99 return &post
100}