Live video on the AT Protocol
1package statedb
2
3import (
4 "errors"
5 "time"
6
7 "gorm.io/gorm"
8)
9
10// Very, very basic stateful repo representation. The complex stuff
11// is handled by the indexer in model. This is literally just a set
12// of DIDs that we care about so that new nodes know what to index.
13type Repo struct {
14 DID string `gorm:"primaryKey;column:did"`
15 IndexedAt time.Time `gorm:"column:indexed_at;index:idx_recent_repos"`
16}
17
18func (r *Repo) TableName() string {
19 return "repos"
20}
21
22func (state *StatefulDB) ListRepos(limit int, offset int) ([]Repo, error) {
23 var repos []Repo
24 err := state.DB.
25 Order("indexed_at ASC").
26 Limit(limit).
27 Offset(offset).
28 Find(&repos).
29 Error
30 if err != nil {
31 return nil, err
32 }
33 return repos, nil
34}
35
36func (state *StatefulDB) AddRepo(did string) error {
37 // Check if repo already exists
38 var existingRepo Repo
39 err := state.DB.Where("did = ?", did).First(&existingRepo).Error
40 if err == nil {
41 // Repo already exists, do nothing
42 return nil
43 }
44
45 // If error is not "record not found", return the error
46 if !errors.Is(err, gorm.ErrRecordNotFound) {
47 return err
48 }
49
50 // Repo doesn't exist, create it
51 newRepo := Repo{
52 DID: did,
53 IndexedAt: time.Now(),
54 }
55
56 return state.DB.Create(&newRepo).Error
57}