Unverified Commit 5980b7a7 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Add Nakama API to get users by facebook ID (#551)

Resolves #548
parent fe2763ac
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com) and this project uses [semantic versioning](http://semver.org).

## [Unreleased]
### Added
- Added Nakama API to get users by facebook ID.

### Fixed
- Fix an issue that prevented the JavaScript runtime hooks to be invoked correctly.
- Fix the delete button not working in the console leaderboard listing.
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ require (
	github.com/gorilla/mux v1.7.4
	github.com/gorilla/websocket v1.4.2
	github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1
	github.com/heroiclabs/nakama-common v1.12.0
	github.com/heroiclabs/nakama-common v1.12.1-0.20210215124839-bf495fa30713
	github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
	github.com/jackc/pgx v3.5.0+incompatible
	github.com/jmhodges/levigo v1.0.0 // indirect
+2 −4
+3 −3
Original line number Diff line number Diff line
@@ -415,8 +415,8 @@ func (n *RuntimeGoNakamaModule) AccountExportId(ctx context.Context, userID stri
	return exportString, nil
}

func (n *RuntimeGoNakamaModule) UsersGetId(ctx context.Context, userIDs []string) ([]*api.User, error) {
	if len(userIDs) == 0 {
func (n *RuntimeGoNakamaModule) UsersGetId(ctx context.Context, userIDs []string, facebookIDs []string) ([]*api.User, error) {
	if len(userIDs) == 0 && len(facebookIDs) == 0 {
		return make([]*api.User, 0), nil
	}

@@ -426,7 +426,7 @@ func (n *RuntimeGoNakamaModule) UsersGetId(ctx context.Context, userIDs []string
		}
	}

	users, err := GetUsers(ctx, n.logger, n.db, n.tracker, userIDs, nil, nil)
	users, err := GetUsers(ctx, n.logger, n.db, n.tracker, userIDs, nil, facebookIDs)
	if err != nil {
		return nil, err
	}
+32 −13
Original line number Diff line number Diff line
@@ -1470,29 +1470,48 @@ func (n *runtimeJavascriptNakamaModule) accountExportId(r *goja.Runtime) func(go

func (n *runtimeJavascriptNakamaModule) usersGetId(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
		var input []interface{}
		if f.Argument(0) == goja.Undefined() {
			panic(r.NewTypeError("expects list of user ids"))
		} else {
		var userIds []string
		if f.Argument(0) != goja.Undefined() && f.Argument(0) != goja.Null() {
			var ok bool
			input, ok = f.Argument(0).Export().([]interface{})
			userIdsIn, ok := f.Argument(0).Export().([]interface{})
			if !ok {
				panic(r.NewTypeError("Invalid argument - user ids must be an array."))
			}
		}

		userIDs := make([]string, 0, len(input))
		for _, userID := range input {
			uIds := make([]string, 0, len(userIdsIn))
			for _, userID := range userIdsIn {
				id, ok := userID.(string)
				if !ok {
					panic(r.NewTypeError(fmt.Sprintf("invalid user id: %v - must be a string", userID)))
				} else if _, err := uuid.FromString(id); err != nil {
					panic(r.NewTypeError(fmt.Sprintf("invalid user id: %v", userID)))
				}
			userIDs = append(userIDs, id)
				uIds = append(uIds, id)
			}
			userIds = uIds
		}

		var facebookIds []string
		if f.Argument(1) != goja.Undefined() && f.Argument(1) != goja.Null() {
			facebookIdsIn, ok := f.Argument(1).Export().([]interface{})
			if !ok {
				panic(r.NewTypeError("Invalid argument - facebook ids must be an array."))
			}
			fIds := make([]string, 0, len(facebookIdsIn))
			for _, fIdIn := range facebookIdsIn {
				fId, ok := fIdIn.(string)
				if !ok {
					panic(r.NewTypeError("Invalid argument - facebook id must be a string"))
				}
				fIds = append(fIds, fId)
			}
			facebookIds = fIds
		}

		if userIds == nil && facebookIds == nil {
			return r.ToValue(make([]string, 0, 0))
		}

		users, err := GetUsers(context.Background(), n.logger, n.db, n.tracker, userIDs, nil, nil)
		users, err := GetUsers(context.Background(), n.logger, n.db, n.tracker, userIds, nil, facebookIds)
		if err != nil {
			panic(r.NewGoError(fmt.Errorf("failed to get users: %s", err.Error())))
		}
Loading