Refactor server to go-sqlite and ginext [WIP]
This commit is contained in:
@@ -8,18 +8,19 @@ const (
|
||||
|
||||
NO_ERROR APIError = 0000
|
||||
|
||||
MISSING_UID APIError = 1101
|
||||
MISSING_TOK APIError = 1102
|
||||
MISSING_TITLE APIError = 1103
|
||||
INVALID_PRIO APIError = 1104
|
||||
REQ_METHOD APIError = 1105
|
||||
INVALID_CLIENTTYPE APIError = 1106
|
||||
PAGETOKEN_ERROR APIError = 1121
|
||||
BINDFAIL_QUERY_PARAM APIError = 1151
|
||||
BINDFAIL_BODY_PARAM APIError = 1152
|
||||
BINDFAIL_URI_PARAM APIError = 1153
|
||||
INVALID_BODY_PARAM APIError = 1161
|
||||
INVALID_ENUM_VALUE APIError = 1171
|
||||
MISSING_UID APIError = 1101
|
||||
MISSING_TOK APIError = 1102
|
||||
MISSING_TITLE APIError = 1103
|
||||
INVALID_PRIO APIError = 1104
|
||||
REQ_METHOD APIError = 1105
|
||||
INVALID_CLIENTTYPE APIError = 1106
|
||||
PAGETOKEN_ERROR APIError = 1121
|
||||
BINDFAIL_QUERY_PARAM APIError = 1151
|
||||
BINDFAIL_BODY_PARAM APIError = 1152
|
||||
BINDFAIL_URI_PARAM APIError = 1153
|
||||
BINDFAIL_HEADER_PARAM APIError = 1152
|
||||
INVALID_BODY_PARAM APIError = 1161
|
||||
INVALID_ENUM_VALUE APIError = 1171
|
||||
|
||||
NO_TITLE APIError = 1201
|
||||
TITLE_TOO_LONG APIError = 1202
|
||||
|
@@ -93,10 +93,6 @@ func SendAPIError(g *gin.Context, status int, errorid apierr.APIError, highlight
|
||||
return createApiError(g, "SendAPIError", status, errorid, highlight, msg, e)
|
||||
}
|
||||
|
||||
func NotImplemented(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
return createApiError(g, "NotImplemented", 500, apierr.NOT_IMPLEMENTED, 0, "Not Implemented", nil)
|
||||
}
|
||||
|
||||
func createApiError(g *gin.Context, ident string, status int, errorid apierr.APIError, highlight apihighlight.ErrHighlight, msg string, e error) ginext.HTTPResponse {
|
||||
reqUri := ""
|
||||
if g != nil && g.Request != nil {
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -56,67 +57,65 @@ func (h APIHandler) ListChannels(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
ctx, errResp := h.app.StartRequest(g, &u, &q, nil, nil)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
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)
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
res = langext.ArrMap(channels, func(v models.ChannelWithSubscription) models.ChannelWithSubscriptionJSON { return v.JSON(true) })
|
||||
|
||||
} else if sel == "subscribed_any" {
|
||||
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) })
|
||||
|
||||
} else if sel == "subscribed_any" {
|
||||
|
||||
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, nil)
|
||||
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) })
|
||||
|
||||
} else if sel == "all_any" {
|
||||
|
||||
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, nil)
|
||||
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) })
|
||||
|
||||
} else if sel == "subscribed" {
|
||||
|
||||
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, langext.Ptr(true))
|
||||
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) })
|
||||
|
||||
} else if sel == "all" {
|
||||
|
||||
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, langext.Ptr(true))
|
||||
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) })
|
||||
|
||||
} else {
|
||||
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_ENUM_VALUE, "Invalid value for the [selector] parameter", nil)
|
||||
|
||||
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, nil)
|
||||
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) })
|
||||
|
||||
} else if sel == "all_any" {
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
|
||||
|
||||
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, nil)
|
||||
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) })
|
||||
|
||||
} else if sel == "subscribed" {
|
||||
|
||||
channels, err := h.database.ListChannelsBySubscriber(ctx, u.UserID, langext.Ptr(true))
|
||||
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) })
|
||||
|
||||
} else if sel == "all" {
|
||||
|
||||
channels, err := h.database.ListChannelsByAccess(ctx, u.UserID, langext.Ptr(true))
|
||||
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) })
|
||||
|
||||
} else {
|
||||
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_ENUM_VALUE, "Invalid value for the [selector] parameter", nil)
|
||||
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Channels: res}))
|
||||
})
|
||||
}
|
||||
|
||||
// GetChannel swaggerdoc
|
||||
@@ -142,25 +141,29 @@ func (h APIHandler) GetChannel(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
|
||||
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// CreateChannel swaggerdoc
|
||||
@@ -192,75 +195,78 @@ func (h APIHandler) CreateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if b.Name == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Missing parameter: name", nil)
|
||||
}
|
||||
|
||||
channelDisplayName := h.app.NormalizeChannelDisplayName(b.Name)
|
||||
channelInternalName := h.app.NormalizeChannelInternalName(b.Name)
|
||||
|
||||
channelExisting, err := h.database.GetChannelByName(ctx, u.UserID, channelInternalName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
if len(channelDisplayName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
}
|
||||
if len(strings.TrimSpace(channelDisplayName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
|
||||
}
|
||||
if len(channelInternalName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
}
|
||||
if len(strings.TrimSpace(channelInternalName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel internalname cannot be empty"), nil)
|
||||
}
|
||||
|
||||
if channelExisting != nil {
|
||||
return ginresp.APIError(g, 409, apierr.CHANNEL_ALREADY_EXISTS, "Channel with this name already exists", nil)
|
||||
}
|
||||
|
||||
subscribeKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
channel, err := h.database.CreateChannel(ctx, u.UserID, channelDisplayName, channelInternalName, subscribeKey, b.Description)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create channel", err)
|
||||
}
|
||||
|
||||
if langext.Coalesce(b.Subscribe, true) {
|
||||
|
||||
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, true)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(langext.Ptr(sub)).JSON(true)))
|
||||
if b.Name == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Missing parameter: name", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
channelDisplayName := h.app.NormalizeChannelDisplayName(b.Name)
|
||||
channelInternalName := h.app.NormalizeChannelInternalName(b.Name)
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
|
||||
channelExisting, err := h.database.GetChannelByName(ctx, u.UserID, channelInternalName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
}
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
if len(channelDisplayName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
}
|
||||
if len(strings.TrimSpace(channelDisplayName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
|
||||
}
|
||||
if len(channelInternalName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
}
|
||||
if len(strings.TrimSpace(channelInternalName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel internalname cannot be empty"), nil)
|
||||
}
|
||||
|
||||
if channelExisting != nil {
|
||||
return ginresp.APIError(g, 409, apierr.CHANNEL_ALREADY_EXISTS, "Channel with this name already exists", nil)
|
||||
}
|
||||
|
||||
subscribeKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
channel, err := h.database.CreateChannel(ctx, u.UserID, channelDisplayName, channelInternalName, subscribeKey, b.Description)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create channel", err)
|
||||
}
|
||||
|
||||
if langext.Coalesce(b.Subscribe, true) {
|
||||
|
||||
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, true)
|
||||
if err != nil {
|
||||
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)))
|
||||
|
||||
} else {
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, channel.WithSubscription(nil).JSON(true)))
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateChannel swaggerdoc
|
||||
@@ -296,84 +302,88 @@ func (h APIHandler) UpdateChannel(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
if langext.Coalesce(b.RefreshSubscribeKey, false) {
|
||||
newkey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
err := h.database.UpdateChannelSubscribeKey(ctx, u.ChannelID, newkey)
|
||||
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.DisplayName != nil {
|
||||
|
||||
newDisplayName := h.app.NormalizeChannelDisplayName(*b.DisplayName)
|
||||
|
||||
if len(newDisplayName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(newDisplayName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 400, apierr.USER_NOT_FOUND, "User not found", nil)
|
||||
}
|
||||
|
||||
err := h.database.UpdateChannelDisplayName(ctx, u.ChannelID, newDisplayName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
}
|
||||
if langext.Coalesce(b.RefreshSubscribeKey, false) {
|
||||
newkey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
if b.DescriptionName != nil {
|
||||
|
||||
var descName *string = nil
|
||||
if strings.TrimSpace(*b.DescriptionName) != "" {
|
||||
descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName))
|
||||
err := h.database.UpdateChannelSubscribeKey(ctx, u.ChannelID, newkey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
}
|
||||
}
|
||||
|
||||
if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil)
|
||||
if b.DisplayName != nil {
|
||||
|
||||
newDisplayName := h.app.NormalizeChannelDisplayName(*b.DisplayName)
|
||||
|
||||
if len(newDisplayName) > user.MaxChannelNameLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_TOO_LONG, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(newDisplayName)) == 0 {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NAME_EMPTY, fmt.Sprintf("Channel displayname cannot be empty"), nil)
|
||||
}
|
||||
|
||||
err := h.database.UpdateChannelDisplayName(ctx, u.ChannelID, newDisplayName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName)
|
||||
if b.DescriptionName != nil {
|
||||
|
||||
var descName *string = nil
|
||||
if strings.TrimSpace(*b.DescriptionName) != "" {
|
||||
descName = langext.Ptr(strings.TrimSpace(*b.DescriptionName))
|
||||
}
|
||||
|
||||
if descName != nil && len(*descName) > user.MaxChannelDescriptionLength() {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_DESCRIPTION_TOO_LONG, fmt.Sprintf("Channel-Description too long (max %d characters)", user.MaxChannelDescriptionLength()), nil)
|
||||
}
|
||||
|
||||
err := h.database.UpdateChannelDescriptionName(ctx, u.ChannelID, descName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channel", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
|
||||
}
|
||||
|
||||
}
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
|
||||
|
||||
channel, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) channel", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSON(true)))
|
||||
})
|
||||
}
|
||||
|
||||
// ListChannelMessages swaggerdoc
|
||||
@@ -416,50 +426,54 @@ func (h APIHandler) ListChannelMessages(pctx ginext.PreContext) ginext.HTTPRespo
|
||||
|
||||
var u uri
|
||||
var q query
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Query(&q).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
trimmed := langext.Coalesce(q.Trimmed, true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
maxPageSize := langext.Conditional(trimmed, 16, 256)
|
||||
trimmed := langext.Coalesce(q.Trimmed, true)
|
||||
|
||||
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
|
||||
maxPageSize := langext.Conditional(trimmed, 16, 256)
|
||||
|
||||
channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
|
||||
|
||||
if permResp := ctx.CheckPermissionChanMessagesRead(channel.Channel); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
channel, err := h.database.GetChannel(ctx, u.ChannelUserID, u.ChannelID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionChanMessagesRead(channel.Channel); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
filter := models.MessageFilter{
|
||||
ChannelID: langext.Ptr([]models.ChannelID{channel.ChannelID}),
|
||||
}
|
||||
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
||||
}
|
||||
|
||||
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
||||
}
|
||||
filter := models.MessageFilter{
|
||||
ChannelID: langext.Ptr([]models.ChannelID{channel.ChannelID}),
|
||||
}
|
||||
|
||||
var res []models.MessageJSON
|
||||
if trimmed {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
|
||||
} else {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
|
||||
}
|
||||
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
|
||||
var res []models.MessageJSON
|
||||
if trimmed {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
|
||||
} else {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -34,24 +35,28 @@ func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
clients, err := h.database.ListClients(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
|
||||
clients, err := h.database.ListClients(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
|
||||
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetClient swaggerdoc
|
||||
@@ -77,25 +82,29 @@ func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// AddClient swaggerdoc
|
||||
@@ -128,32 +137,36 @@ func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if !b.ClientType.Valid() {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
||||
}
|
||||
clientType := b.ClientType
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
if !b.ClientType.Valid() {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
||||
}
|
||||
clientType := b.ClientType
|
||||
|
||||
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.Name)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
|
||||
}
|
||||
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
client, err := h.database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.Name)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteClient swaggerdoc
|
||||
@@ -179,30 +192,34 @@ func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
err = h.database.DeleteClient(ctx, u.ClientID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
||||
}
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
err = h.database.DeleteClient(ctx, u.ClientID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateClient swaggerdoc
|
||||
@@ -239,69 +256,73 @@ func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
if b.FCMToken != nil && *b.FCMToken != client.FCMToken {
|
||||
|
||||
err = h.database.DeleteClientsByFCM(ctx, *b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
err = h.database.UpdateClientFCMToken(ctx, u.ClientID, *b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.AgentModel != nil {
|
||||
err = h.database.UpdateClientAgentModel(ctx, u.ClientID, *b.AgentModel)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.AgentVersion != nil {
|
||||
err = h.database.UpdateClientAgentVersion(ctx, u.ClientID, *b.AgentVersion)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
}
|
||||
if b.FCMToken != nil && *b.FCMToken != client.FCMToken {
|
||||
|
||||
if b.Name != nil {
|
||||
if *b.Name == "" {
|
||||
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, nil)
|
||||
err = h.database.DeleteClientsByFCM(ctx, *b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
}
|
||||
} else {
|
||||
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, langext.Ptr(*b.Name))
|
||||
|
||||
err = h.database.UpdateClientFCMToken(ctx, u.ClientID, *b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client, err = h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
|
||||
}
|
||||
if b.AgentModel != nil {
|
||||
err = h.database.UpdateClientAgentModel(ctx, u.ClientID, *b.AgentModel)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
if b.AgentVersion != nil {
|
||||
err = h.database.UpdateClientAgentVersion(ctx, u.ClientID, *b.AgentVersion)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.Name != nil {
|
||||
if *b.Name == "" {
|
||||
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, nil)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
} else {
|
||||
err = h.database.UpdateClientDescriptionName(ctx, u.ClientID, langext.Ptr(*b.Name))
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
client, err = h.database.GetClient(ctx, u.UserID, u.ClientID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -36,24 +37,28 @@ func (h APIHandler) ListUserKeys(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
toks, err := h.database.ListKeyTokens(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
|
||||
toks, err := h.database.ListKeyTokens(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query keys", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
|
||||
res := langext.ArrMap(toks, func(v models.KeyToken) models.KeyTokenJSON { return v.JSON() })
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Keys: res}))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetCurrentUserKey swaggerdoc
|
||||
@@ -79,30 +84,34 @@ func (h APIHandler) GetCurrentUserKey(pctx ginext.PreContext) ginext.HTTPRespons
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
tokid := ctx.GetPermissionKeyTokenID()
|
||||
if tokid == nil {
|
||||
return ginresp.APIError(g, 400, apierr.USER_AUTH_FAILED, "Missing KeyTokenID in context", nil)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, *tokid)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
tokid := ctx.GetPermissionKeyTokenID()
|
||||
if tokid == nil {
|
||||
return ginresp.APIError(g, 400, apierr.USER_AUTH_FAILED, "Missing KeyTokenID in context", nil)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, *tokid)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON().WithToken(keytoken.Token)))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetUserKey swaggerdoc
|
||||
@@ -129,25 +138,29 @@ func (h APIHandler) GetUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUserKey swaggerdoc
|
||||
@@ -182,70 +195,74 @@ func (h APIHandler) UpdateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
if b.Name != nil {
|
||||
err := h.database.UpdateKeyTokenName(ctx, u.KeyID, *b.Name)
|
||||
keytoken, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update name", err)
|
||||
}
|
||||
keytoken.Name = *b.Name
|
||||
}
|
||||
|
||||
if b.Permissions != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
permlist := models.ParseTokenPermissionList(*b.Permissions)
|
||||
err := h.database.UpdateKeyTokenPermissions(ctx, u.KeyID, permlist)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update permissions", err)
|
||||
}
|
||||
keytoken.Permissions = permlist
|
||||
}
|
||||
|
||||
if b.AllChannels != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
if b.Name != nil {
|
||||
err := h.database.UpdateKeyTokenName(ctx, u.KeyID, *b.Name)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update name", err)
|
||||
}
|
||||
keytoken.Name = *b.Name
|
||||
}
|
||||
|
||||
err := h.database.UpdateKeyTokenAllChannels(ctx, u.KeyID, *b.AllChannels)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update all_channels", err)
|
||||
}
|
||||
keytoken.AllChannels = *b.AllChannels
|
||||
}
|
||||
if b.Permissions != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
}
|
||||
|
||||
if b.Channels != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
permlist := models.ParseTokenPermissionList(*b.Permissions)
|
||||
err := h.database.UpdateKeyTokenPermissions(ctx, u.KeyID, permlist)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update permissions", err)
|
||||
}
|
||||
keytoken.Permissions = permlist
|
||||
}
|
||||
|
||||
err := h.database.UpdateKeyTokenChannels(ctx, u.KeyID, *b.Channels)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channels", err)
|
||||
}
|
||||
keytoken.Channels = *b.Channels
|
||||
}
|
||||
if b.AllChannels != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
|
||||
err := h.database.UpdateKeyTokenAllChannels(ctx, u.KeyID, *b.AllChannels)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update all_channels", err)
|
||||
}
|
||||
keytoken.AllChannels = *b.AllChannels
|
||||
}
|
||||
|
||||
if b.Channels != nil {
|
||||
if keytoken.KeyTokenID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFUPDATE_KEY, "Cannot update the currently used key", err)
|
||||
}
|
||||
|
||||
err := h.database.UpdateKeyTokenChannels(ctx, u.KeyID, *b.Channels)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update channels", err)
|
||||
}
|
||||
keytoken.Channels = *b.Channels
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// CreateUserKey swaggerdoc
|
||||
@@ -278,43 +295,47 @@ func (h APIHandler) CreateUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
channels := langext.Coalesce(b.Channels, make([]models.ChannelID, 0))
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
var allChan bool
|
||||
if b.AllChannels == nil && b.Channels != nil {
|
||||
allChan = false
|
||||
} else if b.AllChannels == nil && b.Channels == nil {
|
||||
allChan = true
|
||||
} else {
|
||||
allChan = *b.AllChannels
|
||||
}
|
||||
channels := langext.Coalesce(b.Channels, make([]models.ChannelID, 0))
|
||||
|
||||
for _, c := range channels {
|
||||
if err := c.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Invalid ChannelID", err)
|
||||
var allChan bool
|
||||
if b.AllChannels == nil && b.Channels != nil {
|
||||
allChan = false
|
||||
} else if b.AllChannels == nil && b.Channels == nil {
|
||||
allChan = true
|
||||
} else {
|
||||
allChan = *b.AllChannels
|
||||
}
|
||||
}
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
for _, c := range channels {
|
||||
if err := c.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Invalid ChannelID", err)
|
||||
}
|
||||
}
|
||||
|
||||
token := h.app.GenerateRandomAuthKey()
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
perms := models.ParseTokenPermissionList(b.Permissions)
|
||||
token := h.app.GenerateRandomAuthKey()
|
||||
|
||||
keytok, err := h.database.CreateKeyToken(ctx, b.Name, *ctx.GetPermissionUserID(), allChan, channels, perms, token)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create keytoken in db", err)
|
||||
}
|
||||
perms := models.ParseTokenPermissionList(b.Permissions)
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytok.JSON().WithToken(token)))
|
||||
keytok, err := h.database.CreateKeyToken(ctx, b.Name, *ctx.GetPermissionUserID(), allChan, channels, perms, token)
|
||||
if err != nil {
|
||||
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)))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteUserKey swaggerdoc
|
||||
@@ -341,32 +362,36 @@ func (h APIHandler) DeleteUserKey(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
client, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
if u.KeyID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFDELETE_KEY, "Cannot delete the currently used key", err)
|
||||
}
|
||||
client, err := h.database.GetKeyToken(ctx, u.UserID, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
err = h.database.DeleteKeyToken(ctx, u.KeyID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
||||
}
|
||||
if u.KeyID == *ctx.GetPermissionKeyTokenID() {
|
||||
return ginresp.APIError(g, 400, apierr.CANNOT_SELFDELETE_KEY, "Cannot delete the currently used key", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
err = h.database.DeleteKeyToken(ctx, u.KeyID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
||||
@@ -55,107 +56,111 @@ func (h APIHandler) ListMessages(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var q query
|
||||
ctx, errResp := h.app.StartRequest(g, nil, &q, nil, nil)
|
||||
ctx, g, errResp := pctx.Query(&q).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
trimmed := langext.Coalesce(q.Trimmed, true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
maxPageSize := langext.Conditional(trimmed, 16, 256)
|
||||
trimmed := langext.Coalesce(q.Trimmed, true)
|
||||
|
||||
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
|
||||
maxPageSize := langext.Conditional(trimmed, 16, 256)
|
||||
|
||||
if permResp := ctx.CheckPermissionSelfAllMessagesRead(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
pageSize := mathext.Clamp(langext.Coalesce(q.PageSize, 64), 1, maxPageSize)
|
||||
|
||||
userid := *ctx.GetPermissionUserID()
|
||||
|
||||
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
||||
}
|
||||
|
||||
err = h.database.UpdateUserLastRead(ctx, userid)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
|
||||
}
|
||||
|
||||
filter := models.MessageFilter{
|
||||
ConfirmedSubscriptionBy: langext.Ptr(userid),
|
||||
}
|
||||
|
||||
if q.Filter != nil && strings.TrimSpace(*q.Filter) != "" {
|
||||
filter.SearchString = langext.Ptr([]string{strings.TrimSpace(*q.Filter)})
|
||||
}
|
||||
|
||||
if len(q.Channels) != 0 {
|
||||
filter.ChannelNameCS = langext.Ptr(q.Channels)
|
||||
}
|
||||
|
||||
if len(q.ChannelIDs) != 0 {
|
||||
cids := make([]models.ChannelID, 0, len(q.ChannelIDs))
|
||||
for _, v := range q.ChannelIDs {
|
||||
cid := models.ChannelID(v)
|
||||
if err = cid.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid channel-id", err)
|
||||
}
|
||||
cids = append(cids, cid)
|
||||
if permResp := ctx.CheckPermissionSelfAllMessagesRead(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
filter.ChannelID = &cids
|
||||
}
|
||||
|
||||
if len(q.Senders) != 0 {
|
||||
filter.SenderNameCS = langext.Ptr(q.Senders)
|
||||
}
|
||||
userid := *ctx.GetPermissionUserID()
|
||||
|
||||
if q.TimeBefore != nil {
|
||||
t0, err := time.Parse(time.RFC3339, *q.TimeBefore)
|
||||
tok, err := ct.Decode(langext.Coalesce(q.NextPageToken, ""))
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid before-time", err)
|
||||
return ginresp.APIError(g, 400, apierr.PAGETOKEN_ERROR, "Failed to decode next_page_token", err)
|
||||
}
|
||||
filter.TimestampCoalesceBefore = &t0
|
||||
}
|
||||
|
||||
if q.TimeAfter != nil {
|
||||
t0, err := time.Parse(time.RFC3339, *q.TimeAfter)
|
||||
err = h.database.UpdateUserLastRead(ctx, userid)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid after-time", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update last-read", err)
|
||||
}
|
||||
filter.TimestampCoalesceAfter = &t0
|
||||
}
|
||||
|
||||
if len(q.Priority) != 0 {
|
||||
filter.Priority = langext.Ptr(q.Priority)
|
||||
}
|
||||
filter := models.MessageFilter{
|
||||
ConfirmedSubscriptionBy: langext.Ptr(userid),
|
||||
}
|
||||
|
||||
if len(q.KeyTokens) != 0 {
|
||||
tids := make([]models.KeyTokenID, 0, len(q.KeyTokens))
|
||||
for _, v := range q.KeyTokens {
|
||||
tid := models.KeyTokenID(v)
|
||||
if err = tid.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid keytoken-id", err)
|
||||
if q.Filter != nil && strings.TrimSpace(*q.Filter) != "" {
|
||||
filter.SearchString = langext.Ptr([]string{strings.TrimSpace(*q.Filter)})
|
||||
}
|
||||
|
||||
if len(q.Channels) != 0 {
|
||||
filter.ChannelNameCS = langext.Ptr(q.Channels)
|
||||
}
|
||||
|
||||
if len(q.ChannelIDs) != 0 {
|
||||
cids := make([]models.ChannelID, 0, len(q.ChannelIDs))
|
||||
for _, v := range q.ChannelIDs {
|
||||
cid := models.ChannelID(v)
|
||||
if err = cid.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid channel-id", err)
|
||||
}
|
||||
cids = append(cids, cid)
|
||||
}
|
||||
tids = append(tids, tid)
|
||||
filter.ChannelID = &cids
|
||||
}
|
||||
filter.UsedKeyID = &tids
|
||||
}
|
||||
|
||||
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
||||
}
|
||||
if len(q.Senders) != 0 {
|
||||
filter.SenderNameCS = langext.Ptr(q.Senders)
|
||||
}
|
||||
|
||||
var res []models.MessageJSON
|
||||
if trimmed {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.TrimmedJSON() })
|
||||
} else {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
|
||||
}
|
||||
if q.TimeBefore != nil {
|
||||
t0, err := time.Parse(time.RFC3339, *q.TimeBefore)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid before-time", err)
|
||||
}
|
||||
filter.TimestampCoalesceBefore = &t0
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
|
||||
if q.TimeAfter != nil {
|
||||
t0, err := time.Parse(time.RFC3339, *q.TimeAfter)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid after-time", err)
|
||||
}
|
||||
filter.TimestampCoalesceAfter = &t0
|
||||
}
|
||||
|
||||
if len(q.Priority) != 0 {
|
||||
filter.Priority = langext.Ptr(q.Priority)
|
||||
}
|
||||
|
||||
if len(q.KeyTokens) != 0 {
|
||||
tids := make([]models.KeyTokenID, 0, len(q.KeyTokens))
|
||||
for _, v := range q.KeyTokens {
|
||||
tid := models.KeyTokenID(v)
|
||||
if err = tid.Valid(); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid keytoken-id", err)
|
||||
}
|
||||
tids = append(tids, tid)
|
||||
}
|
||||
filter.UsedKeyID = &tids
|
||||
}
|
||||
|
||||
messages, npt, err := h.database.ListMessages(ctx, filter, &pageSize, tok)
|
||||
if err != nil {
|
||||
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() })
|
||||
} else {
|
||||
res = langext.ArrMap(messages, func(v models.Message) models.MessageJSON { return v.FullJSON() })
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Messages: res, NextPageToken: npt.Token(), PageSize: pageSize}))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetMessage swaggerdoc
|
||||
@@ -182,50 +187,54 @@ func (h APIHandler) GetMessage(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
msg, err := h.database.GetMessage(ctx, u.MessageID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
// either we have direct read permissions (it is our message + read/admin key)
|
||||
// or we subscribe (+confirmed) to the channel and have read/admin key
|
||||
|
||||
if ctx.CheckPermissionMessageRead(msg) {
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
}
|
||||
|
||||
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
|
||||
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
|
||||
msg, err := h.database.GetMessage(ctx, u.MessageID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if sub == nil {
|
||||
// not subbed
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
if !sub.Confirmed {
|
||||
// sub not confirmed
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
||||
}
|
||||
|
||||
// => perm okay
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
}
|
||||
// either we have direct read permissions (it is our message + read/admin key)
|
||||
// or we subscribe (+confirmed) to the channel and have read/admin key
|
||||
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
if ctx.CheckPermissionMessageRead(msg) {
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
}
|
||||
|
||||
if uid := ctx.GetPermissionUserID(); uid != nil && ctx.CheckPermissionUserRead(*uid) == nil {
|
||||
sub, err := h.database.GetSubscriptionBySubscriber(ctx, *uid, msg.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if sub == nil {
|
||||
// not subbed
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
if !sub.Confirmed {
|
||||
// sub not confirmed
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
|
||||
// => perm okay
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
}
|
||||
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteMessage swaggerdoc
|
||||
@@ -250,37 +259,41 @@ func (h APIHandler) DeleteMessage(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
msg, err := h.database.GetMessage(ctx, u.MessageID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
if !ctx.CheckPermissionMessageDelete(msg) {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
msg, err := h.database.GetMessage(ctx, u.MessageID, false)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.MESSAGE_NOT_FOUND, "message not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query message", err)
|
||||
}
|
||||
|
||||
err = h.database.DeleteMessage(ctx, msg.MessageID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err)
|
||||
}
|
||||
if !ctx.CheckPermissionMessageDelete(msg) {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
|
||||
err = h.database.CancelPendingDeliveries(ctx, msg.MessageID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
|
||||
}
|
||||
err = h.database.DeleteMessage(ctx, msg.MessageID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete message", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
err = h.database.CancelPendingDeliveries(ctx, msg.MessageID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to cancel deliveries", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, msg.FullJSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -31,25 +32,29 @@ func (h APIHandler) GetUserPreview(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, user.JSONPreview()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetChannelPreview swaggerdoc
|
||||
@@ -73,25 +78,29 @@ func (h APIHandler) GetChannelPreview(pctx ginext.PreContext) ginext.HTTPRespons
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
channel, err := h.database.GetChannelByID(ctx, u.ChannelID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
|
||||
channel, err := h.database.GetChannelByID(ctx, u.ChannelID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, channel.JSONPreview()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetUserKeyPreview swaggerdoc
|
||||
@@ -115,23 +124,27 @@ func (h APIHandler) GetUserKeyPreview(pctx ginext.PreContext) ginext.HTTPRespons
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
|
||||
keytoken, err := h.database.GetKeyTokenByID(ctx, u.KeyID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.KEY_NOT_FOUND, "Key not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, keytoken.JSONPreview()))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -64,71 +65,75 @@ func (h APIHandler) ListUserSubscriptions(pctx ginext.PreContext) ginext.HTTPRes
|
||||
|
||||
var u uri
|
||||
var q query
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Query(&q).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
filter := models.SubscriptionFilter{}
|
||||
filter.AnyUserID = langext.Ptr(u.UserID)
|
||||
|
||||
if q.Direction != nil {
|
||||
if strings.EqualFold(*q.Direction, "incoming") {
|
||||
filter.ChannelOwnerUserID = langext.Ptr([]models.UserID{u.UserID})
|
||||
} else if strings.EqualFold(*q.Direction, "outgoing") {
|
||||
filter.SubscriberUserID = langext.Ptr([]models.UserID{u.UserID})
|
||||
} else if strings.EqualFold(*q.Direction, "both") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'direction'", nil)
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
}
|
||||
|
||||
if q.Confirmation != nil {
|
||||
if strings.EqualFold(*q.Confirmation, "confirmed") {
|
||||
filter.Confirmed = langext.PTrue
|
||||
} else if strings.EqualFold(*q.Confirmation, "unconfirmed") {
|
||||
filter.Confirmed = langext.PFalse
|
||||
} else if strings.EqualFold(*q.Confirmation, "all") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'confirmation'", nil)
|
||||
filter := models.SubscriptionFilter{}
|
||||
filter.AnyUserID = langext.Ptr(u.UserID)
|
||||
|
||||
if q.Direction != nil {
|
||||
if strings.EqualFold(*q.Direction, "incoming") {
|
||||
filter.ChannelOwnerUserID = langext.Ptr([]models.UserID{u.UserID})
|
||||
} else if strings.EqualFold(*q.Direction, "outgoing") {
|
||||
filter.SubscriberUserID = langext.Ptr([]models.UserID{u.UserID})
|
||||
} else if strings.EqualFold(*q.Direction, "both") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'direction'", nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if q.External != nil {
|
||||
if strings.EqualFold(*q.External, "true") {
|
||||
filter.SubscriberIsChannelOwner = langext.PFalse
|
||||
} else if strings.EqualFold(*q.External, "false") {
|
||||
filter.SubscriberIsChannelOwner = langext.PTrue
|
||||
} else if strings.EqualFold(*q.External, "all") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'external'", nil)
|
||||
if q.Confirmation != nil {
|
||||
if strings.EqualFold(*q.Confirmation, "confirmed") {
|
||||
filter.Confirmed = langext.PTrue
|
||||
} else if strings.EqualFold(*q.Confirmation, "unconfirmed") {
|
||||
filter.Confirmed = langext.PFalse
|
||||
} else if strings.EqualFold(*q.Confirmation, "all") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'confirmation'", nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if q.SubscriberUserID != nil {
|
||||
filter.SubscriberUserID2 = langext.Ptr([]models.UserID{*q.SubscriberUserID})
|
||||
}
|
||||
if q.External != nil {
|
||||
if strings.EqualFold(*q.External, "true") {
|
||||
filter.SubscriberIsChannelOwner = langext.PFalse
|
||||
} else if strings.EqualFold(*q.External, "false") {
|
||||
filter.SubscriberIsChannelOwner = langext.PTrue
|
||||
} else if strings.EqualFold(*q.External, "all") {
|
||||
// both
|
||||
} else {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_QUERY_PARAM, "Invalid value for param 'external'", nil)
|
||||
}
|
||||
}
|
||||
|
||||
if q.ChannelOwnerUserID != nil {
|
||||
filter.ChannelOwnerUserID2 = langext.Ptr([]models.UserID{*q.ChannelOwnerUserID})
|
||||
}
|
||||
if q.SubscriberUserID != nil {
|
||||
filter.SubscriberUserID2 = langext.Ptr([]models.UserID{*q.SubscriberUserID})
|
||||
}
|
||||
|
||||
res, err := h.database.ListSubscriptions(ctx, filter)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
|
||||
}
|
||||
if q.ChannelOwnerUserID != nil {
|
||||
filter.ChannelOwnerUserID2 = langext.Ptr([]models.UserID{*q.ChannelOwnerUserID})
|
||||
}
|
||||
|
||||
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
||||
res, err := h.database.ListSubscriptions(ctx, filter)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscriptions", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
|
||||
jsonres := langext.ArrMap(res, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: jsonres}))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// ListChannelSubscriptions swaggerdoc
|
||||
@@ -157,32 +162,36 @@ func (h APIHandler) ListChannelSubscriptions(pctx ginext.PreContext) ginext.HTTP
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
clients, 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)
|
||||
}
|
||||
_, err := h.database.GetChannel(ctx, u.UserID, u.ChannelID, true)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
|
||||
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
||||
clients, 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)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
|
||||
res := langext.ArrMap(clients, func(v models.Subscription) models.SubscriptionJSON { return v.JSON() })
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{Subscriptions: res}))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// GetSubscription swaggerdoc
|
||||
@@ -208,28 +217,32 @@ func (h APIHandler) GetSubscription(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// CancelSubscription swaggerdoc
|
||||
@@ -255,33 +268,37 @@ func (h APIHandler) CancelSubscription(pctx ginext.PreContext) ginext.HTTPRespon
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
err = h.database.DeleteSubscription(ctx, u.SubscriptionID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
|
||||
}
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
err = h.database.DeleteSubscription(ctx, u.SubscriptionID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete subscription", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// CreateSubscription swaggerdoc
|
||||
@@ -317,76 +334,80 @@ func (h APIHandler) CreateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
|
||||
var u uri
|
||||
var q query
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Query(&q).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Query(&q).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
var channel models.Channel
|
||||
|
||||
if b.ChannelOwnerUserID != nil && b.ChannelInternalName != nil && b.ChannelID == nil {
|
||||
|
||||
channelInternalName := h.app.NormalizeChannelInternalName(*b.ChannelInternalName)
|
||||
|
||||
outchannel, err := h.database.GetChannelByName(ctx, *b.ChannelOwnerUserID, channelInternalName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if outchannel == nil {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
channel = *outchannel
|
||||
var channel models.Channel
|
||||
|
||||
} else if b.ChannelOwnerUserID == nil && b.ChannelInternalName == nil && b.ChannelID != nil {
|
||||
if b.ChannelOwnerUserID != nil && b.ChannelInternalName != nil && b.ChannelID == nil {
|
||||
|
||||
outchannel, err := h.database.GetChannelByID(ctx, *b.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if outchannel == nil {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
channelInternalName := h.app.NormalizeChannelInternalName(*b.ChannelInternalName)
|
||||
|
||||
channel = *outchannel
|
||||
|
||||
} else {
|
||||
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Must either supply [channel_owner_user_id, channel_internal_name] or [channel_id]", nil)
|
||||
|
||||
}
|
||||
|
||||
if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
|
||||
existingSub, err := h.database.GetSubscriptionBySubscriber(ctx, u.UserID, channel.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query existing subscription", err)
|
||||
}
|
||||
if existingSub != nil {
|
||||
if !existingSub.Confirmed && channel.OwnerUserID == u.UserID {
|
||||
err = h.database.UpdateSubscriptionConfirmed(ctx, existingSub.SubscriptionID, true)
|
||||
outchannel, err := h.database.GetChannelByName(ctx, *b.ChannelOwnerUserID, channelInternalName)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
existingSub.Confirmed = true
|
||||
if outchannel == nil {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
|
||||
channel = *outchannel
|
||||
|
||||
} else if b.ChannelOwnerUserID == nil && b.ChannelInternalName == nil && b.ChannelID != nil {
|
||||
|
||||
outchannel, err := h.database.GetChannelByID(ctx, *b.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query channel", err)
|
||||
}
|
||||
if outchannel == nil {
|
||||
return ginresp.APIError(g, 400, apierr.CHANNEL_NOT_FOUND, "Channel not found", err)
|
||||
}
|
||||
|
||||
channel = *outchannel
|
||||
|
||||
} else {
|
||||
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_BODY_PARAM, "Must either supply [channel_owner_user_id, channel_internal_name] or [channel_id]", nil)
|
||||
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
|
||||
}
|
||||
if channel.OwnerUserID != u.UserID && (q.ChanSubscribeKey == nil || *q.ChanSubscribeKey != channel.SubscribeKey) {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
|
||||
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
|
||||
}
|
||||
existingSub, err := h.database.GetSubscriptionBySubscriber(ctx, u.UserID, channel.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query existing subscription", err)
|
||||
}
|
||||
if existingSub != nil {
|
||||
if !existingSub.Confirmed && channel.OwnerUserID == u.UserID {
|
||||
err = h.database.UpdateSubscriptionConfirmed(ctx, existingSub.SubscriptionID, true)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
||||
}
|
||||
existingSub.Confirmed = true
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, existingSub.JSON()))
|
||||
}
|
||||
|
||||
sub, err := h.database.CreateSubscription(ctx, u.UserID, channel, channel.OwnerUserID == u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create subscription", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, sub.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateSubscription swaggerdoc
|
||||
@@ -417,43 +438,47 @@ func (h APIHandler) UpdateSubscription(pctx ginext.PreContext) ginext.HTTPRespon
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
userid := *ctx.GetPermissionUserID()
|
||||
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
|
||||
if b.Confirmed != nil {
|
||||
if subscription.ChannelOwnerUserID != userid {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
userid := *ctx.GetPermissionUserID()
|
||||
|
||||
subscription, err := h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_NOT_FOUND, "Subscription not found", err)
|
||||
}
|
||||
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if subscription.SubscriberUserID != u.UserID && subscription.ChannelOwnerUserID != u.UserID {
|
||||
return ginresp.APIError(g, 404, apierr.SUBSCRIPTION_USER_MISMATCH, "Subscription not found", nil)
|
||||
}
|
||||
}
|
||||
|
||||
subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
if b.Confirmed != nil {
|
||||
if subscription.ChannelOwnerUserID != userid {
|
||||
return ginresp.APIError(g, 401, apierr.USER_AUTH_FAILED, "You are not authorized for this action", nil)
|
||||
}
|
||||
err = h.database.UpdateSubscriptionConfirmed(ctx, u.SubscriptionID, *b.Confirmed)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update subscription", err)
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
subscription, err = h.database.GetSubscription(ctx, u.SubscriptionID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query subscription", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, subscription.JSON()))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"database/sql"
|
||||
"errors"
|
||||
@@ -39,99 +40,101 @@ func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.Body(&b).Start())
|
||||
ctx, g, errResp := pctx.Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
var clientType models.ClientType
|
||||
if !b.NoClient {
|
||||
if b.FCMToken == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing FCMToken", nil)
|
||||
}
|
||||
if b.AgentVersion == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing AgentVersion", nil)
|
||||
}
|
||||
if b.ClientType == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing ClientType", nil)
|
||||
}
|
||||
if !b.ClientType.Valid() {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Invalid ClientType", nil)
|
||||
}
|
||||
clientType = b.ClientType
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if b.ProToken != nil {
|
||||
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
||||
var clientType models.ClientType
|
||||
if !b.NoClient {
|
||||
if b.FCMToken == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing FCMToken", nil)
|
||||
}
|
||||
if b.AgentVersion == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing AgentVersion", nil)
|
||||
}
|
||||
if b.ClientType == "" {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing ClientType", nil)
|
||||
}
|
||||
if !b.ClientType.Valid() {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Invalid ClientType", nil)
|
||||
}
|
||||
clientType = b.ClientType
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
||||
}
|
||||
}
|
||||
|
||||
readKey := h.app.GenerateRandomAuthKey()
|
||||
sendKey := h.app.GenerateRandomAuthKey()
|
||||
adminKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
||||
if b.ProToken != nil {
|
||||
err := h.database.ClearProTokens(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing pro tokens", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readKey := h.app.GenerateRandomAuthKey()
|
||||
sendKey := h.app.GenerateRandomAuthKey()
|
||||
adminKey := h.app.GenerateRandomAuthKey()
|
||||
username := b.Username
|
||||
if username != nil {
|
||||
username = langext.Ptr(h.app.NormalizeUsername(*username))
|
||||
}
|
||||
|
||||
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
err := h.database.ClearProTokens(ctx, *b.ProToken)
|
||||
userobj, err := h.database.CreateUser(ctx, b.ProToken, username)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing pro tokens", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
||||
}
|
||||
}
|
||||
|
||||
username := b.Username
|
||||
if username != nil {
|
||||
username = langext.Ptr(h.app.NormalizeUsername(*username))
|
||||
}
|
||||
|
||||
userobj, err := h.database.CreateUser(ctx, b.ProToken, username)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
||||
}
|
||||
|
||||
_, err = h.database.CreateKeyToken(ctx, "AdminKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
|
||||
}
|
||||
|
||||
_, err = h.database.CreateKeyToken(ctx, "SendKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermChannelSend}, sendKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create send-key in db", err)
|
||||
}
|
||||
|
||||
_, err = h.database.CreateKeyToken(ctx, "ReadKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermUserRead, models.PermChannelRead}, readKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create read-key in db", err)
|
||||
}
|
||||
|
||||
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
|
||||
|
||||
if b.NoClient {
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
|
||||
} else {
|
||||
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
|
||||
_, err = h.database.CreateKeyToken(ctx, "AdminKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
|
||||
}
|
||||
|
||||
client, err := h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.ClientName)
|
||||
_, err = h.database.CreateKeyToken(ctx, "SendKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermChannelSend}, sendKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create send-key in db", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, userobj.JSONWithClients([]models.Client{client}, adminKey, sendKey, readKey)))
|
||||
}
|
||||
_, err = h.database.CreateKeyToken(ctx, "ReadKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermUserRead, models.PermChannelRead}, readKey)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create read-key in db", err)
|
||||
}
|
||||
|
||||
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)))
|
||||
} else {
|
||||
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
||||
}
|
||||
|
||||
client, err := h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.ClientName)
|
||||
if err != nil {
|
||||
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)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// GetUser swaggerdoc
|
||||
@@ -155,25 +158,30 @@ func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
|
||||
var u uri
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
|
||||
|
||||
})
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
|
||||
}
|
||||
|
||||
// UpdateUser swaggerdoc
|
||||
@@ -206,60 +214,63 @@ func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
var b body
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if b.Username != nil {
|
||||
username := langext.Ptr(h.app.NormalizeUsername(*b.Username))
|
||||
if *username == "" {
|
||||
username = nil
|
||||
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
||||
return *permResp
|
||||
}
|
||||
|
||||
err := h.database.UpdateUserUsername(ctx, u.UserID, username)
|
||||
if b.Username != nil {
|
||||
username := langext.Ptr(h.app.NormalizeUsername(*b.Username))
|
||||
if *username == "" {
|
||||
username = nil
|
||||
}
|
||||
|
||||
err := h.database.UpdateUserUsername(ctx, u.UserID, username)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
if *b.ProToken == "" {
|
||||
err := h.database.UpdateUserProToken(ctx, u.UserID, nil)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
}
|
||||
} else {
|
||||
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
||||
}
|
||||
|
||||
err = h.database.ClearProTokens(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
|
||||
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
|
||||
}
|
||||
}
|
||||
|
||||
if b.ProToken != nil {
|
||||
if *b.ProToken == "" {
|
||||
err := h.database.UpdateUserProToken(ctx, u.UserID, nil)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
}
|
||||
} else {
|
||||
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
||||
}
|
||||
|
||||
err = h.database.ClearProTokens(ctx, *b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
||||
}
|
||||
|
||||
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, u.UserID)
|
||||
if err != nil {
|
||||
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, user.JSON()))
|
||||
})
|
||||
}
|
||||
|
@@ -58,19 +58,23 @@ func (h CommonHandler) Ping(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(g.Request.Body)
|
||||
resuestBody := buf.String()
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, _ = buf.ReadFrom(g.Request.Body)
|
||||
resuestBody := buf.String()
|
||||
|
||||
return ginext.JSON(http.StatusOK, pingResponse{
|
||||
Message: "Pong",
|
||||
Info: pingResponseInfo{
|
||||
Method: g.Request.Method,
|
||||
Request: resuestBody,
|
||||
Headers: g.Request.Header,
|
||||
URI: g.Request.RequestURI,
|
||||
Address: g.Request.RemoteAddr,
|
||||
},
|
||||
})
|
||||
|
||||
return ginext.JSON(http.StatusOK, pingResponse{
|
||||
Message: "Pong",
|
||||
Info: pingResponseInfo{
|
||||
Method: g.Request.Method,
|
||||
Request: resuestBody,
|
||||
Headers: g.Request.Header,
|
||||
URI: g.Request.RequestURI,
|
||||
Address: g.Request.RemoteAddr,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -92,24 +96,28 @@ func (h CommonHandler) DatabaseTest(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
SourceID string `json:"sourceID"`
|
||||
}
|
||||
|
||||
ctx, _, errResp := pctx.Start()
|
||||
ctx, g, errResp := pctx.Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
libVersion, libVersionNumber, sourceID := sqlite3.Version()
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
err := h.app.Database.Ping(ctx)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
libVersion, libVersionNumber, sourceID := sqlite3.Version()
|
||||
|
||||
err := h.app.Database.Ping(ctx)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
return ginext.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
LibVersion: libVersion,
|
||||
LibVersionNumber: libVersionNumber,
|
||||
SourceID: sourceID,
|
||||
})
|
||||
|
||||
return ginext.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
LibVersion: libVersion,
|
||||
LibVersionNumber: libVersionNumber,
|
||||
SourceID: sourceID,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -128,52 +136,56 @@ func (h CommonHandler) Health(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
ctx, _, errResp := pctx.Start()
|
||||
ctx, g, errResp := pctx.Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
_, libVersionNumber, _ := sqlite3.Version()
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if libVersionNumber < 3039000 {
|
||||
return ginresp.InternalError(errors.New("sqlite version too low"))
|
||||
}
|
||||
_, libVersionNumber, _ := sqlite3.Version()
|
||||
|
||||
tctx := simplectx.CreateSimpleContext(ctx, nil)
|
||||
if libVersionNumber < 3039000 {
|
||||
return ginresp.InternalError(errors.New("sqlite version too low"))
|
||||
}
|
||||
|
||||
err := h.app.Database.Ping(tctx)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
tctx := simplectx.CreateSimpleContext(ctx, nil)
|
||||
|
||||
for _, subdb := range h.app.Database.List() {
|
||||
|
||||
uuidKey, _ := langext.NewHexUUID()
|
||||
uuidWrite, _ := langext.NewHexUUID()
|
||||
|
||||
err = subdb.WriteMetaString(tctx, uuidKey, uuidWrite)
|
||||
err := h.app.Database.Ping(tctx)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
uuidRead, err := subdb.ReadMetaString(tctx, uuidKey)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
for _, subdb := range h.app.Database.List() {
|
||||
|
||||
uuidKey, _ := langext.NewHexUUID()
|
||||
uuidWrite, _ := langext.NewHexUUID()
|
||||
|
||||
err = subdb.WriteMetaString(tctx, uuidKey, uuidWrite)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
uuidRead, err := subdb.ReadMetaString(tctx, uuidKey)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
if uuidRead == nil || uuidWrite != *uuidRead {
|
||||
return ginresp.InternalError(errors.New("writing into DB was not consistent"))
|
||||
}
|
||||
|
||||
err = subdb.DeleteMeta(tctx, uuidKey)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if uuidRead == nil || uuidWrite != *uuidRead {
|
||||
return ginresp.InternalError(errors.New("writing into DB was not consistent"))
|
||||
}
|
||||
return ginext.JSON(http.StatusOK, response{Status: "ok"})
|
||||
|
||||
err = subdb.DeleteMeta(tctx, uuidKey)
|
||||
if err != nil {
|
||||
return ginresp.InternalError(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ginext.JSON(http.StatusOK, response{Status: "ok"})
|
||||
})
|
||||
}
|
||||
|
||||
// Sleep swaggerdoc
|
||||
@@ -205,21 +217,25 @@ func (h CommonHandler) Sleep(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
t0 := time.Now().Format(time.RFC3339Nano)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err)
|
||||
}
|
||||
t0 := time.Now().Format(time.RFC3339Nano)
|
||||
|
||||
time.Sleep(timeext.FromSeconds(u.Seconds))
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_URI_PARAM, "Failed to read uri", err)
|
||||
}
|
||||
|
||||
t1 := time.Now().Format(time.RFC3339Nano)
|
||||
time.Sleep(timeext.FromSeconds(u.Seconds))
|
||||
|
||||
t1 := time.Now().Format(time.RFC3339Nano)
|
||||
|
||||
return ginext.JSON(http.StatusOK, response{
|
||||
Start: t0,
|
||||
End: t1,
|
||||
Duration: u.Seconds,
|
||||
})
|
||||
|
||||
return ginext.JSON(http.StatusOK, response{
|
||||
Start: t0,
|
||||
End: t1,
|
||||
Duration: u.Seconds,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -230,14 +246,18 @@ func (h CommonHandler) NoRoute(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return ginext.JSON(http.StatusNotFound, gin.H{
|
||||
"": "================ ROUTE NOT FOUND ================",
|
||||
"FullPath": g.FullPath(),
|
||||
"Method": g.Request.Method,
|
||||
"URL": g.Request.URL.String(),
|
||||
"RequestURI": g.Request.RequestURI,
|
||||
"Proto": g.Request.Proto,
|
||||
"Header": g.Request.Header,
|
||||
"~": "================ ROUTE NOT FOUND ================",
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
return ginext.JSON(http.StatusNotFound, gin.H{
|
||||
"": "================ ROUTE NOT FOUND ================",
|
||||
"FullPath": g.FullPath(),
|
||||
"Method": g.Request.Method,
|
||||
"URL": g.Request.URL.String(),
|
||||
"RequestURI": g.Request.RequestURI,
|
||||
"Proto": g.Request.Proto,
|
||||
"Header": g.Request.Header,
|
||||
"~": "================ ROUTE NOT FOUND ================",
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -74,61 +74,65 @@ func (h ExternalHandler) UptimeKuma(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
|
||||
var b body
|
||||
var q query
|
||||
ctx, httpErr := h.app.StartRequest(g, nil, &q, &b, nil)
|
||||
if httpErr != nil {
|
||||
return *httpErr
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
if b.Heartbeat == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'heartbeat' in request body", nil)
|
||||
}
|
||||
if b.Monitor == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'monitor' in request body", nil)
|
||||
}
|
||||
if b.Msg == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'msg' in request body", nil)
|
||||
}
|
||||
|
||||
title := langext.Conditional(b.Heartbeat.Status == 1, fmt.Sprintf("Monitor %v is back online", b.Monitor.Name), fmt.Sprintf("Monitor %v went down!", b.Monitor.Name))
|
||||
|
||||
content := b.Heartbeat.Msg
|
||||
|
||||
var timestamp *float64 = nil
|
||||
if tz, err := time.LoadLocation(b.Heartbeat.Timezone); err == nil {
|
||||
if ts, err := time.ParseInLocation("2006-01-02 15:04:05", b.Heartbeat.LocalDateTime, tz); err == nil {
|
||||
timestamp = langext.Ptr(float64(ts.Unix()))
|
||||
}
|
||||
}
|
||||
|
||||
var channel *string = nil
|
||||
if q.Channel != nil {
|
||||
channel = q.Channel
|
||||
}
|
||||
if q.ChannelUp != nil && b.Heartbeat.Status == 1 {
|
||||
channel = q.ChannelUp
|
||||
}
|
||||
if q.ChannelDown != nil && b.Heartbeat.Status != 1 {
|
||||
channel = q.ChannelDown
|
||||
}
|
||||
|
||||
var priority *int = nil
|
||||
if q.Priority != nil {
|
||||
priority = q.Priority
|
||||
}
|
||||
if q.PriorityUp != nil && b.Heartbeat.Status == 1 {
|
||||
priority = q.PriorityUp
|
||||
}
|
||||
if q.PriorityDown != nil && b.Heartbeat.Status != 1 {
|
||||
priority = q.PriorityDown
|
||||
}
|
||||
|
||||
okResp, errResp := h.app.SendMessage(g, ctx, q.UserID, q.KeyToken, channel, &title, &content, priority, nil, timestamp, q.SenderName)
|
||||
ctx, g, errResp := pctx.Query(&q).Body(&b).Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
|
||||
MessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
if b.Heartbeat == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'heartbeat' in request body", nil)
|
||||
}
|
||||
if b.Monitor == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'monitor' in request body", nil)
|
||||
}
|
||||
if b.Msg == nil {
|
||||
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "missing field 'msg' in request body", nil)
|
||||
}
|
||||
|
||||
title := langext.Conditional(b.Heartbeat.Status == 1, fmt.Sprintf("Monitor %v is back online", b.Monitor.Name), fmt.Sprintf("Monitor %v went down!", b.Monitor.Name))
|
||||
|
||||
content := b.Heartbeat.Msg
|
||||
|
||||
var timestamp *float64 = nil
|
||||
if tz, err := time.LoadLocation(b.Heartbeat.Timezone); err == nil {
|
||||
if ts, err := time.ParseInLocation("2006-01-02 15:04:05", b.Heartbeat.LocalDateTime, tz); err == nil {
|
||||
timestamp = langext.Ptr(float64(ts.Unix()))
|
||||
}
|
||||
}
|
||||
|
||||
var channel *string = nil
|
||||
if q.Channel != nil {
|
||||
channel = q.Channel
|
||||
}
|
||||
if q.ChannelUp != nil && b.Heartbeat.Status == 1 {
|
||||
channel = q.ChannelUp
|
||||
}
|
||||
if q.ChannelDown != nil && b.Heartbeat.Status != 1 {
|
||||
channel = q.ChannelDown
|
||||
}
|
||||
|
||||
var priority *int = nil
|
||||
if q.Priority != nil {
|
||||
priority = q.Priority
|
||||
}
|
||||
if q.PriorityUp != nil && b.Heartbeat.Status == 1 {
|
||||
priority = q.PriorityUp
|
||||
}
|
||||
if q.PriorityDown != nil && b.Heartbeat.Status != 1 {
|
||||
priority = q.PriorityDown
|
||||
}
|
||||
|
||||
okResp, errResp := h.app.SendMessage(g, ctx, q.UserID, q.KeyToken, channel, &title, &content, priority, nil, timestamp, q.SenderName)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
||||
MessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -77,30 +77,34 @@ func (h MessageHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
var b combined
|
||||
var q combined
|
||||
var f combined
|
||||
ctx, g, errResp := h.app.StartRequest(pctx.Form(&f).Query(&q).Body(&b).Start())
|
||||
ctx, g, errResp := pctx.Form(&f).Query(&q).Body(&b).IgnoreWrongContentType().Start()
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
// query has highest prio, then form, then json
|
||||
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
okResp, errResp := h.app.SendMessage(g, ctx, data.UserID, data.KeyToken, data.Channel, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
} else {
|
||||
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: langext.Conditional(okResp.MessageIsOld, "Message already sent", "Message sent"),
|
||||
SuppressSend: okResp.MessageIsOld,
|
||||
MessageCount: okResp.User.MessagesSent,
|
||||
Quota: okResp.User.QuotaUsedToday(),
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
}
|
||||
// query has highest prio, then form, then json
|
||||
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
|
||||
|
||||
okResp, errResp := h.app.SendMessage(g, ctx, data.UserID, data.KeyToken, data.Channel, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
} else {
|
||||
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: langext.Conditional(okResp.MessageIsOld, "Message already sent", "Message sent"),
|
||||
SuppressSend: okResp.MessageIsOld,
|
||||
MessageCount: okResp.User.MessagesSent,
|
||||
Quota: okResp.User.QuotaUsedToday(),
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
@@ -35,7 +35,9 @@ func (h WebsiteHandler) Index(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "index.html", true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "index.html", true)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) APIDocs(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -45,7 +47,9 @@ func (h WebsiteHandler) APIDocs(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "api.html", true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "api.html", true)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) APIDocsMore(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -55,7 +59,9 @@ func (h WebsiteHandler) APIDocsMore(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "api_more.html", true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "api_more.html", true)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) MessageSent(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -65,7 +71,9 @@ func (h WebsiteHandler) MessageSent(pctx ginext.PreContext) ginext.HTTPResponse
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "message_sent.html", true)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "message_sent.html", true)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) FaviconIco(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -75,7 +83,9 @@ func (h WebsiteHandler) FaviconIco(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "favicon.ico", false)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "favicon.ico", false)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) FaviconPNG(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -85,7 +95,9 @@ func (h WebsiteHandler) FaviconPNG(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "favicon.png", false)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "favicon.png", false)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -95,16 +107,19 @@ func (h WebsiteHandler) Javascript(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
type uri struct {
|
||||
Filename string `uri:"fn"`
|
||||
}
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginext.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
type uri struct {
|
||||
Filename string `uri:"fn"`
|
||||
}
|
||||
|
||||
return h.serveAsset(g, "js/"+u.Filename, false)
|
||||
var u uri
|
||||
if err := g.ShouldBindUri(&u); err != nil {
|
||||
return ginext.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
}
|
||||
|
||||
return h.serveAsset(g, "js/"+u.Filename, false)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
@@ -119,7 +134,9 @@ func (h WebsiteHandler) CSS(pctx ginext.PreContext) ginext.HTTPResponse {
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
return h.serveAsset(g, "css/"+u.Filename, false)
|
||||
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
||||
return h.serveAsset(g, "css/"+u.Filename, false)
|
||||
})
|
||||
}
|
||||
|
||||
func (h WebsiteHandler) serveAsset(g *gin.Context, fn string, repl bool) ginext.HTTPResponse {
|
||||
|
@@ -1,13 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/handler"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"blackforestbytes.com/simplecloudnotifier/swagger"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
||||
@@ -61,119 +59,117 @@ func (r *Router) Init(e *ginext.GinWrapper) error {
|
||||
return errors.New("failed to add validators - wrong engine")
|
||||
}
|
||||
|
||||
wrap := func(fn ginext.WHandlerFunc) ginext.WHandlerFunc{return Wrap(r.app, fn)}
|
||||
|
||||
// ================ General (unversioned) ================
|
||||
|
||||
commonAPI := e.Routes().Group("/api")
|
||||
{
|
||||
commonAPI.Any("/ping").Handle(wrap(r.commonHandler.Ping))
|
||||
commonAPI.POST("/db-test").Handle(wrap(r.commonHandler.DatabaseTest))
|
||||
commonAPI.GET("/health").Handle(wrap(r.commonHandler.Health))
|
||||
commonAPI.POST("/sleep/:secs").Handle(wrap(r.commonHandler.Sleep))
|
||||
commonAPI.Any("/ping").Handle(r.commonHandler.Ping)
|
||||
commonAPI.POST("/db-test").Handle(r.commonHandler.DatabaseTest)
|
||||
commonAPI.GET("/health").Handle(r.commonHandler.Health)
|
||||
commonAPI.POST("/sleep/:secs").Handle(r.commonHandler.Sleep)
|
||||
}
|
||||
|
||||
// ================ Swagger ================
|
||||
|
||||
docs := e.Routes().Group("/documentation")
|
||||
{
|
||||
docs.GET("/swagger").Handle(wrap(ginext.RedirectTemporary("/documentation/swagger/")))
|
||||
docs.GET("/swagger/*sub").Handle(wrap(swagger.Handle))
|
||||
docs.GET("/swagger").Handle(ginext.RedirectTemporary("/documentation/swagger/"))
|
||||
docs.GET("/swagger/*sub").Handle(swagger.Handle)
|
||||
}
|
||||
|
||||
// ================ Website ================
|
||||
|
||||
frontend := e.Routes().Group("")
|
||||
{
|
||||
frontend.GET("/").Handle(wrap(r.websiteHandler.Index))
|
||||
frontend.GET("/index.php").Handle(wrap(r.websiteHandler.Index))
|
||||
frontend.GET("/index.html").Handle(wrap(r.websiteHandler.Index))
|
||||
frontend.GET("/index").Handle(wrap(r.websiteHandler.Index))
|
||||
frontend.GET("/").Handle(r.websiteHandler.Index)
|
||||
frontend.GET("/index.php").Handle(r.websiteHandler.Index)
|
||||
frontend.GET("/index.html").Handle(r.websiteHandler.Index)
|
||||
frontend.GET("/index").Handle(r.websiteHandler.Index)
|
||||
|
||||
frontend.GET("/api").Handle(wrap(r.websiteHandler.APIDocs))
|
||||
frontend.GET("/api.php").Handle(wrap(r.websiteHandler.APIDocs))
|
||||
frontend.GET("/api.html").Handle(wrap(r.websiteHandler.APIDocs))
|
||||
frontend.GET("/api").Handle(r.websiteHandler.APIDocs)
|
||||
frontend.GET("/api.php").Handle(r.websiteHandler.APIDocs)
|
||||
frontend.GET("/api.html").Handle(r.websiteHandler.APIDocs)
|
||||
|
||||
frontend.GET("/api_more").Handle(wrap(r.websiteHandler.APIDocsMore))
|
||||
frontend.GET("/api_more.php").Handle(wrap(r.websiteHandler.APIDocsMore))
|
||||
frontend.GET("/api_more.html").Handle(wrap(r.websiteHandler.APIDocsMore))
|
||||
frontend.GET("/api_more").Handle(r.websiteHandler.APIDocsMore)
|
||||
frontend.GET("/api_more.php").Handle(r.websiteHandler.APIDocsMore)
|
||||
frontend.GET("/api_more.html").Handle(r.websiteHandler.APIDocsMore)
|
||||
|
||||
frontend.GET("/message_sent").Handle(wrap(r.websiteHandler.MessageSent))
|
||||
frontend.GET("/message_sent.php").Handle(wrap(r.websiteHandler.MessageSent))
|
||||
frontend.GET("/message_sent.html").Handle(wrap(r.websiteHandler.MessageSent))
|
||||
frontend.GET("/message_sent").Handle(r.websiteHandler.MessageSent)
|
||||
frontend.GET("/message_sent.php").Handle(r.websiteHandler.MessageSent)
|
||||
frontend.GET("/message_sent.html").Handle(r.websiteHandler.MessageSent)
|
||||
|
||||
frontend.GET("/favicon.ico").Handle(wrap(r.websiteHandler.FaviconIco))
|
||||
frontend.GET("/favicon.png").Handle(wrap(r.websiteHandler.FaviconPNG))
|
||||
frontend.GET("/favicon.ico").Handle(r.websiteHandler.FaviconIco)
|
||||
frontend.GET("/favicon.png").Handle(r.websiteHandler.FaviconPNG)
|
||||
|
||||
frontend.GET("/js/:fn").Handle(wrap(r.websiteHandler.Javascript))
|
||||
frontend.GET("/css/:fn").Handle(wrap(r.websiteHandler.CSS))
|
||||
frontend.GET("/js/:fn").Handle(r.websiteHandler.Javascript)
|
||||
frontend.GET("/css/:fn").Handle(r.websiteHandler.CSS)
|
||||
}
|
||||
|
||||
// ================ Compat (v1) ================
|
||||
|
||||
compat := e.Routes().Group("/api")
|
||||
{
|
||||
compat.GET("/register.php").Handle(wrap(r.compatHandler.Register))
|
||||
compat.GET("/info.php").Handle(wrap(r.compatHandler.Info))
|
||||
compat.GET("/ack.php").Handle(wrap(r.compatHandler.Ack))
|
||||
compat.GET("/requery.php").Handle(wrap(r.compatHandler.Requery))
|
||||
compat.GET("/update.php").Handle(wrap(r.compatHandler.Update))
|
||||
compat.GET("/expand.php").Handle(wrap(r.compatHandler.Expand))
|
||||
compat.GET("/upgrade.php").Handle(wrap(r.compatHandler.Upgrade))
|
||||
compat.GET("/register.php").Handle(r.compatHandler.Register)
|
||||
compat.GET("/info.php").Handle(r.compatHandler.Info)
|
||||
compat.GET("/ack.php").Handle(r.compatHandler.Ack)
|
||||
compat.GET("/requery.php").Handle(r.compatHandler.Requery)
|
||||
compat.GET("/update.php").Handle(r.compatHandler.Update)
|
||||
compat.GET("/expand.php").Handle(r.compatHandler.Expand)
|
||||
compat.GET("/upgrade.php").Handle(r.compatHandler.Upgrade)
|
||||
}
|
||||
|
||||
// ================ Manage API (v2) ================
|
||||
|
||||
apiv2 := e.Routes().Group("/api/v2/")
|
||||
{
|
||||
apiv2.POST("/users").Handle(wrap(r.apiHandler.CreateUser))
|
||||
apiv2.GET("/users/:uid").Handle(wrap(r.apiHandler.GetUser))
|
||||
apiv2.PATCH("/users/:uid").Handle(wrap(r.apiHandler.UpdateUser))
|
||||
apiv2.POST("/users").Handle(r.apiHandler.CreateUser)
|
||||
apiv2.GET("/users/:uid").Handle(r.apiHandler.GetUser)
|
||||
apiv2.PATCH("/users/:uid").Handle(r.apiHandler.UpdateUser)
|
||||
|
||||
apiv2.GET("/users/:uid/keys").Handle(wrap(r.apiHandler.ListUserKeys))
|
||||
apiv2.POST("/users/:uid/keys").Handle(wrap(r.apiHandler.CreateUserKey))
|
||||
apiv2.GET("/users/:uid/keys/current").Handle(wrap(r.apiHandler.GetCurrentUserKey))
|
||||
apiv2.GET("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.GetUserKey))
|
||||
apiv2.PATCH("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.UpdateUserKey))
|
||||
apiv2.DELETE("/users/:uid/keys/:kid").Handle(wrap(r.apiHandler.DeleteUserKey))
|
||||
apiv2.GET("/users/:uid/keys").Handle(r.apiHandler.ListUserKeys)
|
||||
apiv2.POST("/users/:uid/keys").Handle(r.apiHandler.CreateUserKey)
|
||||
apiv2.GET("/users/:uid/keys/current").Handle(r.apiHandler.GetCurrentUserKey)
|
||||
apiv2.GET("/users/:uid/keys/:kid").Handle(r.apiHandler.GetUserKey)
|
||||
apiv2.PATCH("/users/:uid/keys/:kid").Handle(r.apiHandler.UpdateUserKey)
|
||||
apiv2.DELETE("/users/:uid/keys/:kid").Handle(r.apiHandler.DeleteUserKey)
|
||||
|
||||
apiv2.GET("/users/:uid/clients").Handle(wrap(r.apiHandler.ListClients))
|
||||
apiv2.GET("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.GetClient))
|
||||
apiv2.PATCH("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.UpdateClient))
|
||||
apiv2.POST("/users/:uid/clients").Handle(wrap(r.apiHandler.AddClient))
|
||||
apiv2.DELETE("/users/:uid/clients/:cid").Handle(wrap(r.apiHandler.DeleteClient))
|
||||
apiv2.GET("/users/:uid/clients").Handle(r.apiHandler.ListClients)
|
||||
apiv2.GET("/users/:uid/clients/:cid").Handle(r.apiHandler.GetClient)
|
||||
apiv2.PATCH("/users/:uid/clients/:cid").Handle(r.apiHandler.UpdateClient)
|
||||
apiv2.POST("/users/:uid/clients").Handle(r.apiHandler.AddClient)
|
||||
apiv2.DELETE("/users/:uid/clients/:cid").Handle(r.apiHandler.DeleteClient)
|
||||
|
||||
apiv2.GET("/users/:uid/channels").Handle(wrap(r.apiHandler.ListChannels))
|
||||
apiv2.POST("/users/:uid/channels").Handle(wrap(r.apiHandler.CreateChannel))
|
||||
apiv2.GET("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.GetChannel))
|
||||
apiv2.PATCH("/users/:uid/channels/:cid").Handle(wrap(r.apiHandler.UpdateChannel))
|
||||
apiv2.GET("/users/:uid/channels/:cid/messages").Handle(wrap(r.apiHandler.ListChannelMessages))
|
||||
apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(wrap(r.apiHandler.ListChannelSubscriptions))
|
||||
apiv2.GET("/users/:uid/channels").Handle(r.apiHandler.ListChannels)
|
||||
apiv2.POST("/users/:uid/channels").Handle(r.apiHandler.CreateChannel)
|
||||
apiv2.GET("/users/:uid/channels/:cid").Handle(r.apiHandler.GetChannel)
|
||||
apiv2.PATCH("/users/:uid/channels/:cid").Handle(r.apiHandler.UpdateChannel)
|
||||
apiv2.GET("/users/:uid/channels/:cid/messages").Handle(r.apiHandler.ListChannelMessages)
|
||||
apiv2.GET("/users/:uid/channels/:cid/subscriptions").Handle(r.apiHandler.ListChannelSubscriptions)
|
||||
|
||||
apiv2.GET("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.ListUserSubscriptions))
|
||||
apiv2.POST("/users/:uid/subscriptions").Handle(wrap(r.apiHandler.CreateSubscription))
|
||||
apiv2.GET("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.GetSubscription))
|
||||
apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.CancelSubscription))
|
||||
apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(wrap(r.apiHandler.UpdateSubscription))
|
||||
apiv2.GET("/users/:uid/subscriptions").Handle(r.apiHandler.ListUserSubscriptions)
|
||||
apiv2.POST("/users/:uid/subscriptions").Handle(r.apiHandler.CreateSubscription)
|
||||
apiv2.GET("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.GetSubscription)
|
||||
apiv2.DELETE("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.CancelSubscription)
|
||||
apiv2.PATCH("/users/:uid/subscriptions/:sid").Handle(r.apiHandler.UpdateSubscription)
|
||||
|
||||
apiv2.GET("/messages").Handle(wrap(r.apiHandler.ListMessages))
|
||||
apiv2.GET("/messages/:mid").Handle(wrap(r.apiHandler.GetMessage))
|
||||
apiv2.DELETE("/messages/:mid").Handle(wrap(r.apiHandler.DeleteMessage))
|
||||
apiv2.GET("/messages").Handle(r.apiHandler.ListMessages)
|
||||
apiv2.GET("/messages/:mid").Handle(r.apiHandler.GetMessage)
|
||||
apiv2.DELETE("/messages/:mid").Handle(r.apiHandler.DeleteMessage)
|
||||
|
||||
apiv2.GET("/preview/users/:uid").Handle(wrap(r.apiHandler.GetUserPreview))
|
||||
apiv2.GET("/preview/keys/:kid").Handle(wrap(r.apiHandler.GetUserKeyPreview))
|
||||
apiv2.GET("/preview/channels/:cid").Handle(wrap(r.apiHandler.GetChannelPreview))
|
||||
apiv2.GET("/preview/users/:uid").Handle(r.apiHandler.GetUserPreview)
|
||||
apiv2.GET("/preview/keys/:kid").Handle(r.apiHandler.GetUserKeyPreview)
|
||||
apiv2.GET("/preview/channels/:cid").Handle(r.apiHandler.GetChannelPreview)
|
||||
}
|
||||
|
||||
// ================ Send API (unversioned) ================
|
||||
|
||||
sendAPI := e.Routes().Group("")
|
||||
{
|
||||
sendAPI.POST("/").Handle(wrap(r.messageHandler.SendMessage)
|
||||
sendAPI.POST("/send").Handle(wrap(r.messageHandler.SendMessage)
|
||||
sendAPI.POST("/send.php").Handle(wrap(r.compatHandler.SendMessage)
|
||||
sendAPI.POST("/").Handle(r.messageHandler.SendMessage)
|
||||
sendAPI.POST("/send").Handle(r.messageHandler.SendMessage)
|
||||
sendAPI.POST("/send.php").Handle(r.compatHandler.SendMessage)
|
||||
|
||||
sendAPI.POST("/external/v1/uptime-kuma").Handle(wrap(r.externalHandler.UptimeKuma)
|
||||
sendAPI.POST("/external/v1/uptime-kuma").Handle(r.externalHandler.UptimeKuma)
|
||||
|
||||
}
|
||||
|
||||
|
@@ -1,195 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
scn "blackforestbytes.com/simplecloudnotifier"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
||||
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/go-sqlite"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"math/rand"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestLogAcceptor interface {
|
||||
InsertRequestLog(data models.RequestLog)
|
||||
}
|
||||
|
||||
func Wrap(rlacc RequestLogAcceptor, fn ginext.WHandlerFunc) ginext.WHandlerFunc {
|
||||
|
||||
maxRetry := scn.Conf.RequestMaxRetry
|
||||
retrySleep := scn.Conf.RequestRetrySleep
|
||||
|
||||
return func(pctx *ginext.PreContext) {
|
||||
|
||||
reqctx := g.Request.Context()
|
||||
|
||||
if g.Request.Body != nil {
|
||||
g.Request.Body = dataext.NewBufferedReadCloser(g.Request.Body)
|
||||
}
|
||||
|
||||
t0 := time.Now()
|
||||
|
||||
for ctr := 1; ; ctr++ {
|
||||
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, g)
|
||||
if panicObj != nil {
|
||||
log.Error().Interface("panicObj", panicObj).Msg("Panic occured (in gin handler)")
|
||||
log.Error().Msg(stackTrace)
|
||||
wrap = ginresp.APIError(g, 500, apierr.PANIC, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v\n\n@:\n%s", panicObj, stackTrace)))
|
||||
}
|
||||
|
||||
if g.Writer.Written() {
|
||||
if scn.Conf.ReqLogEnabled {
|
||||
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, nil, langext.Ptr("Writing in WrapperFunc is not supported")))
|
||||
}
|
||||
panic("Writing in WrapperFunc is not supported")
|
||||
}
|
||||
|
||||
if ctr < maxRetry && isSqlite3Busy(wrap) {
|
||||
log.Warn().Int("counter", ctr).Str("url", g.Request.URL.String()).Msg("Retry request (ErrBusy)")
|
||||
|
||||
err := resetBody(g)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(int64(float64(retrySleep) * (0.5 + rand.Float64()))))
|
||||
continue
|
||||
}
|
||||
|
||||
if reqctx.Err() == nil {
|
||||
if scn.Conf.ReqLogEnabled {
|
||||
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil))
|
||||
}
|
||||
|
||||
if scw, ok := wrap.(ginext.InspectableHTTPResponse); ok {
|
||||
|
||||
statuscode := scw.Statuscode()
|
||||
if statuscode/100 != 2 {
|
||||
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode %d", statuscode))
|
||||
}
|
||||
} else {
|
||||
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode [unknown]"))
|
||||
}
|
||||
|
||||
wrap.Write(g)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func createRequestLog(g *gin.Context, t0 time.Time, ctr int, resp ginext.HTTPResponse, panicstr *string) models.RequestLog {
|
||||
|
||||
t1 := time.Now()
|
||||
|
||||
ua := g.Request.UserAgent()
|
||||
auth := g.Request.Header.Get("Authorization")
|
||||
ct := g.Request.Header.Get("Content-Type")
|
||||
|
||||
var reqbody []byte = nil
|
||||
if g.Request.Body != nil {
|
||||
brcbody, err := g.Request.Body.(dataext.BufferedReadCloser).BufferedAll()
|
||||
if err == nil {
|
||||
reqbody = brcbody
|
||||
}
|
||||
}
|
||||
var strreqbody *string = nil
|
||||
if len(reqbody) < scn.Conf.ReqLogMaxBodySize {
|
||||
strreqbody = langext.Ptr(string(reqbody))
|
||||
}
|
||||
|
||||
var respbody *string = nil
|
||||
|
||||
var strrespbody *string = nil
|
||||
if resp != nil {
|
||||
if resp2, ok := resp.(ginext.InspectableHTTPResponse); ok {
|
||||
respbody = resp2.BodyString(g)
|
||||
if respbody != nil && len(*respbody) < scn.Conf.ReqLogMaxBodySize {
|
||||
strrespbody = respbody
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
permObj, hasPerm := g.Get("perm")
|
||||
|
||||
hasTok := false
|
||||
if hasPerm {
|
||||
hasTok = permObj.(models.PermissionSet).Token != nil
|
||||
}
|
||||
|
||||
return models.RequestLog{
|
||||
Method: g.Request.Method,
|
||||
URI: g.Request.URL.String(),
|
||||
UserAgent: langext.Conditional(ua == "", nil, &ua),
|
||||
Authentication: langext.Conditional(auth == "", nil, &auth),
|
||||
RequestBody: strreqbody,
|
||||
RequestBodySize: int64(len(reqbody)),
|
||||
RequestContentType: ct,
|
||||
RemoteIP: g.RemoteIP(),
|
||||
KeyID: langext.ConditionalFn10(hasTok, func() *models.KeyTokenID { return langext.Ptr(permObj.(models.PermissionSet).Token.KeyTokenID) }, nil),
|
||||
UserID: langext.ConditionalFn10(hasTok, func() *models.UserID { return langext.Ptr(permObj.(models.PermissionSet).Token.OwnerUserID) }, nil),
|
||||
Permissions: langext.ConditionalFn10(hasTok, func() *string { return langext.Ptr(permObj.(models.PermissionSet).Token.Permissions.String()) }, nil),
|
||||
ResponseStatuscode: langext.ConditionalFn10(resp != nil, func() *int64 { return langext.Ptr(int64(resp.Statuscode())) }, nil),
|
||||
ResponseBodySize: langext.ConditionalFn10(strrespbody != nil, func() *int64 { return langext.Ptr(int64(len(*respbody))) }, nil),
|
||||
ResponseBody: strrespbody,
|
||||
ResponseContentType: langext.ConditionalFn10(resp != nil, func() string { return resp.ContentType() }, ""),
|
||||
RetryCount: int64(ctr),
|
||||
Panicked: panicstr != nil,
|
||||
PanicStr: panicstr,
|
||||
ProcessingTime: t1.Sub(t0),
|
||||
TimestampStart: t0,
|
||||
TimestampFinish: t1,
|
||||
}
|
||||
}
|
||||
|
||||
func callPanicSafe(fn ginext.WHandlerFunc, g ginext.PreContext) (res ginext.HTTPResponse, stackTrace string, panicObj any) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
res = nil
|
||||
stackTrace = string(debug.Stack())
|
||||
panicObj = rec
|
||||
}
|
||||
}()
|
||||
|
||||
res = fn(g)
|
||||
return res, "", nil
|
||||
}
|
||||
|
||||
func resetBody(g *gin.Context) error {
|
||||
if g.Request.Body == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := g.Request.Body.(dataext.BufferedReadCloser).Reset()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isSqlite3Busy(r ginext.HTTPResponse) bool {
|
||||
if errwrap, ok := r.(interface{ Unwrap() error }); ok && errwrap != nil {
|
||||
{
|
||||
var s3err *sqlite.Error
|
||||
if errors.As(errwrap.Unwrap(), &s3err) {
|
||||
if s3err.Code() == 5 { // [5] == SQLITE_BUSY
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user