2022-11-18 21:25:40 +01:00
package handler
import (
2022-11-19 15:13:47 +01:00
"blackforestbytes.com/simplecloudnotifier/api/apierr"
2022-11-30 17:58:04 +01:00
hl "blackforestbytes.com/simplecloudnotifier/api/apihighlight"
2022-12-20 13:55:09 +01:00
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
2023-01-06 00:39:21 +01:00
primarydb "blackforestbytes.com/simplecloudnotifier/db/impl/primary"
2022-11-18 21:25:40 +01:00
"blackforestbytes.com/simplecloudnotifier/logic"
2022-11-20 22:18:24 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
2022-11-19 15:13:47 +01:00
"database/sql"
2023-07-30 15:58:37 +02:00
"errors"
2022-11-19 15:13:47 +01:00
"fmt"
2022-11-18 21:25:40 +01:00
"github.com/gin-gonic/gin"
2023-06-18 01:36:34 +02:00
"github.com/rs/zerolog/log"
2022-11-19 15:13:47 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
"gogs.mikescher.com/BlackForestBytes/goext/timeext"
"net/http"
"strings"
"time"
2022-11-18 21:25:40 +01:00
)
2023-01-16 18:53:22 +01:00
type SendMessageResponse struct {
2023-02-03 22:51:03 +01:00
User models . User
Message models . Message
MessageIsOld bool
CompatMessageID int64
2023-01-16 18:53:22 +01:00
}
2022-11-18 21:25:40 +01:00
type MessageHandler struct {
2022-11-19 15:13:47 +01:00
app * logic . Application
2023-01-06 00:39:21 +01:00
database * primarydb . Database
2022-11-18 21:25:40 +01:00
}
2022-11-19 15:13:47 +01:00
func NewMessageHandler ( app * logic . Application ) MessageHandler {
return MessageHandler {
app : app ,
2023-01-06 00:39:21 +01:00
database : app . Database . Primary ,
2022-11-19 15:13:47 +01:00
}
}
// SendMessage swaggerdoc
//
2023-04-21 21:45:16 +02:00
// @Summary Send a new message
// @Description All parameter can be set via query-parameter or the json body. Only UserID, UserKey and Title are required
// @Tags External
2022-11-19 15:13:47 +01:00
//
2023-04-21 21:45:16 +02:00
// @Param query_data query handler.SendMessage.combined false " "
// @Param post_body body handler.SendMessage.combined false " "
// @Param form_body formData handler.SendMessage.combined false " "
2022-11-19 15:13:47 +01:00
//
2023-04-21 21:45:16 +02:00
// @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"
// @Failure 500 {object} ginresp.apiError "An internal server error occurred - try again later"
2022-11-19 15:13:47 +01:00
//
2023-04-21 21:45:16 +02:00
// @Router / [POST]
// @Router /send [POST]
2022-11-18 21:25:40 +01:00
func ( h MessageHandler ) SendMessage ( g * gin . Context ) ginresp . HTTPResponse {
2022-11-30 17:58:04 +01:00
type combined struct {
UserID * models . UserID ` json:"user_id" form:"user_id" example:"7725" `
2023-04-21 21:45:16 +02:00
KeyToken * string ` json:"key" form:"key" example:"P3TNH8mvv14fm" `
2022-11-30 17:58:04 +01:00
Channel * string ` json:"channel" form:"channel" example:"test" `
Title * string ` json:"title" form:"title" example:"Hello World" `
Content * string ` json:"content" form:"content" example:"This is a message" `
Priority * int ` json:"priority" form:"priority" example:"1" enums:"0,1,2" `
UserMessageID * string ` json:"msg_id" form:"msg_id" example:"db8b0e6a-a08c-4646" `
SendTimestamp * float64 ` json:"timestamp" form:"timestamp" example:"1669824037" `
SenderName * string ` json:"sender_name" form:"sender_name" example:"example-server" `
}
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 models . MessageID ` json:"scn_msg_id" `
}
2022-11-30 17:58:04 +01:00
var b combined
var q combined
var f combined
2023-06-10 03:41:54 +02:00
ctx , errResp := h . app . StartRequest ( g , nil , & q , & b , & f , logic . RequestOptions { IgnoreWrongContentType : true } )
2022-11-20 03:06:08 +01:00
if errResp != nil {
return * errResp
}
defer ctx . Cancel ( )
2022-11-30 20:23:31 +01:00
// query has highest prio, then form, then json
2022-11-20 20:34:18 +01:00
data := dataext . ObjectMerge ( dataext . ObjectMerge ( b , f ) , q )
2022-11-20 03:06:08 +01:00
2023-04-21 21:45:16 +02:00
okResp , errResp := h . sendMessageInternal ( g , ctx , data . UserID , data . KeyToken , data . Channel , data . Title , data . Content , data . Priority , data . UserMessageID , data . SendTimestamp , data . SenderName )
2023-01-16 18:53:22 +01:00
if errResp != nil {
return * errResp
} else {
2023-06-17 20:16:02 +02:00
return ctx . FinishSuccess ( ginresp . 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 . Message . MessageID ,
} ) )
2022-11-19 15:13:47 +01:00
}
2023-01-16 18:53:22 +01:00
}
2022-11-19 15:13:47 +01:00
2023-04-21 21:45:16 +02:00
func ( h MessageHandler ) sendMessageInternal ( g * gin . Context , ctx * logic . AppContext , UserID * models . UserID , Key * string , Channel * string , Title * string , Content * string , Priority * int , UserMessageID * string , SendTimestamp * float64 , SenderName * string ) ( * SendMessageResponse , * ginresp . HTTPResponse ) {
2022-11-20 12:59:43 +01:00
if Title != nil {
Title = langext . Ptr ( strings . TrimSpace ( * Title ) )
}
if UserMessageID != nil {
UserMessageID = langext . Ptr ( strings . TrimSpace ( * UserMessageID ) )
}
2022-11-20 03:06:08 +01:00
if UserID == nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . MISSING_UID , hl . USER_ID , "Missing parameter [[user_id]]" , nil ) )
2022-11-19 15:13:47 +01:00
}
2023-04-21 21:45:16 +02:00
if Key == nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . MISSING_TOK , hl . USER_KEY , "Missing parameter [[key]]" , nil ) )
2022-11-19 15:13:47 +01:00
}
2022-11-20 03:06:08 +01:00
if Title == nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . MISSING_TITLE , hl . TITLE , "Missing parameter [[title]]" , nil ) )
2022-11-19 15:13:47 +01:00
}
2022-11-20 03:06:08 +01:00
if Priority != nil && ( * Priority != 0 && * Priority != 1 && * Priority != 2 ) {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . INVALID_PRIO , hl . PRIORITY , "Invalid priority" , nil ) )
2022-11-19 15:13:47 +01:00
}
2022-11-20 12:59:43 +01:00
if len ( * Title ) == 0 {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . NO_TITLE , hl . TITLE , "No title specified" , nil ) )
2022-11-19 15:13:47 +01:00
}
2022-11-20 03:06:08 +01:00
user , err := h . database . GetUser ( ctx , * UserID )
2023-07-30 15:58:37 +02:00
if errors . Is ( err , sql . ErrNoRows ) {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . USER_NOT_FOUND , hl . USER_ID , "User not found" , err ) )
2022-11-19 15:13:47 +01:00
}
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query user" , err ) )
2022-11-19 15:13:47 +01:00
}
2022-12-22 11:22:36 +01:00
channelDisplayName := user . DefaultChannel ( )
channelInternalName := user . DefaultChannel ( )
2022-11-20 22:18:24 +01:00
if Channel != nil {
2022-12-22 11:22:36 +01:00
channelDisplayName = h . app . NormalizeChannelDisplayName ( * Channel )
channelInternalName = h . app . NormalizeChannelInternalName ( * Channel )
2022-11-20 22:18:24 +01:00
}
2022-11-20 20:34:18 +01:00
if len ( * Title ) > user . MaxTitleLength ( ) {
2023-01-16 18:53:22 +01:00
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 ) )
2022-11-19 15:13:47 +01:00
}
2022-11-20 12:59:43 +01:00
if Content != nil && len ( * Content ) > user . MaxContentLength ( ) {
2023-01-16 18:53:22 +01:00
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 ) )
2022-11-19 15:13:47 +01:00
}
2022-12-22 11:22:36 +01:00
if len ( channelDisplayName ) > user . MaxChannelNameLength ( ) {
2023-01-16 18:53:22 +01:00
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 ) )
2022-12-22 11:22:36 +01:00
}
2023-05-28 13:14:05 +02:00
if len ( strings . TrimSpace ( channelDisplayName ) ) == 0 {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . CHANNEL_NAME_EMPTY , hl . CHANNEL , fmt . Sprintf ( "Channel displayname cannot be empty" ) , nil ) )
}
2022-12-22 11:22:36 +01:00
if len ( channelInternalName ) > user . MaxChannelNameLength ( ) {
2023-01-16 18:53:22 +01:00
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 ) )
2022-11-21 22:52:44 +01:00
}
2023-05-28 13:14:05 +02:00
if len ( strings . TrimSpace ( channelInternalName ) ) == 0 {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . CHANNEL_NAME_EMPTY , hl . CHANNEL , fmt . Sprintf ( "Channel internalname cannot be empty" ) , nil ) )
}
2023-07-30 16:53:46 +02:00
if SenderName != nil && len ( * SenderName ) > user . MaxSenderNameLength ( ) {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 400 , apierr . SENDERNAME_TOO_LONG , hl . SENDER_NAME , fmt . Sprintf ( "SenderName too long (max %d characters)" , user . MaxSenderNameLength ( ) ) , nil ) )
2022-11-29 11:07:15 +01:00
}
2023-07-30 16:53:46 +02:00
if UserMessageID != nil && len ( * UserMessageID ) > user . MaxUserMessageIDLength ( ) {
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 . MaxUserMessageIDLength ( ) ) , nil ) )
2022-11-29 11:07:15 +01:00
}
2022-11-30 22:29:12 +01:00
if SendTimestamp != nil && mathext . Abs ( * SendTimestamp - float64 ( time . Now ( ) . Unix ( ) ) ) > timeext . FromHours ( user . MaxTimestampDiffHours ( ) ) . Seconds ( ) {
2023-01-16 18:53:22 +01:00
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 ) )
2022-11-30 22:29:12 +01:00
}
2022-11-19 15:13:47 +01:00
2022-11-20 03:06:08 +01:00
if UserMessageID != nil {
msg , err := h . database . GetMessageByUserMessageID ( ctx , * UserMessageID )
2022-11-19 15:13:47 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query existing message" , err ) )
2022-11-19 15:13:47 +01:00
}
if msg != nil {
2023-02-03 22:51:03 +01:00
existingCompID , _ , err := h . database . ConvertToCompatID ( ctx , msg . MessageID . String ( ) )
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query compat-id" , err ) )
}
if existingCompID == nil {
v , err := h . database . CreateCompatID ( ctx , "messageid" , msg . MessageID . String ( ) )
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to create compat-id" , err ) )
}
existingCompID = & v
}
2022-12-14 12:29:55 +01:00
//the found message can be deleted (!), but we still return NO_ERROR here...
2023-01-16 18:53:22 +01:00
return & SendMessageResponse {
2023-02-03 22:51:03 +01:00
User : user ,
Message : * msg ,
MessageIsOld : true ,
CompatMessageID : * existingCompID ,
2023-01-16 18:53:22 +01:00
} , nil
2022-11-19 15:13:47 +01:00
}
}
if user . QuotaRemainingToday ( ) <= 0 {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 403 , apierr . QUOTA_REACHED , hl . NONE , fmt . Sprintf ( "Daily quota reached (%d)" , user . QuotaPerDay ( ) ) , nil ) )
2022-11-19 15:13:47 +01:00
}
2023-04-21 21:45:16 +02:00
channel , err := h . app . GetOrCreateChannel ( ctx , * UserID , channelDisplayName , channelInternalName )
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query/create (owned) channel" , err ) )
2022-11-19 15:13:47 +01:00
}
2023-04-21 21:45:16 +02:00
keytok , permResp := ctx . CheckPermissionSend ( channel , * Key )
if permResp != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 401 , apierr . USER_AUTH_FAILED , hl . USER_KEY , "You are not authorized for this action" , nil ) )
2022-11-19 15:13:47 +01:00
}
var sendTimestamp * time . Time = nil
2022-11-20 03:06:08 +01:00
if SendTimestamp != nil {
sendTimestamp = langext . Ptr ( timeext . UnixFloatSeconds ( * SendTimestamp ) )
2022-11-19 15:13:47 +01:00
}
2022-11-30 21:39:14 +01:00
priority := langext . Coalesce ( Priority , user . DefaultPriority ( ) )
2022-11-19 15:13:47 +01:00
2022-11-29 11:07:15 +01:00
clientIP := g . ClientIP ( )
2023-05-27 18:51:20 +02:00
msg , err := h . database . CreateMessage ( ctx , * UserID , channel , sendTimestamp , * Title , Content , priority , UserMessageID , clientIP , SenderName , keytok . KeyTokenID )
2022-11-19 15:13:47 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to create message in db" , err ) )
2022-11-19 15:13:47 +01:00
}
2023-06-18 01:55:58 +02:00
compatMsgID , err := h . database . CreateCompatID ( ctx , "messageid" , msg . MessageID . String ( ) )
2023-02-03 22:51:03 +01:00
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to create compat-id" , err ) )
}
2023-07-30 15:58:37 +02:00
subFilter := models . SubscriptionFilter { ChannelID : langext . Ptr ( [ ] models . ChannelID { channel . ChannelID } ) , Confirmed : langext . PTrue }
activeSubscriptions , err := h . database . ListSubscriptions ( ctx , subFilter )
2022-11-19 15:13:47 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query subscriptions" , err ) )
2022-11-19 15:13:47 +01:00
}
2023-06-17 20:08:39 +02:00
err = h . database . IncUserMessageCounter ( ctx , & user )
2022-11-19 23:16:54 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to inc user msg-counter" , err ) )
2022-11-19 23:16:54 +01:00
}
2023-06-17 20:08:39 +02:00
err = h . database . IncChannelMessageCounter ( ctx , & channel )
2022-11-19 23:16:54 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to inc channel msg-counter" , err ) )
2023-04-21 21:45:16 +02:00
}
2023-06-17 20:08:39 +02:00
err = h . database . IncKeyTokenMessageCounter ( ctx , keytok )
2023-04-21 21:45:16 +02:00
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to inc token msg-counter" , err ) )
2022-11-19 23:16:54 +01:00
}
2023-06-18 01:36:34 +02:00
log . Info ( ) . Msg ( fmt . Sprintf ( "Sending new notification %s for user %s" , msg . MessageID , UserID ) )
2023-07-30 15:58:37 +02:00
for _ , sub := range activeSubscriptions {
2022-11-19 15:13:47 +01:00
clients , err := h . database . ListClients ( ctx , sub . SubscriberUserID )
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query clients" , err ) )
2022-11-19 15:13:47 +01:00
}
for _ , client := range clients {
2023-05-27 20:02:16 +02:00
isCompatClient , err := h . database . IsCompatClient ( ctx , client . ClientID )
if err != nil {
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to query compat_clients" , err ) )
}
var titleOverride * string = nil
2023-06-18 01:55:58 +02:00
var msgidOverride * string = nil
2023-05-27 20:02:16 +02:00
if isCompatClient {
2023-06-18 01:55:58 +02:00
titleOverride = langext . Ptr ( h . app . CompatizeMessageTitle ( ctx , msg ) )
msgidOverride = langext . Ptr ( fmt . Sprintf ( "%d" , compatMsgID ) )
2023-05-27 20:02:16 +02:00
}
2023-06-18 01:55:58 +02:00
fcmDelivID , err := h . app . DeliverMessage ( ctx , client , msg , titleOverride , msgidOverride )
2022-11-19 15:13:47 +01:00
if err != nil {
_ , err = h . database . CreateRetryDelivery ( ctx , client , msg )
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to create delivery" , err ) )
2022-11-19 15:13:47 +01:00
}
} else {
2023-05-28 23:25:18 +02:00
_ , err = h . database . CreateSuccessDelivery ( ctx , client , msg , fcmDelivID )
2022-11-19 15:13:47 +01:00
if err != nil {
2023-01-16 18:53:22 +01:00
return nil , langext . Ptr ( ginresp . SendAPIError ( g , 500 , apierr . DATABASE_ERROR , hl . NONE , "Failed to create delivery" , err ) )
2022-11-19 15:13:47 +01:00
}
}
}
}
2023-01-16 18:53:22 +01:00
return & SendMessageResponse {
2023-02-03 22:51:03 +01:00
User : user ,
Message : msg ,
MessageIsOld : false ,
2023-06-18 01:55:58 +02:00
CompatMessageID : compatMsgID ,
2023-01-16 18:53:22 +01:00
} , nil
2022-11-18 21:25:40 +01:00
}
2023-07-31 20:04:38 +02:00
// UptimeKumaWebHook swaggerdoc
//
// @Summary Send a new message
// @Description All parameter can be set via query-parameter or the json body. Only UserID, UserKey and Title are required
// @Tags External
//
// @Param query_data query handler.UptimeKumaWebHook.query false " "
// @Param post_body body handler.UptimeKumaWebHook.uptimeKumaWebhookBody false " "
//
// @Success 200 {object} any
// @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"
// @Failure 500 {object} ginresp.apiError "An internal server error occurred - try again later"
//
// @Router /webhook/uptime-kuma [POST]
func ( h MessageHandler ) UptimeKumaWebHook ( g * gin . Context ) ginresp . HTTPResponse {
type query struct {
UserID * models . UserID ` form:"user_id" example:"7725" `
KeyToken * string ` form:"key" example:"P3TNH8mvv14fm" `
}
type uptimeKumaWebhookBody struct {
Heartbeat * struct {
Time string ` json:"time" `
Status int ` json:"status" `
Msg string ` json:"msg" `
Timezone string ` json:"timezone" `
TimezoneOffset string ` json:"timezoneOffset" `
LocalDateTime string ` json:"localDateTime" `
} ` json:"heartbeat" `
Monitor * struct {
Name string ` json:"name" `
Url * string ` json:"url" `
} ` json:"monitor" `
Msg string ` json:"msg" `
}
var b uptimeKumaWebhookBody
var q query
ctx , httpErr := h . app . StartRequest ( g , nil , & q , & b , nil )
if httpErr != nil {
return * httpErr
}
defer ctx . Cancel ( )
var title = ""
var content = ""
content += fmt . Sprintf ( "%v\n" , b . Msg )
if b . Monitor != nil {
content += fmt . Sprintf ( "%v\n" , b . Monitor . Name )
if b . Monitor . Url != nil {
content += fmt . Sprintf ( "url: %v\n" , * b . Monitor . Url )
}
if b . Heartbeat != nil {
statusString := "down"
if b . Heartbeat . Status == 1 {
statusString = "up"
}
title = fmt . Sprintf ( "%v %v!" , b . Monitor . Name , statusString )
}
}
if b . Heartbeat != nil {
content += "\n===== Heartbeat ======\n"
content += fmt . Sprintf ( "msg: %v\n" , b . Heartbeat . Msg )
content += fmt . Sprintf ( "timestamp: %v\n" , b . Heartbeat . Time )
content += fmt . Sprintf ( "timezone: %v\n" , b . Heartbeat . Timezone )
content += fmt . Sprintf ( "timezone offset: %v\n" , b . Heartbeat . TimezoneOffset )
content += fmt . Sprintf ( "local date time: %v\n" , b . Heartbeat . TimezoneOffset )
}
okResp , errResp := h . sendMessageInternal ( g , ctx , q . UserID , q . KeyToken , nil , & title , & content , langext . Ptr ( 1 ) , nil , nil , nil )
if errResp != nil {
return * errResp
}
return ctx . FinishSuccess ( ginresp . JSON ( http . StatusOK , okResp ) )
}