Unverified Commit 97bad932 authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Expose GroupUsersBan function to runtimes (#746)

parent 9632e798
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
- Add GroupUsersBan function to all runtimes.

### Fixed
- Fix the registered function name for 'nk.channelIdBuild' in the JavaScript runtime.
- Better input validation for Steam link operations.
+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.7.0
	github.com/heroiclabs/nakama-common v1.21.0
	github.com/heroiclabs/nakama-common v1.21.1-0.20220117102903-bc0471af4fe1
	github.com/jackc/pgconn v1.10.0
	github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
	github.com/jackc/pgtype v1.8.1
+2 −4
+38 −0
Original line number Diff line number Diff line
@@ -2920,6 +2920,44 @@ func (n *RuntimeGoNakamaModule) GroupUsersAdd(ctx context.Context, callerID, gro
	return AddGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

// @summary Ban users from a group.
// @param ctx(context.Context) The context object represents information about the server and requester.
// @param groupId(string) The ID of the group to ban users from.
// @param userIds([]string) Table array of user IDs to ban from this group.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeGoNakamaModule) GroupUsersBan(ctx context.Context, callerID, groupID string, userIDs []string) error {
	caller := uuid.Nil
	if callerID != "" {
		var err error
		if caller, err = uuid.FromString(callerID); err != nil {
			return errors.New("expects caller ID to be a valid identifier")
		}
	}

	group, err := uuid.FromString(groupID)
	if err != nil {
		return errors.New("expects group ID to be a valid identifier")
	}

	if len(userIDs) == 0 {
		return nil
	}

	users := make([]uuid.UUID, 0, len(userIDs))
	for _, userID := range userIDs {
		uid, err := uuid.FromString(userID)
		if err != nil {
			return errors.New("expects each user ID to be a valid identifier")
		}
		if uid == uuid.Nil {
			return errors.New("cannot ban the root user")
		}
		users = append(users, uid)
	}

	return BanGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

// @summary Kick users from a group.
// @param ctx(context.Context) The context object represents information about the server and requester.
// @param groupId(string) The ID of the group to kick users from.
+65 −3
Original line number Diff line number Diff line
@@ -241,6 +241,7 @@ func (n *runtimeJavascriptNakamaModule) mappings(r *goja.Runtime) map[string]fun
		"groupUserJoin":                   n.groupUserJoin(r),
		"groupUserLeave":                  n.groupUserLeave(r),
		"groupUsersAdd":                   n.groupUsersAdd(r),
		"groupUsersBan":                   n.groupUsersBan(r),
		"groupUsersPromote":               n.groupUsersPromote(r),
		"groupUsersDemote":                n.groupUsersDemote(r),
		"groupsList":                      n.groupsList(r),
@@ -6379,7 +6380,7 @@ func (n *runtimeJavascriptNakamaModule) groupUserJoin(r *goja.Runtime) func(goja
		}

		if err := JoinGroup(context.Background(), n.logger, n.db, n.router, groupID, userID, username); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to join a group: %v", err.Error())))
			panic(r.NewGoError(fmt.Errorf("error while trying to join group: %v", err.Error())))
		}

		return goja.Undefined()
@@ -6417,7 +6418,7 @@ func (n *runtimeJavascriptNakamaModule) groupUserLeave(r *goja.Runtime) func(goj
		}

		if err := LeaveGroup(context.Background(), n.logger, n.db, n.router, groupID, userID, username); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to leave a group: %v", err.Error())))
			panic(r.NewGoError(fmt.Errorf("error while trying to leave group: %v", err.Error())))
		}

		return goja.Undefined()
@@ -6478,7 +6479,68 @@ func (n *runtimeJavascriptNakamaModule) groupUsersAdd(r *goja.Runtime) func(goja
		}

		if err := AddGroupUsers(context.Background(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to add users into a group: %v", err.Error())))
			panic(r.NewGoError(fmt.Errorf("error while trying to add users into group: %v", err.Error())))
		}

		return goja.Undefined()
	}
}

// @summary Ban users from a group.
// @param groupId(string) The ID of the group to ban users from.
// @param userIds(string[]) Table array of user IDs to ban from this group.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) groupUsersBan(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
		groupIDString := getJsString(r, f.Argument(0))
		if groupIDString == "" {
			panic(r.NewTypeError("expects a group ID string"))
		}
		groupID, err := uuid.FromString(groupIDString)
		if err != nil {
			panic(r.NewTypeError("expects group ID to be a valid identifier"))
		}

		users := f.Argument(1)
		if goja.IsUndefined(users) || goja.IsNull(users) {
			panic(r.NewTypeError("expects an array of user ids"))
		}
		usersSlice, ok := users.Export().([]interface{})
		if !ok {
			panic(r.NewTypeError("expects an array of user ids"))
		}

		userIDs := make([]uuid.UUID, 0, len(usersSlice))
		for _, id := range usersSlice {
			idStr, ok := id.(string)
			if !ok {
				panic(r.NewTypeError("expects user id to be valid identifier"))
			}
			userID, err := uuid.FromString(idStr)
			if err != nil {
				panic(r.NewTypeError("expects user id to be valid identifier"))
			}
			if userID == uuid.Nil {
				panic(r.NewTypeError("cannot ban the root user"))
			}
			userIDs = append(userIDs, userID)
		}
		if len(userIDs) == 0 {
			return goja.Undefined()
		}

		callerID := uuid.Nil
		if !goja.IsUndefined(f.Argument(2)) && !goja.IsNull(f.Argument(2)) {
			callerIdStr := getJsString(r, f.Argument(2))
			cid, err := uuid.FromString(callerIdStr)
			if err != nil {
				panic(r.NewTypeError("expects caller id to be valid identifier"))
			}
			callerID = cid
		}

		if err := BanGroupUsers(context.Background(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to ban users from group: %v", err.Error())))
		}

		return goja.Undefined()
Loading