Live video on the AT Protocol
1package model
2
3import (
4 "bytes"
5 "time"
6
7 comatproto "github.com/bluesky-social/indigo/api/atproto"
8 "gorm.io/gorm/clause"
9)
10
11type Label struct {
12 // cid: Optionally, CID specifying the specific version of 'uri' resource this label applies to.
13 Cid *string `json:"cid,omitempty" cborgen:"cid,omitempty" gorm:"column:cid"`
14 // cts: Timestamp when this label was created.
15 Cts time.Time `json:"cts" cborgen:"cts" gorm:"column:cts"`
16 // exp: Timestamp at which this label expires (no longer applies).
17 Exp time.Time `json:"exp,omitempty" cborgen:"exp,omitempty" gorm:"column:exp"`
18 // neg: If true, this is a negation label, overwriting a previous label.
19 Neg bool `json:"neg,omitempty" cborgen:"neg,omitempty" gorm:"column:neg"`
20 // sig: Signature of dag-cbor encoded label.
21 Sig []byte `json:"sig,omitempty" cborgen:"sig,omitempty" gorm:"column:sig"`
22 // src: DID of the actor who created this label.
23 Src string `json:"src" cborgen:"src" gorm:"primaryKey;column:src"`
24 // uri: AT URI of the record, repository (account), or other resource that this label applies to.
25 Uri string `json:"uri" cborgen:"uri" gorm:"primaryKey;column:uri;index"`
26 // val: The short string name of the value or type of this label.
27 Val string `json:"val" cborgen:"val" gorm:"primaryKey;column:val"`
28 // ver: The AT Protocol version of the label object.
29 Ver *int64 `json:"ver,omitempty" cborgen:"ver,omitempty" gorm:"column:ver"`
30
31 Record []byte `json:"record,omitempty" cborgen:"record,omitempty" gorm:"column:record"`
32 RepoDID string `json:"repoDID,omitempty" cborgen:"repoDID,omitempty" gorm:"column:repo_did"`
33}
34
35func (m *DBModel) CreateLabel(label *Label) error {
36 return m.DB.Clauses(clause.OnConflict{
37 Columns: []clause.Column{
38 {Name: "src"},
39 {Name: "uri"},
40 {Name: "val"},
41 },
42 UpdateAll: true,
43 }).Create(label).Error
44}
45
46func (m *DBModel) GetActiveLabels(uri string) ([]*comatproto.LabelDefs_Label, error) {
47 now := time.Now().UTC()
48 var labels []Label
49 err := m.DB.Where("uri = ? AND (exp IS NULL OR exp < ?) AND neg = ?", uri, now, false).Find(&labels).Error
50 if err != nil {
51 return nil, err
52 }
53 lexs := make([]*comatproto.LabelDefs_Label, len(labels))
54 for i, l := range labels {
55 lex, err := l.ToLexicon()
56 if err != nil {
57 return nil, err
58 }
59 lexs[i] = lex
60 }
61 return lexs, nil
62}
63
64func (l Label) ToLexicon() (*comatproto.LabelDefs_Label, error) {
65 r := bytes.NewReader(l.Record)
66 var lex comatproto.LabelDefs_Label
67 err := lex.UnmarshalCBOR(r)
68 if err != nil {
69 return nil, err
70 }
71 return &lex, nil
72}