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 FeedThreadgate struct {
13 ID string `gorm:"primaryKey"`
14
15 AllowRules []FeedThreadgate_AllowRule
16 CreatedAt string
17 HiddenReplies []FeedThreadgate_HiddenReply
18 Post string
19
20 AutoCreatedAt time.Time `gorm:"autoCreateTime"`
21 AutoUpdatedAt time.Time `gorm:"autoUpdateTime"`
22}
23
24type FeedThreadgate_AllowRule struct {
25 FeedThreadgateID string
26 Rule string
27}
28
29type FeedThreadgate_HiddenReply struct {
30 FeedThreadgateID string
31 Uri string
32}
33
34func NewFeedThreadgate(uri syntax.ATURI, rec []byte) *FeedThreadgate {
35 var out appbsky.FeedThreadgate
36 if err := out.UnmarshalCBOR(bytes.NewReader(rec)); err != nil {
37 slog.Error("could not unmarshal feed threadgate CBOR", "err", err)
38 return nil
39 }
40
41 threadgate := FeedThreadgate{
42 ID: string(uri),
43 CreatedAt: out.CreatedAt,
44 Post: out.Post,
45 }
46
47 for _, hidden := range out.HiddenReplies {
48 threadgate.HiddenReplies = append(threadgate.HiddenReplies, FeedThreadgate_HiddenReply{
49 FeedThreadgateID: threadgate.ID,
50 Uri: hidden,
51 })
52 }
53
54 if out.Allow != nil {
55 for _, rule := range out.Allow {
56 if rule.FeedThreadgate_MentionRule != nil {
57 threadgate.AllowRules = append(threadgate.AllowRules, FeedThreadgate_AllowRule{
58 FeedThreadgateID: threadgate.ID,
59 Rule: rule.FeedThreadgate_MentionRule.LexiconTypeID,
60 })
61 }
62
63 if rule.FeedThreadgate_FollowerRule != nil {
64 threadgate.AllowRules = append(threadgate.AllowRules, FeedThreadgate_AllowRule{
65 FeedThreadgateID: threadgate.ID,
66 Rule: rule.FeedThreadgate_FollowerRule.LexiconTypeID,
67 })
68 }
69
70 if rule.FeedThreadgate_FollowingRule != nil {
71 threadgate.AllowRules = append(threadgate.AllowRules, FeedThreadgate_AllowRule{
72 FeedThreadgateID: threadgate.ID,
73 Rule: rule.FeedThreadgate_FollowingRule.LexiconTypeID,
74 })
75 }
76
77 if rule.FeedThreadgate_ListRule != nil {
78 threadgate.AllowRules = append(threadgate.AllowRules, FeedThreadgate_AllowRule{
79 FeedThreadgateID: threadgate.ID,
80 Rule: rule.FeedThreadgate_ListRule.LexiconTypeID,
81 })
82 }
83 }
84 }
85
86 return &threadgate
87}