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

Add caller ID param to group management operations (#600)

parent cc786c59
Loading
Loading
Loading
Loading
+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.13.2-0.20210423102601-86fe2a6b43db
	github.com/heroiclabs/nakama-common v1.13.2-0.20210430100819-3c41e7f99ed1
	github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
	github.com/jackc/pgx v3.5.0+incompatible
	github.com/m3db/prometheus_client_golang v0.8.1 // indirect
+2 −2
+40 −8
Original line number Diff line number Diff line
@@ -2155,7 +2155,15 @@ func (n *RuntimeGoNakamaModule) GroupUserLeave(ctx context.Context, groupID, use
	return LeaveGroup(ctx, n.logger, n.db, n.router, group, user, username)
}

func (n *RuntimeGoNakamaModule) GroupUsersAdd(ctx context.Context, groupID string, userIDs []string) error {
func (n *RuntimeGoNakamaModule) GroupUsersAdd(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")
@@ -2177,10 +2185,18 @@ func (n *RuntimeGoNakamaModule) GroupUsersAdd(ctx context.Context, groupID strin
		users = append(users, uid)
	}

	return AddGroupUsers(ctx, n.logger, n.db, n.router, uuid.Nil, group, users)
	return AddGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

func (n *RuntimeGoNakamaModule) GroupUsersKick(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")
		}
	}

func (n *RuntimeGoNakamaModule) GroupUsersKick(ctx context.Context, groupID string, userIDs []string) error {
	group, err := uuid.FromString(groupID)
	if err != nil {
		return errors.New("expects group ID to be a valid identifier")
@@ -2202,10 +2218,18 @@ func (n *RuntimeGoNakamaModule) GroupUsersKick(ctx context.Context, groupID stri
		users = append(users, uid)
	}

	return KickGroupUsers(ctx, n.logger, n.db, n.router, uuid.Nil, group, users)
	return KickGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

func (n *RuntimeGoNakamaModule) GroupUsersPromote(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")
		}
	}

func (n *RuntimeGoNakamaModule) GroupUsersPromote(ctx context.Context, groupID string, userIDs []string) error {
	group, err := uuid.FromString(groupID)
	if err != nil {
		return errors.New("expects group ID to be a valid identifier")
@@ -2227,10 +2251,18 @@ func (n *RuntimeGoNakamaModule) GroupUsersPromote(ctx context.Context, groupID s
		users = append(users, uid)
	}

	return PromoteGroupUsers(ctx, n.logger, n.db, n.router, uuid.Nil, group, users)
	return PromoteGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

func (n *RuntimeGoNakamaModule) GroupUsersDemote(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")
		}
	}

func (n *RuntimeGoNakamaModule) GroupUsersDemote(ctx context.Context, groupID string, userIDs []string) error {
	group, err := uuid.FromString(groupID)
	if err != nil {
		return errors.New("expects group ID to be a valid identifier")
@@ -2252,7 +2284,7 @@ func (n *RuntimeGoNakamaModule) GroupUsersDemote(ctx context.Context, groupID st
		users = append(users, uid)
	}

	return DemoteGroupUsers(ctx, n.logger, n.db, n.router, uuid.Nil, group, users)
	return DemoteGroupUsers(ctx, n.logger, n.db, n.router, caller, group, users)
}

func (n *RuntimeGoNakamaModule) GroupUsersList(ctx context.Context, id string, limit int, state *int, cursor string) ([]*api.GroupUserList_GroupUser, string, error) {
+44 −4
Original line number Diff line number Diff line
@@ -5289,7 +5289,17 @@ func (n *runtimeJavascriptNakamaModule) groupUsersKick(r *goja.Runtime) func(goj
			userIDs = append(userIDs, userID)
		}

		if err := KickGroupUsers(context.Background(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
		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 := KickGroupUsers(context.Background(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to kick users from a group: %v", err.Error())))
		}

@@ -5679,7 +5689,17 @@ func (n *runtimeJavascriptNakamaModule) groupUsersAdd(r *goja.Runtime) func(goja
			return goja.Undefined()
		}

		if err := AddGroupUsers(context.Background(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
		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 := 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())))
		}

@@ -5726,7 +5746,17 @@ func (n *runtimeJavascriptNakamaModule) groupUsersPromote(r *goja.Runtime) func(
			return goja.Undefined()
		}

		if err := PromoteGroupUsers(context.Background(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
		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 := PromoteGroupUsers(context.Background(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to promote users in a group: %v", err.Error())))
		}

@@ -5773,7 +5803,17 @@ func (n *runtimeJavascriptNakamaModule) groupUsersDemote(r *goja.Runtime) func(g
			return goja.Undefined()
		}

		if err := DemoteGroupUsers(context.Background(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
		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 := DemoteGroupUsers(context.Background(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
			panic(r.NewGoError(fmt.Errorf("error while trying to demote users in a group: %v", err.Error())))
		}

+44 −4
Original line number Diff line number Diff line
@@ -6728,7 +6728,17 @@ func (n *RuntimeLuaNakamaModule) groupUsersAdd(l *lua.LState) int {
		return 0
	}

	if err := AddGroupUsers(l.Context(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
	callerID := uuid.Nil
	callerIDStr := l.OptString(3, "")
	if callerIDStr != "" {
		callerID, err = uuid.FromString(callerIDStr)
		if err != nil {
			l.ArgError(1, "expects caller ID to be a valid identifier")
			return 0
		}
	}

	if err := AddGroupUsers(l.Context(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
		l.RaiseError("error while trying to add users into a group: %v", err.Error())
	}
	return 0
@@ -6776,7 +6786,17 @@ func (n *RuntimeLuaNakamaModule) groupUsersPromote(l *lua.LState) int {
		return 0
	}

	if err := PromoteGroupUsers(l.Context(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
	callerID := uuid.Nil
	callerIDStr := l.OptString(3, "")
	if callerIDStr != "" {
		callerID, err = uuid.FromString(callerIDStr)
		if err != nil {
			l.ArgError(1, "expects caller ID to be a valid identifier")
			return 0
		}
	}

	if err := PromoteGroupUsers(l.Context(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
		l.RaiseError("error while trying to promote users in a group: %v", err.Error())
	}
	return 0
@@ -6824,7 +6844,17 @@ func (n *RuntimeLuaNakamaModule) groupUsersDemote(l *lua.LState) int {
		return 0
	}

	if err := DemoteGroupUsers(l.Context(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
	callerID := uuid.Nil
	callerIDStr := l.OptString(3, "")
	if callerIDStr != "" {
		callerID, err = uuid.FromString(callerIDStr)
		if err != nil {
			l.ArgError(1, "expects caller ID to be a valid identifier")
			return 0
		}
	}

	if err := DemoteGroupUsers(l.Context(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
		l.RaiseError("error while trying to demote users in a group: %v", err.Error())
	}
	return 0
@@ -6872,7 +6902,17 @@ func (n *RuntimeLuaNakamaModule) groupUsersKick(l *lua.LState) int {
		return 0
	}

	if err := KickGroupUsers(l.Context(), n.logger, n.db, n.router, uuid.Nil, groupID, userIDs); err != nil {
	callerID := uuid.Nil
	callerIDStr := l.OptString(3, "")
	if callerIDStr != "" {
		callerID, err = uuid.FromString(callerIDStr)
		if err != nil {
			l.ArgError(1, "expects caller ID to be a valid identifier")
			return 0
		}
	}

	if err := KickGroupUsers(l.Context(), n.logger, n.db, n.router, callerID, groupID, userIDs); err != nil {
		l.RaiseError("error while trying to kick users from a group: %v", err.Error())
	}
	return 0
Loading