Monorepo for Tangled
tangled.org
1package eventconsumer
2
3import (
4 "net/url"
5 "strconv"
6
7 "tangled.org/core/eventconsumer/cursor"
8)
9
10type Kind string
11
12const (
13 KindKnot Kind = "knot"
14 KindSpindle Kind = "spindle"
15)
16
17type Source struct {
18 Kind Kind
19 Host string
20}
21
22func NewKnotSource(host string) Source { return Source{Kind: KindKnot, Host: host} }
23func NewSpindleSource(host string) Source { return Source{Kind: KindSpindle, Host: host} }
24
25func (s Source) Key() string { return string(s.Kind) + ":" + s.Host }
26
27func MigrateLegacyCursor(store cursor.Store, s Source) {
28 if store.Get(s.Key()) != 0 {
29 return
30 }
31 if legacy := store.Get(s.Host); legacy != 0 {
32 store.Set(s.Key(), legacy)
33 }
34}
35
36func DefaultURL(dev bool) func(Source, int64) (*url.URL, error) {
37 scheme := "wss"
38 if dev {
39 scheme = "ws"
40 }
41 return func(s Source, cursor int64) (*url.URL, error) {
42 u, err := url.Parse(scheme + "://" + s.Host + "/events")
43 if err != nil {
44 return nil, err
45 }
46 if cursor != 0 {
47 q := url.Values{}
48 q.Add("cursor", strconv.FormatInt(cursor, 10))
49 u.RawQuery = q.Encode()
50 }
51 return u, nil
52 }
53}