Monorepo for Tangled
tangled.org
1package models
2
3import (
4 "fmt"
5
6 "github.com/bluesky-social/indigo/atproto/syntax"
7 "tangled.org/core/api/tangled"
8)
9
10type Repo struct {
11 Did syntax.DID
12 Rkey syntax.RecordKey
13 Cid *syntax.CID
14 // content of tangled.Repo
15 Name string
16 KnotDomain string
17 RepoDid syntax.DID
18
19 GitRev syntax.TID // last processed git.refUpdate revision
20 RepoSha string // sha256 sum of git refs (to avoid no-op git fetch)
21 State RepoState
22 ErrorMsg string
23 RetryCount int
24 RetryAfter int64 // Unix timestamp (seconds)
25}
26
27func (r *Repo) AtUri() syntax.ATURI {
28 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
29}
30
31func (r *Repo) RepoIdentifier() string {
32 return r.RepoDid.String()
33}
34
35type RepoState string
36
37const (
38 RepoStatePending RepoState = "pending"
39 RepoStateDesynchronized RepoState = "desynchronized"
40 RepoStateResyncing RepoState = "resyncing"
41 RepoStateActive RepoState = "active"
42 RepoStateSuspended RepoState = "suspended"
43 RepoStateError RepoState = "error"
44)
45
46var AllRepoStates = []RepoState{
47 RepoStatePending,
48 RepoStateDesynchronized,
49 RepoStateResyncing,
50 RepoStateActive,
51 RepoStateSuspended,
52 RepoStateError,
53}
54
55func (s RepoState) IsResyncing() bool {
56 return s == RepoStateResyncing
57}
58
59type HostCursor struct {
60 Hostname string
61 LastSeq int64
62}
63
64type Host struct {
65 Hostname string
66 NoSSL bool
67 Status HostStatus
68 LastSeq int64
69}
70
71type HostStatus string
72
73const (
74 HostStatusActive HostStatus = "active"
75 HostStatusIdle HostStatus = "idle"
76 HostStatusOffline HostStatus = "offline"
77 HostStatusThrottled HostStatus = "throttled"
78 HostStatusBanned HostStatus = "banned"
79)
80
81var AllHostStatuses = []HostStatus{
82 HostStatusActive,
83 HostStatusIdle,
84 HostStatusOffline,
85 HostStatusThrottled,
86 HostStatusBanned,
87}
88
89func (h *Host) URL() string {
90 if h.NoSSL {
91 return fmt.Sprintf("http://%s", h.Hostname)
92 } else {
93 return fmt.Sprintf("https://%s", h.Hostname)
94 }
95}
96
97func (h *Host) WsURL() string {
98 if h.NoSSL {
99 return fmt.Sprintf("ws://%s", h.Hostname)
100 } else {
101 return fmt.Sprintf("wss://%s", h.Hostname)
102 }
103}
104
105// func (h *Host) SubscribeGitRefsURL(cursor int64) string {
106// scheme := "wss"
107// if h.NoSSL {
108// scheme = "ws"
109// }
110// u := fmt.Sprintf("%s://%s/xrpc/%s", scheme, h.Hostname, tangled.SubscribeGitRefsNSID)
111// if cursor > 0 {
112// u = fmt.Sprintf("%s?cursor=%d", u, h.LastSeq)
113// }
114// return u
115// }
116
117func (h *Host) LegacyEventsURL(cursor int64) string {
118 u := fmt.Sprintf("%s/events", h.WsURL())
119 if cursor > 0 {
120 u = fmt.Sprintf("%s?cursor=%d", u, cursor)
121 }
122 return u
123}