Unverified Commit 87cbd7e4 authored by Fernando Takagi's avatar Fernando Takagi Committed by GitHub
Browse files

Add NotificationsDelete function to all runtimes. (#888)

parent ae21db7f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Chat Messages listing and deletion Console API endpoints.
- Add extended filtering options to devconsole Matches view.
- Add additional filter handling to devconsole Accounts view.
- Add NotificationsDelete function to all runtimes.

### Changed
- Improve runtime handling of non-persisted purchases and subscriptions.
+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.23.1-0.20220705161131-2c524612cd4a
	github.com/heroiclabs/nakama-common v1.23.1-0.20220730130319-ce1112c5c816
	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
Original line number Diff line number Diff line
@@ -290,10 +290,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 v1.23.1-0.20220529114835-7f3eb1c918ff h1:ulAC9PUbx+9hSwDBg+nHLNFa5uOZv0/lO93eecMRz0Q=
github.com/heroiclabs/nakama-common v1.23.1-0.20220529114835-7f3eb1c918ff/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
github.com/heroiclabs/nakama-common v1.23.1-0.20220705161131-2c524612cd4a h1:W8Od/R4nkjX14vnDWRMkc/9BFKEbFqwWQtvFl8H4tkY=
github.com/heroiclabs/nakama-common v1.23.1-0.20220705161131-2c524612cd4a/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
github.com/heroiclabs/nakama-common v1.23.1-0.20220730130319-ce1112c5c816 h1:cXiPPor1nu+a3G1ffVgJfSm7sJz89Qma3HZf+n2RdmQ=
github.com/heroiclabs/nakama-common v1.23.1-0.20220730130319-ce1112c5c816/go.mod h1:WF4YG46afwY3ibzsXnkt3zvhQ3tBY03IYeU7xSLr8HE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
+36 −0
Original line number Diff line number Diff line
@@ -1691,6 +1691,42 @@ func (n *RuntimeGoNakamaModule) NotificationSendAll(ctx context.Context, subject
	return NotificationSendAll(ctx, n.logger, n.db, n.tracker, n.router, not)
}

// @group notifications
// @summary Delete one or more in-app notifications.
// @param ctx(type=context.Context) The context object represents information about the server and requester.
// @param notifications(type=[]*runtime.NotificationsDelete) A list of notifications to be deleted.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeGoNakamaModule) NotificationsDelete(ctx context.Context, notifications []*runtime.NotificationDelete) error {
	ns := make(map[uuid.UUID][]string)

	for _, notification := range notifications {
		uid, err := uuid.FromString(notification.UserID)
		if err != nil {
			return errors.New("expects userID to be a valid UUID")
		}

		_, err = uuid.FromString(notification.NotificationID)
		if err != nil {
			return errors.New("expects notificationID to be a valid UUID")
		}

		no := ns[uid]
		if no == nil {
			no = make([]string, 0, 1)
		}
		no = append(no, notification.NotificationID)
		ns[uid] = no
	}

	for uid, notificationIDs := range ns {
		err := NotificationDelete(ctx, n.logger, n.db, uid, notificationIDs)
		if err != nil {
			return err
		}
	}
	return nil
}

// @group wallets
// @summary Update a user's wallet with the given changeset.
// @param ctx(type=context.Context) The context object represents information about the server and requester.
+68 −0
Original line number Diff line number Diff line
@@ -208,6 +208,7 @@ func (n *runtimeJavascriptNakamaModule) mappings(r *goja.Runtime) map[string]fun
		"notificationSend":                n.notificationSend(r),
		"notificationsSend":               n.notificationsSend(r),
		"notificationSendAll":             n.notificationSendAll(r),
		"notificationsDelete":             n.notificationsDelete(r),
		"walletUpdate":                    n.walletUpdate(r),
		"walletsUpdate":                   n.walletsUpdate(r),
		"walletLedgerUpdate":              n.walletLedgerUpdate(r),
@@ -3742,6 +3743,73 @@ func (n *runtimeJavascriptNakamaModule) notificationSendAll(r *goja.Runtime) fun
	}
}

// @group notifications
// @summary Delete one or more in-app notifications.
// @param notifications(type=any[]) A list of notifications to be deleted.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) notificationsDelete(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
	return func(f goja.FunctionCall) goja.Value {
		notificationsIn := f.Argument(0)
		if notificationsIn == goja.Undefined() {
			panic(r.NewTypeError("expects a valid set of notifications"))
		}

		notificationsSlice, ok := notificationsIn.Export().([]interface{})
		if !ok {
			panic(r.NewTypeError("expects notifications to be an array"))
		}

		notifications := make(map[uuid.UUID][]string)
		for _, notificationRaw := range notificationsSlice {
			notificationObj, ok := notificationRaw.(map[string]interface{})
			if !ok {
				panic(r.NewTypeError("expects notification to be an object"))
			}

			userID := uuid.Nil
			notificationIDStr := ""

			if _, ok := notificationObj["userId"]; ok {
				userIDStr, ok := notificationObj["userId"].(string)
				if !ok {
					panic(r.NewTypeError("expects 'userId' value to be a string"))
				}
				uid, err := uuid.FromString(userIDStr)
				if err != nil {
					panic(r.NewTypeError("expects 'userId' value to be a valid id"))
				}
				userID = uid
			}

			if _, ok := notificationObj["notificationId"]; ok {
				notificationIDStr, ok = notificationObj["notificationId"].(string)
				if !ok {
					panic(r.NewTypeError("expects 'notificationId' value to be a string"))
				}
				_, err := uuid.FromString(notificationIDStr)
				if err != nil {
					panic(r.NewTypeError("expects 'notificationId' value to be a valid id"))
				}
			}

			no := notifications[userID]
			if no == nil {
				no = make([]string, 0, 1)
			}
			no = append(no, notificationIDStr)
			notifications[userID] = no
		}

		for uid, notificationIDs := range notifications {
			if err := NotificationDelete(context.Background(), n.logger, n.db, uid, notificationIDs); err != nil {
				panic(r.NewGoError(fmt.Errorf("failed to delete notifications: %s", err.Error())))
			}
		}

		return goja.Undefined()
	}
}

// @group wallets
// @summary Update a user's wallet with the given changeset.
// @param userId(type=string) The ID of the user whose wallet to update.
Loading