forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_user.go
290 lines (255 loc) · 8.3 KB
/
core_user.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
// 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"
"database/sql"
"fmt"
"github.com/gofrs/uuid/v5"
"github.com/heroiclabs/nakama-common/api"
"github.com/jackc/pgx/v5/pgtype"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
)
func GetUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, statusRegistry StatusRegistry, ids, usernames, fbIDs []string) (*api.Users, error) {
query := `
SELECT id, username, display_name, avatar_url, lang_tag, location, timezone, metadata,
apple_id, facebook_id, facebook_instant_game_id, google_id, gamecenter_id, steam_id, edge_count, create_time, update_time
FROM users
WHERE`
params := make([]any, 0)
counter := 1
useSQLOr := false
if len(ids) > 0 {
params = append(params, ids)
query = query + fmt.Sprintf(" id = ANY($%d)", counter)
counter++
useSQLOr = true
}
if len(usernames) > 0 {
params = append(params, usernames)
if useSQLOr {
query = query + " OR"
}
query = query + fmt.Sprintf(" username = ANY($%d::text[])", counter)
counter++
useSQLOr = true
}
if len(fbIDs) > 0 {
params = append(params, fbIDs)
if useSQLOr {
query = query + " OR"
}
query = query + fmt.Sprintf(" facebook_id = ANY($%d::text[])", counter)
}
rows, err := db.QueryContext(ctx, query, params...)
if err != nil {
logger.Error("Error retrieving user accounts.", zap.Error(err), zap.Strings("user_ids", ids), zap.Strings("usernames", usernames), zap.Strings("facebook_ids", fbIDs))
return nil, err
}
users := &api.Users{Users: make([]*api.User, 0)}
for rows.Next() {
user, err := convertUser(rows)
if err != nil {
_ = rows.Close()
logger.Error("Error retrieving user accounts.", zap.Error(err), zap.Strings("user_ids", ids), zap.Strings("usernames", usernames), zap.Strings("facebook_ids", fbIDs))
return nil, err
}
users.Users = append(users.Users, user)
}
_ = rows.Close()
if err = rows.Err(); err != nil {
logger.Error("Error retrieving user accounts.", zap.Error(err), zap.Strings("user_ids", ids), zap.Strings("usernames", usernames), zap.Strings("facebook_ids", fbIDs))
return nil, err
}
statusRegistry.FillOnlineUsers(users.Users)
return users, nil
}
func GetRandomUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, statusRegistry StatusRegistry, count int) ([]*api.User, error) {
if count == 0 {
return []*api.User{}, nil
}
query := `
SELECT id, username, display_name, avatar_url, lang_tag, location, timezone, metadata,
apple_id, facebook_id, facebook_instant_game_id, google_id, gamecenter_id, steam_id, edge_count, create_time, update_time
FROM users
WHERE id > $1
LIMIT $2`
rows, err := db.QueryContext(ctx, query, uuid.Must(uuid.NewV4()).String(), count)
if err != nil {
logger.Error("Error retrieving random user accounts.", zap.Error(err))
return nil, err
}
users := make([]*api.User, 0, count)
for rows.Next() {
user, err := convertUser(rows)
if err != nil {
_ = rows.Close()
logger.Error("Error retrieving random user accounts.", zap.Error(err))
return nil, err
}
users = append(users, user)
}
_ = rows.Close()
if len(users) < count {
// Need more users.
rows, err = db.QueryContext(ctx, query, uuid.Nil.String(), count)
if err != nil {
logger.Error("Error retrieving random user accounts.", zap.Error(err))
return nil, err
}
for rows.Next() {
user, err := convertUser(rows)
if err != nil {
_ = rows.Close()
logger.Error("Error retrieving random user accounts.", zap.Error(err))
return nil, err
}
var found bool
for _, existing := range users {
if existing.Id == user.Id {
found = true
break
}
}
if !found {
users = append(users, user)
}
if len(users) >= count {
break
}
}
_ = rows.Close()
}
statusRegistry.FillOnlineUsers(users)
return users, nil
}
func DeleteUser(ctx context.Context, tx *sql.Tx, userID uuid.UUID) (int64, error) {
res, err := tx.ExecContext(ctx, "DELETE FROM users WHERE id = $1", userID)
if err != nil {
return 0, err
}
return res.RowsAffected()
}
func BanUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, config Config, sessionCache SessionCache, sessionRegistry SessionRegistry, tracker Tracker, ids []uuid.UUID) error {
query := "UPDATE users SET disable_time = now() WHERE id = ANY($1::UUID[])"
_, err := db.ExecContext(ctx, query, ids)
if err != nil {
logger.Error("Error banning user accounts.", zap.Error(err), zap.Any("ids", ids))
return err
}
sessionCache.Ban(ids)
for _, id := range ids {
// Disconnect.
for _, presence := range tracker.ListPresenceIDByStream(PresenceStream{Mode: StreamModeNotifications, Subject: id}) {
if err = sessionRegistry.Disconnect(ctx, presence.SessionID, true); err != nil {
return err
}
}
}
return nil
}
func UnbanUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, sessionCache SessionCache, ids []uuid.UUID) error {
query := "UPDATE users SET disable_time = '1970-01-01 00:00:00 UTC' WHERE id = ANY($1::UUID[])"
_, err := db.ExecContext(ctx, query, ids)
if err != nil {
logger.Error("Error unbanning user accounts.", zap.Error(err), zap.Any("ids", ids))
return err
}
sessionCache.Unban(ids)
return nil
}
func UserExistsAndDoesNotBlock(ctx context.Context, db *sql.DB, checkUserID, blocksUserID uuid.UUID) (bool, error) {
var count int
err := db.QueryRowContext(ctx, `
SELECT COUNT(id) FROM users
WHERE id = $1::UUID AND NOT EXISTS (
SELECT state FROM user_edge
WHERE source_id = $1::UUID AND destination_id = $2::UUID AND state = 3
)
`, checkUserID, blocksUserID).Scan(&count)
return count != 0, err
}
func convertUser(rows *sql.Rows) (*api.User, error) {
var id string
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 []byte
var apple sql.NullString
var facebook sql.NullString
var facebookInstantGame sql.NullString
var google sql.NullString
var gamecenter sql.NullString
var steam sql.NullString
var edgeCount int
var createTime pgtype.Timestamptz
var updateTime pgtype.Timestamptz
err := rows.Scan(&id, &username, &displayName, &avatarURL, &langTag, &location, &timezone, &metadata,
&apple, &facebook, &facebookInstantGame, &google, &gamecenter, &steam, &edgeCount, &createTime, &updateTime)
if err != nil {
return nil, err
}
userID := uuid.FromStringOrNil(id)
return &api.User{
Id: userID.String(),
Username: username.String,
DisplayName: displayName.String,
AvatarUrl: avatarURL.String,
LangTag: langTag.String,
Location: location.String,
Timezone: timezone.String,
Metadata: string(metadata),
AppleId: apple.String,
FacebookId: facebook.String,
FacebookInstantGameId: facebookInstantGame.String,
GoogleId: google.String,
GamecenterId: gamecenter.String,
SteamId: steam.String,
EdgeCount: int32(edgeCount),
CreateTime: ×tamppb.Timestamp{Seconds: createTime.Time.Unix()},
UpdateTime: ×tamppb.Timestamp{Seconds: updateTime.Time.Unix()},
// Online filled later.
}, nil
}
func fetchUserID(ctx context.Context, db *sql.DB, usernames []string) ([]string, error) {
ids := make([]string, 0, len(usernames))
if len(usernames) == 0 {
return ids, nil
}
query := "SELECT id FROM users WHERE username = ANY($1::text[])"
rows, err := db.QueryContext(ctx, query, usernames)
if err != nil {
if err == sql.ErrNoRows {
return ids, nil
}
return nil, err
}
defer rows.Close()
for rows.Next() {
var id string
err := rows.Scan(&id)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
if err = rows.Err(); err != nil {
return nil, err
}
return ids, nil
}