2024-09-19 19:46:46 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2024-09-20 16:33:45 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
2024-09-19 19:46:46 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2024-09-20 16:33:45 +02:00
|
|
|
"net/http"
|
2024-09-19 19:46:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListUserSenderNames swaggerdoc
|
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Summary List sender-names (of allthe messages of this user)
|
|
|
|
// @ID api-usersendernames-list
|
|
|
|
// @Tags API-v2
|
2024-09-19 19:46:46 +02:00
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Param uid path string true "UserID"
|
2024-09-19 19:46:46 +02:00
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Success 200 {object} handler.ListUserKeys.response
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "message not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
2024-09-19 19:46:46 +02:00
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Router /api/v2/users/{uid}/keys [GET]
|
2024-09-19 19:46:46 +02:00
|
|
|
func (h APIHandler) ListUserSenderNames(pctx ginext.PreContext) ginext.HTTPResponse {
|
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
type response struct {
|
2024-09-20 16:33:45 +02:00
|
|
|
SenderNames []models.SenderNameStatistics `json:"sender_names"`
|
2024-09-19 19:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
|
|
|
ctx, g, errResp := pctx.URI(&u).Start()
|
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
|
|
|
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
|
|
|
|
|
|
|
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
|
|
|
|
2024-09-20 16:33:45 +02:00
|
|
|
names, err := h.database.ListSenderNames(ctx, u.UserID, false)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{SenderNames: names}))
|
2024-09-19 19:46:46 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSenderNames swaggerdoc
|
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Summary List sender-names (of all messages this user can view, eitehr own or foreign-subscribed)
|
|
|
|
// @ID api-sendernames-list
|
|
|
|
// @Tags API-v2
|
2024-09-19 19:46:46 +02:00
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Success 200 {object} handler.ListSenderNames.response
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "message not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
2024-09-19 19:46:46 +02:00
|
|
|
//
|
2024-09-20 17:13:36 +02:00
|
|
|
// @Router /api/v2/sender-names [GET]
|
2024-09-19 19:46:46 +02:00
|
|
|
func (h APIHandler) ListSenderNames(pctx ginext.PreContext) ginext.HTTPResponse {
|
|
|
|
type response struct {
|
2024-09-20 16:33:45 +02:00
|
|
|
SenderNames []models.SenderNameStatistics `json:"sender_names"`
|
2024-09-19 19:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, g, errResp := pctx.Start()
|
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
|
|
|
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
|
|
|
|
|
|
|
if permResp := ctx.CheckPermissionAny(); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
|
|
|
|
2024-09-20 16:33:45 +02:00
|
|
|
userID := *ctx.GetPermissionUserID()
|
2024-09-19 19:46:46 +02:00
|
|
|
|
2024-09-20 16:33:45 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserRead(userID); permResp != nil {
|
2024-09-19 19:46:46 +02:00
|
|
|
return *permResp
|
|
|
|
}
|
|
|
|
|
2024-09-20 16:33:45 +02:00
|
|
|
names, err := h.database.ListSenderNames(ctx, userID, true)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query messages", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{SenderNames: names}))
|
2024-09-19 19:46:46 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|