-
Notifications
You must be signed in to change notification settings - Fork 66
/
profile.go
294 lines (248 loc) · 8.25 KB
/
profile.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package lib
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"github.com/qri-io/qfs"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/profile"
"github.com/qri-io/qri/registry"
)
// ProfileMethods encapsulates business logic for this node's
// user profile
// TODO (b5) - alterations to user profile are a subset of configuration
// changes. all of this code should be refactored into subroutines of general
// configuration getters & setters
type ProfileMethods struct {
d dispatcher
}
// Name returns the name of this method group
func (m ProfileMethods) Name() string {
return "profile"
}
// Attributes defines attributes for each method
func (m ProfileMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"getprofile": {Endpoint: AEGetProfile, HTTPVerb: "POST", DenyRPC: true},
"setprofile": {Endpoint: AESetProfile, HTTPVerb: "POST", DenyRPC: true},
"setprofilephoto": {Endpoint: AESetProfilePhoto, HTTPVerb: "POST", DenyRPC: true},
"setposterphoto": {Endpoint: AESetPosterPhoto, HTTPVerb: "POST", DenyRPC: true},
}
}
// ProfileParams define parameters for getting a profile
type ProfileParams struct{}
// GetProfile get's this node's peer profile
func (m ProfileMethods) GetProfile(ctx context.Context, p *ProfileParams) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "getprofile"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// SetProfileParams defines parameters for setting parts of a profile
// Cannot use this to set private keys, your peername, or peer id
type SetProfileParams struct {
Pro *config.ProfilePod
}
// SetProfile stores changes to the active peer's editable profile
func (m ProfileMethods) SetProfile(ctx context.Context, p *SetProfileParams) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "setprofile"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// FileParams defines parameters for Files as arguments to lib methods
// either `Filename` or `Data` is required. If both fields are set, the content in the `Data` field is favored
type FileParams struct {
// url to download data from. either Url or Data is required
// Url string
// Filename of data file. extension is used for filetype detection
Filename string `qri:"fspath"`
// Data is the file as slice of bytes
Data []byte
}
// SetProfilePhoto changes the active peer's profile image
func (m ProfileMethods) SetProfilePhoto(ctx context.Context, p *FileParams) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "setprofilephoto"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// SetPosterPhoto changes this active peer's poster image
func (m ProfileMethods) SetPosterPhoto(ctx context.Context, p *FileParams) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "setposterphoto"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// profileImpl holds the method implementations for ProfileMethods
type profileImpl struct{}
// GetProfile get's this node's peer profile
func (profileImpl) GetProfile(scope scope, p *ProfileParams) (*config.ProfilePod, error) {
pro := scope.ActiveProfile()
cfg := scope.Config()
// TODO (b5) - this isn't the right way to check if you're online
if cfg != nil && cfg.P2P != nil {
pro.Online = cfg.P2P.Enabled
}
enc, err := pro.Encode()
if err != nil {
log.Debug(err.Error())
return nil, err
}
enc.PrivKey = ""
return enc, nil
}
// SetProfile stores changes to the active peer's editable profile
func (profileImpl) SetProfile(scope scope, p *SetProfileParams) (*config.ProfilePod, error) {
if p.Pro == nil {
return nil, fmt.Errorf("profile required for update")
}
pro := p.Pro
cfg := scope.Config()
r := scope.Repo()
cfg.Set("profile.name", pro.Name)
cfg.Set("profile.email", pro.Email)
cfg.Set("profile.description", pro.Description)
cfg.Set("profile.homeurl", pro.HomeURL)
cfg.Set("profile.twitter", pro.Twitter)
if pro.Color != "" {
cfg.Set("profile.color", pro.Color)
}
// TODO (b5) - strange bug:
if cfg.Profile.Type == "" {
cfg.Profile.Type = "peer"
}
prevPeername := cfg.Profile.Peername
if pro.Peername != "" && pro.Peername != cfg.Profile.Peername {
cfg.Set("profile.peername", pro.Peername)
}
if err := cfg.Profile.Validate(); err != nil {
return nil, err
}
if pro.Peername != "" && pro.Peername != prevPeername {
if reg := scope.RegistryClient(); reg != nil {
current, err := profile.NewProfile(cfg.Profile)
if err != nil {
return nil, err
}
if _, err := reg.PutProfile(®istry.Profile{Username: pro.Peername}, current.PrivKey); err != nil {
return nil, err
}
}
}
enc, err := profile.NewProfile(cfg.Profile)
if err != nil {
return nil, err
}
if err := r.Profiles().SetOwner(enc); err != nil {
return nil, err
}
res := &config.ProfilePod{}
// Copy the global config, except without the private key.
*res = *cfg.Profile
res.PrivKey = ""
// TODO (b5) - we should have a betteer way of determining onlineness
if cfg.P2P != nil {
res.Online = cfg.P2P.Enabled
}
if err := scope.ChangeConfig(cfg); err != nil {
return nil, err
}
return res, nil
}
// SetProfilePhoto changes the active peer's profile image
func (profileImpl) SetProfilePhoto(scope scope, p *FileParams) (*config.ProfilePod, error) {
if err := loadAndValidateJPEG(p, 256000); err != nil {
return nil, err
}
// TODO - if file extension is .jpg / .jpeg ipfs does weird shit that makes this not work
path, err := scope.Filesystem().DefaultWriteFS().Put(scope.Context(), qfs.NewMemfileBytes("plz_just_encode", p.Data))
if err != nil {
log.Debug(err.Error())
return nil, fmt.Errorf("error saving photo: %s", err.Error())
}
cfg := scope.Config().Copy()
cfg.Set("profile.photo", path)
// TODO - resize photo for thumb
cfg.Set("profile.thumb", path)
if err := scope.ChangeConfig(cfg); err != nil {
return nil, err
}
pro := scope.ActiveProfile()
pro.Photo = path
pro.Thumb = path
if err := scope.Profiles().SetOwner(pro); err != nil {
return nil, err
}
pp, err := pro.Encode()
if err != nil {
return nil, fmt.Errorf("error encoding new profile: %s", err)
}
return pp, nil
}
// SetPosterPhoto changes the active peer's poster image
func (profileImpl) SetPosterPhoto(scope scope, p *FileParams) (*config.ProfilePod, error) {
if err := loadAndValidateJPEG(p, 2<<20); err != nil {
return nil, err
}
// TODO - if file extension is .jpg / .jpeg ipfs does weird shit that makes this not work
path, err := scope.Filesystem().DefaultWriteFS().Put(scope.Context(), qfs.NewMemfileBytes("plz_just_encode", p.Data))
if err != nil {
log.Debug(err.Error())
return nil, fmt.Errorf("error saving photo: %s", err.Error())
}
cfg := scope.Config().Copy()
cfg.Set("profile.poster", path)
if err := scope.ChangeConfig(cfg); err != nil {
return nil, err
}
pro := scope.ActiveProfile()
pro.Poster = path
if err := scope.Profiles().SetOwner(pro); err != nil {
return nil, err
}
pp, err := pro.Encode()
if err != nil {
return nil, fmt.Errorf("error encoding new profile: %s", err)
}
return pp, nil
}
func loadAndValidateJPEG(p *FileParams, maxBytes int) (err error) {
if p.Filename == "" && (p.Data == nil || len(p.Data) == 0) {
return fmt.Errorf("filename or data required")
}
if p.Data == nil || len(p.Data) == 0 {
if p.Data, err = ioutil.ReadFile(p.Filename); err != nil {
return fmt.Errorf("error opening file: %w", err)
}
}
if len(p.Data) > maxBytes {
return fmt.Errorf("file size too large. max size is %s", byteCount(int64(maxBytes)))
} else if len(p.Data) == 0 {
return fmt.Errorf("file is empty")
}
mimetype := http.DetectContentType(p.Data)
if mimetype != "image/jpeg" {
return fmt.Errorf("invalid file format. only .jpg images allowed")
}
return nil
}
// provides human readable byte count
func byteCount(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}