2023-05-28 13:39:20 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
2024-07-16 17:19:55 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
2023-05-28 13:39:20 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
2023-07-30 15:58:37 +02:00
|
|
|
"errors"
|
2024-07-15 17:26:55 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2023-05-28 13:39:20 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListClients swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary List all clients
|
|
|
|
// @ID api-clients-list
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-05-28 17:04:44 +02:00
|
|
|
// @Param uid path string true "UserID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
|
|
|
// @Success 200 {object} handler.ListClients.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 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid}/clients [GET]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) ListClients(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
type response struct {
|
|
|
|
Clients []models.ClientJSON `json:"clients"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
clients, err := h.database.ListClients(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{Clients: res}))
|
|
|
|
|
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClient swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary Get a single client
|
|
|
|
// @ID api-clients-get
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-05-28 17:04:44 +02:00
|
|
|
// @Param uid path string true "UserID"
|
|
|
|
// @Param cid path string true "ClientID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
|
|
|
// @Success 200 {object} models.ClientJSON
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "client not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid}/clients/{cid} [GET]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) GetClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
ClientID models.ClientID `uri:"cid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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()))
|
|
|
|
|
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddClient swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary Add a new clients
|
|
|
|
// @ID api-clients-create
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-06-10 00:15:42 +02:00
|
|
|
// @Param uid path string true "UserID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
|
|
|
// @Param post_body body handler.AddClient.body false " "
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.ClientJSON
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid}/clients [POST]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) AddClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
type body struct {
|
2024-06-01 13:45:28 +02:00
|
|
|
FCMToken string `json:"fcm_token" binding:"required"`
|
|
|
|
AgentModel string `json:"agent_model" binding:"required"`
|
|
|
|
AgentVersion string `json:"agent_version" binding:"required"`
|
|
|
|
Name *string `json:"name"`
|
|
|
|
ClientType models.ClientType `json:"client_type" binding:"required"`
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
|
|
|
var b body
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if !b.ClientType.Valid() {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
|
|
|
|
}
|
|
|
|
clientType := b.ClientType
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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, 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()))
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteClient swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary Delete a client
|
|
|
|
// @ID api-clients-delete
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-05-28 17:04:44 +02:00
|
|
|
// @Param uid path string true "UserID"
|
|
|
|
// @Param cid path string true "ClientID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
|
|
|
// @Success 200 {object} models.ClientJSON
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "client not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid}/clients/{cid} [DELETE]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) DeleteClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
ClientID models.ClientID `uri:"cid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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()))
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
2023-05-28 23:25:18 +02:00
|
|
|
|
|
|
|
// UpdateClient swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary (Partially) update a client
|
|
|
|
// @Description The body-values are optional, only send the ones you want to update
|
|
|
|
// @ID api-client-update
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-06-10 00:15:42 +02:00
|
|
|
// @Param uid path string true "UserID"
|
|
|
|
// @Param cid path string true "ClientID"
|
2023-05-28 23:25:18 +02:00
|
|
|
//
|
|
|
|
// @Param clientname body string false "Change the clientname (send an empty string to clear it)"
|
|
|
|
// @Param pro_token body string false "Send a verification of premium purchase"
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.ClientJSON
|
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "client is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "client not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid}/clients/{cid} [PATCH]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) UpdateClient(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 23:25:18 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
ClientID models.ClientID `uri:"cid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
type body struct {
|
2024-06-01 13:45:28 +02:00
|
|
|
FCMToken *string `json:"fcm_token"`
|
|
|
|
AgentModel *string `json:"agent_model"`
|
|
|
|
AgentVersion *string `json:"agent_version"`
|
|
|
|
Name *string `json:"name"`
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
|
|
|
var b body
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
2023-05-28 23:25:18 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return h.app.DoRequest(ctx, g, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 23:25:18 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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)
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query client", err)
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if b.AgentModel != nil {
|
|
|
|
err = h.database.UpdateClientAgentModel(ctx, u.ClientID, *b.AgentModel)
|
2024-06-01 01:01:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
|
|
|
}
|
2024-07-16 17:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.AgentVersion != nil {
|
|
|
|
err = h.database.UpdateClientAgentVersion(ctx, u.ClientID, *b.AgentVersion)
|
2024-06-01 01:01:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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()))
|
2023-05-28 23:25:18 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2023-05-28 23:25:18 +02:00
|
|
|
}
|