an app.bsky.* indexer
at master 3.2 kB view raw
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 LabelerService struct { 13 ID string `gorm:"primaryKey"` 14 15 CreatedAt string 16 Labels []LabelerService_Label 17 Policies []LabelerService_Policy 18 ReasonTypes []LabelerService_ReasonType 19 SubjectCollections []LabelerService_SubjectCollection 20 SubjctTypes []LabelerService_SubjectType 21 22 AutoCreatedAt time.Time `gorm:"autoCreateTime"` 23 AutoUpdatedAt time.Time `gorm:"autoUpdateTime"` 24} 25 26type LabelerService_Label struct { 27 LabelerServiceID string 28 Value string 29} 30 31type LabelerService_Policy struct { 32 LabelerServiceID string 33 Definition LabelerService_PolicyDefinition `gorm:"embedded"` 34 Label *string 35} 36 37type LabelerService_PolicyDefinition struct { 38 AdultOnly *bool 39 Blurs string 40 DefaultSetting *string 41 Identifier string 42 Severity string 43} 44 45type LabelerService_ReasonType struct { 46 LabelerServiceID string 47 Value *string 48} 49 50type LabelerService_SubjectCollection struct { 51 LabelerServiceID string 52 Value string 53} 54 55type LabelerService_SubjectType struct { 56 LabelerServiceID string 57 Value *string 58} 59 60func NewLabelerService(uri syntax.ATURI, rec []byte) *LabelerService { 61 var out appbsky.LabelerService 62 if err := out.UnmarshalCBOR(bytes.NewReader(rec)); err != nil { 63 slog.Error("could not unmarshal labeler service CBOR", "err", err) 64 return nil 65 } 66 67 labeler := LabelerService{ 68 ID: string(uri), 69 CreatedAt: out.CreatedAt, 70 } 71 72 if out.ReasonTypes != nil { 73 for _, reasontype := range out.ReasonTypes { 74 labeler.ReasonTypes = append(labeler.ReasonTypes, LabelerService_ReasonType{ 75 LabelerServiceID: labeler.ID, 76 Value: reasontype, 77 }) 78 } 79 } 80 81 for _, collection := range out.SubjectCollections { 82 labeler.SubjectCollections = append(labeler.SubjectCollections, LabelerService_SubjectCollection{ 83 LabelerServiceID: labeler.ID, 84 Value: collection, 85 }) 86 } 87 88 if out.SubjectTypes != nil { 89 for _, stype := range out.SubjectTypes { 90 labeler.SubjctTypes = append(labeler.SubjctTypes, LabelerService_SubjectType{ 91 LabelerServiceID: labeler.ID, 92 Value: stype, 93 }) 94 } 95 } 96 97 if out.Labels != nil && out.Labels.LabelDefs_SelfLabels != nil && out.Labels.LabelDefs_SelfLabels.Values != nil { 98 for _, label := range out.Labels.LabelDefs_SelfLabels.Values { 99 labeler.Labels = append(labeler.Labels, LabelerService_Label{ 100 Value: label.Val, 101 }) 102 } 103 } 104 105 if out.Policies != nil && out.Policies.LabelValueDefinitions != nil && out.Policies.LabelValues != nil { 106 for idx, valdef := range out.Policies.LabelValueDefinitions { 107 policy := LabelerService_Policy{ 108 LabelerServiceID: labeler.ID, 109 Definition: LabelerService_PolicyDefinition{ 110 AdultOnly: valdef.AdultOnly, 111 Blurs: valdef.Blurs, 112 DefaultSetting: valdef.DefaultSetting, 113 Identifier: valdef.Identifier, 114 Severity: valdef.Severity, 115 }, 116 Label: out.Policies.LabelValues[idx], 117 } 118 labeler.Policies = append(labeler.Policies, policy) 119 } 120 } 121 122 return &labeler 123}