/
console_account.go
601 lines (532 loc) · 21.7 KB
/
console_account.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/heroiclabs/nakama/api"
"github.com/heroiclabs/nakama/console"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *ConsoleServer) DeleteAccount(ctx context.Context, in *console.AccountDeleteRequest) (*empty.Empty, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Cannot delete the system user.")
}
if err = DeleteAccount(ctx, s.logger, s.db, userID, in.RecordDeletion != nil && in.RecordDeletion.Value); err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to delete the user.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) DeleteFriend(ctx context.Context, in *console.DeleteFriendRequest) (*empty.Empty, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
if _, err := uuid.FromString(in.FriendId); err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid friend ID.")
}
if err = DeleteFriends(ctx, s.logger, s.db, userID, []string{in.FriendId}); err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to delete the friend relationship.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) DeleteGroupUser(ctx context.Context, in *console.DeleteGroupUserRequest) (*empty.Empty, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
groupID, err := uuid.FromString(in.GroupId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid group ID.")
}
if err = KickGroupUsers(ctx, s.logger, s.db, uuid.Nil, groupID, []uuid.UUID{userID}); err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to remove the user from the group.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) DeleteWalletLedger(ctx context.Context, in *console.DeleteWalletLedgerRequest) (*empty.Empty, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
walletID, err := uuid.FromString(in.WalletId)
if err != nil || walletID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid wallet ledger item ID.")
}
_, err = s.db.ExecContext(ctx, "DELETE FROM wallet_ledger WHERE id = $1 AND user_id = $2", walletID, userID)
if err != nil {
s.logger.Error("Error deleting from wallet ledger.", zap.String("id", walletID.String()), zap.String("user_id", userID.String()), zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to remove the user's wallet ledger item.")
}
return &empty.Empty{}, nil
}
func (s *ConsoleServer) ExportAccount(ctx context.Context, in *console.AccountId) (*console.AccountExport, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Cannot export the system user.")
}
// Core user account.
account, _, err := GetAccount(ctx, s.logger, s.db, nil, userID)
if err != nil {
if err == ErrAccountNotFound {
return nil, status.Error(codes.NotFound, "Account not found.")
}
s.logger.Error("Could not export account data", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Friends.
friends, err := GetFriendIDs(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch friend IDs", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Messages.
messages, err := GetChannelMessages(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch messages", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Leaderboard records.
leaderboardRecords, err := LeaderboardRecordReadAll(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch leaderboard records", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
groups := make([]*api.Group, 0)
groupUsers, err := ListUserGroups(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch groups that belong to the user", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
for _, g := range groupUsers.UserGroups {
groups = append(groups, g.Group)
}
// Notifications.
notifications, err := NotificationList(ctx, s.logger, s.db, userID, 0, "", nil)
if err != nil {
s.logger.Error("Could not fetch notifications", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// Storage objects where user is the owner.
storageObjects, err := StorageReadAllUserObjects(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch notifications", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
// History of user's wallet.
walletLedgers, err := ListWalletLedger(ctx, s.logger, s.db, userID)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
wl := make([]*console.WalletLedger, len(walletLedgers))
for i, w := range walletLedgers {
changeset, err := json.Marshal(w.Changeset)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items, error encoding changeset", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
metadata, err := json.Marshal(w.Metadata)
if err != nil {
s.logger.Error("Could not fetch wallet ledger items, error encoding metadata", zap.Error(err), zap.String("user_id", in.Id))
return nil, status.Error(codes.Internal, "An error occurred while trying to export user data.")
}
wl[i] = &console.WalletLedger{
Id: w.ID,
UserId: w.UserID,
Changeset: string(changeset),
Metadata: string(metadata),
CreateTime: ×tamp.Timestamp{Seconds: w.CreateTime},
UpdateTime: ×tamp.Timestamp{Seconds: w.UpdateTime},
}
}
export := &console.AccountExport{
Account: account,
Objects: storageObjects,
Friends: friends.GetFriends(),
Messages: messages,
Groups: groups,
LeaderboardRecords: leaderboardRecords,
Notifications: notifications.GetNotifications(),
WalletLedgers: wl,
}
return export, nil
}
func (s *ConsoleServer) GetAccount(ctx context.Context, in *console.AccountId) (*console.Account, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
account, disableTime, err := GetAccount(ctx, s.logger, s.db, s.tracker, userID)
if err != nil {
// Error already logged in function above.
if err == ErrAccountNotFound {
return nil, status.Error(codes.NotFound, "Account not found.")
}
return nil, status.Error(codes.Internal, "An error occurred while trying to retrieve user account.")
}
acc := &console.Account{Account: account}
if disableTime.Unix() != 0 {
acc.DisableTime = ×tamp.Timestamp{Seconds: disableTime.Unix()}
}
return acc, nil
}
func (s *ConsoleServer) GetFriends(ctx context.Context, in *console.AccountId) (*api.Friends, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
friends, err := GetFriends(ctx, s.logger, s.db, s.tracker, userID)
if err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to list the user's friends.")
}
return friends, nil
}
func (s *ConsoleServer) GetGroups(ctx context.Context, in *console.AccountId) (*api.UserGroupList, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
groups, err := ListUserGroups(ctx, s.logger, s.db, userID)
if err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to list the user's groups.")
}
return groups, nil
}
func (s *ConsoleServer) GetWalletLedger(ctx context.Context, in *console.AccountId) (*console.WalletLedgerList, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
ledger, err := ListWalletLedger(ctx, s.logger, s.db, userID)
if err != nil {
// Error already logged in function above.
return nil, status.Error(codes.Internal, "An error occurred while trying to list the user's wallet ledger.")
}
// Convert to console wire format.
consoleLedger := make([]*console.WalletLedger, 0, len(ledger))
for _, ledgerItem := range ledger {
changeset, err := json.Marshal(ledgerItem.Changeset)
if err != nil {
s.logger.Error("Error encoding wallet ledger changeset.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list the user's wallet ledger.")
}
metadata, err := json.Marshal(ledgerItem.Metadata)
if err != nil {
s.logger.Error("Error encoding wallet ledger metadata.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to list the user's wallet ledger.")
}
consoleLedger = append(consoleLedger, &console.WalletLedger{
Id: ledgerItem.ID,
UserId: ledgerItem.UserID,
Changeset: string(changeset),
Metadata: string(metadata),
CreateTime: ×tamp.Timestamp{Seconds: ledgerItem.CreateTime},
UpdateTime: ×tamp.Timestamp{Seconds: ledgerItem.UpdateTime},
})
}
return &console.WalletLedgerList{Items: consoleLedger}, nil
}
func (s *ConsoleServer) UpdateAccount(ctx context.Context, in *console.UpdateAccountRequest) (*empty.Empty, error) {
userID, err := uuid.FromString(in.Id)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Requires a valid user ID.")
}
if userID == uuid.Nil {
return nil, status.Error(codes.InvalidArgument, "Cannot update the system user.")
}
statements := make([]string, 0)
params := []interface{}{userID}
if v := in.Username; v != nil {
if len(v.Value) == 0 {
return nil, status.Error(codes.InvalidArgument, "Username cannot be empty.")
}
if invalidCharsRegex.MatchString(v.Value) {
return nil, status.Error(codes.InvalidArgument, "Username cannot contain spaces or control characters.")
}
params = append(params, v.Value)
statements = append(statements, "username = $"+strconv.Itoa(len(params)))
}
if v := in.DisplayName; v != nil {
if d := v.Value; d == "" {
statements = append(statements, "display_name = NULL")
} else {
params = append(params, d)
statements = append(statements, "display_name = $"+strconv.Itoa(len(params)))
}
}
if v := in.Metadata; v != nil && v.Value != "" {
var metadataMap map[string]interface{}
if err := json.Unmarshal([]byte(v.Value), &metadataMap); err != nil {
return nil, status.Error(codes.InvalidArgument, "Metadata must be a valid JSON object.")
}
params = append(params, v.Value)
statements = append(statements, "metadata = $"+strconv.Itoa(len(params)))
}
if v := in.AvatarUrl; v != nil {
if a := v.Value; a == "" {
statements = append(statements, "avatar_url = NULL")
} else {
params = append(params, a)
statements = append(statements, "avatar_url = $"+strconv.Itoa(len(params)))
}
}
if v := in.LangTag; v != nil {
if l := v.Value; l == "" {
statements = append(statements, "lang_tag = NULL")
} else {
params = append(params, l)
statements = append(statements, "lang_tag = $"+strconv.Itoa(len(params)))
}
}
if v := in.Location; v != nil {
if l := v.Value; l == "" {
statements = append(statements, "location = NULL")
} else {
params = append(params, l)
statements = append(statements, "location = $"+strconv.Itoa(len(params)))
}
}
if v := in.Timezone; v != nil {
if t := v.Value; t == "" {
statements = append(statements, "timezone = NULL")
} else {
params = append(params, t)
statements = append(statements, "timezone = $"+strconv.Itoa(len(params)))
}
}
var removeCustomId bool
if v := in.CustomId; v != nil {
if c := v.Value; c == "" {
removeCustomId = true
} else {
if invalidCharsRegex.MatchString(c) {
return nil, status.Error(codes.InvalidArgument, "Custom ID invalid, no spaces or control characters allowed.")
} else if len(c) < 6 || len(c) > 128 {
return nil, status.Error(codes.InvalidArgument, "Custom ID invalid, must be 6-128 bytes.")
} else {
params = append(params, c)
statements = append(statements, "custom_id = $"+strconv.Itoa(len(params)))
}
}
}
var removeEmail bool
if v := in.Email; v != nil {
if e := v.Value; e == "" {
removeEmail = true
} else {
if invalidCharsRegex.MatchString(e) {
return nil, status.Error(codes.InvalidArgument, "Invalid email address, no spaces or control characters allowed.")
} else if !emailRegex.MatchString(e) {
return nil, status.Error(codes.InvalidArgument, "Invalid email address format.")
} else if len(e) < 10 || len(e) > 255 {
return nil, status.Error(codes.InvalidArgument, "Invalid email address, must be 10-255 bytes.")
} else {
params = append(params, e)
statements = append(statements, "email = $"+strconv.Itoa(len(params)))
}
}
}
if v := in.Wallet; v != nil && v.Value != "" {
var walletMap map[string]interface{}
if err := json.Unmarshal([]byte(v.Value), &walletMap); err != nil {
return nil, status.Error(codes.InvalidArgument, "Wallet must be a valid JSON object.")
}
if err := checkWalletFormat(walletMap, ""); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
params = append(params, v.Value)
statements = append(statements, "wallet = $"+strconv.Itoa(len(params)))
}
for oldDeviceID, newDeviceID := range in.DeviceIds {
if invalidCharsRegex.MatchString(oldDeviceID) {
return nil, status.Error(codes.InvalidArgument, "Old device ID invalid, no spaces or control characters allowed.")
} else if len(oldDeviceID) < 10 || len(oldDeviceID) > 128 {
return nil, status.Error(codes.InvalidArgument, "Old device ID invalid, must be 10-128 bytes.")
}
if newDeviceID != "" {
// Only validate if device ID is not being removed.
if invalidCharsRegex.MatchString(newDeviceID) {
return nil, status.Error(codes.InvalidArgument, "New device ID invalid, no spaces or control characters allowed.")
} else if len(newDeviceID) < 10 || len(newDeviceID) > 128 {
return nil, status.Error(codes.InvalidArgument, "New device ID invalid, must be 10-128 bytes.")
}
}
}
if len(statements) == 0 && !removeCustomId && !removeEmail && len(in.DeviceIds) == 0 {
// Nothing to update.
return &empty.Empty{}, nil
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
s.logger.Error("Could not begin database transaction.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to update the user.")
}
if err = crdb.ExecuteInTx(ctx, tx, func() error {
for oldDeviceID, newDeviceID := range in.DeviceIds {
if newDeviceID == "" {
query := `DELETE FROM user_device WHERE id = $2 AND user_id = $1
AND (EXISTS (SELECT id FROM users WHERE id = $1 AND
(facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL
OR custom_id IS NOT NULL))
OR EXISTS (SELECT id FROM user_device WHERE user_id = $1 AND id <> $2 LIMIT 1))`
res, err := tx.ExecContext(ctx, query, userID, oldDeviceID)
if err != nil {
s.logger.Error("Could not unlink device ID.", zap.Error(err), zap.Any("input", in))
return err
}
if count, _ := res.RowsAffected(); count == 0 {
return StatusError(codes.InvalidArgument, "Cannot unlink device ID when there are no other identifiers.", ErrRowsAffectedCount)
}
} else {
query := `UPDATE user_device SET id = $1 WHERE id = $2 AND user_id = $3`
res, err := tx.ExecContext(ctx, query, newDeviceID, oldDeviceID, userID)
if err != nil {
s.logger.Error("Could not update device ID.", zap.Error(err), zap.Any("input", in))
return err
}
if count, _ := res.RowsAffected(); count == 0 {
return StatusError(codes.InvalidArgument, "Device ID is already linked to a different user.", ErrRowsAffectedCount)
}
}
}
if len(statements) != 0 {
query := "UPDATE users SET update_time = now(), " + strings.Join(statements, ", ") + " WHERE id = $1"
_, err := tx.ExecContext(ctx, query, params...)
if err != nil {
s.logger.Error("Could not update user account.", zap.Error(err), zap.Any("input", in))
return err
}
}
if removeCustomId && removeEmail {
query := `UPDATE users SET custom_id = NULL, email = NULL, update_time = now()
WHERE id = $1
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
res, err := tx.ExecContext(ctx, query, userID)
if err != nil {
return err
}
if rowsAffected, _ := res.RowsAffected(); rowsAffected == 0 {
return StatusError(codes.InvalidArgument, "Cannot unlink both custom ID and email address when there are no other identifiers.", ErrRowsAffectedCount)
}
} else if removeCustomId {
query := `UPDATE users SET custom_id = NULL, update_time = now()
WHERE id = $1
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR email IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
res, err := tx.ExecContext(ctx, query, userID)
if err != nil {
return err
}
if rowsAffected, _ := res.RowsAffected(); rowsAffected == 0 {
return StatusError(codes.InvalidArgument, "Cannot unlink custom ID when there are no other identifiers.", ErrRowsAffectedCount)
}
} else if removeEmail {
query := `UPDATE users SET email = NULL, password = NULL, update_time = now()
WHERE id = $1
AND ((facebook_id IS NOT NULL
OR google_id IS NOT NULL
OR gamecenter_id IS NOT NULL
OR steam_id IS NOT NULL
OR custom_id IS NOT NULL)
OR
EXISTS (SELECT id FROM user_device WHERE user_id = $1 LIMIT 1))`
res, err := tx.ExecContext(ctx, query, userID)
if err != nil {
return err
}
if rowsAffected, _ := res.RowsAffected(); rowsAffected == 0 {
return StatusError(codes.InvalidArgument, "Cannot unlink email address when there are no other identifiers.", ErrRowsAffectedCount)
}
}
if len(in.DeviceIds) != 0 && len(statements) == 0 && !removeCustomId && !removeEmail {
// Ensure the user account update time is touched if the device IDs have changed but no other updates were applied to the core user record.
_, err := tx.ExecContext(ctx, "UPDATE users SET update_time = now() WHERE id = $1", userID)
if err != nil {
s.logger.Error("Could not unlink device ID.", zap.Error(err), zap.Any("input", in))
return err
}
}
return nil
}); err != nil {
if e, ok := err.(*statusError); ok {
// Errors such as unlinking the last profile or username in use.
return nil, e.Status()
}
s.logger.Error("Error updating user.", zap.Error(err))
return nil, status.Error(codes.Internal, "An error occurred while trying to update the user.")
}
return &empty.Empty{}, nil
}
func checkWalletFormat(wallet map[string]interface{}, path string) error {
for k, v := range wallet {
var currentPath string
if path == "" {
currentPath = k
} else {
currentPath = fmt.Sprintf("%v.%v", path, k)
}
if vm, ok := v.(map[string]interface{}); ok {
// Nested wallets are fine.
if err := checkWalletFormat(vm, currentPath); err != nil {
return err
}
} else if vf, ok := v.(float64); ok {
// If it's a value, check it's not negative.
if vf < 0 {
return errors.Errorf("Wallet rejected negative value at path '%v'.", currentPath)
}
} else {
// Not a nested wallet a value.
return errors.Errorf("Wallet value type at path '%v' must be map or float64.", currentPath)
}
}
return nil
}