create compat-id for messages && TestCreateCompatUser
This commit is contained in:
parent
89fd0dfed7
commit
590665a5e9
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
@ -37,7 +37,7 @@ func NewCompatHandler(app *logic.Application) CompatHandler {
|
||||
// @Param query_data query handler.SendMessageCompat.combined false " "
|
||||
// @Param form_data formData handler.SendMessageCompat.combined false " "
|
||||
//
|
||||
// @Success 200 {object} handler.sendMessageInternal.response
|
||||
// @Success 200 {object} handler.SendMessageCompat.response
|
||||
// @Failure 400 {object} ginresp.apiError
|
||||
// @Failure 401 {object} ginresp.apiError
|
||||
// @Failure 403 {object} ginresp.apiError
|
||||
@ -54,6 +54,18 @@ func (h MessageHandler) SendMessageCompat(g *gin.Context) ginresp.HTTPResponse {
|
||||
UserMessageID *string `json:"msg_id" form:"msg_id"`
|
||||
SendTimestamp *float64 `json:"timestamp" form:"timestamp"`
|
||||
}
|
||||
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"`
|
||||
}
|
||||
|
||||
var f combined
|
||||
var q combined
|
||||
@ -73,7 +85,57 @@ func (h MessageHandler) SendMessageCompat(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found (compat)", nil)
|
||||
}
|
||||
|
||||
return h.sendMessageInternal(g, ctx, langext.Ptr(models.UserID(*newid)), data.UserKey, nil, nil, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, nil)
|
||||
okResp, errResp := h.sendMessageInternal(g, ctx, langext.Ptr(models.UserID(*newid)), data.UserKey, nil, nil, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, nil)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
} else {
|
||||
if okResp.MessageIsOld {
|
||||
|
||||
compatMessageID, _, err := h.database.ConvertToCompatID(ctx, okResp.Message.MessageID.String())
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query compat-id", err)
|
||||
}
|
||||
if compatMessageID == nil {
|
||||
v, err := h.database.CreateCompatID(ctx, "messageid", okResp.Message.MessageID.String())
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create compat-id", err)
|
||||
}
|
||||
compatMessageID = &v
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message already sent",
|
||||
SuppressSend: true,
|
||||
MessageCount: okResp.User.MessagesSent,
|
||||
Quota: okResp.User.QuotaUsedToday(),
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: *compatMessageID,
|
||||
}))
|
||||
} else {
|
||||
|
||||
compatMessageID, err := h.database.CreateCompatID(ctx, "messageid", okResp.Message.MessageID.String())
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create compat-id", err)
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message sent",
|
||||
SuppressSend: false,
|
||||
MessageCount: okResp.User.MessagesSent + 1,
|
||||
Quota: okResp.User.QuotaUsedToday() + 1,
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: compatMessageID,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Register swaggerdoc
|
||||
@ -93,7 +155,7 @@ func (h MessageHandler) SendMessageCompat(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param pro_token formData string true "the (android) IAP token"
|
||||
//
|
||||
// @Success 200 {object} handler.Register.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/register.php [get]
|
||||
func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -168,7 +230,7 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.CompatAPIError(0, "Failed to create user in db")
|
||||
}
|
||||
|
||||
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat")
|
||||
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, "ANDROID|v1|"+*data.FCMToken, "compat", "compat")
|
||||
if err != nil {
|
||||
return ginresp.CompatAPIError(0, "Failed to create client in db")
|
||||
}
|
||||
@ -204,7 +266,7 @@ func (h CompatHandler) Register(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_key formData string true "the user_key"
|
||||
//
|
||||
// @Success 200 {object} handler.Info.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/info.php [get]
|
||||
func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -298,7 +360,7 @@ func (h CompatHandler) Info(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param scn_msg_id formData string true "the message id"
|
||||
//
|
||||
// @Success 200 {object} handler.Ack.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/ack.php [get]
|
||||
func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -354,6 +416,8 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
return ginresp.CompatAPIError(204, "Authentification failed")
|
||||
}
|
||||
|
||||
// we no longer ack messages - this is a no-op
|
||||
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
Message: "ok",
|
||||
@ -377,7 +441,7 @@ func (h CompatHandler) Ack(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param user_key formData string true "the user_key"
|
||||
//
|
||||
// @Success 200 {object} handler.Requery.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/requery.php [get]
|
||||
func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -454,7 +518,7 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param fcm_token formData string true "the (android) fcm token"
|
||||
//
|
||||
// @Success 200 {object} handler.Update.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/update.php [get]
|
||||
func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -535,7 +599,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
||||
}
|
||||
|
||||
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, *data.FCMToken, "compat", "compat")
|
||||
_, err = h.database.CreateClient(ctx, user.UserID, models.ClientTypeAndroid, "ANDROID|v1|"+*data.FCMToken, "compat", "compat")
|
||||
if err != nil {
|
||||
return ginresp.CompatAPIError(0, "Failed to delete client")
|
||||
}
|
||||
@ -575,7 +639,7 @@ func (h CompatHandler) Update(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param scn_msg_id formData string true "The message-id"
|
||||
//
|
||||
// @Success 200 {object} handler.Expand.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/expand.php [get]
|
||||
func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
@ -680,7 +744,7 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
|
||||
// @Param pro_token formData string true "the (android) IAP token"
|
||||
//
|
||||
// @Success 200 {object} handler.Upgrade.response
|
||||
// @Failure 200 {object} ginresp.compatAPIError
|
||||
// @Failure default {object} ginresp.compatAPIError
|
||||
//
|
||||
// @Router /api/upgrade.php [get]
|
||||
func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
|
||||
|
@ -19,6 +19,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type SendMessageResponse struct {
|
||||
User models.User
|
||||
Message models.Message
|
||||
MessageIsOld bool
|
||||
}
|
||||
|
||||
type MessageHandler struct {
|
||||
app *logic.Application
|
||||
database *primarydb.Database
|
||||
@ -41,7 +47,7 @@ func NewMessageHandler(app *logic.Application) MessageHandler {
|
||||
// @Param post_body body handler.SendMessage.combined false " "
|
||||
// @Param form_body formData handler.SendMessage.combined false " "
|
||||
//
|
||||
// @Success 200 {object} handler.sendMessageInternal.response
|
||||
// @Success 200 {object} handler.SendMessage.response
|
||||
// @Failure 400 {object} ginresp.apiError
|
||||
// @Failure 401 {object} ginresp.apiError "The user_id was not found or the user_key is wrong"
|
||||
// @Failure 403 {object} ginresp.apiError "The user has exceeded its daily quota - wait 24 hours or upgrade your account"
|
||||
@ -63,23 +69,6 @@ func (h MessageHandler) SendMessage(g *gin.Context) ginresp.HTTPResponse {
|
||||
SenderName *string `json:"sender_name" form:"sender_name" example:"example-server" `
|
||||
}
|
||||
|
||||
var b combined
|
||||
var q combined
|
||||
var f combined
|
||||
ctx, errResp := h.app.StartRequest(g, nil, &q, &b, &f)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
// query has highest prio, then form, then json
|
||||
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
|
||||
|
||||
return h.sendMessageInternal(g, ctx, data.UserID, data.UserKey, data.Channel, data.ChanKey, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName)
|
||||
|
||||
}
|
||||
|
||||
func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContext, UserID *models.UserID, UserKey *string, Channel *string, ChanKey *string, Title *string, Content *string, Priority *int, UserMessageID *string, SendTimestamp *float64, SenderName *string) ginresp.HTTPResponse {
|
||||
type response struct {
|
||||
Success bool `json:"success"`
|
||||
ErrorID apierr.APIError `json:"error"`
|
||||
@ -93,6 +82,53 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
SCNMessageID models.MessageID `json:"scn_msg_id"`
|
||||
}
|
||||
|
||||
var b combined
|
||||
var q combined
|
||||
var f combined
|
||||
ctx, errResp := h.app.StartRequest(g, nil, &q, &b, &f)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
}
|
||||
defer ctx.Cancel()
|
||||
|
||||
// query has highest prio, then form, then json
|
||||
data := dataext.ObjectMerge(dataext.ObjectMerge(b, f), q)
|
||||
|
||||
okResp, errResp := h.sendMessageInternal(g, ctx, data.UserID, data.UserKey, data.Channel, data.ChanKey, data.Title, data.Content, data.Priority, data.UserMessageID, data.SendTimestamp, data.SenderName)
|
||||
if errResp != nil {
|
||||
return *errResp
|
||||
} else {
|
||||
if okResp.MessageIsOld {
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message already sent",
|
||||
SuppressSend: true,
|
||||
MessageCount: okResp.User.MessagesSent,
|
||||
Quota: okResp.User.QuotaUsedToday(),
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
} else {
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message sent",
|
||||
SuppressSend: false,
|
||||
MessageCount: okResp.User.MessagesSent + 1,
|
||||
Quota: okResp.User.QuotaUsedToday() + 1,
|
||||
IsPro: okResp.User.IsPro,
|
||||
QuotaMax: okResp.User.QuotaPerDay(),
|
||||
SCNMessageID: okResp.Message.MessageID,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContext, UserID *models.UserID, UserKey *string, Channel *string, ChanKey *string, Title *string, Content *string, Priority *int, UserMessageID *string, SendTimestamp *float64, SenderName *string) (*SendMessageResponse, *ginresp.HTTPResponse) {
|
||||
if Title != nil {
|
||||
Title = langext.Ptr(strings.TrimSpace(*Title))
|
||||
}
|
||||
@ -101,27 +137,27 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
}
|
||||
|
||||
if UserID == nil {
|
||||
return ginresp.SendAPIError(g, 400, apierr.MISSING_UID, hl.USER_ID, "Missing parameter [[user_id]]", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.MISSING_UID, hl.USER_ID, "Missing parameter [[user_id]]", nil))
|
||||
}
|
||||
if UserKey == nil {
|
||||
return ginresp.SendAPIError(g, 400, apierr.MISSING_TOK, hl.USER_KEY, "Missing parameter [[user_token]]", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.MISSING_TOK, hl.USER_KEY, "Missing parameter [[user_token]]", nil))
|
||||
}
|
||||
if Title == nil {
|
||||
return ginresp.SendAPIError(g, 400, apierr.MISSING_TITLE, hl.TITLE, "Missing parameter [[title]]", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.MISSING_TITLE, hl.TITLE, "Missing parameter [[title]]", nil))
|
||||
}
|
||||
if Priority != nil && (*Priority != 0 && *Priority != 1 && *Priority != 2) {
|
||||
return ginresp.SendAPIError(g, 400, apierr.INVALID_PRIO, hl.PRIORITY, "Invalid priority", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.INVALID_PRIO, hl.PRIORITY, "Invalid priority", nil))
|
||||
}
|
||||
if len(*Title) == 0 {
|
||||
return ginresp.SendAPIError(g, 400, apierr.NO_TITLE, hl.TITLE, "No title specified", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.NO_TITLE, hl.TITLE, "No title specified", nil))
|
||||
}
|
||||
|
||||
user, err := h.database.GetUser(ctx, *UserID)
|
||||
if err == sql.ErrNoRows {
|
||||
return ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USER_NOT_FOUND, hl.USER_ID, "User not found", err))
|
||||
}
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query user", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query user", err))
|
||||
}
|
||||
|
||||
channelDisplayName := user.DefaultChannel()
|
||||
@ -132,51 +168,44 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
}
|
||||
|
||||
if len(*Title) > user.MaxTitleLength() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.TITLE_TOO_LONG, hl.TITLE, fmt.Sprintf("Title too long (max %d characters)", user.MaxTitleLength()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.TITLE_TOO_LONG, hl.TITLE, fmt.Sprintf("Title too long (max %d characters)", user.MaxTitleLength()), nil))
|
||||
}
|
||||
if Content != nil && len(*Content) > user.MaxContentLength() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.CONTENT_TOO_LONG, hl.CONTENT, fmt.Sprintf("Content too long (%d characters; max := %d characters)", len(*Content), user.MaxContentLength()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CONTENT_TOO_LONG, hl.CONTENT, fmt.Sprintf("Content too long (%d characters; max := %d characters)", len(*Content), user.MaxContentLength()), nil))
|
||||
}
|
||||
if len(channelDisplayName) > user.MaxChannelNameLength() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil))
|
||||
}
|
||||
if len(channelInternalName) > user.MaxChannelNameLength() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_TOO_LONG, hl.CHANNEL, fmt.Sprintf("Channel too long (max %d characters)", user.MaxChannelNameLength()), nil))
|
||||
}
|
||||
if SenderName != nil && len(*SenderName) > user.MaxSenderName() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderName()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.SENDERNAME_TOO_LONG, hl.SENDER_NAME, fmt.Sprintf("SenderName too long (max %d characters)", user.MaxSenderName()), nil))
|
||||
}
|
||||
if UserMessageID != nil && len(*UserMessageID) > user.MaxUserMessageID() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageID()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.USR_MSG_ID_TOO_LONG, hl.USER_MESSAGE_ID, fmt.Sprintf("MessageID too long (max %d characters)", user.MaxUserMessageID()), nil))
|
||||
}
|
||||
if SendTimestamp != nil && mathext.Abs(*SendTimestamp-float64(time.Now().Unix())) > timeext.FromHours(user.MaxTimestampDiffHours()).Seconds() {
|
||||
return ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, hl.NONE, fmt.Sprintf("The timestamp mus be within %d hours of now()", user.MaxTimestampDiffHours()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.TIMESTAMP_OUT_OF_RANGE, hl.NONE, fmt.Sprintf("The timestamp mus be within %d hours of now()", user.MaxTimestampDiffHours()), nil))
|
||||
}
|
||||
|
||||
if UserMessageID != nil {
|
||||
msg, err := h.database.GetMessageByUserMessageID(ctx, *UserMessageID)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query existing message", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query existing message", err))
|
||||
}
|
||||
if msg != nil {
|
||||
//the found message can be deleted (!), but we still return NO_ERROR here...
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message already sent",
|
||||
SuppressSend: true,
|
||||
MessageCount: user.MessagesSent,
|
||||
Quota: user.QuotaUsedToday(),
|
||||
IsPro: user.IsPro,
|
||||
QuotaMax: user.QuotaPerDay(),
|
||||
SCNMessageID: msg.MessageID,
|
||||
}))
|
||||
return &SendMessageResponse{
|
||||
User: user,
|
||||
Message: *msg,
|
||||
MessageIsOld: true,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
if user.QuotaRemainingToday() <= 0 {
|
||||
return ginresp.SendAPIError(g, 403, apierr.QUOTA_REACHED, hl.NONE, fmt.Sprintf("Daily quota reached (%d)", user.QuotaPerDay()), nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 403, apierr.QUOTA_REACHED, hl.NONE, fmt.Sprintf("Daily quota reached (%d)", user.QuotaPerDay()), nil))
|
||||
}
|
||||
|
||||
var channel models.Channel
|
||||
@ -185,10 +214,10 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
|
||||
foreignChan, err := h.database.GetChannelByNameAndSendKey(ctx, channelInternalName, *ChanKey)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query (foreign) channel", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query (foreign) channel", err))
|
||||
}
|
||||
if foreignChan == nil {
|
||||
return ginresp.SendAPIError(g, 400, apierr.CHANNEL_NOT_FOUND, hl.CHANNEL, "(Foreign) Channel not found", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 400, apierr.CHANNEL_NOT_FOUND, hl.CHANNEL, "(Foreign) Channel not found", err))
|
||||
}
|
||||
channel = *foreignChan
|
||||
} else {
|
||||
@ -196,7 +225,7 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
|
||||
channel, err = h.app.GetOrCreateChannel(ctx, *UserID, channelDisplayName, channelInternalName)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query/create (owned) channel", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query/create (owned) channel", err))
|
||||
}
|
||||
}
|
||||
|
||||
@ -205,7 +234,7 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
forgChanSend := *UserID != channel.OwnerUserID && ChanKey != nil && *ChanKey == channel.SendKey
|
||||
|
||||
if !selfChanAdmin && !selfChanSend && !forgChanSend {
|
||||
return ginresp.SendAPIError(g, 401, apierr.USER_AUTH_FAILED, hl.USER_KEY, "You are not authorized for this action", nil)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 401, apierr.USER_AUTH_FAILED, hl.USER_KEY, "You are not authorized for this action", nil))
|
||||
}
|
||||
|
||||
var sendTimestamp *time.Time = nil
|
||||
@ -219,28 +248,28 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
|
||||
msg, err := h.database.CreateMessage(ctx, *UserID, channel, sendTimestamp, *Title, Content, priority, UserMessageID, clientIP, SenderName)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err))
|
||||
}
|
||||
|
||||
subscriptions, err := h.database.ListSubscriptionsByChannel(ctx, channel.ChannelID)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query subscriptions", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query subscriptions", err))
|
||||
}
|
||||
|
||||
err = h.database.IncUserMessageCounter(ctx, user)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc user msg-counter", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc user msg-counter", err))
|
||||
}
|
||||
|
||||
err = h.database.IncChannelMessageCounter(ctx, channel)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc channel msg-counter", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to inc channel msg-counter", err))
|
||||
}
|
||||
|
||||
for _, sub := range subscriptions {
|
||||
clients, err := h.database.ListClients(ctx, sub.SubscriberUserID)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query clients", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to query clients", err))
|
||||
}
|
||||
|
||||
if !sub.Confirmed {
|
||||
@ -253,28 +282,21 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
|
||||
if err != nil {
|
||||
_, err = h.database.CreateRetryDelivery(ctx, client, msg)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create delivery", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create delivery", err))
|
||||
}
|
||||
} else {
|
||||
_, err = h.database.CreateSuccessDelivery(ctx, client, msg, *fcmDelivID)
|
||||
if err != nil {
|
||||
return ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create delivery", err)
|
||||
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create delivery", err))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.FinishSuccess(ginresp.JSON(http.StatusOK, response{
|
||||
Success: true,
|
||||
ErrorID: apierr.NO_ERROR,
|
||||
ErrorHighlight: -1,
|
||||
Message: "Message sent",
|
||||
SuppressSend: false,
|
||||
MessageCount: user.MessagesSent + 1,
|
||||
Quota: user.QuotaUsedToday() + 1,
|
||||
IsPro: user.IsPro,
|
||||
QuotaMax: user.QuotaPerDay(),
|
||||
SCNMessageID: msg.MessageID,
|
||||
}))
|
||||
return &SendMessageResponse{
|
||||
User: user,
|
||||
Message: msg,
|
||||
MessageIsOld: false,
|
||||
}, nil
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ func (db *Database) ListRetrieableDeliveries(ctx TxContext, pageSize int) ([]mod
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, err := tx.Query(ctx, "SELECT * FROM deliveries WHERE status = 'RETRY' AND next_delivery < :next LIMIT :lim ORDER BY next_delivery ASC", sq.PP{
|
||||
rows, err := tx.Query(ctx, "SELECT * FROM deliveries WHERE status = 'RETRY' AND next_delivery < :next ORDER BY next_delivery ASC LIMIT :lim", sq.PP{
|
||||
"next": time2DB(time.Now()),
|
||||
"lim": pageSize,
|
||||
})
|
||||
|
@ -66,8 +66,8 @@ func (db *Database) InsertRequestLog(ctx context.Context, requestid models.Reque
|
||||
}
|
||||
|
||||
func (db *Database) Cleanup(ctx context.Context, count int, duration time.Duration) (int64, error) {
|
||||
res1, err := db.db.Exec(ctx, "DELETE FROM requests WHERE request_id NOT IN ( SELECT request_id FROM requests ORDER BY timestamp_created DESC LIMIT :lim ) ", sq.PP{
|
||||
"lim": count,
|
||||
res1, err := db.db.Exec(ctx, "DELETE FROM requests WHERE request_id NOT IN ( SELECT request_id FROM requests ORDER BY timestamp_created DESC LIMIT :keep ) ", sq.PP{
|
||||
"keep": count,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -160,7 +160,7 @@
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.sendMessageInternal.response"
|
||||
"$ref": "#/definitions/handler.SendMessage.response"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
@ -245,6 +245,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Ack.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -330,6 +336,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Expand.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -401,6 +413,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Info.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -732,6 +750,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Register.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -780,6 +804,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Requery.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -880,6 +910,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Update.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -964,6 +1000,12 @@
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.Upgrade.response"
|
||||
}
|
||||
},
|
||||
"default": {
|
||||
"description": "",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/ginresp.compatAPIError"
|
||||
}
|
||||
@ -2182,7 +2224,7 @@
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.sendMessageInternal.response"
|
||||
"$ref": "#/definitions/handler.SendMessage.response"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
@ -2296,7 +2338,7 @@
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/handler.sendMessageInternal.response"
|
||||
"$ref": "#/definitions/handler.SendMessageCompat.response"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
@ -2695,6 +2737,76 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"handler.SendMessage.response": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"errhighlight": {
|
||||
"type": "integer"
|
||||
},
|
||||
"error": {
|
||||
"type": "integer"
|
||||
},
|
||||
"is_pro": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"messagecount": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota_max": {
|
||||
"type": "integer"
|
||||
},
|
||||
"scn_msg_id": {
|
||||
"type": "string"
|
||||
},
|
||||
"success": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"suppress_send": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"handler.SendMessageCompat.response": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"errhighlight": {
|
||||
"type": "integer"
|
||||
},
|
||||
"error": {
|
||||
"type": "integer"
|
||||
},
|
||||
"is_pro": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"messagecount": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota_max": {
|
||||
"type": "integer"
|
||||
},
|
||||
"scn_msg_id": {
|
||||
"type": "integer"
|
||||
},
|
||||
"success": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"suppress_send": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"handler.Sleep.response": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@ -2803,41 +2915,6 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"handler.sendMessageInternal.response": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"errhighlight": {
|
||||
"type": "integer"
|
||||
},
|
||||
"error": {
|
||||
"type": "integer"
|
||||
},
|
||||
"is_pro": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"messagecount": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota": {
|
||||
"type": "integer"
|
||||
},
|
||||
"quota_max": {
|
||||
"type": "integer"
|
||||
},
|
||||
"scn_msg_id": {
|
||||
"type": "string"
|
||||
},
|
||||
"success": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"suppress_send": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"models.ChannelWithSubscriptionJSON": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -243,6 +243,52 @@ definitions:
|
||||
example: P3TNH8mvv14fm
|
||||
type: string
|
||||
type: object
|
||||
handler.SendMessage.response:
|
||||
properties:
|
||||
errhighlight:
|
||||
type: integer
|
||||
error:
|
||||
type: integer
|
||||
is_pro:
|
||||
type: boolean
|
||||
message:
|
||||
type: string
|
||||
messagecount:
|
||||
type: integer
|
||||
quota:
|
||||
type: integer
|
||||
quota_max:
|
||||
type: integer
|
||||
scn_msg_id:
|
||||
type: string
|
||||
success:
|
||||
type: boolean
|
||||
suppress_send:
|
||||
type: boolean
|
||||
type: object
|
||||
handler.SendMessageCompat.response:
|
||||
properties:
|
||||
errhighlight:
|
||||
type: integer
|
||||
error:
|
||||
type: integer
|
||||
is_pro:
|
||||
type: boolean
|
||||
message:
|
||||
type: string
|
||||
messagecount:
|
||||
type: integer
|
||||
quota:
|
||||
type: integer
|
||||
quota_max:
|
||||
type: integer
|
||||
scn_msg_id:
|
||||
type: integer
|
||||
success:
|
||||
type: boolean
|
||||
suppress_send:
|
||||
type: boolean
|
||||
type: object
|
||||
handler.Sleep.response:
|
||||
properties:
|
||||
duration:
|
||||
@ -313,29 +359,6 @@ definitions:
|
||||
uri:
|
||||
type: string
|
||||
type: object
|
||||
handler.sendMessageInternal.response:
|
||||
properties:
|
||||
errhighlight:
|
||||
type: integer
|
||||
error:
|
||||
type: integer
|
||||
is_pro:
|
||||
type: boolean
|
||||
message:
|
||||
type: string
|
||||
messagecount:
|
||||
type: integer
|
||||
quota:
|
||||
type: integer
|
||||
quota_max:
|
||||
type: integer
|
||||
scn_msg_id:
|
||||
type: string
|
||||
success:
|
||||
type: boolean
|
||||
suppress_send:
|
||||
type: boolean
|
||||
type: object
|
||||
models.ChannelWithSubscriptionJSON:
|
||||
properties:
|
||||
channel_id:
|
||||
@ -618,7 +641,7 @@ paths:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.sendMessageInternal.response'
|
||||
$ref: '#/definitions/handler.SendMessage.response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
@ -677,6 +700,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Ack.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Acknowledge that a message was received
|
||||
@ -735,6 +762,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Expand.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Get a whole (potentially truncated) message
|
||||
@ -783,6 +814,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Info.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Get information about the current user
|
||||
@ -1011,6 +1046,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Register.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Register a new account
|
||||
@ -1044,6 +1083,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Requery.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Return all not-acknowledged messages
|
||||
@ -1112,6 +1155,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Update.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Set the fcm-token (android)
|
||||
@ -1171,6 +1218,10 @@ paths:
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.Upgrade.response'
|
||||
default:
|
||||
description: ""
|
||||
schema:
|
||||
$ref: '#/definitions/ginresp.compatAPIError'
|
||||
summary: Upgrade a free account to a paid account
|
||||
@ -2002,7 +2053,7 @@ paths:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.sendMessageInternal.response'
|
||||
$ref: '#/definitions/handler.SendMessage.response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
@ -2075,7 +2126,7 @@ paths:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/handler.sendMessageInternal.response'
|
||||
$ref: '#/definitions/handler.SendMessageCompat.response'
|
||||
"400":
|
||||
description: Bad Request
|
||||
schema:
|
||||
|
@ -1,5 +1,28 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
tt "blackforestbytes.com/simplecloudnotifier/test/util"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateCompatUser(t *testing.T) {
|
||||
_, baseUrl, stop := tt.StartSimpleWebserver(t)
|
||||
defer stop()
|
||||
|
||||
r0 := tt.RequestGet[gin.H](t, baseUrl, fmt.Sprintf("/api/register.php?fcm_token=%s&pro=%s&pro_token=%s", "DUMMY_FCM", "0", ""))
|
||||
|
||||
tt.AssertEqual(t, "success", true, r0["success"])
|
||||
|
||||
userid := int64(r0["user_id"].(float64))
|
||||
userkey := r0["user_key"].(string)
|
||||
|
||||
r1 := tt.RequestGet[gin.H](t, baseUrl, fmt.Sprintf("/api/info.php?user_id=%d&user_key=%s", userid, userkey))
|
||||
|
||||
tt.AssertEqual(t, "success", true, r1["success"])
|
||||
}
|
||||
|
||||
//TODO test compat methods
|
||||
|
||||
//TODO also test compat_id mapping
|
||||
|
Loading…
Reference in New Issue
Block a user