Refactor server to go-sqlite and ginext [WIP]
This commit is contained in:
@@ -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 {
|
||||
|
Reference in New Issue
Block a user