SimpleCloudNotifier/server/api/handler/api.go

454 lines
13 KiB
Go
Raw Normal View History

2022-11-18 21:25:40 +01:00
package handler
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"blackforestbytes.com/simplecloudnotifier/api/models"
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
"blackforestbytes.com/simplecloudnotifier/logic"
2022-11-18 23:12:37 +01:00
"database/sql"
2022-11-18 21:25:40 +01:00
"github.com/gin-gonic/gin"
2022-11-18 23:28:37 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-11-18 21:25:40 +01:00
"net/http"
2022-11-18 23:28:37 +01:00
"regexp"
2022-11-18 21:25:40 +01:00
)
type APIHandler struct {
app *logic.Application
}
// CreateUser swaggerdoc
//
// @Summary Create a new user
// @ID api-user-create
//
2022-11-18 23:28:37 +01:00
// @Param post_body body handler.CreateUser.body false " "
2022-11-18 21:25:40 +01:00
//
2022-11-18 23:28:37 +01:00
// @Success 200 {object} models.UserJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
2022-11-18 21:25:40 +01:00
//
// @Router /api-v2/user/ [POST]
func (h APIHandler) CreateUser(g *gin.Context) ginresp.HTTPResponse {
type body struct {
2022-11-18 23:28:37 +01:00
FCMToken string `json:"fcm_token"`
ProToken *string `json:"pro_token"`
Username *string `json:"username"`
AgentModel string `json:"agent_model"`
AgentVersion string `json:"agent_version"`
ClientType string `json:"client_type"`
2022-11-18 21:25:40 +01:00
}
var b body
2022-11-18 23:12:37 +01:00
ctx, errResp := h.app.StartRequest(g, nil, nil, &b)
if errResp != nil {
return *errResp
2022-11-18 21:25:40 +01:00
}
2022-11-18 23:12:37 +01:00
defer ctx.Cancel()
2022-11-18 21:25:40 +01:00
var clientType models.ClientType
if b.ClientType == string(models.ClientTypeAndroid) {
clientType = models.ClientTypeAndroid
} else if b.ClientType == string(models.ClientTypeIOS) {
clientType = models.ClientTypeIOS
} else {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
2022-11-18 21:25:40 +01:00
}
if b.ProToken != nil {
ptok, err := h.app.VerifyProToken(*b.ProToken)
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
2022-11-18 21:25:40 +01:00
}
if !ptok {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
2022-11-18 21:25:40 +01:00
}
}
readKey := h.app.GenerateRandomAuthKey()
sendKey := h.app.GenerateRandomAuthKey()
adminKey := h.app.GenerateRandomAuthKey()
err := h.app.Database.ClearFCMTokens(ctx, b.FCMToken)
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
2022-11-18 21:25:40 +01:00
}
if b.ProToken != nil {
2022-11-18 23:28:37 +01:00
err := h.app.Database.ClearProTokens(ctx, *b.ProToken)
2022-11-18 21:25:40 +01:00
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
2022-11-18 21:25:40 +01:00
}
}
userobj, err := h.app.Database.CreateUser(ctx, readKey, sendKey, adminKey, b.ProToken, b.Username)
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
2022-11-18 21:25:40 +01:00
}
_, err = h.app.Database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
2022-11-18 21:25:40 +01:00
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, userobj.JSON()))
}
2022-11-18 23:12:37 +01:00
// GetUser swaggerdoc
//
2022-11-19 12:47:23 +01:00
// @Summary Get a user
2022-11-18 23:28:37 +01:00
// @ID api-user-get
2022-11-18 23:12:37 +01:00
//
2022-11-18 23:28:37 +01:00
// @Param uid path int true "UserID"
2022-11-18 23:12:37 +01:00
//
// @Success 200 {object} models.UserJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid} [GET]
2022-11-18 21:25:40 +01:00
func (h APIHandler) GetUser(g *gin.Context) ginresp.HTTPResponse {
2022-11-18 23:12:37 +01:00
type uri struct {
UserID int64 `uri:"uid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
user, err := h.app.Database.GetUser(ctx, u.UserID)
if err == sql.ErrNoRows {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(404, apierr.USER_NOT_FOUND, "User not found", err)
2022-11-18 23:12:37 +01:00
}
if err != nil {
2022-11-18 23:28:37 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query user", err)
2022-11-18 23:12:37 +01:00
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
2022-11-18 21:25:40 +01:00
}
2022-11-18 23:28:37 +01:00
// UpdateUser swaggerdoc
//
2022-11-19 12:47:23 +01:00
// @Summary (Partially) update a user
2022-11-18 23:28:37 +01:00
// @Description The body-values are optional, only send the ones you want to update
// @ID api-user-update
//
// @Param post_body body handler.UpdateUser.body false " "
//
// @Success 200 {object} models.UserJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid} [PATCH]
2022-11-18 21:25:40 +01:00
func (h APIHandler) UpdateUser(g *gin.Context) ginresp.HTTPResponse {
2022-11-18 23:28:37 +01:00
type uri struct {
UserID int64 `uri:"uid"`
}
type body struct {
Username *string `json:"username"`
ProToken *string `json:"pro_token"`
}
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
if b.Username != nil {
username := langext.Ptr(regexp.MustCompile(`[[:alnum:]\-_]`).ReplaceAllString(*b.Username, ""))
if *username == "" {
username = nil
}
err := h.app.Database.UpdateUserUsername(ctx, u.UserID, b.Username)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
}
if b.ProToken != nil {
ptok, err := h.app.VerifyProToken(*b.ProToken)
if err != nil {
return ginresp.InternAPIError(500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
}
if !ptok {
return ginresp.InternAPIError(400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
}
err = h.app.Database.ClearProTokens(ctx, *b.ProToken)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
}
err = h.app.Database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to update user", err)
}
}
user, err := h.app.Database.GetUser(ctx, u.UserID)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, user.JSON()))
2022-11-18 21:25:40 +01:00
}
2022-11-19 12:47:23 +01:00
// ListClients swaggerdoc
//
// @Summary List all clients
// @ID api-clients-list
//
// @Param uid path int true "UserID"
//
// @Success 200 {object} handler.ListClients.result
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid}/clients [GET]
2022-11-18 21:25:40 +01:00
func (h APIHandler) ListClients(g *gin.Context) ginresp.HTTPResponse {
2022-11-19 12:47:23 +01:00
type uri struct {
UserID int64 `uri:"uid"`
}
type result struct {
Clients []models.ClientJSON `json:"clients"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
clients, err := h.app.Database.ListClients(ctx, u.UserID)
if err != nil {
2022-11-19 12:50:41 +01:00
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query clients", err)
2022-11-19 12:47:23 +01:00
}
res := langext.ArrMap(clients, func(v models.Client) models.ClientJSON { return v.JSON() })
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, result{Clients: res}))
2022-11-18 21:25:40 +01:00
}
2022-11-19 12:50:41 +01:00
// GetClient swaggerdoc
//
2022-11-19 12:59:25 +01:00
// @Summary Get a single clients
2022-11-19 12:50:41 +01:00
// @ID api-clients-get
//
// @Param uid path int true "UserID"
// @Param cid path int true "ClientID"
//
// @Success 200 {object} models.ClientJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid}/clients/{cid} [GET]
2022-11-18 21:25:40 +01:00
func (h APIHandler) GetClient(g *gin.Context) ginresp.HTTPResponse {
2022-11-19 12:50:41 +01:00
type uri struct {
UserID int64 `uri:"uid"`
ClientID int64 `uri:"cid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
return *permResp
}
client, err := h.app.Database.GetClient(ctx, u.UserID, u.ClientID)
if err == sql.ErrNoRows {
return ginresp.InternAPIError(404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
}
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
2022-11-18 21:25:40 +01:00
}
2022-11-19 12:56:44 +01:00
// AddClient swaggerdoc
//
2022-11-19 12:59:25 +01:00
// @Summary Add a new clients
// @ID api-clients-create
2022-11-19 12:56:44 +01:00
//
2022-11-19 12:59:25 +01:00
// @Param uid path int true "UserID"
2022-11-19 12:56:44 +01:00
//
// @Param post_body body handler.AddClient.body false " "
//
2022-11-19 12:59:25 +01:00
// @Success 200 {object} models.ClientJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
2022-11-19 12:56:44 +01:00
//
// @Router /api-v2/user/{uid}/clients [POST]
2022-11-18 21:25:40 +01:00
func (h APIHandler) AddClient(g *gin.Context) ginresp.HTTPResponse {
2022-11-19 12:56:44 +01:00
type uri struct {
UserID int64 `uri:"uid"`
}
type body struct {
FCMToken string `json:"fcm_token"`
AgentModel string `json:"agent_model"`
AgentVersion string `json:"agent_version"`
ClientType string `json:"client_type"`
}
var u uri
var b body
ctx, errResp := h.app.StartRequest(g, &u, nil, &b)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
var clientType models.ClientType
if b.ClientType == string(models.ClientTypeAndroid) {
clientType = models.ClientTypeAndroid
} else if b.ClientType == string(models.ClientTypeIOS) {
clientType = models.ClientTypeIOS
} else {
return ginresp.InternAPIError(400, apierr.INVALID_CLIENTTYPE, "Invalid ClientType", nil)
}
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
client, err := h.app.Database.CreateClient(ctx, u.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
2022-11-18 21:25:40 +01:00
}
2022-11-19 12:59:25 +01:00
// DeleteClient swaggerdoc
//
// @Summary Delete a client
// @ID api-clients-delete
//
// @Param uid path int true "UserID"
// @Param cid path int true "ClientID"
//
// @Success 200 {object} models.ClientJSON
// @Failure 400 {object} ginresp.apiError
// @Failure 401 {object} ginresp.apiError
// @Failure 404 {object} ginresp.apiError
// @Failure 500 {object} ginresp.apiError
//
// @Router /api-v2/user/{uid}/clients [POST]
2022-11-18 21:25:40 +01:00
func (h APIHandler) DeleteClient(g *gin.Context) ginresp.HTTPResponse {
2022-11-19 12:59:25 +01:00
type uri struct {
UserID int64 `uri:"uid"`
ClientID int64 `uri:"cid"`
}
var u uri
ctx, errResp := h.app.StartRequest(g, &u, nil, nil)
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
return *permResp
}
client, err := h.app.Database.GetClient(ctx, u.UserID, u.ClientID)
if err == sql.ErrNoRows {
return ginresp.InternAPIError(404, apierr.CLIENT_NOT_FOUND, "Client not found", err)
}
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to query client", err)
}
err = h.app.Database.DeleteClient(ctx, u.ClientID)
if err != nil {
return ginresp.InternAPIError(500, apierr.DATABASE_ERROR, "Failed to delete client", err)
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, client.JSON()))
2022-11-18 21:25:40 +01:00
}
func (h APIHandler) ListChannels(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) GetChannel(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) GetChannelMessages(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) ListUserSubscriptions(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) ListChannelSubscriptions(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) GetSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) CancelSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) CreateSubscription(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) ListMessages(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) GetMessage(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func (h APIHandler) DeleteMessage(g *gin.Context) ginresp.HTTPResponse {
return ginresp.NotImplemented()
}
func NewAPIHandler(app *logic.Application) APIHandler {
return APIHandler{
app: app,
}
}