2022-11-13 19:17:07 +01:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-01-14 00:48:51 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
hl "blackforestbytes.com/simplecloudnotifier/api/apihighlight"
|
2022-12-20 13:55:09 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
2023-02-03 22:51:03 +01:00
|
|
|
ct "blackforestbytes.com/simplecloudnotifier/db/cursortoken"
|
2023-01-06 00:39:21 +01:00
|
|
|
primarydb "blackforestbytes.com/simplecloudnotifier/db/impl/primary"
|
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"
|
2023-07-30 15:58:37 +02:00
|
|
|
"errors"
|
2023-06-18 13:09:36 +02:00
|
|
|
"fmt"
|
2022-11-20 01:28:32 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
|
2024-07-15 17:26:55 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2022-11-20 01:28:32 +01:00
|
|
|
"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
|
2023-01-06 00:39:21 +01:00
|
|
|
database *primarydb.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,
|
2023-01-06 00:39:21 +01:00
|
|
|
database: app.Database.Primary,
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:04:04 +02:00
|
|
|
// SendMessage swaggerdoc
|
2023-01-14 00:48:51 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2023-01-14 00:48:51 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Send a new message (compatibility)
|
|
|
|
// @Description All parameter can be set via query-parameter or form-data body. Only UserID, UserKey and Title are required
|
|
|
|
// @Tags External
|
2023-01-14 00:48:51 +01:00
|
|
|
//
|
2023-08-12 19:04:04 +02:00
|
|
|
// @Param query_data query handler.SendMessage.combined false " "
|
|
|
|
// @Param form_data formData handler.SendMessage.combined false " "
|
2023-01-14 00:48:51 +01:00
|
|
|
//
|
2023-08-12 19:04:04 +02:00
|
|
|
// @Success 200 {object} handler.SendMessage.response
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Failure 400 {object} ginresp.apiError
|
|
|
|
// @Failure 401 {object} ginresp.apiError
|
|
|
|
// @Failure 403 {object} ginresp.apiError
|
|
|
|
// @Failure 500 {object} ginresp.apiError
|
2023-01-14 00:48:51 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /send.php [POST]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) SendMessage(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-01-14 00:48:51 +01:00
|
|
|
type combined struct {
|
|
|
|
UserID *int64 `json:"user_id" form:"user_id"`
|
|
|
|
UserKey *string `json:"user_key" form:"user_key"`
|
|
|
|
Title *string `json:"title" form:"title"`
|
|
|
|
Content *string `json:"content" form:"content"`
|
|
|
|
Priority *int `json:"priority" form:"priority"`
|
|
|
|
UserMessageID *string `json:"msg_id" form:"msg_id"`
|
|
|
|
SendTimestamp *float64 `json:"timestamp" form:"timestamp"`
|
|
|
|
}
|
2023-01-16 18:53:22 +01:00
|
|
|
type response struct {
|
|
|
|
Success bool `json:"success"`
|
|
|
|
ErrorID apierr.APIError `json:"error"`
|
|
|
|
ErrorHighlight int `json:"errhighlight"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
SuppressSend bool `json:"suppress_send"`
|
|
|
|
MessageCount int `json:"messagecount"`
|
|
|
|
Quota int `json:"quota"`
|
|
|
|
IsPro bool `json:"is_pro"`
|
|
|
|
QuotaMax int `json:"quota_max"`
|
|
|
|
SCNMessageID int64 `json:"scn_msg_id"`
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
|
|
|
var f combined
|
|
|
|
var q combined
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&q).Form(&f).IgnoreWrongContentType().Start()
|
2023-01-14 00:48:51 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(f, q)
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
newid, err := h.database.ConvertCompatID(ctx, langext.Coalesce(data.UserID, -1), "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if newid == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
okResp, errResp := h.app.SendMessage(g, ctx, langext.Ptr(models.UserID(*newid)), data.UserKey, nil, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, nil)
|
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
} else {
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
ErrorID: apierr.NO_ERROR,
|
|
|
|
ErrorHighlight: -1,
|
|
|
|
Message: langext.Conditional(okResp.MessageIsOld, "Message already sent", "Message sent"),
|
|
|
|
SuppressSend: okResp.MessageIsOld,
|
|
|
|
MessageCount: okResp.User.MessagesSent,
|
|
|
|
Quota: okResp.User.QuotaUsedToday(),
|
|
|
|
IsPro: okResp.User.IsPro,
|
|
|
|
QuotaMax: okResp.User.QuotaPerDay(),
|
|
|
|
SCNMessageID: okResp.CompatMessageID,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
})
|
2023-01-14 00:48:51 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 19:17:07 +01:00
|
|
|
// Register swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Register a new account
|
|
|
|
// @ID compat-register
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02: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-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Register.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/register.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Register(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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 {
|
2022-11-13 22:31:28 +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-13 22:31:28 +01:00
|
|
|
QuotaUsed int `json:"quota"`
|
|
|
|
QuotaMax int `json:"quota_max"`
|
2023-01-17 22:32:13 +01:00
|
|
|
IsPro bool `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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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]]")
|
|
|
|
}
|
2023-01-17 22:03:27 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if data.ProToken != nil {
|
|
|
|
data.ProToken = langext.Ptr("ANDROID|v1|" + *data.ProToken)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if *data.Pro != "true" {
|
|
|
|
data.ProToken = nil
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if data.ProToken != nil {
|
|
|
|
ptok, err := h.app.VerifyProToken(ctx, *data.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query purchase status")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ptok {
|
|
|
|
return ginresp.CompatAPIError(0, "Purchase token could not be verified")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
adminKey := h.app.GenerateRandomAuthKey()
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-09-16 20:11:28 +02:00
|
|
|
err := h.database.DeleteClientsByFCM(ctx, *data.FCMToken)
|
2024-07-16 17:19:55 +02:00
|
|
|
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 pro tokens")
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.CreateUser(ctx, data.ProToken, nil)
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to create user in db")
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateKeyToken(ctx, "CompatKey", user.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat", nil)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to create client in db")
|
|
|
|
}
|
2023-04-21 21:45:16 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
oldid, err := h.database.CreateCompatID(ctx, "userid", user.UserID.String())
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create userid<old>", err)
|
|
|
|
}
|
2022-11-13 22:31:28 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "New user registered",
|
|
|
|
UserID: oldid,
|
|
|
|
UserKey: adminKey,
|
|
|
|
QuotaUsed: user.QuotaUsedToday(),
|
|
|
|
QuotaMax: user.QuotaPerDay(),
|
|
|
|
IsPro: user.IsPro,
|
|
|
|
}))
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Get information about the current user
|
|
|
|
// @ID compat-info
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Param user_id formData string true "the user_id"
|
|
|
|
// @Param user_key formData string true "the user_key"
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Info.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/info.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Info(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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"`
|
2023-06-18 02:09:05 +02:00
|
|
|
UnackCount int64 `json:"unack_count"`
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
2022-11-20 01:28:32 +01:00
|
|
|
var datq query
|
|
|
|
var datb query
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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]]")
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(201, "User not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
clients, err := h.database.ListClients(ctx, user.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query clients")
|
|
|
|
}
|
2023-06-18 02:09:05 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
filter := models.MessageFilter{
|
|
|
|
Sender: langext.Ptr([]models.UserID{user.UserID}),
|
|
|
|
CompatAcknowledged: langext.Ptr(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
unackCount, err := h.database.CountMessages(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2023-06-18 02:09:05 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "ok",
|
|
|
|
UserID: *data.UserID,
|
|
|
|
UserKey: keytok.Token,
|
|
|
|
QuotaUsed: user.QuotaUsedToday(),
|
|
|
|
QuotaMax: user.QuotaPerDay(),
|
|
|
|
IsPro: langext.Conditional(user.IsPro, 1, 0),
|
|
|
|
FCMSet: len(clients) > 0,
|
|
|
|
UnackCount: unackCount,
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ack swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Acknowledge that a message was received
|
|
|
|
// @ID compat-ack
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02: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-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Ack.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/ack.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Ack(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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.MessageID == nil {
|
|
|
|
return ginresp.CompatAPIError(103, "Missing parameter [[scn_msg_id]]")
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, fmt.Sprintf("User %d not found (compat)", *data.UserID), nil)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(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
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
messageIdComp, err := h.database.ConvertCompatID(ctx, *data.MessageID, "messageid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query messageid<old>", err)
|
|
|
|
}
|
|
|
|
if messageIdComp == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.MESSAGE_NOT_FOUND, hl.NONE, fmt.Sprintf("Message %d not found (compat)", *data.MessageID), nil)
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
ackBefore, err := h.database.GetAck(ctx, models.MessageID(*messageIdComp))
|
2023-02-03 22:51:03 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query ack", err)
|
2023-02-03 22:51:03 +01:00
|
|
|
}
|
2023-01-16 18:53:22 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if !ackBefore {
|
|
|
|
err = h.database.SetAck(ctx, user.UserID, models.MessageID(*messageIdComp))
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to set ack", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "ok",
|
|
|
|
PrevAckValue: langext.Conditional(ackBefore, 1, 0),
|
|
|
|
NewAckValue: 1,
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Requery swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Return all not-acknowledged messages
|
|
|
|
// @ID compat-requery
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Param user_id formData string true "the user_id"
|
|
|
|
// @Param user_key formData string true "the user_key"
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Requery.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/requery.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Requery(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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]]")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(201, "User not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
filter := models.MessageFilter{
|
|
|
|
Sender: langext.Ptr([]models.UserID{user.UserID}),
|
|
|
|
CompatAcknowledged: langext.Ptr(false),
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-09-20 15:36:16 +02:00
|
|
|
msgs, _, _, err := h.database.ListMessages(ctx, filter, langext.Ptr(16), ct.Start())
|
2023-02-03 22:51:03 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
2023-02-03 22:51:03 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
compMsgs := make([]models.CompatMessage, 0, len(msgs))
|
|
|
|
for _, v := range msgs {
|
|
|
|
|
|
|
|
messageIdComp, err := h.database.ConvertToCompatIDOrCreate(ctx, "messageid", v.MessageID.String())
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query/create messageid<old>", err)
|
|
|
|
}
|
2023-02-03 22:51:03 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
compMsgs = append(compMsgs, models.CompatMessage{
|
|
|
|
Title: v.Title,
|
|
|
|
Body: v.Content,
|
|
|
|
Priority: v.Priority,
|
|
|
|
Timestamp: v.Timestamp().Unix(),
|
|
|
|
UserMessageID: v.UserMessageID,
|
|
|
|
SCNMessageID: messageIdComp,
|
|
|
|
Trimmed: nil,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "ok",
|
|
|
|
Count: len(compMsgs),
|
|
|
|
Data: compMsgs,
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Set the fcm-token (android)
|
|
|
|
// @ID compat-update
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02: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-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Update.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/update.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Update(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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]]")
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(201, "User not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
clients, err := h.database.ListClients(ctx, user.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to list clients")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
newAdminKey := h.app.GenerateRandomAuthKey()
|
2023-04-21 21:45:16 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateKeyToken(ctx, "CompatKey", user.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, newAdminKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
err = h.database.DeleteKeyToken(ctx, keytok.KeyTokenID)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to update keys")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if data.FCMToken != nil {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
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", nil)
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to create client")
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err = h.database.GetUser(ctx, user.UserID)
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "user updated",
|
|
|
|
UserID: *data.UserID,
|
|
|
|
UserKey: newAdminKey,
|
|
|
|
QuotaUsed: user.QuotaUsedToday(),
|
|
|
|
QuotaMax: user.QuotaPerDay(),
|
|
|
|
IsPro: langext.Conditional(user.IsPro, 1, 0),
|
|
|
|
}))
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Get a whole (potentially truncated) message
|
|
|
|
// @ID compat-expand
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02: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-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Expand.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/expand.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Expand(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
2022-11-20 01:28:32 +01:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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.MessageID == nil {
|
|
|
|
return ginresp.CompatAPIError(103, "Missing parameter [[scn_msg_id]]")
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(201, "User not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
messageCompNew, err := h.database.ConvertCompatID(ctx, *data.MessageID, "messageid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query messagid<old>", err)
|
|
|
|
}
|
|
|
|
if messageCompNew == nil {
|
|
|
|
return ginresp.CompatAPIError(301, "Message not found")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
msg, err := h.database.GetMessage(ctx, models.MessageID(*messageCompNew), false)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(301, "Message not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "ok",
|
|
|
|
Data: models.CompatMessage{
|
|
|
|
Title: msg.Title,
|
|
|
|
Body: msg.Content,
|
|
|
|
Trimmed: langext.Ptr(false),
|
|
|
|
Priority: msg.Priority,
|
|
|
|
Timestamp: msg.Timestamp().Unix(),
|
|
|
|
UserMessageID: msg.UserMessageID,
|
|
|
|
SCNMessageID: *data.MessageID,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrade swaggerdoc
|
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Summary Upgrade a free account to a paid account
|
|
|
|
// @ID compat-upgrade
|
|
|
|
// @Tags API-v1
|
2022-11-23 19:32:23 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Deprecated
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02: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
|
|
|
//
|
2023-04-21 21:45:16 +02: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-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Success 200 {object} handler.Upgrade.response
|
|
|
|
// @Failure default {object} ginresp.compatAPIError
|
2022-11-20 13:18:09 +01:00
|
|
|
//
|
2023-04-21 21:45:16 +02:00
|
|
|
// @Router /api/upgrade.php [get]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h CompatHandler) Upgrade(pctx ginext.PreContext) ginext.HTTPResponse {
|
2022-11-13 19:17:07 +01:00
|
|
|
type query struct {
|
2022-11-30 16:46:14 +01:00
|
|
|
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
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Query(&datq).Body(&datb).IgnoreWrongContentType().Start()
|
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()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
data := dataext.ObjectMerge(datb, datq)
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02: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 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
useridCompNew, err := h.database.ConvertCompatID(ctx, *data.UserID, "userid")
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query userid<old>", err)
|
|
|
|
}
|
|
|
|
if useridCompNew == nil {
|
|
|
|
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
|
|
|
}
|
2023-01-17 22:56:04 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, models.UserID(*useridCompNew))
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.CompatAPIError(201, "User not found")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
keytok, err := h.database.GetKeyTokenByToken(ctx, *data.UserKey)
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to query token")
|
|
|
|
}
|
|
|
|
if keytok == nil {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
|
|
|
}
|
|
|
|
if !keytok.IsAdmin(user.UserID) {
|
|
|
|
return ginresp.CompatAPIError(204, "Authentification failed")
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if data.ProToken != nil {
|
|
|
|
data.ProToken = langext.Ptr("ANDROID|v1|" + *data.ProToken)
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if *data.Pro != "true" {
|
|
|
|
data.ProToken = nil
|
2023-01-17 22:56:04 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if data.ProToken != nil {
|
|
|
|
ptok, err := h.app.VerifyProToken(ctx, *data.ProToken)
|
|
|
|
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.ClearProTokens(ctx, *data.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.database.UpdateUserProToken(ctx, user.UserID, langext.Ptr(*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")
|
|
|
|
}
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
2024-07-16 17:19:55 +02:00
|
|
|
|
|
|
|
user, err = h.database.GetUser(ctx, user.UserID)
|
2022-11-20 01:28:32 +01:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.CompatAPIError(0, "Failed to query user")
|
2022-11-20 01:28:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, response{
|
|
|
|
Success: true,
|
|
|
|
Message: "user updated",
|
|
|
|
UserID: *data.UserID,
|
|
|
|
QuotaUsed: user.QuotaUsedToday(),
|
|
|
|
QuotaMax: user.QuotaPerDay(),
|
|
|
|
IsPro: user.IsPro,
|
|
|
|
}))
|
2022-11-13 19:17:07 +01:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|