forked from
tangled.org/core
Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
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
18 GitRev syntax.TID // last processed git.refUpdate revision
19 RepoSha string // sha256 sum of git refs (to avoid no-op git fetch)
20 State RepoState
21 ErrorMsg string
22 RetryCount int
23 RetryAfter int64 // Unix timestamp (seconds)
24}
25
26func (r *Repo) AtUri() syntax.ATURI {
27 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
28}
29
30func (r *Repo) DidSlashRepo() string {
31 return fmt.Sprintf("%s/%s", r.Did, r.Name)
32}
33
34type RepoState string
35
36const (
37 RepoStatePending RepoState = "pending"
38 RepoStateDesynchronized RepoState = "desynchronized"
39 RepoStateResyncing RepoState = "resyncing"
40 RepoStateActive RepoState = "active"
41 RepoStateSuspended RepoState = "suspended"
42 RepoStateError RepoState = "error"
43)
44
45var AllRepoStates = []RepoState{
46 RepoStatePending,
47 RepoStateDesynchronized,
48 RepoStateResyncing,
49 RepoStateActive,
50 RepoStateSuspended,
51 RepoStateError,
52}
53
54func (s RepoState) IsResyncing() bool {
55 return s == RepoStateResyncing
56}
57
58type HostCursor struct {
59 Hostname string
60 LastSeq int64
61}
62
63type Host struct {
64 Hostname string
65 NoSSL bool
66 Status HostStatus
67 LastSeq int64
68}
69
70type HostStatus string
71
72const (
73 HostStatusActive HostStatus = "active"
74 HostStatusIdle HostStatus = "idle"
75 HostStatusOffline HostStatus = "offline"
76 HostStatusThrottled HostStatus = "throttled"
77 HostStatusBanned HostStatus = "banned"
78)
79
80var AllHostStatuses = []HostStatus{
81 HostStatusActive,
82 HostStatusIdle,
83 HostStatusOffline,
84 HostStatusThrottled,
85 HostStatusBanned,
86}
87
88func (h *Host) URL() string {
89 if h.NoSSL {
90 return fmt.Sprintf("http://%s", h.Hostname)
91 } else {
92 return fmt.Sprintf("https://%s", h.Hostname)
93 }
94}
95
96func (h *Host) WsURL() string {
97 if h.NoSSL {
98 return fmt.Sprintf("ws://%s", h.Hostname)
99 } else {
100 return fmt.Sprintf("wss://%s", h.Hostname)
101 }
102}
103
104// func (h *Host) SubscribeGitRefsURL(cursor int64) string {
105// scheme := "wss"
106// if h.NoSSL {
107// scheme = "ws"
108// }
109// u := fmt.Sprintf("%s://%s/xrpc/%s", scheme, h.Hostname, tangled.SubscribeGitRefsNSID)
110// if cursor > 0 {
111// u = fmt.Sprintf("%s?cursor=%d", u, h.LastSeq)
112// }
113// return u
114// }
115
116func (h *Host) LegacyEventsURL(cursor int64) string {
117 u := fmt.Sprintf("%s/events", h.WsURL())
118 if cursor > 0 {
119 u = fmt.Sprintf("%s?cursor=%d", u, cursor)
120 }
121 return u
122}