SimpleCloudNotifier/scnserver/api/handler/apiClient.go

308 lines
9.9 KiB
Go
Raw Normal View History

2023-05-28 13:39:20 +02:00
package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
"blackforestbytes.com/simplecloudnotifier/models"
"database/sql"
"errors"
"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]
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
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
2023-05-28 13:39:20 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
clients, err := h.database.ListClients(ctx, u.UserID)
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query clients", err)
}
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return ctx.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]
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
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
2023-05-28 13:39:20 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if errors.Is(err, sql.ErrNoRows) {
2023-05-28 13:39:20 +02:00
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()))
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]
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 {
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
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
2023-05-28 13:39:20 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
2024-05-31 15:22:27 +02:00
if !b.ClientType.Valid() {
2023-05-28 13:39:20 +02:00
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
}
2024-05-31 15:22:27 +02:00
clientType := b.ClientType
2023-05-28 13:39:20 +02:00
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
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)
2023-05-28 13:39:20 +02:00
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
}
return ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
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]
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
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Start())
2023-05-28 13:39:20 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if errors.Is(err, sql.ErrNoRows) {
2023-05-28 13:39:20 +02:00
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 ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
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]
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 {
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
ctx, g, errResp := h.app.StartRequest(pctx.URI(&u).Body(&b).Start())
2023-05-28 23:25:18 +02:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
client, err := h.database.GetClient(ctx, u.UserID, u.ClientID)
if errors.Is(err, sql.ErrNoRows) {
2023-05-28 23:25:18 +02:00
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)
}
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)
}
}
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)
}
}
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 == "" {
2024-06-01 01:01:58 +02:00
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))
2024-06-01 01:01:58 +02:00
if err != nil {
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update client", err)
}
}
}
2023-05-28 23:25:18 +02:00
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 ctx.FinishSuccess(ginext.JSON(http.StatusOK, client.JSON()))
2023-05-28 23:25:18 +02:00
}