implement a bit of the register.php call (and the DB schema)
This commit is contained in:
@@ -5,7 +5,13 @@ import (
|
||||
"blackforestbytes.com/simplecloudnotifier/api/models"
|
||||
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
|
||||
"blackforestbytes.com/simplecloudnotifier/logic"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CompatHandler struct {
|
||||
@@ -21,6 +27,7 @@ func NewCompatHandler(app *logic.Application) CompatHandler {
|
||||
// Register swaggerdoc
|
||||
//
|
||||
// @Summary Register a new account
|
||||
// @ID compat-register
|
||||
// @Param fcm_token query string true "the (android) fcm token"
|
||||
// @Param pro query string true "if the user is a paid account" Enums(true, false)
|
||||
// @Param pro_token query string true "the (android) IAP token"
|
||||
@@ -29,28 +36,101 @@ func NewCompatHandler(app *logic.Application) CompatHandler {
|
||||
// @Router /register.php [get]
|
||||
func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
FCMToken string `form:"fcm_token"`
|
||||
Pro string `form:"pro"`
|
||||
ProToken string `form:"pro_token"`
|
||||
FCMToken *string `form:"fcm_token"`
|
||||
Pro *string `form:"pro"`
|
||||
ProToken *string `form:"pro_token"`
|
||||
}
|
||||
type response struct {
|
||||
Success string `json:"success"`
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
UserID string `json:"user_id"`
|
||||
UserKey string `json:"user_key"`
|
||||
QuotaUsed string `json:"quota"`
|
||||
QuotaMax string `json:"quota_max"`
|
||||
IsPro string `json:"is_pro"`
|
||||
QuotaUsed int `json:"quota"`
|
||||
QuotaMax int `json:"quota_max"`
|
||||
IsPro int `json:"is_pro"`
|
||||
}
|
||||
|
||||
//TODO
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
var q query
|
||||
if err := g.ShouldBindQuery(&q); err != nil {
|
||||
return ginresp.InternAPIError(0, "Failed to read arguments")
|
||||
}
|
||||
|
||||
if q.FCMToken == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[fcm_token]]")
|
||||
}
|
||||
if q.Pro == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[pro]]")
|
||||
}
|
||||
if q.ProToken == nil {
|
||||
return ginresp.InternAPIError(0, "Missing parameter [[pro_token]]")
|
||||
}
|
||||
|
||||
isProInt := 0
|
||||
isProBool := false
|
||||
if *q.Pro == "true" {
|
||||
isProInt = 1
|
||||
isProBool = true
|
||||
} else {
|
||||
q.ProToken = nil
|
||||
}
|
||||
|
||||
if isProBool {
|
||||
ptok, err := h.app.VerifyProToken(*q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to query purchaste status: %v", err))
|
||||
}
|
||||
|
||||
if !ptok {
|
||||
return ginresp.InternAPIError(0, "Purchase token could not be verified")
|
||||
}
|
||||
}
|
||||
|
||||
userKey := h.app.GenerateRandomAuthKey()
|
||||
|
||||
return h.app.RunTransaction(ctx, nil, func(tx *sql.Tx) (ginresp.HTTPResponse, bool) {
|
||||
|
||||
res, err := tx.ExecContext(ctx, "INSERT INTO users (user_key, fcm_token, is_pro, pro_token, timestamp_accessed) VALUES (?, ?, ?, ?, NOW())", userKey, *q.FCMToken, isProInt, q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to create user: %v", err)), false
|
||||
}
|
||||
|
||||
userId, err := res.LastInsertId()
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to get user_id: %v", err)), false
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "UPDATE users SET fcm_token=NULL WHERE user_id <> ? AND fcm_token=?", userId, q.FCMToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to update fcm: %v", err)), false
|
||||
}
|
||||
|
||||
if isProInt == 1 {
|
||||
_, err := tx.ExecContext(ctx, "UPDATE users SET is_pro=0, pro_token=NULL WHERE user_id <> ? AND pro_token = ?", userId, q.ProToken)
|
||||
if err != nil {
|
||||
return ginresp.InternAPIError(0, fmt.Sprintf("Failed to update ispro: %v", err)), false
|
||||
}
|
||||
}
|
||||
|
||||
return ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
Message: "New user registered",
|
||||
UserID: strconv.FormatInt(userId, 10),
|
||||
UserKey: userKey,
|
||||
QuotaUsed: 0,
|
||||
QuotaMax: h.app.QuotaMax(isProBool),
|
||||
IsPro: isProInt,
|
||||
}), true
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// Info swaggerdoc
|
||||
//
|
||||
// @Summary Get information about the current user
|
||||
// @ID compat-info
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Success 200 {object} handler.Info.response
|
||||
@@ -75,12 +155,13 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Ack swaggerdoc
|
||||
//
|
||||
// @Summary Acknowledge that a message was received
|
||||
// @ID compat-ack
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param scn_msg_id query string true "the message id"
|
||||
@@ -102,12 +183,13 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Requery swaggerdoc
|
||||
//
|
||||
// @Summary Return all not-acknowledged messages
|
||||
// @ID compat-requery
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Success 200 {object} handler.Requery.response
|
||||
@@ -127,12 +209,13 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Update swaggerdoc
|
||||
//
|
||||
// @Summary Set the fcm-token (android)
|
||||
// @ID compat-update
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param fcm_token query string true "the (android) fcm token"
|
||||
@@ -157,12 +240,13 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Expand swaggerdoc
|
||||
//
|
||||
// @Summary Get a whole (potentially truncated) message
|
||||
// @ID compat-expand
|
||||
// @Success 200 {object} handler.Expand.response
|
||||
// @Failure 500 {object} ginresp.internAPIError
|
||||
// @Router /expand.php [get]
|
||||
@@ -180,12 +264,13 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Upgrade swaggerdoc
|
||||
//
|
||||
// @Summary Upgrade a free account to a paid account
|
||||
// @ID compat-upgrade
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param pro query string true "if the user is a paid account" Enums(true, false)
|
||||
@@ -208,33 +293,39 @@ func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
//TODO
|
||||
|
||||
return ginresp.NotImplemented(0)
|
||||
return ginresp.InternAPIError(0, "NotImplemented")
|
||||
}
|
||||
|
||||
// Send swaggerdoc
|
||||
//
|
||||
// @Summary Send a message
|
||||
// @Description all aeguments can either be supplied in the query or in the json body
|
||||
// @Param user_id query string true "the user_id"
|
||||
// @Param user_key query string true "the user_key"
|
||||
// @Param title query string true "The message title"
|
||||
// @Param content query string false "The message content"
|
||||
// @Param priority query string false "The message priority" Enum(0, 1, 2)
|
||||
// @Param msg_id query string false "The message idempotency id"
|
||||
// @Param timestamp query string false "The message timestamp"
|
||||
// @Param user_id body string true "the user_id"
|
||||
// @Param user_key body string true "the user_key"
|
||||
// @Param title body string true "The message title"
|
||||
// @Param content body string false "The message content"
|
||||
// @Param priority body string false "The message priority" Enum(0, 1, 2)
|
||||
// @Param msg_id body string false "The message idempotency id"
|
||||
// @Param timestamp body string false "The message timestamp"
|
||||
// @Success 200 {object} handler.Send.response
|
||||
// @Failure 500 {object} ginresp.sendAPIError
|
||||
// @Router /send.php [post]
|
||||
// @Summary Send a message
|
||||
// @Description (all arguments can either be supplied in the query or in the json body)
|
||||
// @ID compat-send
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param _ query handler.Send.query false " "
|
||||
// @Param post_body body handler.Send.body false " "
|
||||
// @Success 200 {object} handler.Send.response
|
||||
// @Failure 500 {object} ginresp.sendAPIError
|
||||
// @Router /send.php [post]
|
||||
func (h CompatHandler) Send(g *gin.Context) ginresp.HTTPResponse {
|
||||
type query struct {
|
||||
//TODO
|
||||
UserID string `form:"user_id" required:"true"`
|
||||
UserKey string `form:"user_key" required:"true"`
|
||||
Title string `form:"title" required:"true"`
|
||||
Content *string `form:"content"`
|
||||
Priority *string `form:"priority"`
|
||||
MessageID *string `form:"msg_id"`
|
||||
Timestamp *string `form:"timestamp"`
|
||||
}
|
||||
type body struct {
|
||||
UserID string `json:"user_id" required:"true"`
|
||||
UserKey string `json:"user_key" required:"true"`
|
||||
Title string `json:"title" required:"true"`
|
||||
Content *string `json:"content"`
|
||||
Priority *string `json:"priority"`
|
||||
MessageID *string `json:"msg_id"`
|
||||
Timestamp *string `json:"timestamp"`
|
||||
}
|
||||
type response struct {
|
||||
Success string `json:"success"`
|
||||
|
Reference in New Issue
Block a user