SimpleCloudNotifier/server/api/handler/compat.go

671 lines
19 KiB
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package handler
import (
"blackforestbytes.com/simplecloudnotifier/common/ginresp"
2022-11-20 01:28:32 +01:00
"blackforestbytes.com/simplecloudnotifier/db"
2022-11-13 19:17:07 +01:00
"blackforestbytes.com/simplecloudnotifier/logic"
2022-11-19 15:13:47 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
2022-11-20 01:28:32 +01:00
"database/sql"
2022-11-13 19:17:07 +01:00
"github.com/gin-gonic/gin"
2022-11-20 01:28:32 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http"
2022-11-13 19:17:07 +01:00
)
type CompatHandler struct {
2022-11-20 01:28:32 +01:00
app *logic.Application
database *db.Database
2022-11-13 19:17:07 +01:00
}
func NewCompatHandler(app *logic.Application) CompatHandler {
return CompatHandler{
2022-11-20 01:28:32 +01:00
app: app,
database: app.Database,
2022-11-13 19:17:07 +01:00
}
}
// Register swaggerdoc
//
// @Summary Register a new account
// @ID compat-register
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
2022-11-13 19:17:07 +01:00
// @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"
2022-11-20 13:18:09 +01:00
//
// @Param fcm_token formData string true "the (android) fcm token"
// @Param pro formData string true "if the user is a paid account" Enums(true, false)
// @Param pro_token formData string true "the (android) IAP token"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Register.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/register.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
type query struct {
FCMToken *string `json:"fcm_token" form:"fcm_token"`
Pro *string `json:"pro" form:"pro"`
ProToken *string `json:"pro_token" form:"pro_token"`
2022-11-13 19:17:07 +01:00
}
type response struct {
Success bool `json:"success"`
2022-11-13 19:17:07 +01:00
Message string `json:"message"`
2022-11-20 01:28:32 +01:00
UserID int64 `json:"user_id"`
2022-11-13 19:17:07 +01:00
UserKey string `json:"user_key"`
QuotaUsed int `json:"quota"`
QuotaMax int `json:"quota_max"`
IsPro int `json:"is_pro"`
2022-11-13 19:17:07 +01:00
}
2022-11-20 01:28:32 +01:00
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
if data.FCMToken == nil {
return ginresp.CompatAPIError(0, "Missing parameter [[fcm_token]]")
}
if data.Pro == nil {
return ginresp.CompatAPIError(0, "Missing parameter [[pro]]")
}
if data.ProToken == nil {
return ginresp.CompatAPIError(0, "Missing parameter [[pro_token]]")
}
if *data.Pro != "true" {
data.ProToken = nil
}
if data.ProToken != nil {
ptok, err := h.app.VerifyProToken(ctx, "ANDROID|v2|"+*data.ProToken)
2022-11-20 01:28:32 +01:00
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query purchase status")
}
if !ptok {
return ginresp.CompatAPIError(0, "Purchase token could not be verified")
}
}
readKey := h.app.GenerateRandomAuthKey()
sendKey := h.app.GenerateRandomAuthKey()
adminKey := h.app.GenerateRandomAuthKey()
err := h.database.ClearFCMTokens(ctx, *data.FCMToken)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to clear existing fcm tokens")
}
if data.ProToken != nil {
err := h.database.ClearProTokens(ctx, *data.ProToken)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to clear existing fcm tokens")
}
}
user, err := h.database.CreateUser(ctx, readKey, sendKey, adminKey, data.ProToken, nil)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to create user in db")
}
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat")
if err != nil {
2022-11-24 12:53:27 +01:00
return ginresp.CompatAPIError(0, "Failed to create client in db")
2022-11-20 01:28:32 +01:00
}
2022-11-20 01:28:32 +01:00
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "New user registered",
2022-11-20 22:18:24 +01:00
UserID: user.UserID.IntID(),
2022-11-20 01:28:32 +01:00
UserKey: user.AdminKey,
QuotaUsed: user.QuotaUsedToday(),
QuotaMax: user.QuotaPerDay(),
IsPro: langext.Conditional(user.IsPro, 1, 0),
}))
2022-11-13 19:17:07 +01:00
}
// Info swaggerdoc
//
// @Summary Get information about the current user
// @ID compat-info
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
2022-11-13 19:17:07 +01:00
// @Param user_id query string true "the user_id"
// @Param user_key query string true "the user_key"
2022-11-20 13:18:09 +01:00
//
// @Param user_id formData string true "the user_id"
// @Param user_key formData string true "the user_key"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Info.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/info.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
2022-11-13 19:17:07 +01:00
Message string `json:"message"`
2022-11-20 01:28:32 +01:00
UserID int64 `json:"user_id"`
2022-11-13 19:17:07 +01:00
UserKey string `json:"user_key"`
2022-11-20 01:28:32 +01:00
QuotaUsed int `json:"quota"`
QuotaMax int `json:"quota_max"`
IsPro int `json:"is_pro"`
2022-11-13 19:17:07 +01:00
FCMSet bool `json:"fcm_token_set"`
UnackCount int `json:"unack_count"`
}
2022-11-20 01:28:32 +01:00
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
clients, err := h.database.ListClients(ctx, user.UserID)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query clients")
}
2022-12-11 02:47:23 +01:00
fcmSet := langext.ArrAny(clients, func(c models.Client) bool { return c.FCMToken != nil })
2022-11-20 01:28:32 +01:00
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
2022-11-20 22:18:24 +01:00
UserID: user.UserID.IntID(),
2022-11-20 01:28:32 +01:00
UserKey: user.AdminKey,
QuotaUsed: user.QuotaUsedToday(),
QuotaMax: user.QuotaPerDay(),
IsPro: langext.Conditional(user.IsPro, 1, 0),
FCMSet: fcmSet,
UnackCount: 0,
}))
2022-11-13 19:17:07 +01:00
}
// Ack swaggerdoc
//
// @Summary Acknowledge that a message was received
// @ID compat-ack
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
2022-11-13 19:17:07 +01:00
// @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"
2022-11-20 13:18:09 +01:00
//
// @Param user_id formData string true "the user_id"
// @Param user_key formData string true "the user_key"
// @Param scn_msg_id formData string true "the message id"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Ack.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/ack.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
MessageID *int64 `json:"scn_msg_id" form:"scn_msg_id"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
2022-11-13 19:17:07 +01:00
Message string `json:"message"`
PrevAckValue int `json:"prev_ack"`
NewAckValue int `json:"new_ack"`
}
2022-11-20 01:28:32 +01:00
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
if data.MessageID == nil {
return ginresp.CompatAPIError(103, "Missing parameter [[scn_msg_id]]")
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
PrevAckValue: 0,
NewAckValue: 1,
}))
2022-11-13 19:17:07 +01:00
}
// Requery swaggerdoc
//
// @Summary Return all not-acknowledged messages
// @ID compat-requery
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
2022-11-13 19:17:07 +01:00
// @Param user_id query string true "the user_id"
// @Param user_key query string true "the user_key"
2022-11-20 13:18:09 +01:00
//
// @Param user_id formData string true "the user_id"
// @Param user_key formData string true "the user_key"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Requery.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/requery.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
2022-11-13 19:17:07 +01:00
Message string `json:"message"`
Count int `json:"count"`
Data []models.CompatMessage `json:"data"`
}
2022-11-20 01:28:32 +01:00
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Count: 0,
Data: make([]models.CompatMessage, 0),
}))
2022-11-13 19:17:07 +01:00
}
// Update swaggerdoc
//
// @Summary Set the fcm-token (android)
// @ID compat-update
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
2022-11-13 19:17:07 +01:00
// @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"
2022-11-20 13:18:09 +01:00
//
// @Param user_id formData string true "the user_id"
// @Param user_key formData string true "the user_key"
// @Param fcm_token formData string true "the (android) fcm token"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Update.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/update.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
FCMToken *string `json:"fcm_token" form:"fcm_token"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
2022-11-13 19:17:07 +01:00
Message string `json:"message"`
2022-11-20 01:28:32 +01:00
UserID int64 `json:"user_id"`
2022-11-13 19:17:07 +01:00
UserKey string `json:"user_key"`
2022-11-20 01:28:32 +01:00
QuotaUsed int `json:"quota"`
QuotaMax int `json:"quota_max"`
IsPro int `json:"is_pro"`
}
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
clients, err := h.database.ListClients(ctx, user.UserID)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to list clients")
}
newAdminKey := h.app.GenerateRandomAuthKey()
newReadKey := h.app.GenerateRandomAuthKey()
newSendKey := h.app.GenerateRandomAuthKey()
err = h.database.UpdateUserKeys(ctx, user.UserID, newSendKey, newReadKey, newAdminKey)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to update keys")
}
if data.FCMToken != nil {
for _, client := range clients {
err = h.database.DeleteClient(ctx, client.ClientID)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to delete client")
}
}
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat")
if err != nil {
return ginresp.CompatAPIError(0, "Failed to delete client")
}
2022-11-13 19:17:07 +01:00
}
2022-11-20 01:28:32 +01:00
user, err = h.database.GetUser(ctx, user.UserID)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
2022-11-20 22:18:24 +01:00
UserID: user.UserID.IntID(),
2022-11-20 01:28:32 +01:00
UserKey: user.AdminKey,
QuotaUsed: user.QuotaUsedToday(),
QuotaMax: user.QuotaPerDay(),
IsPro: langext.Conditional(user.IsPro, 1, 0),
}))
2022-11-13 19:17:07 +01:00
}
// Expand swaggerdoc
//
// @Summary Get a whole (potentially truncated) message
// @ID compat-expand
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 01:28:32 +01:00
// @Deprecated
2022-11-20 13:18:09 +01:00
//
// @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"
//
// @Param user_id formData string true "The user_id"
// @Param user_key formData string true "The user_key"
// @Param scn_msg_id formData string true "The message-id"
//
// @Success 200 {object} handler.Expand.response
// @Failure 200 {object} ginresp.compatAPIError
//
2022-11-18 21:25:40 +01:00
// @Router /api/expand.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
MessageID *int64 `json:"scn_msg_id" form:"scn_msg_id"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
Message string `json:"message"`
Data models.CompatMessage `json:"data"`
}
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
}
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
if data.MessageID == nil {
return ginresp.CompatAPIError(103, "Missing parameter [[scn_msg_id]]")
2022-11-13 19:17:07 +01:00
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
2022-11-20 22:18:24 +01:00
msg, err := h.database.GetMessage(ctx, models.SCNMessageID(*data.MessageID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(301, "Message not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query message")
}
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "ok",
Data: models.CompatMessage{
Title: msg.Title,
Body: langext.Coalesce(msg.Content, ""),
Trimmed: langext.Ptr(false),
Priority: msg.Priority,
Timestamp: msg.Timestamp().Unix(),
UserMessageID: msg.UserMessageID,
2022-11-20 22:18:24 +01:00
SCNMessageID: msg.SCNMessageID.IntID(),
2022-11-20 01:28:32 +01:00
},
}))
2022-11-13 19:17:07 +01:00
}
// Upgrade swaggerdoc
//
// @Summary Upgrade a free account to a paid account
// @ID compat-upgrade
2022-11-23 19:32:23 +01:00
// @Tags API-v1
//
2022-11-20 13:18:09 +01:00
// @Deprecated
//
2022-11-13 19:17:07 +01:00
// @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)
// @Param pro_token query string true "the (android) IAP token"
2022-11-20 13:18:09 +01:00
//
// @Param user_id formData string true "the user_id"
// @Param user_key formData string true "the user_key"
// @Param pro formData string true "if the user is a paid account" Enums(true, false)
// @Param pro_token formData string true "the (android) IAP token"
//
2022-11-13 19:17:07 +01:00
// @Success 200 {object} handler.Upgrade.response
2022-11-20 01:28:32 +01:00
// @Failure 200 {object} ginresp.compatAPIError
2022-11-20 13:18:09 +01:00
//
2022-11-18 21:25:40 +01:00
// @Router /api/upgrade.php [get]
2022-11-13 19:17:07 +01:00
func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
type query struct {
UserID *int64 `json:"user_id" form:"user_id"`
UserKey *string `json:"user_key" form:"user_key"`
Pro *string `json:"pro" form:"pro"`
ProToken *string `json:"pro_token" form:"pro_token"`
2022-11-13 19:17:07 +01:00
}
type response struct {
2022-11-20 01:28:32 +01:00
Success bool `json:"success"`
Message string `json:"message"`
UserID int64 `json:"user_id"`
QuotaUsed int `json:"quota"`
QuotaMax int `json:"quota_max"`
IsPro bool `json:"is_pro"`
}
var datq query
var datb query
2022-11-20 13:18:09 +01:00
ctx, errResp := h.app.StartRequest(g, nil, &datq, nil, &datb)
2022-11-20 01:28:32 +01:00
if errResp != nil {
return *errResp
2022-11-13 19:17:07 +01:00
}
2022-11-20 01:28:32 +01:00
defer ctx.Cancel()
data := dataext.ObjectMerge(datb, datq)
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
if data.UserID == nil {
return ginresp.CompatAPIError(101, "Missing parameter [[user_id]]")
}
if data.UserKey == nil {
return ginresp.CompatAPIError(102, "Missing parameter [[user_key]]")
}
if data.Pro == nil {
return ginresp.CompatAPIError(103, "Missing parameter [[pro]]")
}
if data.ProToken == nil {
return ginresp.CompatAPIError(104, "Missing parameter [[pro_token]]")
}
2022-11-20 22:18:24 +01:00
user, err := h.database.GetUser(ctx, models.UserID(*data.UserID))
2022-11-20 01:28:32 +01:00
if err == sql.ErrNoRows {
return ginresp.CompatAPIError(201, "User not found")
}
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
if user.AdminKey != *data.UserKey {
return ginresp.CompatAPIError(204, "Authentification failed")
}
if *data.Pro != "true" {
data.ProToken = nil
}
if data.ProToken != nil {
ptok, err := h.app.VerifyProToken(ctx, "ANDROID|v2|"+*data.ProToken)
2022-11-20 01:28:32 +01:00
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query purchase status")
}
if !ptok {
return ginresp.CompatAPIError(0, "Purchase token could not be verified")
}
err = h.database.UpdateUserProToken(ctx, user.UserID, data.ProToken)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to update user")
}
} else {
err = h.database.UpdateUserProToken(ctx, user.UserID, nil)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to update user")
}
}
user, err = h.database.GetUser(ctx, user.UserID)
if err != nil {
return ginresp.CompatAPIError(0, "Failed to query user")
}
2022-11-13 19:17:07 +01:00
2022-11-20 01:28:32 +01:00
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
Success: true,
Message: "user updated",
2022-11-20 22:18:24 +01:00
UserID: user.UserID.IntID(),
2022-11-20 01:28:32 +01:00
QuotaUsed: user.QuotaUsedToday(),
QuotaMax: user.QuotaPerDay(),
IsPro: user.IsPro,
}))
2022-11-13 19:17:07 +01:00
}