1package models
2
3import (
4 "context"
5 "time"
6
7 "github.com/Azure/go-autorest/autorest/to"
8 "github.com/bluesky-social/indigo/atproto/atcrypto"
9)
10
11type Repo struct {
12 Did string `gorm:"primaryKey"`
13 CreatedAt time.Time
14 Email string `gorm:"uniqueIndex"`
15 EmailConfirmedAt *time.Time
16 EmailVerificationCode *string
17 EmailVerificationCodeExpiresAt *time.Time
18 EmailUpdateCode *string
19 EmailUpdateCodeExpiresAt *time.Time
20 PasswordResetCode *string
21 PasswordResetCodeExpiresAt *time.Time
22 Password string
23 SigningKey []byte
24 Rev string
25 Root []byte
26 Preferences []byte
27 Deactivated bool
28}
29
30func (r *Repo) SignFor(ctx context.Context, did string, msg []byte) ([]byte, error) {
31 k, err := atcrypto.ParsePrivateBytesK256(r.SigningKey)
32 if err != nil {
33 return nil, err
34 }
35
36 sig, err := k.HashAndSign(msg)
37 if err != nil {
38 return nil, err
39 }
40
41 return sig, nil
42}
43
44func (r *Repo) Status() *string {
45 var status *string
46 if r.Deactivated {
47 status = to.StringPtr("deactivated")
48 }
49 return status
50}
51
52func (r *Repo) Active() bool {
53 return r.Status() == nil
54}
55
56type Actor struct {
57 Did string `gorm:"primaryKey"`
58 Handle string `gorm:"uniqueIndex"`
59}
60
61type RepoActor struct {
62 Repo
63 Actor
64}
65
66type InviteCode struct {
67 Code string `gorm:"primaryKey"`
68 Did string `gorm:"index"`
69 RemainingUseCount int
70}
71
72type Token struct {
73 Token string `gorm:"primaryKey"`
74 Did string `gorm:"index"`
75 RefreshToken string `gorm:"index"`
76 CreatedAt time.Time
77 ExpiresAt time.Time `gorm:"index:,sort:asc"`
78}
79
80type RefreshToken struct {
81 Token string `gorm:"primaryKey"`
82 Did string `gorm:"index"`
83 CreatedAt time.Time
84 ExpiresAt time.Time `gorm:"index:,sort:asc"`
85}
86
87type Record struct {
88 Did string `gorm:"primaryKey:idx_record_did_created_at;index:idx_record_did_nsid"`
89 CreatedAt string `gorm:"index;index:idx_record_did_created_at,sort:desc"`
90 Nsid string `gorm:"primaryKey;index:idx_record_did_nsid"`
91 Rkey string `gorm:"primaryKey"`
92 Cid string
93 Value []byte
94}
95
96type Block struct {
97 Did string `gorm:"primaryKey;index:idx_blocks_by_rev"`
98 Cid []byte `gorm:"primaryKey"`
99 Rev string `gorm:"index:idx_blocks_by_rev,sort:desc"`
100 Value []byte
101}
102
103type Blob struct {
104 ID uint
105 CreatedAt string `gorm:"index"`
106 Did string `gorm:"index;index:idx_blob_did_cid"`
107 Cid []byte `gorm:"index;index:idx_blob_did_cid"`
108 RefCount int
109}
110
111type BlobPart struct {
112 Blob Blob
113 BlobID uint `gorm:"primaryKey"`
114 Idx int `gorm:"primaryKey"`
115 Data []byte
116}