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 PlcOperationCode *string
23 PlcOperationCodeExpiresAt *time.Time
24 Password string
25 SigningKey []byte
26 Rev string
27 Root []byte
28 Preferences []byte
29 Deactivated bool
30}
31
32func (r *Repo) SignFor(ctx context.Context, did string, msg []byte) ([]byte, error) {
33 k, err := atcrypto.ParsePrivateBytesK256(r.SigningKey)
34 if err != nil {
35 return nil, err
36 }
37
38 sig, err := k.HashAndSign(msg)
39 if err != nil {
40 return nil, err
41 }
42
43 return sig, nil
44}
45
46func (r *Repo) Status() *string {
47 var status *string
48 if r.Deactivated {
49 status = to.StringPtr("deactivated")
50 }
51 return status
52}
53
54func (r *Repo) Active() bool {
55 return r.Status() == nil
56}
57
58type Actor struct {
59 Did string `gorm:"primaryKey"`
60 Handle string `gorm:"uniqueIndex"`
61}
62
63type RepoActor struct {
64 Repo
65 Actor
66}
67
68type InviteCode struct {
69 Code string `gorm:"primaryKey"`
70 Did string `gorm:"index"`
71 RemainingUseCount int
72}
73
74type Token struct {
75 Token string `gorm:"primaryKey"`
76 Did string `gorm:"index"`
77 RefreshToken string `gorm:"index"`
78 CreatedAt time.Time
79 ExpiresAt time.Time `gorm:"index:,sort:asc"`
80}
81
82type RefreshToken struct {
83 Token string `gorm:"primaryKey"`
84 Did string `gorm:"index"`
85 CreatedAt time.Time
86 ExpiresAt time.Time `gorm:"index:,sort:asc"`
87}
88
89type Record struct {
90 Did string `gorm:"primaryKey:idx_record_did_created_at;index:idx_record_did_nsid"`
91 CreatedAt string `gorm:"index;index:idx_record_did_created_at,sort:desc"`
92 Nsid string `gorm:"primaryKey;index:idx_record_did_nsid"`
93 Rkey string `gorm:"primaryKey"`
94 Cid string
95 Value []byte
96}
97
98type Block struct {
99 Did string `gorm:"primaryKey;index:idx_blocks_by_rev"`
100 Cid []byte `gorm:"primaryKey"`
101 Rev string `gorm:"index:idx_blocks_by_rev,sort:desc"`
102 Value []byte
103}
104
105type Blob struct {
106 ID uint
107 CreatedAt string `gorm:"index"`
108 Did string `gorm:"index;index:idx_blob_did_cid"`
109 Cid []byte `gorm:"index;index:idx_blob_did_cid"`
110 RefCount int
111 Storage string `gorm:"default:sqlite"`
112}
113
114type BlobPart struct {
115 Blob Blob
116 BlobID uint `gorm:"primaryKey"`
117 Idx int `gorm:"primaryKey"`
118 Data []byte
119}