Commit 2e45edf8 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Add runtime function to retrieve a random set of users.

parent 82d42407
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
## [Unreleased]
### Added
- Handle thrown JS runtime custom exceptions containing a message and a grpc code to be returned in the server response.
- Add runtime function to retrieve a random set of users.

### Changed
- Size limit for status messages increased from 128 to 2048 characters.
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ require (
	github.com/gorilla/mux v1.8.0
	github.com/gorilla/websocket v1.4.2
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.3.0
	github.com/heroiclabs/nakama-common v1.15.0
	github.com/heroiclabs/nakama-common v0.0.0-20210809163116-633338b7c44b
	github.com/jackc/pgconn v1.8.1
	github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
	github.com/jackc/pgtype v1.7.0
+2 −0
Original line number Diff line number Diff line
@@ -310,6 +310,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/heroiclabs/nakama-common v0.0.0-20210809163116-633338b7c44b h1:Nn0UeBw6/bDTidJ75MwjfRBzu47iGklqtnPVZrY2tgY=
github.com/heroiclabs/nakama-common v0.0.0-20210809163116-633338b7c44b/go.mod h1:jzIGV5bI45ALRQFzHPkJn4Z0tV+xhtho1+pZhOXVAsk=
github.com/heroiclabs/nakama-common v1.14.1-0.20210707135714-ed381a338271 h1:iZt1PkdjXKYg7bSFw2X5oEUf5m7NFuku7nTJZibGNOY=
github.com/heroiclabs/nakama-common v1.14.1-0.20210707135714-ed381a338271/go.mod h1:jzIGV5bI45ALRQFzHPkJn4Z0tV+xhtho1+pZhOXVAsk=
github.com/heroiclabs/nakama-common v1.14.1-0.20210707144747-9d4fc7ac4406 h1:lBA9SThV3uTwtnq9pD+0eEPJpUERjP+/WlFBmgaYI1s=
+68 −0
Original line number Diff line number Diff line
@@ -103,6 +103,74 @@ WHERE`
	return users, nil
}

func GetRandomUsers(ctx context.Context, logger *zap.Logger, db *sql.DB, tracker Tracker, 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(tracker, 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.
		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.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(tracker, 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()
	}

	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 {
+12 −0
Original line number Diff line number Diff line
@@ -459,6 +459,18 @@ func (n *RuntimeGoNakamaModule) UsersGetUsername(ctx context.Context, usernames
	return users.Users, nil
}

func (n *RuntimeGoNakamaModule) UsersGetRandom(ctx context.Context, count int) ([]*api.User, error) {
	if count == 0 {
		return make([]*api.User, 0), nil
	}

	if count < 0 || count > 1000 {
		return nil, errors.New("count must be 0-1000")
	}

	return GetRandomUsers(ctx, n.logger, n.db, n.tracker, count)
}

func (n *RuntimeGoNakamaModule) UsersBanId(ctx context.Context, userIDs []string) error {
	if len(userIDs) == 0 {
		return nil
Loading