-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
125 lines (111 loc) · 4.44 KB
/
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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"context"
nakamaCommands "github.com/challenge-league/nakama-go/commands"
log "github.com/micro/go-micro/v2/logger"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/heroiclabs/nakama-common/api"
"github.com/heroiclabs/nakama-common/runtime"
"github.com/jackc/pgx/pgtype"
"go.uber.org/zap"
)
func AccountByUsernameGetRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var accountGetRequest *nakamaCommands.AccountGetRequest
json.Unmarshal([]byte(payload), &accountGetRequest)
users, err := nk.UsersGetUsername(ctx, []string{accountGetRequest.Identifier})
if err != nil {
log.Error(err)
return "", err
}
if len(users) == 0 {
log.Error(err)
return "", fmt.Errorf("No users found with identifier %v", accountGetRequest.Identifier)
}
account, err := nk.AccountGetId(ctx, users[0].Id)
if err != nil {
log.Error(err)
return "", err
}
return string(Marshal(account)), nil
}
func AccountByCustomIDGetRPC(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
var accountGetRequest *nakamaCommands.AccountGetRequest
json.Unmarshal([]byte(payload), &accountGetRequest)
var displayName sql.NullString
var username sql.NullString
var avatarURL sql.NullString
var langTag sql.NullString
var location sql.NullString
var timezone sql.NullString
var metadata sql.NullString
var wallet sql.NullString
var email sql.NullString
var facebook sql.NullString
var facebookInstantGame sql.NullString
var google sql.NullString
var gamecenter sql.NullString
var steam sql.NullString
var userID uuid.UUID
var edgeCount int
var createTime pgtype.Timestamptz
var updateTime pgtype.Timestamptz
var verifyTime pgtype.Timestamptz
var disableTime pgtype.Timestamptz
var deviceIDs pgtype.VarcharArray
query := `
SELECT u.username, u.display_name, u.avatar_url, u.lang_tag, u.location, u.timezone, u.metadata, u.wallet,
u.email, u.facebook_id, u.facebook_instant_game_id, u.google_id, u.gamecenter_id, u.steam_id, u.id, u.edge_count,
u.create_time, u.update_time, u.verify_time, u.disable_time, array(select ud.id from user_device ud where u.id = ud.user_id)
FROM users u
WHERE u.custom_id = $1`
if err := db.QueryRowContext(ctx, query, accountGetRequest.Identifier).Scan(&username, &displayName, &avatarURL, &langTag, &location, &timezone, &metadata, &wallet, &email, &facebook, &facebookInstantGame, &google, &gamecenter, &steam, &userID, &edgeCount, &createTime, &updateTime, &verifyTime, &disableTime, &deviceIDs); err != nil {
if err == sql.ErrNoRows {
return "", fmt.Errorf("Account not found, customID: %v", accountGetRequest.Identifier)
}
logger.Error("Error retrieving user account.", zap.Error(err))
return "", err
}
devices := make([]*api.AccountDevice, 0, len(deviceIDs.Elements))
for _, deviceID := range deviceIDs.Elements {
devices = append(devices, &api.AccountDevice{Id: deviceID.String})
}
var verifyTimestamp *timestamp.Timestamp
if verifyTime.Status == pgtype.Present && verifyTime.Time.Unix() != 0 {
verifyTimestamp = ×tamp.Timestamp{Seconds: verifyTime.Time.Unix()}
}
var disableTimestamp *timestamp.Timestamp
if disableTime.Status == pgtype.Present && disableTime.Time.Unix() != 0 {
disableTimestamp = ×tamp.Timestamp{Seconds: disableTime.Time.Unix()}
}
return string(Marshal(&api.Account{
User: &api.User{
Id: userID.String(),
Username: username.String,
DisplayName: displayName.String,
AvatarUrl: avatarURL.String,
LangTag: langTag.String,
Location: location.String,
Timezone: timezone.String,
Metadata: metadata.String,
FacebookId: facebook.String,
FacebookInstantGameId: facebookInstantGame.String,
GoogleId: google.String,
GamecenterId: gamecenter.String,
SteamId: steam.String,
EdgeCount: int32(edgeCount),
CreateTime: ×tamp.Timestamp{Seconds: createTime.Time.Unix()},
UpdateTime: ×tamp.Timestamp{Seconds: updateTime.Time.Unix()},
Online: false,
},
Wallet: wallet.String,
Email: email.String,
Devices: devices,
CustomId: accountGetRequest.Identifier,
VerifyTime: verifyTimestamp,
DisableTime: disableTimestamp,
})), nil
}