From 527a659a1b3b624403b2ba51ef5ee1bacc2ca7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 15 Sep 2024 21:07:46 +0200 Subject: [PATCH] Refactor models to use single struct per entity --- scnserver/api/handler/apiChannel.go | 44 ++--- scnserver/api/handler/apiClient.go | 22 +-- scnserver/api/handler/apiKeyToken.go | 26 ++- scnserver/api/handler/apiMessage.go | 26 ++- scnserver/api/handler/apiPreview.go | 10 +- scnserver/api/handler/apiSubscription.go | 32 ++- scnserver/api/handler/apiUser.go | 14 +- scnserver/db/impl/logs/database.go | 4 +- scnserver/db/impl/primary/channels.go | 99 ++-------- scnserver/db/impl/primary/clients.go | 33 +--- scnserver/db/impl/primary/database.go | 4 +- scnserver/db/impl/primary/deliveries.go | 30 +-- scnserver/db/impl/primary/keytokens.go | 69 ++----- scnserver/db/impl/primary/messages.go | 46 +---- scnserver/db/impl/primary/subscriptions.go | 52 +---- scnserver/db/impl/primary/users.go | 23 +-- scnserver/db/impl/requests/database.go | 4 +- scnserver/db/impl/requests/requestlogs.go | 16 +- scnserver/go.mod | 32 +-- scnserver/go.sum | 74 ++++--- scnserver/logic/request.go | 6 +- scnserver/models/channel.go | 168 +++------------- scnserver/models/client.go | 88 +-------- scnserver/models/delivery.go | 100 +--------- scnserver/models/duration.go | 35 ++++ scnserver/models/enums_gen.go | 2 +- scnserver/models/ids_gen.go | 2 +- scnserver/models/keytoken.go | 162 +++++----------- scnserver/models/message.go | 148 +++----------- scnserver/models/requestlog.go | 207 +++----------------- scnserver/models/subscription.go | 82 +------- scnserver/models/time.go | 65 +++++++ scnserver/models/user.go | 167 +++++----------- scnserver/models/utils.go | 8 + scnserver/swagger/swagger.json | 215 ++++++++++----------- scnserver/swagger/swagger.yaml | 174 ++++++++--------- scnserver/test/keytoken_test.go | 2 +- scnserver/test/response_test.go | 53 +++++ scnserver/test/send_test.go | 2 +- scnserver/test/util/requests.go | 4 + scnserver/test/util/structure.go | 4 +- 41 files changed, 778 insertions(+), 1576 deletions(-) create mode 100644 scnserver/models/duration.go create mode 100644 scnserver/models/time.go diff --git a/scnserver/api/handler/apiChannel.go b/scnserver/api/handler/apiChannel.go index f3950c0..b06a6bd 100644 --- a/scnserver/api/handler/apiChannel.go +++ b/scnserver/api/handler/apiChannel.go @@ -46,7 +46,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { Selector *string `json:"selector" form:"selector" enums:"owned,subscribed_any,all_any,subscribed,all"` } type response struct { - Channels []models.ChannelWithSubscriptionJSON `json:"channels"` + Channels []models.ChannelWithSubscription `json:"channels"` } var u uri @@ -65,15 +65,13 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { sel := strings.ToLower(langext.Coalesce(q.Selector, "owned")) - var res []models.ChannelWithSubscriptionJSON - if sel == "owned" { channels, err := h.database.ListChannelsByOwner(ctx, u.UserID, u.UserID) if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err) } - res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(true) }) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, response{Channels: channels}, "INCLUDE_KEY")) } else if sel == "subscribed_any" { @@ -81,7 +79,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err) } - res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels})) } else if sel == "all_any" { @@ -89,7 +87,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err) } - res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels})) } else if sel == "subscribed" { @@ -97,7 +95,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err) } - res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels})) } else if sel == "all" { @@ -105,7 +103,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channels", err) } - res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(false) }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: channels})) } else { @@ -113,8 +111,6 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { } - return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: res})) - }) } @@ -127,7 +123,7 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param cid path string true "ChannelID" // -// @Success 200 {object} models.ChannelWithSubscriptionJSON +// @Success 200 {object} models.ChannelWithSubscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "channel not found" @@ -161,7 +157,7 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err) } - return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel, "INCLUDE_KEY")) }) } @@ -175,7 +171,7 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param post_body body handler.CreateChannel.body false " " // -// @Success 200 {object} models.ChannelWithSubscriptionJSON +// @Success 200 {object} models.ChannelWithSubscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 409 {object} ginresp.apiError "channel already exists" @@ -258,11 +254,11 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err) } - return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)), "INCLUDE_KEY")) } else { - return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel.WithSubscription(nil), "INCLUDE_KEY")) } @@ -282,7 +278,7 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse { // @Param send_key body string false "Send `true` to create a new send_key" // @Param display_name body string false "Change the cahnnel display-name (only chnages to lowercase/uppercase are allowed - internal_name must stay the same)" // -// @Success 200 {object} models.ChannelWithSubscriptionJSON +// @Success 200 {object} models.ChannelWithSubscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "channel not found" @@ -381,7 +377,7 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err) } - return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, channel, "INCLUDE_KEY")) }) } @@ -419,9 +415,9 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo Trimmed *bool `json:"trimmed" form:"trimmed"` } type response struct { - Messages []models.MessageJSON `json:"messages"` - NextPageToken string `json:"next_page_token"` - PageSize int `json:"page_size"` + Messages []models.Message `json:"messages"` + NextPageToken string `json:"next_page_token"` + PageSize int `json:"page_size"` } var u uri @@ -466,14 +462,12 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err) } - var res []models.MessageJSON if trimmed { - res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() }) + res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.Trim() }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) } else { - res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: messages, NextPageToken: npt.Token(), PageSize: pageSize})) } - return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) - }) } diff --git a/scnserver/api/handler/apiClient.go b/scnserver/api/handler/apiClient.go index 94f9414..bbf8449 100644 --- a/scnserver/api/handler/apiClient.go +++ b/scnserver/api/handler/apiClient.go @@ -31,7 +31,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse { UserID models.UserID `uri:"uid" binding:"entityid"` } type response struct { - Clients []models.ClientJSON `json:"clients"` + Clients []models.Client `json:"clients"` } var u uri @@ -52,9 +52,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err) } - res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() }) - - return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res})) + return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: clients})) }) } @@ -68,7 +66,7 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param cid path string true "ClientID" // -// @Success 200 {object} models.ClientJSON +// @Success 200 {object} models.Client // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "client not found" @@ -102,7 +100,7 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, client.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, client)) }) } @@ -117,7 +115,7 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param post_body body handler.AddClient.body false " " // -// @Success 200 {object} models.ClientJSON +// @Success 200 {object} models.Client // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 500 {object} ginresp.apiError "internal server error" @@ -164,7 +162,7 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err) } - return finishSuccess(ginext.JSON(http.StatusOK, client.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, client)) }) } @@ -178,7 +176,7 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param cid path string true "ClientID" // -// @Success 200 {object} models.ClientJSON +// @Success 200 {object} models.Client // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "client not found" @@ -217,7 +215,7 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, client.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, client)) }) } @@ -235,7 +233,7 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse { // @Param clientname body string false "Change the clientname (send an empty string to clear it)" // @Param pro_token body string false "Send a verification of premium purchase" // -// @Success 200 {object} models.ClientJSON +// @Success 200 {object} models.Client // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "client is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "client not found" @@ -322,7 +320,7 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, client.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, client)) }) } diff --git a/scnserver/api/handler/apiKeyToken.go b/scnserver/api/handler/apiKeyToken.go index 69ea96f..c4016b0 100644 --- a/scnserver/api/handler/apiKeyToken.go +++ b/scnserver/api/handler/apiKeyToken.go @@ -33,7 +33,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse { UserID models.UserID `uri:"uid" binding:"entityid"` } type response struct { - Keys []models.KeyTokenJSON `json:"keys"` + Keys []models.KeyToken `json:"keys"` } var u uri @@ -54,9 +54,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err) } - res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() }) - - return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: res})) + return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: toks})) }) } @@ -71,7 +69,7 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param kid path string true "TokenKeyID" // -// @Success 200 {object} models.KeyTokenWithTokenJSON +// @Success 200 {object} models.KeyToken // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -109,7 +107,7 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, keytoken, "INCLUDE_TOKEN")) }) } @@ -124,7 +122,7 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons // @Param uid path string true "UserID" // @Param kid path string true "TokenKeyID" // -// @Success 200 {object} models.KeyTokenJSON +// @Success 200 {object} models.KeyToken // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -158,7 +156,7 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, keytoken)) }) } @@ -174,7 +172,7 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param post_body body handler.UpdateUserKey.body false " " // -// @Success 200 {object} models.KeyTokenJSON +// @Success 200 {object} models.KeyToken // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -260,7 +258,7 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse { keytoken.Channels = *b.Channels } - return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, keytoken)) }) } @@ -275,7 +273,7 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param post_body body handler.CreateUserKey.body false " " // -// @Success 200 {object} models.KeyTokenJSON +// @Success 200 {object} models.KeyToken // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -333,7 +331,7 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err) } - return finishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token))) + return finishSuccess(ginext.JSONWithFilter(http.StatusOK, keytok, "INCLUDE_TOKEN")) }) } @@ -348,7 +346,7 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse { // @Param uid path string true "UserID" // @Param kid path string true "TokenKeyID" // -// @Success 200 {object} models.KeyTokenJSON +// @Success 200 {object} models.KeyToken // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -391,7 +389,7 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, client.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, client)) }) } diff --git a/scnserver/api/handler/apiMessage.go b/scnserver/api/handler/apiMessage.go index 79392ec..6f0c70b 100644 --- a/scnserver/api/handler/apiMessage.go +++ b/scnserver/api/handler/apiMessage.go @@ -50,9 +50,9 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse { KeyTokens []string `json:"used_key" form:"used_key"` } type response struct { - Messages []models.MessageJSON `json:"messages"` - NextPageToken string `json:"next_page_token"` - PageSize int `json:"page_size"` + Messages []models.Message `json:"messages"` + NextPageToken string `json:"next_page_token"` + PageSize int `json:"page_size"` } var q query @@ -151,15 +151,13 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err) } - var res []models.MessageJSON if trimmed { - res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() }) + res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.PreMarshal().Trim() }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) } else { - res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() }) + res := langext.ArrMap(messages, func(v models.Message) models.Message { return v.PreMarshal() }) + return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) } - - return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize})) - }) } @@ -174,7 +172,7 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param mid path string true "MessageID" // -// @Success 200 {object} models.MessageJSON +// @Success 200 {object} models.Message // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -211,7 +209,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse { // or we subscribe (+confirmed) to the channel and have read/admin key if ctx.CheckPermissionMessageRead(msg) { - return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON())) + return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal())) } if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil { @@ -229,7 +227,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse { } // => perm okay - return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON())) + return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal())) } return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil) @@ -246,7 +244,7 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param mid path string true "MessageID" // -// @Success 200 {object} models.MessageJSON +// @Success 200 {object} models.Message // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -293,7 +291,7 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err) } - return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON())) + return finishSuccess(ginext.JSON(http.StatusOK, msg.PreMarshal())) }) } diff --git a/scnserver/api/handler/apiPreview.go b/scnserver/api/handler/apiPreview.go index fa4da7b..d9c258b 100644 --- a/scnserver/api/handler/apiPreview.go +++ b/scnserver/api/handler/apiPreview.go @@ -19,7 +19,7 @@ import ( // // @Param uid path string true "UserID" // -// @Success 200 {object} models.UserPreviewJSON +// @Success 200 {object} models.UserPreview // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "user not found" @@ -65,7 +65,7 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param cid path string true "ChannelID" // -// @Success 200 {object} models.ChannelPreviewJSON +// @Success 200 {object} models.ChannelPreview // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "channel not found" @@ -98,7 +98,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err) } - return finishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview())) + return finishSuccess(ginext.JSON(http.StatusOK, channel.Preview())) }) } @@ -111,7 +111,7 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons // // @Param kid path string true "TokenKeyID" // -// @Success 200 {object} models.KeyTokenPreviewJSON +// @Success 200 {object} models.KeyTokenPreview // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "message not found" @@ -144,7 +144,7 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err) } - return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview())) + return finishSuccess(ginext.JSON(http.StatusOK, keytoken.Preview())) }) } diff --git a/scnserver/api/handler/apiSubscription.go b/scnserver/api/handler/apiSubscription.go index 2cabc9d..3c33570 100644 --- a/scnserver/api/handler/apiSubscription.go +++ b/scnserver/api/handler/apiSubscription.go @@ -60,7 +60,7 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes ChannelOwnerUserID *models.UserID `json:"channel_owner_user_id" form:"channel_owner_user_id"` } type response struct { - Subscriptions []models.SubscriptionJSON `json:"subscriptions"` + Subscriptions []models.Subscription `json:"subscriptions"` } var u uri @@ -129,9 +129,7 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err) } - jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() }) - - return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres})) + return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res})) }) } @@ -158,7 +156,7 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP ChannelID models.ChannelID `uri:"cid" binding:"entityid"` } type response struct { - Subscriptions []models.SubscriptionJSON `json:"subscriptions"` + Subscriptions []models.Subscription `json:"subscriptions"` } var u uri @@ -182,14 +180,12 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err) } - clients, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})}) + subs, err := h.database.ListSubscriptions(ctx, models.SubscriptionFilter{AnyUserID: langext.Ptr(u.UserID), ChannelID: langext.Ptr([]models.ChannelID{u.ChannelID})}) if err != nil { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err) } - res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() }) - - return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res})) + return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: subs})) }) } @@ -203,7 +199,7 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP // @Param uid path string true "UserID" // @Param sid path string true "SubscriptionID" // -// @Success 200 {object} models.SubscriptionJSON +// @Success 200 {object} models.Subscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "subscription not found" @@ -240,7 +236,7 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil) } - return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, subscription)) }) } @@ -254,7 +250,7 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse // @Param uid path string true "UserID" // @Param sid path string true "SubscriptionID" // -// @Success 200 {object} models.SubscriptionJSON +// @Success 200 {object} models.Subscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "subscription not found" @@ -296,7 +292,7 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err) } - return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, subscription)) }) } @@ -312,7 +308,7 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon // @Param query_data query handler.CreateSubscription.query false " " // @Param post_data body handler.CreateSubscription.body false " " // -// @Success 200 {object} models.SubscriptionJSON +// @Success 200 {object} models.Subscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 500 {object} ginresp.apiError "internal server error" @@ -397,7 +393,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon existingSub.Confirmed = true } - return finishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, existingSub)) } sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID) @@ -405,7 +401,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err) } - return finishSuccess(ginext.JSON(http.StatusOK, sub.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, sub)) }) } @@ -420,7 +416,7 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon // @Param sid path string true "SubscriptionID" // @Param post_data body handler.UpdateSubscription.body false " " // -// @Success 200 {object} models.SubscriptionJSON +// @Success 200 {object} models.Subscription // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "subscription not found" @@ -478,7 +474,7 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err) } - return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, subscription)) }) } diff --git a/scnserver/api/handler/apiUser.go b/scnserver/api/handler/apiUser.go index b3a10c2..a943227 100644 --- a/scnserver/api/handler/apiUser.go +++ b/scnserver/api/handler/apiUser.go @@ -22,7 +22,7 @@ import ( // // @Param post_body body handler.CreateUser.body false " " // -// @Success 200 {object} models.UserJSONWithClientsAndKeys +// @Success 200 {object} models.UserWithClientsAndKeys // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 500 {object} ginresp.apiError "internal server error" // @@ -120,7 +120,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse { log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient)) if b.NoClient { - return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey))) + return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients(make([]models.Client, 0), adminKey, sendKey, readKey))) } else { err := h.database.DeleteClientsByFCM(ctx, b.FCMToken) if err != nil { @@ -132,7 +132,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err) } - return finishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey))) + return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients([]models.Client{client}, adminKey, sendKey, readKey))) } }) } @@ -145,7 +145,7 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse { // // @Param uid path string true "UserID" // -// @Success 200 {object} models.UserJSON +// @Success 200 {object} models.User // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "user not found" @@ -178,7 +178,7 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err) } - return finishSuccess(ginext.JSON(http.StatusOK, user.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal())) }) @@ -196,7 +196,7 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse { // @Param username body string false "Change the username (send an empty string to clear it)" // @Param pro_token body string false "Send a verification of premium purchase" // -// @Success 200 {object} models.UserJSON +// @Success 200 {object} models.User // @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid" // @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions" // @Failure 404 {object} ginresp.apiError "user not found" @@ -271,6 +271,6 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse { return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err) } - return finishSuccess(ginext.JSON(http.StatusOK, user.JSON())) + return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal())) }) } diff --git a/scnserver/db/impl/logs/database.go b/scnserver/db/impl/logs/database.go index 2fbcab7..56254fe 100644 --- a/scnserver/db/impl/logs/database.go +++ b/scnserver/db/impl/logs/database.go @@ -5,6 +5,7 @@ import ( "blackforestbytes.com/simplecloudnotifier/db/dbtools" "blackforestbytes.com/simplecloudnotifier/db/schema" "blackforestbytes.com/simplecloudnotifier/db/simplectx" + "blackforestbytes.com/simplecloudnotifier/models" "context" "database/sql" "errors" @@ -51,7 +52,8 @@ func NewLogsDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb, sq.DBOptions{}) + qqdb := sq.NewDB(xdb, sq.DBOptions{RegisterDefaultConverter: langext.PTrue, RegisterCommentTrimmer: langext.PTrue}) + models.RegisterConverter(qqdb) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) diff --git a/scnserver/db/impl/primary/channels.go b/scnserver/db/impl/primary/channels.go index 157c4ec..ee9b6b6 100644 --- a/scnserver/db/impl/primary/channels.go +++ b/scnserver/db/impl/primary/channels.go @@ -3,8 +3,6 @@ package primary import ( "blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/models" - "database/sql" - "errors" "gogs.mikescher.com/BlackForestBytes/goext/sq" "time" ) @@ -15,23 +13,7 @@ func (db *Database) GetChannelByName(ctx db.TxContext, userid models.UserID, cha return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM channels WHERE owner_user_id = :uid AND internal_name = :nam LIMIT 1", sq.PP{ - "uid": userid, - "nam": chanName, - }) - if err != nil { - return nil, err - } - - channel, err := models.DecodeChannel(ctx, tx, rows) - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - if err != nil { - return nil, err - } - - return &channel, nil + return sq.QuerySingleOpt[models.Channel](ctx, tx, "SELECT * FROM channels WHERE owner_user_id = :uid AND internal_name = :nam LIMIT 1", sq.PP{"uid": userid, "nam": chanName}, sq.SModeExtended, sq.Safe) } func (db *Database) GetChannelByID(ctx db.TxContext, chanid models.ChannelID) (*models.Channel, error) { @@ -40,22 +22,7 @@ func (db *Database) GetChannelByID(ctx db.TxContext, chanid models.ChannelID) (* return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM channels WHERE channel_id = :cid LIMIT 1", sq.PP{ - "cid": chanid, - }) - if err != nil { - return nil, err - } - - channel, err := models.DecodeChannel(ctx, tx, rows) - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - if err != nil { - return nil, err - } - - return &channel, nil + return sq.QuerySingleOpt[models.Channel](ctx, tx, "SELECT * FROM channels WHERE channel_id = :cid LIMIT 1", sq.PP{"cid": chanid}, sq.SModeExtended, sq.Safe) } type CreateChanel struct { @@ -72,14 +39,14 @@ func (db *Database) CreateChannel(ctx db.TxContext, userid models.UserID, dispNa return models.Channel{}, err } - entity := models.ChannelDB{ + entity := models.Channel{ ChannelID: models.NewChannelID(), OwnerUserID: userid, DisplayName: dispName, InternalName: intName, SubscribeKey: subscribeKey, DescriptionName: description, - TimestampCreated: time2DB(time.Now()), + TimestampCreated: models.NowSCNTime(), TimestampLastSent: nil, MessagesSent: 0, } @@ -89,7 +56,7 @@ func (db *Database) CreateChannel(ctx db.TxContext, userid models.UserID, dispNa return models.Channel{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ListChannelsByOwner(ctx db.TxContext, userid models.UserID, subUserID models.UserID) ([]models.ChannelWithSubscription, error) { @@ -100,20 +67,14 @@ func (db *Database) ListChannelsByOwner(ctx db.TxContext, userid models.UserID, order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC " - rows, err := tx.Query(ctx, "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub ON channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid"+order, sq.PP{ + sql := "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub ON channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid" + order + + pp := sq.PP{ "ouid": userid, "subuid": subUserID, - }) - if err != nil { - return nil, err } - data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.ChannelWithSubscription](ctx, tx, sql, pp, sq.SModeExtended, sq.Safe) } func (db *Database) ListChannelsBySubscriber(ctx db.TxContext, userid models.UserID, confirmed *bool) ([]models.ChannelWithSubscription, error) { @@ -131,19 +92,13 @@ func (db *Database) ListChannelsBySubscriber(ctx db.TxContext, userid models.Use order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC " - rows, err := tx.Query(ctx, "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE sub.subscription_id IS NOT NULL "+confCond+order, sq.PP{ + sql := "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE sub.subscription_id IS NOT NULL " + confCond + order + + pp := sq.PP{ "subuid": userid, - }) - if err != nil { - return nil, err } - data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.ChannelWithSubscription](ctx, tx, sql, pp, sq.SModeExtended, sq.Safe) } func (db *Database) ListChannelsByAccess(ctx db.TxContext, userid models.UserID, confirmed *bool) ([]models.ChannelWithSubscription, error) { @@ -161,20 +116,14 @@ func (db *Database) ListChannelsByAccess(ctx db.TxContext, userid models.UserID, order := " ORDER BY channels.timestamp_created ASC, channels.channel_id ASC " - rows, err := tx.Query(ctx, "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid "+confCond+order, sq.PP{ + sql := "SELECT channels.*, sub.* FROM channels LEFT JOIN subscriptions AS sub on channels.channel_id = sub.channel_id AND sub.subscriber_user_id = :subuid WHERE owner_user_id = :ouid " + confCond + order + + pp := sq.PP{ "ouid": userid, "subuid": userid, - }) - if err != nil { - return nil, err } - data, err := models.DecodeChannelsWithSubscription(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.ChannelWithSubscription](ctx, tx, sql, pp, sq.SModeExtended, sq.Safe) } func (db *Database) GetChannel(ctx db.TxContext, userid models.UserID, channelid models.ChannelID, enforceOwner bool) (models.ChannelWithSubscription, error) { @@ -198,17 +147,9 @@ func (db *Database) GetChannel(ctx db.TxContext, userid models.UserID, channelid params["ouid"] = userid } - rows, err := tx.Query(ctx, "SELECT "+selectors+" FROM channels "+join+" WHERE "+cond+" LIMIT 1", params) - if err != nil { - return models.ChannelWithSubscription{}, err - } + sql := "SELECT " + selectors + " FROM channels " + join + " WHERE " + cond + " LIMIT 1" - channel, err := models.DecodeChannelWithSubscription(ctx, tx, rows) - if err != nil { - return models.ChannelWithSubscription{}, err - } - - return channel, nil + return sq.QuerySingle[models.ChannelWithSubscription](ctx, tx, sql, params, sq.SModeExtended, sq.Safe) } func (db *Database) IncChannelMessageCounter(ctx db.TxContext, channel *models.Channel) error { @@ -228,7 +169,7 @@ func (db *Database) IncChannelMessageCounter(ctx db.TxContext, channel *models.C } channel.MessagesSent += 1 - channel.TimestampLastSent = &now + channel.TimestampLastSent = models.NewSCNTimePtr(&now) return nil } diff --git a/scnserver/db/impl/primary/clients.go b/scnserver/db/impl/primary/clients.go index d324e68..13ee113 100644 --- a/scnserver/db/impl/primary/clients.go +++ b/scnserver/db/impl/primary/clients.go @@ -4,7 +4,6 @@ import ( "blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/models" "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" ) func (db *Database) CreateClient(ctx db.TxContext, userid models.UserID, ctype models.ClientType, fcmToken string, agentModel string, agentVersion string, name *string) (models.Client, error) { @@ -13,12 +12,12 @@ func (db *Database) CreateClient(ctx db.TxContext, userid models.UserID, ctype m return models.Client{}, err } - entity := models.ClientDB{ + entity := models.Client{ ClientID: models.NewClientID(), UserID: userid, Type: ctype, FCMToken: fcmToken, - TimestampCreated: time2DB(time.Now()), + TimestampCreated: models.NowSCNTime(), AgentModel: agentModel, AgentVersion: agentVersion, Name: name, @@ -29,7 +28,7 @@ func (db *Database) CreateClient(ctx db.TxContext, userid models.UserID, ctype m return models.Client{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ClearFCMTokens(ctx db.TxContext, fcmtoken string) error { @@ -52,17 +51,7 @@ func (db *Database) ListClients(ctx db.TxContext, userid models.UserID) ([]model return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM clients WHERE user_id = :uid ORDER BY clients.timestamp_created DESC, clients.client_id ASC", sq.PP{"uid": userid}) - if err != nil { - return nil, err - } - - data, err := models.DecodeClients(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.Client](ctx, tx, "SELECT * FROM clients WHERE user_id = :uid ORDER BY clients.timestamp_created DESC, clients.client_id ASC", sq.PP{"uid": userid}, sq.SModeExtended, sq.Safe) } func (db *Database) GetClient(ctx db.TxContext, userid models.UserID, clientid models.ClientID) (models.Client, error) { @@ -71,20 +60,10 @@ func (db *Database) GetClient(ctx db.TxContext, userid models.UserID, clientid m return models.Client{}, err } - rows, err := tx.Query(ctx, "SELECT * FROM clients WHERE user_id = :uid AND client_id = :cid LIMIT 1", sq.PP{ + return sq.QuerySingle[models.Client](ctx, tx, "SELECT * FROM clients WHERE user_id = :uid AND client_id = :cid LIMIT 1", sq.PP{ "uid": userid, "cid": clientid, - }) - if err != nil { - return models.Client{}, err - } - - client, err := models.DecodeClient(ctx, tx, rows) - if err != nil { - return models.Client{}, err - } - - return client, nil + }, sq.SModeExtended, sq.Safe) } func (db *Database) DeleteClient(ctx db.TxContext, clientid models.ClientID) error { diff --git a/scnserver/db/impl/primary/database.go b/scnserver/db/impl/primary/database.go index f9a7c01..0cd831c 100644 --- a/scnserver/db/impl/primary/database.go +++ b/scnserver/db/impl/primary/database.go @@ -5,6 +5,7 @@ import ( "blackforestbytes.com/simplecloudnotifier/db/dbtools" "blackforestbytes.com/simplecloudnotifier/db/schema" "blackforestbytes.com/simplecloudnotifier/db/simplectx" + "blackforestbytes.com/simplecloudnotifier/models" "context" "database/sql" "errors" @@ -51,7 +52,8 @@ func NewPrimaryDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb, sq.DBOptions{}) + qqdb := sq.NewDB(xdb, sq.DBOptions{RegisterDefaultConverter: langext.PTrue, RegisterCommentTrimmer: langext.PTrue}) + models.RegisterConverter(qqdb) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) diff --git a/scnserver/db/impl/primary/deliveries.go b/scnserver/db/impl/primary/deliveries.go index cdb7f55..94ee239 100644 --- a/scnserver/db/impl/primary/deliveries.go +++ b/scnserver/db/impl/primary/deliveries.go @@ -18,16 +18,16 @@ func (db *Database) CreateRetryDelivery(ctx db.TxContext, client models.Client, now := time.Now() next := scn.NextDeliveryTimestamp(now) - entity := models.DeliveryDB{ + entity := models.Delivery{ DeliveryID: models.NewDeliveryID(), MessageID: msg.MessageID, ReceiverUserID: client.UserID, ReceiverClientID: client.ClientID, - TimestampCreated: time2DB(now), + TimestampCreated: models.NewSCNTime(now), TimestampFinalized: nil, Status: models.DeliveryStatusRetry, RetryCount: 0, - NextDelivery: langext.Ptr(time2DB(next)), + NextDelivery: models.NewSCNTimePtr(&next), FCMMessageID: nil, } @@ -36,7 +36,7 @@ func (db *Database) CreateRetryDelivery(ctx db.TxContext, client models.Client, return models.Delivery{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) CreateSuccessDelivery(ctx db.TxContext, client models.Client, msg models.Message, fcmDelivID string) (models.Delivery, error) { @@ -47,13 +47,13 @@ func (db *Database) CreateSuccessDelivery(ctx db.TxContext, client models.Client now := time.Now() - entity := models.DeliveryDB{ + entity := models.Delivery{ DeliveryID: models.NewDeliveryID(), MessageID: msg.MessageID, ReceiverUserID: client.UserID, ReceiverClientID: client.ClientID, - TimestampCreated: time2DB(now), - TimestampFinalized: langext.Ptr(time2DB(now)), + TimestampCreated: models.NewSCNTime(now), + TimestampFinalized: models.NewSCNTimePtr(&now), Status: models.DeliveryStatusSuccess, RetryCount: 0, NextDelivery: nil, @@ -65,7 +65,7 @@ func (db *Database) CreateSuccessDelivery(ctx db.TxContext, client models.Client return models.Delivery{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ListRetrieableDeliveries(ctx db.TxContext, pageSize int) ([]models.Delivery, error) { @@ -74,20 +74,10 @@ func (db *Database) ListRetrieableDeliveries(ctx db.TxContext, pageSize int) ([] return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM deliveries WHERE status = 'RETRY' AND next_delivery < :next ORDER BY next_delivery ASC LIMIT :lim", sq.PP{ + return sq.QueryAll[models.Delivery](ctx, tx, "SELECT * FROM deliveries WHERE status = 'RETRY' AND next_delivery < :next ORDER BY next_delivery ASC LIMIT :lim", sq.PP{ "next": time2DB(time.Now()), "lim": pageSize, - }) - if err != nil { - return nil, err - } - - data, err := models.DecodeDeliveries(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + }, sq.SModeExtended, sq.Safe) } func (db *Database) SetDeliverySuccess(ctx db.TxContext, delivery models.Delivery, fcmDelivID string) error { diff --git a/scnserver/db/impl/primary/keytokens.go b/scnserver/db/impl/primary/keytokens.go index 8be9c8a..3e9dfad 100644 --- a/scnserver/db/impl/primary/keytokens.go +++ b/scnserver/db/impl/primary/keytokens.go @@ -3,8 +3,6 @@ package primary import ( "blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/models" - "database/sql" - "errors" "gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/sq" "strings" @@ -17,16 +15,16 @@ func (db *Database) CreateKeyToken(ctx db.TxContext, name string, owner models.U return models.KeyToken{}, err } - entity := models.KeyTokenDB{ + entity := models.KeyToken{ KeyTokenID: models.NewKeyTokenID(), Name: name, - TimestampCreated: time2DB(time.Now()), + TimestampCreated: models.NowSCNTime(), TimestampLastUsed: nil, OwnerUserID: owner, AllChannels: allChannels, - Channels: strings.Join(langext.ArrMap(channels, func(v models.ChannelID) string { return v.String() }), ";"), + Channels: channels, Token: token, - Permissions: permissions.String(), + Permissions: permissions, MessagesSent: 0, } @@ -35,7 +33,7 @@ func (db *Database) CreateKeyToken(ctx db.TxContext, name string, owner models.U return models.KeyToken{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ListKeyTokens(ctx db.TxContext, ownerID models.UserID) ([]models.KeyToken, error) { @@ -44,17 +42,7 @@ func (db *Database) ListKeyTokens(ctx db.TxContext, ownerID models.UserID) ([]mo return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM keytokens WHERE owner_user_id = :uid ORDER BY keytokens.timestamp_created DESC, keytokens.keytoken_id ASC", sq.PP{"uid": ownerID}) - if err != nil { - return nil, err - } - - data, err := models.DecodeKeyTokens(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.KeyToken](ctx, tx, "SELECT * FROM keytokens WHERE owner_user_id = :uid ORDER BY keytokens.timestamp_created DESC, keytokens.keytoken_id ASC", sq.PP{"uid": ownerID}, sq.SModeExtended, sq.Safe) } func (db *Database) GetKeyToken(ctx db.TxContext, userid models.UserID, keyTokenid models.KeyTokenID) (models.KeyToken, error) { @@ -63,20 +51,10 @@ func (db *Database) GetKeyToken(ctx db.TxContext, userid models.UserID, keyToken return models.KeyToken{}, err } - rows, err := tx.Query(ctx, "SELECT * FROM keytokens WHERE owner_user_id = :uid AND keytoken_id = :cid LIMIT 1", sq.PP{ + return sq.QuerySingle[models.KeyToken](ctx, tx, "SELECT * FROM keytokens WHERE owner_user_id = :uid AND keytoken_id = :cid LIMIT 1", sq.PP{ "uid": userid, "cid": keyTokenid, - }) - if err != nil { - return models.KeyToken{}, err - } - - keyToken, err := models.DecodeKeyToken(ctx, tx, rows) - if err != nil { - return models.KeyToken{}, err - } - - return keyToken, nil + }, sq.SModeExtended, sq.Safe) } func (db *Database) GetKeyTokenByID(ctx db.TxContext, keyTokenid models.KeyTokenID) (models.KeyToken, error) { @@ -85,19 +63,7 @@ func (db *Database) GetKeyTokenByID(ctx db.TxContext, keyTokenid models.KeyToken return models.KeyToken{}, err } - rows, err := tx.Query(ctx, "SELECT * FROM keytokens WHERE keytoken_id = :cid LIMIT 1", sq.PP{ - "cid": keyTokenid, - }) - if err != nil { - return models.KeyToken{}, err - } - - keyToken, err := models.DecodeKeyToken(ctx, tx, rows) - if err != nil { - return models.KeyToken{}, err - } - - return keyToken, nil + return sq.QuerySingle[models.KeyToken](ctx, tx, "SELECT * FROM keytokens WHERE keytoken_id = :cid LIMIT 1", sq.PP{"cid": keyTokenid}, sq.SModeExtended, sq.Safe) } func (db *Database) GetKeyTokenByToken(ctx db.TxContext, key string) (*models.KeyToken, error) { @@ -106,20 +72,7 @@ func (db *Database) GetKeyTokenByToken(ctx db.TxContext, key string) (*models.Ke return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM keytokens WHERE token = :key LIMIT 1", sq.PP{"key": key}) - if err != nil { - return nil, err - } - - user, err := models.DecodeKeyToken(ctx, tx, rows) - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - if err != nil { - return nil, err - } - - return &user, nil + return sq.QuerySingleOpt[models.KeyToken](ctx, tx, "SELECT * FROM keytokens WHERE token = :key LIMIT 1", sq.PP{"key": key}, sq.SModeExtended, sq.Safe) } func (db *Database) DeleteKeyToken(ctx db.TxContext, keyTokenid models.KeyTokenID) error { @@ -220,7 +173,7 @@ func (db *Database) IncKeyTokenMessageCounter(ctx db.TxContext, keyToken *models return err } - keyToken.TimestampLastUsed = &now + keyToken.TimestampLastUsed = models.NewSCNTimePtr(&now) keyToken.MessagesSent += 1 return nil diff --git a/scnserver/db/impl/primary/messages.go b/scnserver/db/impl/primary/messages.go index 134331c..6293204 100644 --- a/scnserver/db/impl/primary/messages.go +++ b/scnserver/db/impl/primary/messages.go @@ -4,7 +4,6 @@ import ( "blackforestbytes.com/simplecloudnotifier/db" ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken" "blackforestbytes.com/simplecloudnotifier/models" - "database/sql" "errors" "gogs.mikescher.com/BlackForestBytes/goext/sq" "time" @@ -16,20 +15,7 @@ func (db *Database) GetMessageByUserMessageID(ctx db.TxContext, usrMsgId string) return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM messages WHERE usr_message_id = :umid LIMIT 1", sq.PP{"umid": usrMsgId}) - if err != nil { - return nil, err - } - - msg, err := models.DecodeMessage(ctx, tx, rows) - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - if err != nil { - return nil, err - } - - return &msg, nil + return sq.QuerySingleOpt[models.Message](ctx, tx, "SELECT * FROM messages WHERE usr_message_id = :umid LIMIT 1", sq.PP{"umid": usrMsgId}, sq.SModeExtended, sq.Safe) } func (db *Database) GetMessage(ctx db.TxContext, scnMessageID models.MessageID, allowDeleted bool) (models.Message, error) { @@ -45,17 +31,7 @@ func (db *Database) GetMessage(ctx db.TxContext, scnMessageID models.MessageID, sqlcmd = "SELECT * FROM messages WHERE message_id = :mid AND deleted=0 LIMIT 1" } - rows, err := tx.Query(ctx, sqlcmd, sq.PP{"mid": scnMessageID}) - if err != nil { - return models.Message{}, err - } - - msg, err := models.DecodeMessage(ctx, tx, rows) - if err != nil { - return models.Message{}, err - } - - return msg, nil + return sq.QuerySingle[models.Message](ctx, tx, sqlcmd, sq.PP{"mid": scnMessageID}, sq.SModeExtended, sq.Safe) } func (db *Database) CreateMessage(ctx db.TxContext, senderUserID models.UserID, channel models.Channel, timestampSend *time.Time, title string, content *string, priority int, userMsgId *string, senderIP string, senderName *string, usedKeyID models.KeyTokenID) (models.Message, error) { @@ -64,21 +40,22 @@ func (db *Database) CreateMessage(ctx db.TxContext, senderUserID models.UserID, return models.Message{}, err } - entity := models.MessageDB{ + entity := models.Message{ MessageID: models.NewMessageID(), SenderUserID: senderUserID, ChannelInternalName: channel.InternalName, ChannelID: channel.ChannelID, SenderIP: senderIP, SenderName: senderName, - TimestampReal: time2DB(time.Now()), - TimestampClient: time2DBOpt(timestampSend), + TimestampReal: models.NowSCNTime(), + TimestampClient: models.NewSCNTimePtr(timestampSend), Title: title, Content: content, Priority: priority, UserMessageID: userMsgId, UsedKeyID: usedKeyID, - Deleted: bool2DB(false), + Deleted: false, + MessageExtra: models.MessageExtra{}, } _, err = sq.InsertSingle(ctx, tx, "messages", entity) @@ -86,7 +63,7 @@ func (db *Database) CreateMessage(ctx db.TxContext, senderUserID models.UserID, return models.Message{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) DeleteMessage(ctx db.TxContext, messageID models.MessageID) error { @@ -133,12 +110,7 @@ func (db *Database) ListMessages(ctx db.TxContext, filter models.MessageFilter, prepParams["tokts"] = inTok.Timestamp prepParams["tokid"] = inTok.Id - rows, err := tx.Query(ctx, sqlQuery, prepParams) - if err != nil { - return nil, ct.CursorToken{}, err - } - - data, err := models.DecodeMessages(ctx, tx, rows) + data, err := sq.QueryAll[models.Message](ctx, tx, sqlQuery, prepParams, sq.SModeExtended, sq.Safe) if err != nil { return nil, ct.CursorToken{}, err } diff --git a/scnserver/db/impl/primary/subscriptions.go b/scnserver/db/impl/primary/subscriptions.go index 7f1d521..7b3d96f 100644 --- a/scnserver/db/impl/primary/subscriptions.go +++ b/scnserver/db/impl/primary/subscriptions.go @@ -3,10 +3,7 @@ package primary import ( "blackforestbytes.com/simplecloudnotifier/db" "blackforestbytes.com/simplecloudnotifier/models" - "database/sql" - "errors" "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" ) func (db *Database) CreateSubscription(ctx db.TxContext, subscriberUID models.UserID, channel models.Channel, confirmed bool) (models.Subscription, error) { @@ -15,14 +12,14 @@ func (db *Database) CreateSubscription(ctx db.TxContext, subscriberUID models.Us return models.Subscription{}, err } - entity := models.SubscriptionDB{ + entity := models.Subscription{ SubscriptionID: models.NewSubscriptionID(), SubscriberUserID: subscriberUID, ChannelOwnerUserID: channel.OwnerUserID, ChannelID: channel.ChannelID, ChannelInternalName: channel.InternalName, - TimestampCreated: time2DB(time.Now()), - Confirmed: bool2DB(confirmed), + TimestampCreated: models.NowSCNTime(), + Confirmed: confirmed, } _, err = sq.InsertSingle(ctx, tx, "subscriptions", entity) @@ -30,7 +27,7 @@ func (db *Database) CreateSubscription(ctx db.TxContext, subscriberUID models.Us return models.Subscription{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ListSubscriptions(ctx db.TxContext, filter models.SubscriptionFilter) ([]models.Subscription, error) { @@ -45,17 +42,7 @@ func (db *Database) ListSubscriptions(ctx db.TxContext, filter models.Subscripti sqlQuery := "SELECT " + "subscriptions.*" + " FROM subscriptions " + filterJoin + " WHERE ( " + filterCond + " ) " + orderClause - rows, err := tx.Query(ctx, sqlQuery, prepParams) - if err != nil { - return nil, err - } - - data, err := models.DecodeSubscriptions(ctx, tx, rows) - if err != nil { - return nil, err - } - - return data, nil + return sq.QueryAll[models.Subscription](ctx, tx, sqlQuery, prepParams, sq.SModeExtended, sq.Safe) } func (db *Database) GetSubscription(ctx db.TxContext, subid models.SubscriptionID) (models.Subscription, error) { @@ -64,17 +51,7 @@ func (db *Database) GetSubscription(ctx db.TxContext, subid models.SubscriptionI return models.Subscription{}, err } - rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE subscription_id = :sid LIMIT 1", sq.PP{"sid": subid}) - if err != nil { - return models.Subscription{}, err - } - - sub, err := models.DecodeSubscription(ctx, tx, rows) - if err != nil { - return models.Subscription{}, err - } - - return sub, nil + return sq.QuerySingle[models.Subscription](ctx, tx, "SELECT * FROM subscriptions WHERE subscription_id = :sid LIMIT 1", sq.PP{"sid": subid}, sq.SModeExtended, sq.Safe) } func (db *Database) GetSubscriptionBySubscriber(ctx db.TxContext, subscriberId models.UserID, channelId models.ChannelID) (*models.Subscription, error) { @@ -83,23 +60,10 @@ func (db *Database) GetSubscriptionBySubscriber(ctx db.TxContext, subscriberId m return nil, err } - rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE subscriber_user_id = :suid AND channel_id = :cid LIMIT 1", sq.PP{ + return sq.QuerySingleOpt[models.Subscription](ctx, tx, "SELECT * FROM subscriptions WHERE subscriber_user_id = :suid AND channel_id = :cid LIMIT 1", sq.PP{ "suid": subscriberId, "cid": channelId, - }) - if err != nil { - return nil, err - } - - user, err := models.DecodeSubscription(ctx, tx, rows) - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - if err != nil { - return nil, err - } - - return &user, nil + }, sq.SModeExtended, sq.Safe) } func (db *Database) DeleteSubscription(ctx db.TxContext, subid models.SubscriptionID) error { diff --git a/scnserver/db/impl/primary/users.go b/scnserver/db/impl/primary/users.go index aeab690..bf49199 100644 --- a/scnserver/db/impl/primary/users.go +++ b/scnserver/db/impl/primary/users.go @@ -15,10 +15,10 @@ func (db *Database) CreateUser(ctx db.TxContext, protoken *string, username *str return models.User{}, err } - entity := models.UserDB{ + entity := models.User{ UserID: models.NewUserID(), Username: username, - TimestampCreated: time2DB(time.Now()), + TimestampCreated: models.NowSCNTime(), TimestampLastRead: nil, TimestampLastSent: nil, MessagesSent: 0, @@ -26,14 +26,17 @@ func (db *Database) CreateUser(ctx db.TxContext, protoken *string, username *str QuotaUsedDay: nil, IsPro: protoken != nil, ProToken: protoken, + UserExtra: models.UserExtra{}, } + entity.PreMarshal() + _, err = sq.InsertSingle(ctx, tx, "users", entity) if err != nil { return models.User{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) ClearProTokens(ctx db.TxContext, protoken string) error { @@ -56,17 +59,7 @@ func (db *Database) GetUser(ctx db.TxContext, userid models.UserID) (models.User return models.User{}, err } - rows, err := tx.Query(ctx, "SELECT * FROM users WHERE user_id = :uid LIMIT 1", sq.PP{"uid": userid}) - if err != nil { - return models.User{}, err - } - - user, err := models.DecodeUser(ctx, tx, rows) - if err != nil { - return models.User{}, err - } - - return user, nil + return sq.QuerySingle[models.User](ctx, tx, "SELECT * FROM users WHERE user_id = :uid LIMIT 1", sq.PP{"uid": userid}, sq.SModeExtended, sq.Safe) } func (db *Database) UpdateUserUsername(ctx db.TxContext, userid models.UserID, username *string) error { @@ -127,7 +120,7 @@ func (db *Database) IncUserMessageCounter(ctx db.TxContext, user *models.User) e return err } - user.TimestampLastSent = &now + user.TimestampLastSent = models.NewSCNTimePtr(&now) user.MessagesSent = user.MessagesSent + 1 return nil diff --git a/scnserver/db/impl/requests/database.go b/scnserver/db/impl/requests/database.go index 975af79..f554744 100644 --- a/scnserver/db/impl/requests/database.go +++ b/scnserver/db/impl/requests/database.go @@ -5,6 +5,7 @@ import ( "blackforestbytes.com/simplecloudnotifier/db/dbtools" "blackforestbytes.com/simplecloudnotifier/db/schema" "blackforestbytes.com/simplecloudnotifier/db/simplectx" + "blackforestbytes.com/simplecloudnotifier/models" "context" "database/sql" "errors" @@ -51,7 +52,8 @@ func NewRequestsDatabase(cfg server.Config) (*Database, error) { xdb.SetConnMaxIdleTime(60 * time.Minute) } - qqdb := sq.NewDB(xdb, sq.DBOptions{}) + qqdb := sq.NewDB(xdb, sq.DBOptions{RegisterDefaultConverter: langext.PTrue, RegisterCommentTrimmer: langext.PTrue}) + models.RegisterConverter(qqdb) if conf.EnableLogger { qqdb.AddListener(dbtools.DBLogger{}) diff --git a/scnserver/db/impl/requests/requestlogs.go b/scnserver/db/impl/requests/requestlogs.go index 3bad77f..7b725ce 100644 --- a/scnserver/db/impl/requests/requestlogs.go +++ b/scnserver/db/impl/requests/requestlogs.go @@ -8,18 +8,17 @@ import ( "time" ) -func (db *Database) InsertRequestLog(ctx context.Context, requestid models.RequestID, data models.RequestLog) (models.RequestLog, error) { +func (db *Database) InsertRequestLog(ctx context.Context, requestid models.RequestID, entity models.RequestLog) (models.RequestLog, error) { - entity := data.DB() entity.RequestID = requestid - entity.TimestampCreated = time2DB(time.Now()) + entity.TimestampCreated = models.NowSCNTime() _, err := sq.InsertSingle(ctx, db.db, "requests", entity) if err != nil { return models.RequestLog{}, err } - return entity.Model(), nil + return entity, nil } func (db *Database) Cleanup(ctx context.Context, count int, duration time.Duration) (int64, error) { @@ -73,12 +72,7 @@ func (db *Database) ListRequestLogs(ctx context.Context, filter models.RequestLo prepParams["tokts"] = inTok.Timestamp prepParams["tokid"] = inTok.Id - rows, err := db.db.Query(ctx, sqlQuery, prepParams) - if err != nil { - return nil, ct.CursorToken{}, err - } - - data, err := models.DecodeRequestLogs(ctx, db.db, rows) + data, err := sq.QueryAll[models.RequestLog](ctx, db.db, sqlQuery, prepParams, sq.SModeExtended, sq.Safe) if err != nil { return nil, ct.CursorToken{}, err } @@ -86,7 +80,7 @@ func (db *Database) ListRequestLogs(ctx context.Context, filter models.RequestLo if pageSize == nil || len(data) <= *pageSize { return data, ct.End(), nil } else { - outToken := ct.Normal(data[*pageSize-1].TimestampCreated, data[*pageSize-1].RequestID.String(), "DESC", filter.Hash()) + outToken := ct.Normal(data[*pageSize-1].TimestampCreated.Time(), data[*pageSize-1].RequestID.String(), "DESC", filter.Hash()) return data[0:*pageSize], outToken, nil } } diff --git a/scnserver/go.mod b/scnserver/go.mod index 3abf237..6aa56db 100644 --- a/scnserver/go.mod +++ b/scnserver/go.mod @@ -7,23 +7,23 @@ toolchain go1.22.3 require ( github.com/gin-gonic/gin v1.10.0 github.com/glebarez/go-sqlite v1.22.0 - github.com/go-playground/validator/v10 v10.22.0 + github.com/go-playground/validator/v10 v10.22.1 github.com/go-sql-driver/mysql v1.8.1 github.com/jmoiron/sqlx v1.4.0 github.com/mattn/go-sqlite3 v1.14.22 github.com/rs/zerolog v1.33.0 - gogs.mikescher.com/BlackForestBytes/goext v0.0.485 + gogs.mikescher.com/BlackForestBytes/goext v0.0.512 gopkg.in/loremipsum.v1 v1.1.2 ) require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/bytedance/sonic v1.11.9 // indirect - github.com/bytedance/sonic/loader v0.1.1 // indirect + github.com/bytedance/sonic v1.12.2 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/gabriel-vasile/mimetype v1.4.4 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect @@ -39,23 +39,23 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/montanaflynn/stats v0.7.1 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - github.com/rs/xid v1.5.0 // indirect + github.com/rs/xid v1.6.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect - github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect - go.mongodb.org/mongo-driver v1.16.0 // indirect - golang.org/x/arch v0.8.0 // indirect - golang.org/x/crypto v0.25.0 // indirect - golang.org/x/net v0.27.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/term v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect + go.mongodb.org/mongo-driver v1.16.1 // indirect + golang.org/x/arch v0.10.0 // indirect + golang.org/x/crypto v0.27.0 // indirect + golang.org/x/net v0.29.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/term v0.24.0 // indirect + golang.org/x/text v0.18.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.37.6 // indirect diff --git a/scnserver/go.sum b/scnserver/go.sum index f368e3b..2d36218 100644 --- a/scnserver/go.sum +++ b/scnserver/go.sum @@ -1,9 +1,10 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg= -github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= -github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= +github.com/bytedance/sonic v1.12.2 h1:oaMFuRTpMHYLpCntGca65YWt5ny+wAceDERTkT2L9lg= +github.com/bytedance/sonic v1.12.2/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= @@ -14,8 +15,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= -github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= @@ -28,8 +29,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao= -github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= +github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= @@ -73,27 +74,26 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= @@ -106,36 +106,31 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= -github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 h1:tBiBTKHnIjovYoLX/TPkcf+OjqqKGQrPtGT3Foz+Pgo= -github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U= +github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= +github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.16.0 h1:tpRsfBJMROVHKpdGyc1BBEzzjDUWjItxbVSZ8Ls4BQ4= -go.mongodb.org/mongo-driver v1.16.0/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw= -gogs.mikescher.com/BlackForestBytes/goext v0.0.482 h1:veU8oJdGZ9rjLB8sluagBduiBs3BbEDf60sGmEEv8lk= -gogs.mikescher.com/BlackForestBytes/goext v0.0.482/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM= -gogs.mikescher.com/BlackForestBytes/goext v0.0.483 h1:fxhe3U5bpkv1SvSae7F/ixPp7DUiRxga4Zvg82iQSsI= -gogs.mikescher.com/BlackForestBytes/goext v0.0.483/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM= -gogs.mikescher.com/BlackForestBytes/goext v0.0.484 h1:fu60J83OBtnUkXCIt+dycHrin5OUmL1B46IY6GTQosw= -gogs.mikescher.com/BlackForestBytes/goext v0.0.484/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM= -gogs.mikescher.com/BlackForestBytes/goext v0.0.485 h1:hjXxl7bwHkzYBpfsX81UZj929bKUDIoNFl0XQSvt4Qk= -gogs.mikescher.com/BlackForestBytes/goext v0.0.485/go.mod h1:GxqLkJwPWQB5lVgWhmBPnx9RC+F0Dvi2xHKwfCmCQgM= -golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= -golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= -golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +go.mongodb.org/mongo-driver v1.16.1 h1:rIVLL3q0IHM39dvE+z2ulZLp9ENZKThVfuvN/IiN4l8= +go.mongodb.org/mongo-driver v1.16.1/go.mod h1:oB6AhJQvFQL4LEHyXi6aJzQJtBiTQHiAd83l0GdFaiw= +gogs.mikescher.com/BlackForestBytes/goext v0.0.511 h1:vAEhXdexKlLTNf/mGHzemp/4rzmv7n2jf5l4NK38tIw= +gogs.mikescher.com/BlackForestBytes/goext v0.0.511/go.mod h1:9Q9EjraeE3yih7EXgBlnwLLJXWuRZNsl7s5TVTh3aOU= +gogs.mikescher.com/BlackForestBytes/goext v0.0.512 h1:cdLUi1bSnGujtx8/K0fPql142aOvUyNPt+8aWMKKDFk= +gogs.mikescher.com/BlackForestBytes/goext v0.0.512/go.mod h1:9Q9EjraeE3yih7EXgBlnwLLJXWuRZNsl7s5TVTh3aOU= +golang.org/x/arch v0.10.0 h1:S3huipmSclq3PJMNe76NGwkBR504WFkQ5dhzWzP8ZW8= +golang.org/x/arch v0.10.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -145,18 +140,18 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= -golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= +golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= @@ -179,4 +174,3 @@ modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ= modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= -rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/scnserver/logic/request.go b/scnserver/logic/request.go index fb21978..f5f6df9 100644 --- a/scnserver/logic/request.go +++ b/scnserver/logic/request.go @@ -185,9 +185,9 @@ func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp ginext.HTTPRes RetryCount: int64(ctr), Panicked: panicstr != nil, PanicStr: panicstr, - ProcessingTime: t1.Sub(t0), - TimestampStart: t0, - TimestampFinish: t1, + ProcessingTime: models.SCNDuration(t1.Sub(t0)), + TimestampStart: models.NewSCNTime(t0), + TimestampFinish: models.NewSCNTime(t1), } } diff --git a/scnserver/models/channel.go b/scnserver/models/channel.go index 4eac4d6..25d1405 100644 --- a/scnserver/models/channel.go +++ b/scnserver/models/channel.go @@ -1,37 +1,28 @@ package models -import ( - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" -) - type Channel struct { - ChannelID ChannelID - OwnerUserID UserID - InternalName string - DisplayName string - DescriptionName *string - SubscribeKey string - TimestampCreated time.Time - TimestampLastSent *time.Time - MessagesSent int + ChannelID ChannelID `db:"channel_id" json:"channel_id"` + OwnerUserID UserID `db:"owner_user_id" json:"owner_user_id"` + InternalName string `db:"internal_name" json:"internal_name"` + DisplayName string `db:"display_name" json:"display_name"` + DescriptionName *string `db:"description_name" json:"description_name"` + SubscribeKey string `db:"subscribe_key" json:"subscribe_key" jsonfilter:"INCLUDE_KEY"` // can be nil, depending on endpoint + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + TimestampLastSent *SCNTime `db:"timestamp_lastsent" json:"timestamp_lastsent"` + MessagesSent int `db:"messages_sent" json:"messages_sent"` } -func (c Channel) JSON(includeKey bool) ChannelJSON { - return ChannelJSON{ - ChannelID: c.ChannelID, - OwnerUserID: c.OwnerUserID, - InternalName: c.InternalName, - DisplayName: c.DisplayName, - DescriptionName: c.DescriptionName, - SubscribeKey: langext.Conditional(includeKey, langext.Ptr(c.SubscribeKey), nil), - TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano), - TimestampLastSent: timeOptFmt(c.TimestampLastSent, time.RFC3339Nano), - MessagesSent: c.MessagesSent, - } +type ChannelWithSubscription struct { + Channel + Subscription *Subscription `db:"sub" json:"subscription"` +} + +type ChannelPreview struct { + ChannelID ChannelID `json:"channel_id"` + OwnerUserID UserID `json:"owner_user_id"` + InternalName string `json:"internal_name"` + DisplayName string `json:"display_name"` + DescriptionName *string `json:"description_name"` } func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription { @@ -41,8 +32,8 @@ func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription { } } -func (c Channel) JSONPreview() ChannelPreviewJSON { - return ChannelPreviewJSON{ +func (c Channel) Preview() ChannelPreview { + return ChannelPreview{ ChannelID: c.ChannelID, OwnerUserID: c.OwnerUserID, InternalName: c.InternalName, @@ -50,118 +41,3 @@ func (c Channel) JSONPreview() ChannelPreviewJSON { DescriptionName: c.DescriptionName, } } - -type ChannelWithSubscription struct { - Channel - Subscription *Subscription -} - -func (c ChannelWithSubscription) JSON(includeChannelKey bool) ChannelWithSubscriptionJSON { - var sub *SubscriptionJSON = nil - if c.Subscription != nil { - sub = langext.Ptr(c.Subscription.JSON()) - } - return ChannelWithSubscriptionJSON{ - ChannelJSON: c.Channel.JSON(includeChannelKey), - Subscription: sub, - } -} - -type ChannelJSON struct { - ChannelID ChannelID `json:"channel_id"` - OwnerUserID UserID `json:"owner_user_id"` - InternalName string `json:"internal_name"` - DisplayName string `json:"display_name"` - DescriptionName *string `json:"description_name"` - SubscribeKey *string `json:"subscribe_key"` // can be nil, depending on endpoint - TimestampCreated string `json:"timestamp_created"` - TimestampLastSent *string `json:"timestamp_lastsent"` - MessagesSent int `json:"messages_sent"` -} - -type ChannelWithSubscriptionJSON struct { - ChannelJSON - Subscription *SubscriptionJSON `json:"subscription"` -} - -type ChannelPreviewJSON struct { - ChannelID ChannelID `json:"channel_id"` - OwnerUserID UserID `json:"owner_user_id"` - InternalName string `json:"internal_name"` - DisplayName string `json:"display_name"` - DescriptionName *string `json:"description_name"` -} - -type ChannelDB struct { - ChannelID ChannelID `db:"channel_id"` - OwnerUserID UserID `db:"owner_user_id"` - InternalName string `db:"internal_name"` - DisplayName string `db:"display_name"` - DescriptionName *string `db:"description_name"` - SubscribeKey string `db:"subscribe_key"` - TimestampCreated int64 `db:"timestamp_created"` - TimestampLastSent *int64 `db:"timestamp_lastsent"` - MessagesSent int `db:"messages_sent"` -} - -func (c ChannelDB) Model() Channel { - return Channel{ - ChannelID: c.ChannelID, - OwnerUserID: c.OwnerUserID, - InternalName: c.InternalName, - DisplayName: c.DisplayName, - DescriptionName: c.DescriptionName, - SubscribeKey: c.SubscribeKey, - TimestampCreated: timeFromMilli(c.TimestampCreated), - TimestampLastSent: timeOptFromMilli(c.TimestampLastSent), - MessagesSent: c.MessagesSent, - } -} - -type ChannelWithSubscriptionDB struct { - ChannelDB - Subscription *SubscriptionDB `db:"sub"` -} - -func (c ChannelWithSubscriptionDB) Model() ChannelWithSubscription { - var sub *Subscription = nil - if c.Subscription != nil { - sub = langext.Ptr(c.Subscription.Model()) - } - return ChannelWithSubscription{ - Channel: c.ChannelDB.Model(), - Subscription: sub, - } -} - -func DecodeChannel(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Channel, error) { - data, err := sq.ScanSingle[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return Channel{}, err - } - return data.Model(), nil -} - -func DecodeChannels(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Channel, error) { - data, err := sq.ScanAll[ChannelDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil -} - -func DecodeChannelWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (ChannelWithSubscription, error) { - data, err := sq.ScanSingle[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true) - if err != nil { - return ChannelWithSubscription{}, err - } - return data.Model(), nil -} - -func DecodeChannelsWithSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]ChannelWithSubscription, error) { - data, err := sq.ScanAll[ChannelWithSubscriptionDB](ctx, q, r, sq.SModeExtended, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v ChannelWithSubscriptionDB) ChannelWithSubscription { return v.Model() }), nil -} diff --git a/scnserver/models/client.go b/scnserver/models/client.go index 69a7c15..e97de3c 100644 --- a/scnserver/models/client.go +++ b/scnserver/models/client.go @@ -1,13 +1,5 @@ package models -import ( - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" -) - type ClientType string //@enum:type const ( @@ -19,76 +11,12 @@ const ( ) type Client struct { - ClientID ClientID - UserID UserID - Type ClientType - FCMToken string - TimestampCreated time.Time - AgentModel string - AgentVersion string - Name *string -} - -func (c Client) JSON() ClientJSON { - return ClientJSON{ - ClientID: c.ClientID, - UserID: c.UserID, - Type: c.Type, - FCMToken: c.FCMToken, - TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano), - AgentModel: c.AgentModel, - AgentVersion: c.AgentVersion, - Name: c.Name, - } -} - -type ClientJSON struct { - ClientID ClientID `json:"client_id"` - UserID UserID `json:"user_id"` - Type ClientType `json:"type"` - FCMToken string `json:"fcm_token"` - TimestampCreated string `json:"timestamp_created"` - AgentModel string `json:"agent_model"` - AgentVersion string `json:"agent_version"` - Name *string `json:"name"` -} - -type ClientDB struct { - ClientID ClientID `db:"client_id"` - UserID UserID `db:"user_id"` - Type ClientType `db:"type"` - FCMToken string `db:"fcm_token"` - TimestampCreated int64 `db:"timestamp_created"` - AgentModel string `db:"agent_model"` - AgentVersion string `db:"agent_version"` - Name *string `db:"name"` -} - -func (c ClientDB) Model() Client { - return Client{ - ClientID: c.ClientID, - UserID: c.UserID, - Type: c.Type, - FCMToken: c.FCMToken, - TimestampCreated: timeFromMilli(c.TimestampCreated), - AgentModel: c.AgentModel, - AgentVersion: c.AgentVersion, - Name: c.Name, - } -} - -func DecodeClient(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Client, error) { - data, err := sq.ScanSingle[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return Client{}, err - } - return data.Model(), nil -} - -func DecodeClients(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Client, error) { - data, err := sq.ScanAll[ClientDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v ClientDB) Client { return v.Model() }), nil + ClientID ClientID `db:"client_id" json:"client_id"` + UserID UserID `db:"user_id" json:"user_id"` + Type ClientType `db:"type" json:"type"` + FCMToken string `db:"fcm_token" json:"fcm_token"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + AgentModel string `db:"agent_model" json:"agent_model"` + AgentVersion string `db:"agent_version" json:"agent_version"` + Name *string `db:"name" json:"name"` } diff --git a/scnserver/models/delivery.go b/scnserver/models/delivery.go index 8812ebe..ce51910 100644 --- a/scnserver/models/delivery.go +++ b/scnserver/models/delivery.go @@ -1,13 +1,5 @@ package models -import ( - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" -) - type DeliveryStatus string //@enum:type const ( @@ -17,90 +9,18 @@ const ( ) type Delivery struct { - DeliveryID DeliveryID - MessageID MessageID - ReceiverUserID UserID - ReceiverClientID ClientID - TimestampCreated time.Time - TimestampFinalized *time.Time - Status DeliveryStatus - RetryCount int - NextDelivery *time.Time - FCMMessageID *string -} - -func (d Delivery) JSON() DeliveryJSON { - return DeliveryJSON{ - DeliveryID: d.DeliveryID, - MessageID: d.MessageID, - ReceiverUserID: d.ReceiverUserID, - ReceiverClientID: d.ReceiverClientID, - TimestampCreated: d.TimestampCreated.Format(time.RFC3339Nano), - TimestampFinalized: timeOptFmt(d.TimestampFinalized, time.RFC3339Nano), - Status: d.Status, - RetryCount: d.RetryCount, - NextDelivery: timeOptFmt(d.NextDelivery, time.RFC3339Nano), - FCMMessageID: d.FCMMessageID, - } + DeliveryID DeliveryID `db:"delivery_id" json:"delivery_id"` + MessageID MessageID `db:"message_id" json:"message_id"` + ReceiverUserID UserID `db:"receiver_user_id" json:"receiver_user_id"` + ReceiverClientID ClientID `db:"receiver_client_id" json:"receiver_client_id"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + TimestampFinalized *SCNTime `db:"timestamp_finalized" json:"timestamp_finalized"` + Status DeliveryStatus `db:"status" json:"status"` + RetryCount int `db:"retry_count" json:"retry_count"` + NextDelivery *SCNTime `db:"next_delivery" json:"next_delivery"` + FCMMessageID *string `db:"fcm_message_id" json:"fcm_message_id"` } func (d Delivery) MaxRetryCount() int { return 5 } - -type DeliveryJSON struct { - DeliveryID DeliveryID `json:"delivery_id"` - MessageID MessageID `json:"message_id"` - ReceiverUserID UserID `json:"receiver_user_id"` - ReceiverClientID ClientID `json:"receiver_client_id"` - TimestampCreated string `json:"timestamp_created"` - TimestampFinalized *string `json:"timestamp_finalized"` - Status DeliveryStatus `json:"status"` - RetryCount int `json:"retry_count"` - NextDelivery *string `json:"next_delivery"` - FCMMessageID *string `json:"fcm_message_id"` -} - -type DeliveryDB struct { - DeliveryID DeliveryID `db:"delivery_id"` - MessageID MessageID `db:"message_id"` - ReceiverUserID UserID `db:"receiver_user_id"` - ReceiverClientID ClientID `db:"receiver_client_id"` - TimestampCreated int64 `db:"timestamp_created"` - TimestampFinalized *int64 `db:"timestamp_finalized"` - Status DeliveryStatus `db:"status"` - RetryCount int `db:"retry_count"` - NextDelivery *int64 `db:"next_delivery"` - FCMMessageID *string `db:"fcm_message_id"` -} - -func (d DeliveryDB) Model() Delivery { - return Delivery{ - DeliveryID: d.DeliveryID, - MessageID: d.MessageID, - ReceiverUserID: d.ReceiverUserID, - ReceiverClientID: d.ReceiverClientID, - TimestampCreated: timeFromMilli(d.TimestampCreated), - TimestampFinalized: timeOptFromMilli(d.TimestampFinalized), - Status: d.Status, - RetryCount: d.RetryCount, - NextDelivery: timeOptFromMilli(d.NextDelivery), - FCMMessageID: d.FCMMessageID, - } -} - -func DecodeDelivery(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Delivery, error) { - data, err := sq.ScanSingle[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return Delivery{}, err - } - return data.Model(), nil -} - -func DecodeDeliveries(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Delivery, error) { - data, err := sq.ScanAll[DeliveryDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v DeliveryDB) Delivery { return v.Model() }), nil -} diff --git a/scnserver/models/duration.go b/scnserver/models/duration.go new file mode 100644 index 0000000..5e326ab --- /dev/null +++ b/scnserver/models/duration.go @@ -0,0 +1,35 @@ +package models + +import ( + "encoding/json" + "gogs.mikescher.com/BlackForestBytes/goext/timeext" + "time" +) + +type SCNDuration time.Duration + +func (t SCNDuration) MarshalToDB(v SCNDuration) (int64, error) { + return v.Duration().Milliseconds(), nil +} + +func (t SCNDuration) UnmarshalToModel(v int64) (SCNDuration, error) { + return SCNDuration(timeext.FromMilliseconds(v)), nil +} + +func (t SCNDuration) Duration() time.Duration { + return time.Duration(t) +} + +func (t *SCNDuration) UnmarshalJSON(data []byte) error { + flt := float64(0) + if err := json.Unmarshal(data, &flt); err != nil { + return err + } + d0 := timeext.FromSeconds(flt) + *t = SCNDuration(d0) + return nil +} + +func (t SCNDuration) MarshalJSON() ([]byte, error) { + return json.Marshal(t.Duration().Seconds()) +} diff --git a/scnserver/models/enums_gen.go b/scnserver/models/enums_gen.go index d160741..6db4230 100644 --- a/scnserver/models/enums_gen.go +++ b/scnserver/models/enums_gen.go @@ -5,7 +5,7 @@ package models import "gogs.mikescher.com/BlackForestBytes/goext/langext" import "gogs.mikescher.com/BlackForestBytes/goext/enums" -const ChecksumEnumGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485 +const ChecksumEnumGenerator = "8ffad0d7406eb7f17cbbfeff6fee6e6fa7156470203934ebd220c824e6e15e09" // GoExtVersion: 0.0.511 // ================================ ClientType ================================ // diff --git a/scnserver/models/ids_gen.go b/scnserver/models/ids_gen.go index 74eb55e..1a96df4 100644 --- a/scnserver/models/ids_gen.go +++ b/scnserver/models/ids_gen.go @@ -15,7 +15,7 @@ import "reflect" import "regexp" import "strings" -const ChecksumCharsetIDGenerator = "ba14f2f5d0b0357f248dcbd12933de102c80f1e61be697a37ebb723609fc0c59" // GoExtVersion: 0.0.485 +const ChecksumCharsetIDGenerator = "8ffad0d7406eb7f17cbbfeff6fee6e6fa7156470203934ebd220c824e6e15e09" // GoExtVersion: 0.0.511 const idlen = 24 diff --git a/scnserver/models/keytoken.go b/scnserver/models/keytoken.go index 4ae6ea5..dd87fcc 100644 --- a/scnserver/models/keytoken.go +++ b/scnserver/models/keytoken.go @@ -1,12 +1,9 @@ package models import ( - "context" - "github.com/jmoiron/sqlx" + "encoding/json" "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" "strings" - "time" ) type TokenPerm string //@enum:type @@ -45,17 +42,53 @@ func ParseTokenPermissionList(input string) TokenPermissionList { return r } +func (e TokenPermissionList) MarshalToDB(v TokenPermissionList) (string, error) { + return v.String(), nil +} + +func (e TokenPermissionList) UnmarshalToModel(v string) (TokenPermissionList, error) { + return ParseTokenPermissionList(v), nil +} + +func (t TokenPermissionList) MarshalJSON() ([]byte, error) { + return json.Marshal(t.String()) +} + +type ChannelIDArr []ChannelID + +func (t ChannelIDArr) MarshalToDB(v ChannelIDArr) (string, error) { + return strings.Join(langext.ArrMap(v, func(v ChannelID) string { return v.String() }), ";"), nil +} + +func (t ChannelIDArr) UnmarshalToModel(v string) (ChannelIDArr, error) { + channels := make([]ChannelID, 0) + if strings.TrimSpace(v) != "" { + channels = langext.ArrMap(strings.Split(v, ";"), func(v string) ChannelID { return ChannelID(v) }) + } + + return channels, nil +} + type KeyToken struct { - KeyTokenID KeyTokenID - Name string - TimestampCreated time.Time - TimestampLastUsed *time.Time - OwnerUserID UserID - AllChannels bool - Channels []ChannelID // can also be owned by other user (needs active subscription) - Token string - Permissions TokenPermissionList - MessagesSent int + KeyTokenID KeyTokenID `db:"keytoken_id" json:"keytoken_id"` + Name string `db:"name" json:"name"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + TimestampLastUsed *SCNTime `db:"timestamp_lastused" json:"timestamp_lastused"` + OwnerUserID UserID `db:"owner_user_id" json:"owner_user_id"` + AllChannels bool `db:"all_channels" json:"all_channels"` + Channels ChannelIDArr `db:"channels" json:"channels"` + Token string `db:"token" json:"token" jsonfilter:"INCLUDE_TOKEN"` + Permissions TokenPermissionList `db:"permissions" json:"permissions"` + MessagesSent int `db:"messages_sent" json:"messages_sent"` +} + +type KeyTokenPreview struct { + KeyTokenID KeyTokenID `json:"keytoken_id"` + Name string `json:"name"` + OwnerUserID UserID `json:"owner_user_id"` + AllChannels bool `json:"all_channels"` + Channels []ChannelID `json:"channels"` + Permissions string `json:"permissions"` } func (k KeyToken) IsUserRead(uid UserID) bool { @@ -78,22 +111,8 @@ func (k KeyToken) IsChannelMessagesSend(c Channel) bool { return (k.AllChannels == true || langext.InArray(c.ChannelID, k.Channels)) && k.OwnerUserID == c.OwnerUserID && k.Permissions.Any(PermAdmin, PermChannelSend) } -func (k KeyToken) JSON() KeyTokenJSON { - return KeyTokenJSON{ - KeyTokenID: k.KeyTokenID, - Name: k.Name, - TimestampCreated: k.TimestampCreated, - TimestampLastUsed: k.TimestampLastUsed, - OwnerUserID: k.OwnerUserID, - AllChannels: k.AllChannels, - Channels: k.Channels, - Permissions: k.Permissions.String(), - MessagesSent: k.MessagesSent, - } -} - -func (k KeyToken) JSONPreview() KeyTokenPreviewJSON { - return KeyTokenPreviewJSON{ +func (k KeyToken) Preview() KeyTokenPreview { + return KeyTokenPreview{ KeyTokenID: k.KeyTokenID, Name: k.Name, OwnerUserID: k.OwnerUserID, @@ -102,86 +121,3 @@ func (k KeyToken) JSONPreview() KeyTokenPreviewJSON { Permissions: k.Permissions.String(), } } - -type KeyTokenJSON struct { - KeyTokenID KeyTokenID `json:"keytoken_id"` - Name string `json:"name"` - TimestampCreated time.Time `json:"timestamp_created"` - TimestampLastUsed *time.Time `json:"timestamp_lastused"` - OwnerUserID UserID `json:"owner_user_id"` - AllChannels bool `json:"all_channels"` - Channels []ChannelID `json:"channels"` - Permissions string `json:"permissions"` - MessagesSent int `json:"messages_sent"` -} - -type KeyTokenWithTokenJSON struct { - KeyTokenJSON - Token string `json:"token"` -} - -type KeyTokenPreviewJSON struct { - KeyTokenID KeyTokenID `json:"keytoken_id"` - Name string `json:"name"` - OwnerUserID UserID `json:"owner_user_id"` - AllChannels bool `json:"all_channels"` - Channels []ChannelID `json:"channels"` - Permissions string `json:"permissions"` -} - -func (j KeyTokenJSON) WithToken(tok string) KeyTokenWithTokenJSON { - return KeyTokenWithTokenJSON{ - KeyTokenJSON: j, - Token: tok, - } -} - -type KeyTokenDB struct { - KeyTokenID KeyTokenID `db:"keytoken_id"` - Name string `db:"name"` - TimestampCreated int64 `db:"timestamp_created"` - TimestampLastUsed *int64 `db:"timestamp_lastused"` - OwnerUserID UserID `db:"owner_user_id"` - AllChannels bool `db:"all_channels"` - Channels string `db:"channels"` - Token string `db:"token"` - Permissions string `db:"permissions"` - MessagesSent int `db:"messages_sent"` -} - -func (k KeyTokenDB) Model() KeyToken { - - channels := make([]ChannelID, 0) - if strings.TrimSpace(k.Channels) != "" { - channels = langext.ArrMap(strings.Split(k.Channels, ";"), func(v string) ChannelID { return ChannelID(v) }) - } - - return KeyToken{ - KeyTokenID: k.KeyTokenID, - Name: k.Name, - TimestampCreated: timeFromMilli(k.TimestampCreated), - TimestampLastUsed: timeOptFromMilli(k.TimestampLastUsed), - OwnerUserID: k.OwnerUserID, - AllChannels: k.AllChannels, - Channels: channels, - Token: k.Token, - Permissions: ParseTokenPermissionList(k.Permissions), - MessagesSent: k.MessagesSent, - } -} - -func DecodeKeyToken(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (KeyToken, error) { - data, err := sq.ScanSingle[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return KeyToken{}, err - } - return data.Model(), nil -} - -func DecodeKeyTokens(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]KeyToken, error) { - data, err := sq.ScanAll[KeyTokenDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v KeyTokenDB) KeyToken { return v.Model() }), nil -} diff --git a/scnserver/models/message.go b/scnserver/models/message.go index 73f9302..2584387 100644 --- a/scnserver/models/message.go +++ b/scnserver/models/message.go @@ -1,11 +1,8 @@ package models import ( - "context" "fmt" - "github.com/jmoiron/sqlx" "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" "time" ) @@ -15,60 +12,45 @@ const ( ) type Message struct { - MessageID MessageID - SenderUserID UserID // user that sent the message (this is also the owner of the channel that contains it) - ChannelInternalName string - ChannelID ChannelID - SenderName *string - SenderIP string - TimestampReal time.Time - TimestampClient *time.Time - Title string - Content *string - Priority int - UserMessageID *string - UsedKeyID KeyTokenID - Deleted bool + MessageID MessageID `db:"message_id" json:"message_id"` + SenderUserID UserID `db:"sender_user_id" json:"sender_user_id"` // user that sent the message (this is also the owner of the channel that contains it) + ChannelInternalName string `db:"channel_internal_name" json:"channel_internal_name"` + ChannelID ChannelID `db:"channel_id" json:"channel_id"` + SenderName *string `db:"sender_name" json:"sender_name"` + SenderIP string `db:"sender_ip" json:"sender_ip"` + TimestampReal SCNTime `db:"timestamp_real" json:"-"` + TimestampClient *SCNTime `db:"timestamp_client" json:"-"` + Title string `db:"title" json:"title"` + Content *string `db:"content" json:"content"` + Priority int `db:"priority" json:"priority"` + UserMessageID *string `db:"usr_message_id" json:"usr_message_id"` + UsedKeyID KeyTokenID `db:"used_key_id" json:"used_key_id"` + Deleted bool `db:"deleted" json:"-"` + + MessageExtra `db:"-"` // fields that are not in DB and are set on PreMarshal } -func (m Message) FullJSON() MessageJSON { - return MessageJSON{ - MessageID: m.MessageID, - SenderUserID: m.SenderUserID, - ChannelInternalName: m.ChannelInternalName, - ChannelID: m.ChannelID, - SenderName: m.SenderName, - SenderIP: m.SenderIP, - Timestamp: m.Timestamp().Format(time.RFC3339Nano), - Title: m.Title, - Content: m.Content, - Priority: m.Priority, - UserMessageID: m.UserMessageID, - UsedKeyID: m.UsedKeyID, - Trimmed: false, - } +type MessageExtra struct { + Timestamp SCNTime `db:"-" json:"timestamp"` + Trimmed bool `db:"-" json:"trimmed"` } -func (m Message) TrimmedJSON() MessageJSON { - return MessageJSON{ - MessageID: m.MessageID, - SenderUserID: m.SenderUserID, - ChannelInternalName: m.ChannelInternalName, - ChannelID: m.ChannelID, - SenderName: m.SenderName, - SenderIP: m.SenderIP, - Timestamp: m.Timestamp().Format(time.RFC3339Nano), - Title: m.Title, - Content: m.TrimmedContent(), - Priority: m.Priority, - UserMessageID: m.UserMessageID, - UsedKeyID: m.UsedKeyID, - Trimmed: m.NeedsTrim(), +func (u *Message) PreMarshal() Message { + u.MessageExtra.Timestamp = NewSCNTime(u.Timestamp()) + return *u +} + +func (m Message) Trim() Message { + r := m + if !r.Trimmed && r.NeedsTrim() { + r.Content = r.TrimmedContent() + r.MessageExtra.Trimmed = true } + return r.PreMarshal() } func (m Message) Timestamp() time.Time { - return langext.Coalesce(m.TimestampClient, m.TimestampReal) + return langext.Coalesce(m.TimestampClient, m.TimestampReal).Time() } func (m Message) NeedsTrim() bool { @@ -102,71 +84,3 @@ func (m Message) FormatNotificationTitle(user User, channel Channel) string { return fmt.Sprintf("[%s] %s", channel.DisplayName, m.Title) } - -type MessageJSON struct { - MessageID MessageID `json:"message_id"` - SenderUserID UserID `json:"sender_user_id"` - ChannelInternalName string `json:"channel_internal_name"` - ChannelID ChannelID `json:"channel_id"` - SenderName *string `json:"sender_name"` - SenderIP string `json:"sender_ip"` - Timestamp string `json:"timestamp"` - Title string `json:"title"` - Content *string `json:"content"` - Priority int `json:"priority"` - UserMessageID *string `json:"usr_message_id"` - UsedKeyID KeyTokenID `json:"used_key_id"` - Trimmed bool `json:"trimmed"` -} - -type MessageDB struct { - MessageID MessageID `db:"message_id"` - SenderUserID UserID `db:"sender_user_id"` - ChannelInternalName string `db:"channel_internal_name"` - ChannelID ChannelID `db:"channel_id"` - SenderName *string `db:"sender_name"` - SenderIP string `db:"sender_ip"` - TimestampReal int64 `db:"timestamp_real"` - TimestampClient *int64 `db:"timestamp_client"` - Title string `db:"title"` - Content *string `db:"content"` - Priority int `db:"priority"` - UserMessageID *string `db:"usr_message_id"` - UsedKeyID KeyTokenID `db:"used_key_id"` - Deleted int `db:"deleted"` -} - -func (m MessageDB) Model() Message { - return Message{ - MessageID: m.MessageID, - SenderUserID: m.SenderUserID, - ChannelInternalName: m.ChannelInternalName, - ChannelID: m.ChannelID, - SenderName: m.SenderName, - SenderIP: m.SenderIP, - TimestampReal: timeFromMilli(m.TimestampReal), - TimestampClient: timeOptFromMilli(m.TimestampClient), - Title: m.Title, - Content: m.Content, - Priority: m.Priority, - UserMessageID: m.UserMessageID, - UsedKeyID: m.UsedKeyID, - Deleted: m.Deleted != 0, - } -} - -func DecodeMessage(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Message, error) { - data, err := sq.ScanSingle[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return Message{}, err - } - return data.Model(), nil -} - -func DecodeMessages(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Message, error) { - data, err := sq.ScanAll[MessageDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v MessageDB) Message { return v.Model() }), nil -} diff --git a/scnserver/models/requestlog.go b/scnserver/models/requestlog.go index bda1e14..860dca5 100644 --- a/scnserver/models/requestlog.go +++ b/scnserver/models/requestlog.go @@ -1,188 +1,27 @@ package models -import ( - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "gogs.mikescher.com/BlackForestBytes/goext/timeext" - "time" -) - type RequestLog struct { - RequestID RequestID - Method string - URI string - UserAgent *string - Authentication *string - RequestBody *string - RequestBodySize int64 - RequestContentType string - RemoteIP string - KeyID *KeyTokenID - UserID *UserID - Permissions *string - ResponseStatuscode *int64 - ResponseBodySize *int64 - ResponseBody *string - ResponseContentType string - RetryCount int64 - Panicked bool - PanicStr *string - ProcessingTime time.Duration - TimestampCreated time.Time - TimestampStart time.Time - TimestampFinish time.Time -} - -func (c RequestLog) JSON() RequestLogJSON { - return RequestLogJSON{ - RequestID: c.RequestID, - Method: c.Method, - URI: c.URI, - UserAgent: c.UserAgent, - Authentication: c.Authentication, - RequestBody: c.RequestBody, - RequestBodySize: c.RequestBodySize, - RequestContentType: c.RequestContentType, - RemoteIP: c.RemoteIP, - KeyID: c.KeyID, - UserID: c.UserID, - Permissions: c.Permissions, - ResponseStatuscode: c.ResponseStatuscode, - ResponseBodySize: c.ResponseBodySize, - ResponseBody: c.ResponseBody, - ResponseContentType: c.ResponseContentType, - RetryCount: c.RetryCount, - Panicked: c.Panicked, - PanicStr: c.PanicStr, - ProcessingTime: c.ProcessingTime.Seconds(), - TimestampCreated: c.TimestampCreated.Format(time.RFC3339Nano), - TimestampStart: c.TimestampStart.Format(time.RFC3339Nano), - TimestampFinish: c.TimestampFinish.Format(time.RFC3339Nano), - } -} - -func (c RequestLog) DB() RequestLogDB { - return RequestLogDB{ - RequestID: c.RequestID, - Method: c.Method, - URI: c.URI, - UserAgent: c.UserAgent, - Authentication: c.Authentication, - RequestBody: c.RequestBody, - RequestBodySize: c.RequestBodySize, - RequestContentType: c.RequestContentType, - RemoteIP: c.RemoteIP, - KeyID: c.KeyID, - UserID: c.UserID, - Permissions: c.Permissions, - ResponseStatuscode: c.ResponseStatuscode, - ResponseBodySize: c.ResponseBodySize, - ResponseBody: c.ResponseBody, - ResponseContentType: c.ResponseContentType, - RetryCount: c.RetryCount, - Panicked: langext.Conditional[int64](c.Panicked, 1, 0), - PanicStr: c.PanicStr, - ProcessingTime: c.ProcessingTime.Milliseconds(), - TimestampCreated: c.TimestampCreated.UnixMilli(), - TimestampStart: c.TimestampStart.UnixMilli(), - TimestampFinish: c.TimestampFinish.UnixMilli(), - } -} - -type RequestLogJSON struct { - RequestID RequestID `json:"requestLog_id"` - Method string `json:"method"` - URI string `json:"uri"` - UserAgent *string `json:"user_agent"` - Authentication *string `json:"authentication"` - RequestBody *string `json:"request_body"` - RequestBodySize int64 `json:"request_body_size"` - RequestContentType string `json:"request_content_type"` - RemoteIP string `json:"remote_ip"` - KeyID *KeyTokenID `json:"key_id"` - UserID *UserID `json:"userid"` - Permissions *string `json:"permissions"` - ResponseStatuscode *int64 `json:"response_statuscode"` - ResponseBodySize *int64 `json:"response_body_size"` - ResponseBody *string `json:"response_body"` - ResponseContentType string `json:"response_content_type"` - RetryCount int64 `json:"retry_count"` - Panicked bool `json:"panicked"` - PanicStr *string `json:"panic_str"` - ProcessingTime float64 `json:"processing_time"` - TimestampCreated string `json:"timestamp_created"` - TimestampStart string `json:"timestamp_start"` - TimestampFinish string `json:"timestamp_finish"` -} - -type RequestLogDB struct { - RequestID RequestID `db:"request_id"` - Method string `db:"method"` - URI string `db:"uri"` - UserAgent *string `db:"user_agent"` - Authentication *string `db:"authentication"` - RequestBody *string `db:"request_body"` - RequestBodySize int64 `db:"request_body_size"` - RequestContentType string `db:"request_content_type"` - RemoteIP string `db:"remote_ip"` - KeyID *KeyTokenID `db:"key_id"` - UserID *UserID `db:"userid"` - Permissions *string `db:"permissions"` - ResponseStatuscode *int64 `db:"response_statuscode"` - ResponseBodySize *int64 `db:"response_body_size"` - ResponseBody *string `db:"response_body"` - ResponseContentType string `db:"response_content_type"` - RetryCount int64 `db:"retry_count"` - Panicked int64 `db:"panicked"` - PanicStr *string `db:"panic_str"` - ProcessingTime int64 `db:"processing_time"` - TimestampCreated int64 `db:"timestamp_created"` - TimestampStart int64 `db:"timestamp_start"` - TimestampFinish int64 `db:"timestamp_finish"` -} - -func (c RequestLogDB) Model() RequestLog { - return RequestLog{ - RequestID: c.RequestID, - Method: c.Method, - URI: c.URI, - UserAgent: c.UserAgent, - Authentication: c.Authentication, - RequestBody: c.RequestBody, - RequestBodySize: c.RequestBodySize, - RequestContentType: c.RequestContentType, - RemoteIP: c.RemoteIP, - KeyID: c.KeyID, - UserID: c.UserID, - Permissions: c.Permissions, - ResponseStatuscode: c.ResponseStatuscode, - ResponseBodySize: c.ResponseBodySize, - ResponseBody: c.ResponseBody, - ResponseContentType: c.ResponseContentType, - RetryCount: c.RetryCount, - Panicked: c.Panicked != 0, - PanicStr: c.PanicStr, - ProcessingTime: timeext.FromMilliseconds(c.ProcessingTime), - TimestampCreated: timeFromMilli(c.TimestampCreated), - TimestampStart: timeFromMilli(c.TimestampStart), - TimestampFinish: timeFromMilli(c.TimestampFinish), - } -} - -func DecodeRequestLog(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (RequestLog, error) { - data, err := sq.ScanSingle[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return RequestLog{}, err - } - return data.Model(), nil -} - -func DecodeRequestLogs(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]RequestLog, error) { - data, err := sq.ScanAll[RequestLogDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v RequestLogDB) RequestLog { return v.Model() }), nil + RequestID RequestID `db:"request_id" json:"requestLog_id"` + Method string `db:"method" json:"method"` + URI string `db:"uri" json:"uri"` + UserAgent *string `db:"user_agent" json:"user_agent"` + Authentication *string `db:"authentication" json:"authentication"` + RequestBody *string `db:"request_body" json:"request_body"` + RequestBodySize int64 `db:"request_body_size" json:"request_body_size"` + RequestContentType string `db:"request_content_type" json:"request_content_type"` + RemoteIP string `db:"remote_ip" json:"remote_ip"` + KeyID *KeyTokenID `db:"key_id" json:"key_id"` + UserID *UserID `db:"userid" json:"userid"` + Permissions *string `db:"permissions" json:"permissions"` + ResponseStatuscode *int64 `db:"response_statuscode" json:"response_statuscode"` + ResponseBodySize *int64 `db:"response_body_size" json:"response_body_size"` + ResponseBody *string `db:"response_body" json:"response_body"` + ResponseContentType string `db:"response_content_type" json:"response_content_type"` + RetryCount int64 `db:"retry_count" json:"retry_count"` + Panicked bool `db:"panicked" json:"panicked"` + PanicStr *string `db:"panic_str" json:"panic_str"` + ProcessingTime SCNDuration `db:"processing_time" json:"processing_time"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + TimestampStart SCNTime `db:"timestamp_start" json:"timestamp_start"` + TimestampFinish SCNTime `db:"timestamp_finish" json:"timestamp_finish"` } diff --git a/scnserver/models/subscription.go b/scnserver/models/subscription.go index 8ef7785..458f693 100644 --- a/scnserver/models/subscription.go +++ b/scnserver/models/subscription.go @@ -1,13 +1,5 @@ package models -import ( - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" -) - // [!] subscriptions are read-access to channels, // // The set of subscriptions specifies which messages the ListMessages() API call returns @@ -16,71 +8,11 @@ import ( // (use keytokens for write-access) type Subscription struct { - SubscriptionID SubscriptionID - SubscriberUserID UserID - ChannelOwnerUserID UserID - ChannelID ChannelID - ChannelInternalName string - TimestampCreated time.Time - Confirmed bool -} - -func (s Subscription) JSON() SubscriptionJSON { - return SubscriptionJSON{ - SubscriptionID: s.SubscriptionID, - SubscriberUserID: s.SubscriberUserID, - ChannelOwnerUserID: s.ChannelOwnerUserID, - ChannelID: s.ChannelID, - ChannelInternalName: s.ChannelInternalName, - TimestampCreated: s.TimestampCreated.Format(time.RFC3339Nano), - Confirmed: s.Confirmed, - } -} - -type SubscriptionJSON struct { - SubscriptionID SubscriptionID `json:"subscription_id"` - SubscriberUserID UserID `json:"subscriber_user_id"` - ChannelOwnerUserID UserID `json:"channel_owner_user_id"` - ChannelID ChannelID `json:"channel_id"` - ChannelInternalName string `json:"channel_internal_name"` - TimestampCreated string `json:"timestamp_created"` - Confirmed bool `json:"confirmed"` -} - -type SubscriptionDB struct { - SubscriptionID SubscriptionID `db:"subscription_id"` - SubscriberUserID UserID `db:"subscriber_user_id"` - ChannelOwnerUserID UserID `db:"channel_owner_user_id"` - ChannelID ChannelID `db:"channel_id"` - ChannelInternalName string `db:"channel_internal_name"` - TimestampCreated int64 `db:"timestamp_created"` - Confirmed int `db:"confirmed"` -} - -func (s SubscriptionDB) Model() Subscription { - return Subscription{ - SubscriptionID: s.SubscriptionID, - SubscriberUserID: s.SubscriberUserID, - ChannelOwnerUserID: s.ChannelOwnerUserID, - ChannelID: s.ChannelID, - ChannelInternalName: s.ChannelInternalName, - TimestampCreated: timeFromMilli(s.TimestampCreated), - Confirmed: s.Confirmed != 0, - } -} - -func DecodeSubscription(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (Subscription, error) { - data, err := sq.ScanSingle[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return Subscription{}, err - } - return data.Model(), nil -} - -func DecodeSubscriptions(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]Subscription, error) { - data, err := sq.ScanAll[SubscriptionDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v SubscriptionDB) Subscription { return v.Model() }), nil + SubscriptionID SubscriptionID `db:"subscription_id" json:"subscription_id"` + SubscriberUserID UserID `db:"subscriber_user_id" json:"subscriber_user_id"` + ChannelOwnerUserID UserID `db:"channel_owner_user_id" json:"channel_owner_user_id"` + ChannelID ChannelID `db:"channel_id" json:"channel_id"` + ChannelInternalName string `db:"channel_internal_name" json:"channel_internal_name"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + Confirmed bool `db:"confirmed" json:"confirmed"` } diff --git a/scnserver/models/time.go b/scnserver/models/time.go new file mode 100644 index 0000000..3d956b8 --- /dev/null +++ b/scnserver/models/time.go @@ -0,0 +1,65 @@ +package models + +import ( + "encoding/json" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "gogs.mikescher.com/BlackForestBytes/goext/rfctime" + "time" +) + +type SCNTime time.Time + +func (t SCNTime) MarshalToDB(v SCNTime) (int64, error) { + return v.Time().UnixMilli(), nil +} + +func (t SCNTime) UnmarshalToModel(v int64) (SCNTime, error) { + return NewSCNTime(time.UnixMilli(v)), nil +} + +func (t SCNTime) Time() time.Time { + return time.Time(t) +} + +func (t *SCNTime) UnmarshalJSON(data []byte) error { + str := "" + if err := json.Unmarshal(data, &str); err != nil { + return err + } + t0, err := time.Parse(time.RFC3339Nano, str) + if err != nil { + return err + } + *t = SCNTime(t0) + return nil +} + +func (t SCNTime) MarshalJSON() ([]byte, error) { + str := t.Time().Format(time.RFC3339Nano) + return json.Marshal(str) +} + +func NewSCNTime(t time.Time) SCNTime { + return SCNTime(t) +} + +func NewSCNTimePtr(t *time.Time) *SCNTime { + if t == nil { + return nil + } + return langext.Ptr(SCNTime(*t)) +} + +func NowSCNTime() SCNTime { + return SCNTime(time.Now()) +} + +func tt(v rfctime.AnyTime) time.Time { + if r, ok := v.(time.Time); ok { + return r + } + if r, ok := v.(rfctime.RFCTime); ok { + return r.Time() + } + return time.Unix(0, v.UnixNano()).In(v.Location()) +} diff --git a/scnserver/models/user.go b/scnserver/models/user.go index 0073804..a855b6c 100644 --- a/scnserver/models/user.go +++ b/scnserver/models/user.go @@ -2,38 +2,63 @@ package models import ( scn "blackforestbytes.com/simplecloudnotifier" - "context" - "github.com/jmoiron/sqlx" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "gogs.mikescher.com/BlackForestBytes/goext/sq" - "time" ) type User struct { - UserID UserID - Username *string - TimestampCreated time.Time - TimestampLastRead *time.Time - TimestampLastSent *time.Time - MessagesSent int - QuotaUsed int - QuotaUsedDay *string - IsPro bool - ProToken *string + UserID UserID `db:"user_id" json:"user_id"` + Username *string `db:"username" json:"username"` + TimestampCreated SCNTime `db:"timestamp_created" json:"timestamp_created"` + TimestampLastRead *SCNTime `db:"timestamp_lastread" json:"timestamp_lastread"` + TimestampLastSent *SCNTime `db:"timestamp_lastsent" json:"timestamp_lastsent"` + MessagesSent int `db:"messages_sent" json:"messages_sent"` + QuotaUsed int `db:"quota_used" json:"quota_used"` + QuotaUsedDay *string `db:"quota_used_day" json:"-"` + IsPro bool `db:"is_pro" json:"is_pro"` + ProToken *string `db:"pro_token" json:"-"` + + UserExtra `db:"-"` // fields that are not in DB and are set on PreMarshal } -func (u User) JSON() UserJSON { - return UserJSON{ - UserID: u.UserID, - Username: u.Username, - TimestampCreated: u.TimestampCreated.Format(time.RFC3339Nano), - TimestampLastRead: timeOptFmt(u.TimestampLastRead, time.RFC3339Nano), - TimestampLastSent: timeOptFmt(u.TimestampLastSent, time.RFC3339Nano), - MessagesSent: u.MessagesSent, - QuotaUsed: u.QuotaUsedToday(), +type UserExtra struct { + QuotaRemaining int `json:"quota_remaining"` + QuotaPerDay int `json:"quota_max"` + DefaultChannel string `json:"default_channel"` + MaxBodySize int `json:"max_body_size"` + MaxTitleLength int `json:"max_title_length"` + DefaultPriority int `json:"default_priority"` + MaxChannelNameLength int `json:"max_channel_name_length"` + MaxChannelDescriptionLength int `json:"max_channel_description_length"` + MaxSenderNameLength int `json:"max_sender_name_length"` + MaxUserMessageIDLength int `json:"max_user_message_id_length"` +} + +type UserPreview struct { + UserID UserID `json:"user_id"` + Username *string `json:"username"` +} + +type UserWithClientsAndKeys struct { + User + Clients []Client `json:"clients"` + SendKey string `json:"send_key"` + ReadKey string `json:"read_key"` + AdminKey string `json:"admin_key"` +} + +func (u User) WithClients(clients []Client, ak string, sk string, rk string) UserWithClientsAndKeys { + return UserWithClientsAndKeys{ + User: u.PreMarshal(), + Clients: clients, + SendKey: sk, + ReadKey: rk, + AdminKey: ak, + } +} + +func (u *User) PreMarshal() User { + u.UserExtra = UserExtra{ QuotaPerDay: u.QuotaPerDay(), QuotaRemaining: u.QuotaRemainingToday(), - IsPro: u.IsPro, DefaultChannel: u.DefaultChannel(), MaxBodySize: u.MaxContentLength(), MaxTitleLength: u.MaxTitleLength(), @@ -43,16 +68,7 @@ func (u User) JSON() UserJSON { MaxSenderNameLength: u.MaxSenderNameLength(), MaxUserMessageIDLength: u.MaxUserMessageIDLength(), } -} - -func (u User) JSONWithClients(clients []Client, ak string, sk string, rk string) UserJSONWithClientsAndKeys { - return UserJSONWithClientsAndKeys{ - UserJSON: u.JSON(), - Clients: langext.ArrMap(clients, func(v Client) ClientJSON { return v.JSON() }), - SendKey: sk, - ReadKey: rk, - AdminKey: ak, - } + return *u } func (u User) MaxContentLength() int { @@ -116,86 +132,9 @@ func (u User) MaxTimestampDiffHours() int { return 24 } -func (u User) JSONPreview() UserPreviewJSON { - return UserPreviewJSON{ +func (u User) JSONPreview() UserPreview { + return UserPreview{ UserID: u.UserID, Username: u.Username, } } - -type UserJSON struct { - UserID UserID `json:"user_id"` - Username *string `json:"username"` - TimestampCreated string `json:"timestamp_created"` - TimestampLastRead *string `json:"timestamp_lastread"` - TimestampLastSent *string `json:"timestamp_lastsent"` - MessagesSent int `json:"messages_sent"` - QuotaUsed int `json:"quota_used"` - QuotaRemaining int `json:"quota_remaining"` - QuotaPerDay int `json:"quota_max"` - IsPro bool `json:"is_pro"` - DefaultChannel string `json:"default_channel"` - MaxBodySize int `json:"max_body_size"` - MaxTitleLength int `json:"max_title_length"` - DefaultPriority int `json:"default_priority"` - MaxChannelNameLength int `json:"max_channel_name_length"` - MaxChannelDescriptionLength int `json:"max_channel_description_length"` - MaxSenderNameLength int `json:"max_sender_name_length"` - MaxUserMessageIDLength int `json:"max_user_message_id_length"` -} - -type UserPreviewJSON struct { - UserID UserID `json:"user_id"` - Username *string `json:"username"` -} - -type UserJSONWithClientsAndKeys struct { - UserJSON - Clients []ClientJSON `json:"clients"` - SendKey string `json:"send_key"` - ReadKey string `json:"read_key"` - AdminKey string `json:"admin_key"` -} - -type UserDB struct { - UserID UserID `db:"user_id"` - Username *string `db:"username"` - TimestampCreated int64 `db:"timestamp_created"` - TimestampLastRead *int64 `db:"timestamp_lastread"` - TimestampLastSent *int64 `db:"timestamp_lastsent"` - MessagesSent int `db:"messages_sent"` - QuotaUsed int `db:"quota_used"` - QuotaUsedDay *string `db:"quota_used_day"` - IsPro bool `db:"is_pro"` - ProToken *string `db:"pro_token"` -} - -func (u UserDB) Model() User { - return User{ - UserID: u.UserID, - Username: u.Username, - TimestampCreated: timeFromMilli(u.TimestampCreated), - TimestampLastRead: timeOptFromMilli(u.TimestampLastRead), - TimestampLastSent: timeOptFromMilli(u.TimestampLastSent), - MessagesSent: u.MessagesSent, - QuotaUsed: u.QuotaUsed, - QuotaUsedDay: u.QuotaUsedDay, - IsPro: u.IsPro, - } -} - -func DecodeUser(ctx context.Context, q sq.Queryable, r *sqlx.Rows) (User, error) { - data, err := sq.ScanSingle[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return User{}, err - } - return data.Model(), nil -} - -func DecodeUsers(ctx context.Context, q sq.Queryable, r *sqlx.Rows) ([]User, error) { - data, err := sq.ScanAll[UserDB](ctx, q, r, sq.SModeFast, sq.Safe, true) - if err != nil { - return nil, err - } - return langext.ArrMap(data, func(v UserDB) User { return v.Model() }), nil -} diff --git a/scnserver/models/utils.go b/scnserver/models/utils.go index 7d2fb17..1429c10 100644 --- a/scnserver/models/utils.go +++ b/scnserver/models/utils.go @@ -2,6 +2,7 @@ package models import ( "gogs.mikescher.com/BlackForestBytes/goext/langext" + "gogs.mikescher.com/BlackForestBytes/goext/sq" "time" ) @@ -23,3 +24,10 @@ func timeOptFromMilli(millis *int64) *time.Time { func timeFromMilli(millis int64) time.Time { return time.UnixMilli(millis) } + +func RegisterConverter(db sq.DB) { + db.RegisterConverter(sq.NewAutoDBTypeConverter(SCNTime{})) + db.RegisterConverter(sq.NewAutoDBTypeConverter(SCNDuration(0))) + db.RegisterConverter(sq.NewAutoDBTypeConverter(TokenPermissionList{})) + db.RegisterConverter(sq.NewAutoDBTypeConverter(ChannelIDArr{})) +} diff --git a/scnserver/swagger/swagger.json b/scnserver/swagger/swagger.json index fee7ea1..aa9984c 100644 --- a/scnserver/swagger/swagger.json +++ b/scnserver/swagger/swagger.json @@ -978,7 +978,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.MessageJSON" + "$ref": "#/definitions/models.Message" } }, "400": { @@ -1027,7 +1027,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.MessageJSON" + "$ref": "#/definitions/models.Message" } }, "400": { @@ -1077,7 +1077,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ChannelPreviewJSON" + "$ref": "#/definitions/models.ChannelPreview" } }, "400": { @@ -1127,7 +1127,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenPreviewJSON" + "$ref": "#/definitions/models.KeyTokenPreview" } }, "400": { @@ -1177,7 +1177,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.UserPreviewJSON" + "$ref": "#/definitions/models.UserPreview" } }, "400": { @@ -1228,7 +1228,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.UserJSONWithClientsAndKeys" + "$ref": "#/definitions/models.UserWithClientsAndKeys" } }, "400": { @@ -1266,7 +1266,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.UserJSON" + "$ref": "#/definitions/models.User" } }, "400": { @@ -1331,7 +1331,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.UserJSON" + "$ref": "#/definitions/models.User" } }, "400": { @@ -1445,7 +1445,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ChannelWithSubscriptionJSON" + "$ref": "#/definitions/models.ChannelWithSubscription" } }, "400": { @@ -1502,7 +1502,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ChannelWithSubscriptionJSON" + "$ref": "#/definitions/models.ChannelWithSubscription" } }, "400": { @@ -1581,7 +1581,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ChannelWithSubscriptionJSON" + "$ref": "#/definitions/models.ChannelWithSubscription" } }, "400": { @@ -1816,7 +1816,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } }, "400": { @@ -1867,7 +1867,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } }, "400": { @@ -1922,7 +1922,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } }, "400": { @@ -1994,7 +1994,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } }, "400": { @@ -2101,7 +2101,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenJSON" + "$ref": "#/definitions/models.KeyToken" } }, "400": { @@ -2159,7 +2159,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenWithTokenJSON" + "$ref": "#/definitions/models.KeyToken" } }, "400": { @@ -2217,7 +2217,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenJSON" + "$ref": "#/definitions/models.KeyToken" } }, "400": { @@ -2273,7 +2273,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenJSON" + "$ref": "#/definitions/models.KeyToken" } }, "400": { @@ -2336,7 +2336,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.KeyTokenJSON" + "$ref": "#/definitions/models.KeyToken" } }, "400": { @@ -2458,7 +2458,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } }, "400": { @@ -2509,7 +2509,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } }, "400": { @@ -2564,7 +2564,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } }, "400": { @@ -2627,7 +2627,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } }, "400": { @@ -3415,7 +3415,7 @@ "messages": { "type": "array", "items": { - "$ref": "#/definitions/models.MessageJSON" + "$ref": "#/definitions/models.Message" } }, "next_page_token": { @@ -3432,7 +3432,7 @@ "subscriptions": { "type": "array", "items": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } } } @@ -3443,7 +3443,7 @@ "channels": { "type": "array", "items": { - "$ref": "#/definitions/models.ChannelWithSubscriptionJSON" + "$ref": "#/definitions/models.ChannelWithSubscription" } } } @@ -3454,7 +3454,7 @@ "clients": { "type": "array", "items": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } } } @@ -3465,7 +3465,7 @@ "messages": { "type": "array", "items": { - "$ref": "#/definitions/models.MessageJSON" + "$ref": "#/definitions/models.Message" } }, "next_page_token": { @@ -3482,7 +3482,7 @@ "keys": { "type": "array", "items": { - "$ref": "#/definitions/models.KeyTokenJSON" + "$ref": "#/definitions/models.KeyToken" } } } @@ -3493,7 +3493,7 @@ "subscriptions": { "type": "array", "items": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" } } } @@ -3803,7 +3803,7 @@ } } }, - "models.ChannelPreviewJSON": { + "models.ChannelPreview": { "type": "object", "properties": { "channel_id": { @@ -3823,7 +3823,7 @@ } } }, - "models.ChannelWithSubscriptionJSON": { + "models.ChannelWithSubscription": { "type": "object", "properties": { "channel_id": { @@ -3849,7 +3849,7 @@ "type": "string" }, "subscription": { - "$ref": "#/definitions/models.SubscriptionJSON" + "$ref": "#/definitions/models.Subscription" }, "timestamp_created": { "type": "string" @@ -3859,7 +3859,7 @@ } } }, - "models.ClientJSON": { + "models.Client": { "type": "object", "properties": { "agent_model": { @@ -3931,7 +3931,7 @@ } } }, - "models.KeyTokenJSON": { + "models.KeyToken": { "type": "object", "properties": { "all_channels": { @@ -3956,69 +3956,11 @@ "type": "string" }, "permissions": { - "type": "string" - }, - "timestamp_created": { - "type": "string" - }, - "timestamp_lastused": { - "type": "string" - } - } - }, - "models.KeyTokenPreviewJSON": { - "type": "object", - "properties": { - "all_channels": { - "type": "boolean" - }, - "channels": { "type": "array", "items": { - "type": "string" + "$ref": "#/definitions/models.TokenPerm" } }, - "keytoken_id": { - "type": "string" - }, - "name": { - "type": "string" - }, - "owner_user_id": { - "type": "string" - }, - "permissions": { - "type": "string" - } - } - }, - "models.KeyTokenWithTokenJSON": { - "type": "object", - "properties": { - "all_channels": { - "type": "boolean" - }, - "channels": { - "type": "array", - "items": { - "type": "string" - } - }, - "keytoken_id": { - "type": "string" - }, - "messages_sent": { - "type": "integer" - }, - "name": { - "type": "string" - }, - "owner_user_id": { - "type": "string" - }, - "permissions": { - "type": "string" - }, "timestamp_created": { "type": "string" }, @@ -4030,7 +3972,33 @@ } } }, - "models.MessageJSON": { + "models.KeyTokenPreview": { + "type": "object", + "properties": { + "all_channels": { + "type": "boolean" + }, + "channels": { + "type": "array", + "items": { + "type": "string" + } + }, + "keytoken_id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "owner_user_id": { + "type": "string" + }, + "permissions": { + "type": "string" + } + } + }, + "models.Message": { "type": "object", "properties": { "channel_id": { @@ -4055,6 +4023,7 @@ "type": "string" }, "sender_user_id": { + "description": "user that sent the message (this is also the owner of the channel that contains it)", "type": "string" }, "timestamp": { @@ -4066,15 +4035,12 @@ "trimmed": { "type": "boolean" }, - "used_key_id": { - "type": "string" - }, "usr_message_id": { "type": "string" } } }, - "models.SubscriptionJSON": { + "models.Subscription": { "type": "object", "properties": { "channel_id": { @@ -4100,7 +4066,28 @@ } } }, - "models.UserJSON": { + "models.TokenPerm": { + "type": "string", + "enum": [ + "A", + "CR", + "CS", + "UR" + ], + "x-enum-comments": { + "PermAdmin": "Edit userdata (+ includes all other permissions)", + "PermChannelRead": "Read messages", + "PermChannelSend": "Send messages", + "PermUserRead": "Read userdata" + }, + "x-enum-varnames": [ + "PermAdmin", + "PermChannelRead", + "PermChannelSend", + "PermUserRead" + ] + }, + "models.User": { "type": "object", "properties": { "default_channel": { @@ -4159,7 +4146,18 @@ } } }, - "models.UserJSONWithClientsAndKeys": { + "models.UserPreview": { + "type": "object", + "properties": { + "user_id": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "models.UserWithClientsAndKeys": { "type": "object", "properties": { "admin_key": { @@ -4168,7 +4166,7 @@ "clients": { "type": "array", "items": { - "$ref": "#/definitions/models.ClientJSON" + "$ref": "#/definitions/models.Client" } }, "default_channel": { @@ -4232,17 +4230,6 @@ "type": "string" } } - }, - "models.UserPreviewJSON": { - "type": "object", - "properties": { - "user_id": { - "type": "string" - }, - "username": { - "type": "string" - } - } } }, "tags": [ diff --git a/scnserver/swagger/swagger.yaml b/scnserver/swagger/swagger.yaml index 35c1b67..0213be5 100644 --- a/scnserver/swagger/swagger.yaml +++ b/scnserver/swagger/swagger.yaml @@ -244,7 +244,7 @@ definitions: properties: messages: items: - $ref: '#/definitions/models.MessageJSON' + $ref: '#/definitions/models.Message' type: array next_page_token: type: string @@ -255,28 +255,28 @@ definitions: properties: subscriptions: items: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' type: array type: object handler.ListChannels.response: properties: channels: items: - $ref: '#/definitions/models.ChannelWithSubscriptionJSON' + $ref: '#/definitions/models.ChannelWithSubscription' type: array type: object handler.ListClients.response: properties: clients: items: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' type: array type: object handler.ListMessages.response: properties: messages: items: - $ref: '#/definitions/models.MessageJSON' + $ref: '#/definitions/models.Message' type: array next_page_token: type: string @@ -287,14 +287,14 @@ definitions: properties: keys: items: - $ref: '#/definitions/models.KeyTokenJSON' + $ref: '#/definitions/models.KeyToken' type: array type: object handler.ListUserSubscriptions.response: properties: subscriptions: items: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' type: array type: object handler.Register.response: @@ -499,7 +499,7 @@ definitions: uri: type: string type: object - models.ChannelPreviewJSON: + models.ChannelPreview: properties: channel_id: type: string @@ -512,7 +512,7 @@ definitions: owner_user_id: type: string type: object - models.ChannelWithSubscriptionJSON: + models.ChannelWithSubscription: properties: channel_id: type: string @@ -530,13 +530,13 @@ definitions: description: can be nil, depending on endpoint type: string subscription: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' timestamp_created: type: string timestamp_lastsent: type: string type: object - models.ClientJSON: + models.Client: properties: agent_model: type: string @@ -586,7 +586,7 @@ definitions: usr_msg_id: type: string type: object - models.KeyTokenJSON: + models.KeyToken: properties: all_channels: type: boolean @@ -603,47 +603,9 @@ definitions: owner_user_id: type: string permissions: - type: string - timestamp_created: - type: string - timestamp_lastused: - type: string - type: object - models.KeyTokenPreviewJSON: - properties: - all_channels: - type: boolean - channels: items: - type: string + $ref: '#/definitions/models.TokenPerm' type: array - keytoken_id: - type: string - name: - type: string - owner_user_id: - type: string - permissions: - type: string - type: object - models.KeyTokenWithTokenJSON: - properties: - all_channels: - type: boolean - channels: - items: - type: string - type: array - keytoken_id: - type: string - messages_sent: - type: integer - name: - type: string - owner_user_id: - type: string - permissions: - type: string timestamp_created: type: string timestamp_lastused: @@ -651,7 +613,24 @@ definitions: token: type: string type: object - models.MessageJSON: + models.KeyTokenPreview: + properties: + all_channels: + type: boolean + channels: + items: + type: string + type: array + keytoken_id: + type: string + name: + type: string + owner_user_id: + type: string + permissions: + type: string + type: object + models.Message: properties: channel_id: type: string @@ -668,6 +647,8 @@ definitions: sender_name: type: string sender_user_id: + description: user that sent the message (this is also the owner of the channel + that contains it) type: string timestamp: type: string @@ -675,12 +656,10 @@ definitions: type: string trimmed: type: boolean - used_key_id: - type: string usr_message_id: type: string type: object - models.SubscriptionJSON: + models.Subscription: properties: channel_id: type: string @@ -697,7 +676,24 @@ definitions: timestamp_created: type: string type: object - models.UserJSON: + models.TokenPerm: + enum: + - A + - CR + - CS + - UR + type: string + x-enum-comments: + PermAdmin: Edit userdata (+ includes all other permissions) + PermChannelRead: Read messages + PermChannelSend: Send messages + PermUserRead: Read userdata + x-enum-varnames: + - PermAdmin + - PermChannelRead + - PermChannelSend + - PermUserRead + models.User: properties: default_channel: type: string @@ -736,13 +732,20 @@ definitions: username: type: string type: object - models.UserJSONWithClientsAndKeys: + models.UserPreview: + properties: + user_id: + type: string + username: + type: string + type: object + models.UserWithClientsAndKeys: properties: admin_key: type: string clients: items: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' type: array default_channel: type: string @@ -785,13 +788,6 @@ definitions: username: type: string type: object - models.UserPreviewJSON: - properties: - user_id: - type: string - username: - type: string - type: object host: simplecloudnotifier.de info: contact: {} @@ -1460,7 +1456,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.MessageJSON' + $ref: '#/definitions/models.Message' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1496,7 +1492,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.MessageJSON' + $ref: '#/definitions/models.Message' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1529,7 +1525,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ChannelPreviewJSON' + $ref: '#/definitions/models.ChannelPreview' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1563,7 +1559,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenPreviewJSON' + $ref: '#/definitions/models.KeyTokenPreview' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1597,7 +1593,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.UserPreviewJSON' + $ref: '#/definitions/models.UserPreview' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1631,7 +1627,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.UserJSONWithClientsAndKeys' + $ref: '#/definitions/models.UserWithClientsAndKeys' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1656,7 +1652,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.UserJSON' + $ref: '#/definitions/models.User' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1699,7 +1695,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.UserJSON' + $ref: '#/definitions/models.User' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1782,7 +1778,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ChannelWithSubscriptionJSON' + $ref: '#/definitions/models.ChannelWithSubscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1820,7 +1816,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ChannelWithSubscriptionJSON' + $ref: '#/definitions/models.ChannelWithSubscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -1873,7 +1869,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ChannelWithSubscriptionJSON' + $ref: '#/definitions/models.ChannelWithSubscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2032,7 +2028,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2066,7 +2062,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2103,7 +2099,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2151,7 +2147,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.ClientJSON' + $ref: '#/definitions/models.Client' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2223,7 +2219,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenJSON' + $ref: '#/definitions/models.KeyToken' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2262,7 +2258,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenJSON' + $ref: '#/definitions/models.KeyToken' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2301,7 +2297,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenJSON' + $ref: '#/definitions/models.KeyToken' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2343,7 +2339,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenJSON' + $ref: '#/definitions/models.KeyToken' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2383,7 +2379,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.KeyTokenWithTokenJSON' + $ref: '#/definitions/models.KeyToken' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2485,7 +2481,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2519,7 +2515,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2556,7 +2552,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: @@ -2598,7 +2594,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.SubscriptionJSON' + $ref: '#/definitions/models.Subscription' "400": description: supplied values/parameters cannot be parsed / are invalid schema: diff --git a/scnserver/test/keytoken_test.go b/scnserver/test/keytoken_test.go index 507256f..25b7375 100644 --- a/scnserver/test/keytoken_test.go +++ b/scnserver/test/keytoken_test.go @@ -131,7 +131,7 @@ func TestTokenKeys(t *testing.T) { msg1 := tt.RequestAuthGet[gin.H](t, data.AdminKey, baseUrl, fmt.Sprintf("/api/v2/messages/%s", msg1s["scn_msg_id"])) - tt.AssertEqual(t, "AllChannels", key7.KeytokenId, msg1["used_key_id"]) + tt.AssertEqual(t, "used_key_id", key7.KeytokenId, msg1["used_key_id"]) tt.RequestPostShouldFail(t, baseUrl, "/", gin.H{ "key": key7.Token, diff --git a/scnserver/test/response_test.go b/scnserver/test/response_test.go index 48ba8d4..135a6ee 100644 --- a/scnserver/test/response_test.go +++ b/scnserver/test/response_test.go @@ -113,6 +113,59 @@ func TestResponseKeyToken2(t *testing.T) { }) } +func TestResponseKeyToken3(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitSingleData(t, ws) + + response := tt.RequestAuthGetRaw(t, data.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys/current", data.UID)) + + tt.AssertJsonStructureMatch(t, "json[key]", response, map[string]any{ + "keytoken_id": "id", + "name": "string", + "timestamp_created": "rfc3339", + "timestamp_lastused": "rfc3339|null", + "owner_user_id": "id", + "all_channels": "bool", + "channels": []any{"string"}, + "permissions": "string", + "messages_sent": "int", + "token": "string", + }) +} + +func TestResponseKeyToken4(t *testing.T) { + ws, baseUrl, stop := tt.StartSimpleWebserver(t) + defer stop() + + data := tt.InitSingleData(t, ws) + + chan1 := tt.RequestAuthPost[gin.H](t, data.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/channels", data.UID), gin.H{ + "name": "TestChan1asdf", + }) + + response := tt.RequestAuthPostRaw(t, data.AdminKey, baseUrl, fmt.Sprintf("/api/v2/users/%s/keys", data.UID), gin.H{ + "all_channels": false, + "channels": []string{chan1["channel_id"].(string)}, + "name": "TKey1", + "permissions": "CS", + }) + + tt.AssertJsonStructureMatch(t, "json[key]", response, map[string]any{ + "keytoken_id": "id", + "name": "string", + "timestamp_created": "rfc3339", + "timestamp_lastused": "rfc3339|null", + "owner_user_id": "id", + "all_channels": "bool", + "channels": []any{"string"}, + "permissions": "string", + "messages_sent": "int", + "token": "string", + }) +} + func TestResponseMessage(t *testing.T) { ws, baseUrl, stop := tt.StartSimpleWebserver(t) defer stop() diff --git a/scnserver/test/send_test.go b/scnserver/test/send_test.go index c2f89c2..bd493f1 100644 --- a/scnserver/test/send_test.go +++ b/scnserver/test/send_test.go @@ -836,7 +836,7 @@ func TestSendWithTimestamp(t *testing.T) { tt.AssertEqual(t, "messageCount", 1, len(pusher.Data)) tt.AssertStrRepEqual(t, "msg.title", "TTT", pusher.Last().Message.Title) - tt.AssertStrRepEqual(t, "msg.TimestampClient", ts, pusher.Last().Message.TimestampClient.Unix()) + tt.AssertStrRepEqual(t, "msg.TimestampClient", ts, pusher.Last().Message.TimestampClient.Time().Unix()) tt.AssertStrRepEqual(t, "msg.Timestamp", ts, pusher.Last().Message.Timestamp().Unix()) tt.AssertNotStrRepEqual(t, "msg.ts", pusher.Last().Message.TimestampClient, pusher.Last().Message.TimestampReal) tt.AssertStrRepEqual(t, "msg.scn_msg_id", msg1["scn_msg_id"], pusher.Last().Message.MessageID) diff --git a/scnserver/test/util/requests.go b/scnserver/test/util/requests.go index 6a94aa9..a74f228 100644 --- a/scnserver/test/util/requests.go +++ b/scnserver/test/util/requests.go @@ -34,6 +34,10 @@ func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, bo return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body, true) } +func RequestAuthPostRaw(t *testing.T, akey string, baseURL string, urlSuffix string, body any) string { + return RequestAny[string](t, akey, "POST", baseURL, urlSuffix, body, false) +} + func RequestAuthPost[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult { return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body, true) } diff --git a/scnserver/test/util/structure.go b/scnserver/test/util/structure.go index 1ce95eb..d07fa15 100644 --- a/scnserver/test/util/structure.go +++ b/scnserver/test/util/structure.go @@ -135,13 +135,13 @@ func assertjsonStructureMatchMapObject(t *testing.T, mapschema map[string]any, r for k := range mapschema { if _, ok := realValue[k]; !ok { - t.Errorf("Missing Key: < %s >", keyPath) + t.Errorf("Missing Key: < %s >", keyPath+"."+k) } } for k := range realValue { if _, ok := mapschema[k]; !ok { - t.Errorf("Additional key: < %s >", keyPath) + t.Errorf("Additional key: < %s >", keyPath+"."+k) } }