forked from keybase/client
/
bootstrap.go
125 lines (105 loc) · 2.93 KB
/
bootstrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"context"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
// Bootstrap is an engine.
type Bootstrap struct {
libkb.Contextified
status keybase1.BootstrapStatus
usums keybase1.UserSummary2Set
}
// NewBootstrap creates a Bootstrap engine.
func NewBootstrap(g *libkb.GlobalContext) *Bootstrap {
return &Bootstrap{
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *Bootstrap) Name() string {
return "Bootstrap"
}
// GetPrereqs returns the engine prereqs.
func (e *Bootstrap) Prereqs() Prereqs {
return Prereqs{}
}
// RequiredUIs returns the required UIs.
func (e *Bootstrap) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *Bootstrap) SubConsumers() []libkb.UIConsumer {
return nil
}
// Run starts the engine.
func (e *Bootstrap) Run(ctx *Context) error {
e.status.Registered = e.signedUp()
var gerr error
e.G().LoginState().Account(func(a *libkb.Account) {
var in bool
in, gerr = a.LoggedInProvisioned()
if gerr != nil {
e.G().Log.Debug("Bootstrap: LoggedInProvisioned error: %s", gerr)
return
}
e.status.LoggedIn = in
if !e.status.LoggedIn {
e.G().Log.Debug("Bootstrap: not logged in")
return
}
e.status.Uid = e.G().ActiveDevice.UID()
e.G().Log.Debug("Bootstrap: uid = %s", e.status.Uid)
e.status.Username = e.G().Env.GetUsername().String()
e.G().Log.Debug("Bootstrap: username = %s", e.status.Username)
e.status.DeviceID = a.GetDeviceID()
e.status.DeviceName = e.G().ActiveDevice.Name()
}, "Bootstrap")
if gerr != nil {
return gerr
}
if !e.status.LoggedIn {
e.G().Log.Debug("not logged in, not running syncer")
return nil
}
// get user summaries
ts := libkb.NewTracker2Syncer(e.G(), e.status.Uid, true)
if e.G().ConnectivityMonitor.IsConnected(context.Background()) == libkb.ConnectivityMonitorYes {
e.G().Log.Debug("connected, running full tracker2 syncer")
if err := libkb.RunSyncer(ts, e.status.Uid, false, nil); err != nil {
return err
}
} else {
e.G().Log.Debug("not connected, running cached tracker2 syncer")
if err := libkb.RunSyncerCached(ts, e.status.Uid); err != nil {
return err
}
}
e.usums = ts.Result()
// filter usums into followers, following
for _, u := range e.usums.Users {
if u.IsFollower {
e.status.Followers = append(e.status.Followers, u.Username)
}
if u.IsFollowee {
e.status.Following = append(e.status.Following, u.Username)
}
}
return nil
}
// signedUp is true if there's a uid in config.json.
func (e *Bootstrap) signedUp() bool {
cr := e.G().Env.GetConfig()
if cr == nil {
return false
}
if uid := cr.GetUID(); uid.Exists() {
return true
}
return false
}
func (e *Bootstrap) Status() keybase1.BootstrapStatus {
return e.status
}