forked from OpenBazaar/openbazaar-go
/
profile.go
435 lines (402 loc) · 13.8 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package core
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/repo"
cid "gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
"io/ioutil"
"os"
"path"
"strings"
"time"
ipnspath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
"github.com/OpenBazaar/jsonpb"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/golang/protobuf/ptypes"
"github.com/imdario/mergo"
)
// ErrorProfileNotFound - profile not found error
var ErrorProfileNotFound = errors.New("profile not found")
// GetProfile - fetch user profile
func (n *OpenBazaarNode) GetProfile() (pb.Profile, error) {
var profile pb.Profile
f, err := os.Open(path.Join(n.RepoPath, "root", "profile.json"))
if err != nil {
return profile, ErrorProfileNotFound
}
defer f.Close()
err = jsonpb.Unmarshal(f, &profile)
if err != nil {
return profile, err
}
return profile, nil
}
// FetchProfile - fetch peer's profile
func (n *OpenBazaarNode) FetchProfile(peerID string, useCache bool) (pb.Profile, error) {
var pro pb.Profile
b, err := ipfs.ResolveThenCat(n.IpfsNode, ipnspath.FromString(path.Join(peerID, "profile.json")), time.Minute, n.IPNSQuorumSize, true)
if err != nil || len(b) == 0 {
return pro, err
}
p, err := repo.UnmarshalJSONProfile(b)
if err != nil {
log.Error("Profile fetch error", peerID, err)
return pro, err
}
p.NormalizeDataForAllSchemas()
return *p.GetProtobuf(), nil
}
// UpdateProfile - update user profile
func (n *OpenBazaarNode) UpdateProfile(profile *pb.Profile) error {
mPubkey, err := n.MasterPrivateKey.ECPubKey()
if err != nil {
return fmt.Errorf("getting public key: %s", err.Error())
}
profile.Version = repo.ListingVersion
profile.BitcoinPubkey = hex.EncodeToString(mPubkey.SerializeCompressed())
var acceptedCurrencies = profile.GetCurrencies()
settingsData, err := n.Datastore.Settings().Get()
if err != nil {
log.Debug("settings not set, using default preferred currencies")
}
if len(acceptedCurrencies) == 0 {
if settingsData.PreferredCurrencies != nil {
for _, ct := range *settingsData.PreferredCurrencies {
def, err := n.LookupCurrency(ct)
if err != nil {
return fmt.Errorf("lookup currency (%s): %s", ct, err)
}
acceptedCurrencies = append(acceptedCurrencies, def.CurrencyCode().String())
}
} else {
for ct := range n.Multiwallet {
def, err := n.LookupCurrency(ct.CurrencyCode())
if err != nil {
return fmt.Errorf("lookup currency (%s): %s", ct.CurrencyCode(), err)
}
acceptedCurrencies = append(acceptedCurrencies, def.CurrencyCode().String())
}
}
}
profile.Currencies = acceptedCurrencies
if profile.ModeratorInfo != nil {
profile.ModeratorInfo.AcceptedCurrencies = acceptedCurrencies
}
profile.PeerID = n.IpfsNode.Identity.Pretty()
ts, err := ptypes.TimestampProto(time.Now())
if err != nil {
return err
}
profile.LastModified = ts
if err := ValidateProfile(profile); err != nil {
return err
}
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: false,
}
out, err := m.MarshalToString(profile)
if err != nil {
return err
}
profilePath := path.Join(n.RepoPath, "root", "profile.json")
f, err := os.Create(profilePath)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(out); err != nil {
return err
}
return nil
}
// PatchProfile - patch user profile
func (n *OpenBazaarNode) PatchProfile(patch map[string]interface{}) error {
profilePath := path.Join(n.RepoPath, "root", "profile.json")
// Read stored profile data
file, err := os.Open(profilePath)
if err != nil {
return err
}
d := json.NewDecoder(file)
d.UseNumber()
var i interface{}
err = d.Decode(&i)
if err != nil {
return err
}
profile := i.(map[string]interface{})
patchMod, pok := patch["moderator"]
storedMod, sok := profile["moderator"]
if pok && sok {
patchBool, ok := patchMod.(bool)
if !ok {
return errors.New("invalid moderator type")
}
storedBool, ok := storedMod.(bool)
if !ok {
return errors.New("invalid moderator type")
}
if patchBool && patchBool != storedBool {
if err := n.SetSelfAsModerator(nil); err != nil {
return err
}
} else if !patchBool && patchBool != storedBool {
if err := n.RemoveSelfAsModerator(); err != nil {
return err
}
}
}
// Assuming that `profile` map contains complete data, as it is read
// from storage, and `patch` map is possibly incomplete, merge first
// into second recursively, preserving new fields and adding missing
// old ones
if err := mergo.Map(&patch, &profile); err != nil {
return err
}
newProfile, err := json.Marshal(patch)
if err != nil {
return err
}
p := new(pb.Profile)
if err := jsonpb.Unmarshal(bytes.NewReader(newProfile), p); err != nil {
return err
}
repoProfile, err := repo.UnmarshalJSONProfile(newProfile)
if err != nil {
return fmt.Errorf("building profile for validation: %s", err.Error())
}
repoProfile.NormalizeDataForAllSchemas()
if repoProfile.GetProtobuf().ModeratorInfo != nil &&
repoProfile.GetProtobuf().ModeratorInfo.Fee != nil &&
repoProfile.GetProtobuf().ModeratorInfo.Fee.FeeType == pb.Moderator_Fee_PERCENTAGE {
repoProfile.GetProtobuf().ModeratorInfo.Fee.FixedFee = nil
}
if err := repoProfile.Valid(); err != nil {
return fmt.Errorf("invalid profile: %s", err.Error())
}
return n.UpdateProfile(repoProfile.GetProtobuf())
}
func (n *OpenBazaarNode) appendCountsToProfile(profile *pb.Profile) (*pb.Profile, bool) {
if profile.Stats == nil {
profile.Stats = new(pb.Profile_Stats)
}
var changed bool
listingCount := uint32(n.GetListingCount())
if listingCount != profile.Stats.ListingCount {
profile.Stats.ListingCount = listingCount
changed = true
}
postCount := uint32(n.GetPostCount())
if postCount != profile.Stats.PostCount {
profile.Stats.PostCount = postCount
changed = true
}
followerCount := uint32(n.Datastore.Followers().Count())
if followerCount != profile.Stats.FollowerCount {
profile.Stats.FollowerCount = followerCount
changed = true
}
followingCount := uint32(n.Datastore.Following().Count())
if followingCount != profile.Stats.FollowingCount {
profile.Stats.FollowingCount = followingCount
changed = true
}
ratingCount, averageRating, err := n.GetRatingCounts()
if err == nil && (ratingCount != profile.Stats.RatingCount || averageRating != profile.Stats.AverageRating) {
profile.Stats.RatingCount = ratingCount
profile.Stats.AverageRating = averageRating
changed = true
}
return profile, changed
}
func (n *OpenBazaarNode) updateProfileCounts() error {
profilePath := path.Join(n.RepoPath, "root", "profile.json")
profile := new(pb.Profile)
_, ferr := os.Stat(profilePath)
if !os.IsNotExist(ferr) {
// Read existing file
file, err := os.Open(profilePath)
if err != nil {
return err
}
defer file.Close()
err = jsonpb.Unmarshal(file, profile)
if err != nil {
return err
}
} else {
return nil
}
newPro, changed := n.appendCountsToProfile(profile)
if changed {
return n.UpdateProfile(newPro)
}
return nil
}
func (n *OpenBazaarNode) updateProfileRatings(newRating *pb.Rating) error {
profilePath := path.Join(n.RepoPath, "root", "profile.json")
profile := new(pb.Profile)
_, ferr := os.Stat(profilePath)
if !os.IsNotExist(ferr) {
// Read existing file
file, err := ioutil.ReadFile(profilePath)
if err != nil {
return err
}
err = jsonpb.UnmarshalString(string(file), profile)
if err != nil {
return err
}
} else {
return nil
}
if profile.Stats != nil && newRating.RatingData != nil {
total := profile.Stats.AverageRating * float32(profile.Stats.RatingCount)
total += float32(newRating.RatingData.Overall)
profile.Stats.RatingCount++ // += 1
profile.Stats.AverageRating = total / float32(profile.Stats.RatingCount)
}
newPro, _ := n.appendCountsToProfile(profile)
return n.UpdateProfile(newPro)
}
// ValidateProfile - validate fetched profile
func ValidateProfile(profile *pb.Profile) error {
if strings.Contains(profile.Handle, "@") {
return errors.New("handle should not contain @")
}
if len(profile.Handle) > repo.WordMaxCharacters {
return fmt.Errorf("handle character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(profile.Name) == 0 {
return errors.New("profile name not set")
}
if len(profile.Name) > repo.WordMaxCharacters {
return fmt.Errorf("name character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(profile.Location) > repo.WordMaxCharacters {
return fmt.Errorf("location character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(profile.About) > repo.AboutMaxCharacters {
return fmt.Errorf("about character length is greater than the max of %d", repo.AboutMaxCharacters)
}
if len(profile.ShortDescription) > repo.ShortDescriptionLength {
return fmt.Errorf("short description character length is greater than the max of %d", repo.ShortDescriptionLength)
}
if profile.ContactInfo != nil {
if len(profile.ContactInfo.Website) > repo.URLMaxCharacters {
return fmt.Errorf("website character length is greater than the max of %d", repo.URLMaxCharacters)
}
if len(profile.ContactInfo.Email) > repo.SentenceMaxCharacters {
return fmt.Errorf("email character length is greater than the max of %d", repo.SentenceMaxCharacters)
}
if len(profile.ContactInfo.PhoneNumber) > repo.WordMaxCharacters {
return fmt.Errorf("phone number character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(profile.ContactInfo.Social) > repo.MaxListItems {
return fmt.Errorf("number of social accounts is greater than the max of %d", repo.MaxListItems)
}
for _, s := range profile.ContactInfo.Social {
if len(s.Username) > repo.WordMaxCharacters {
return fmt.Errorf("social username character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(s.Type) > repo.WordMaxCharacters {
return fmt.Errorf("social account type character length is greater than the max of %d", repo.WordMaxCharacters)
}
if len(s.Proof) > repo.URLMaxCharacters {
return fmt.Errorf("social proof character length is greater than the max of %d", repo.WordMaxCharacters)
}
}
}
if profile.ModeratorInfo != nil {
if len(profile.ModeratorInfo.Description) > repo.AboutMaxCharacters {
return fmt.Errorf("moderator description character length is greater than the max of %d", repo.AboutMaxCharacters)
}
if len(profile.ModeratorInfo.TermsAndConditions) > repo.PolicyMaxCharacters {
return fmt.Errorf("moderator terms and conditions character length is greater than the max of %d", repo.PolicyMaxCharacters)
}
if len(profile.ModeratorInfo.Languages) > repo.MaxListItems {
return fmt.Errorf("moderator number of languages greater than the max of %d", repo.MaxListItems)
}
for _, l := range profile.ModeratorInfo.Languages {
if len(l) > repo.WordMaxCharacters {
return fmt.Errorf("moderator language character length is greater than the max of %d", repo.WordMaxCharacters)
}
}
if profile.ModeratorInfo.Fee != nil {
var moderatorCurrencyCode string
if profile.ModeratorInfo.Fee.FixedFee != nil {
if profile.ModeratorInfo.Fee.FixedFee.AmountCurrency == nil {
moderatorCurrencyCode = profile.ModeratorInfo.Fee.FixedFee.CurrencyCode
} else {
moderatorCurrencyCode = profile.ModeratorInfo.Fee.FixedFee.AmountCurrency.Code
}
}
if profile.ModeratorInfo.Fee.FeeType != pb.Moderator_Fee_PERCENTAGE && profile.ModeratorInfo.Fee.FixedFee != nil &&
len(moderatorCurrencyCode) > repo.WordMaxCharacters {
return fmt.Errorf("moderator fee currency code character length is greater than the max of %d", repo.WordMaxCharacters)
}
}
}
if profile.AvatarHashes != nil && (profile.AvatarHashes.Large != "" || profile.AvatarHashes.Medium != "" ||
profile.AvatarHashes.Small != "" || profile.AvatarHashes.Tiny != "" || profile.AvatarHashes.Original != "") {
_, err := cid.Decode(profile.AvatarHashes.Tiny)
if err != nil {
return errors.New("tiny image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.AvatarHashes.Small)
if err != nil {
return errors.New("small image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.AvatarHashes.Medium)
if err != nil {
return errors.New("medium image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.AvatarHashes.Large)
if err != nil {
return errors.New("large image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.AvatarHashes.Original)
if err != nil {
return errors.New("original image hashes must be properly formatted CID")
}
}
if profile.HeaderHashes != nil && (profile.HeaderHashes.Large != "" || profile.HeaderHashes.Medium != "" ||
profile.HeaderHashes.Small != "" || profile.HeaderHashes.Tiny != "" || profile.HeaderHashes.Original != "") {
_, err := cid.Decode(profile.HeaderHashes.Tiny)
if err != nil {
return errors.New("tiny image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.HeaderHashes.Small)
if err != nil {
return errors.New("small image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.HeaderHashes.Medium)
if err != nil {
return errors.New("medium image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.HeaderHashes.Large)
if err != nil {
return errors.New("large image hashes must be properly formatted CID")
}
_, err = cid.Decode(profile.HeaderHashes.Original)
if err != nil {
return errors.New("original image hashes must be properly formatted CID")
}
}
if len(profile.BitcoinPubkey) > 66 {
return fmt.Errorf("bitcoin public key character length is greater than the max of %d", 66)
}
if profile.Stats != nil {
if profile.Stats.AverageRating > 5 {
return fmt.Errorf("average rating cannot be greater than %d", 5)
}
}
return nil
}