mirror of
https://github.com/pushbits/server.git
synced 2025-05-03 12:16:20 +02:00
Sanitize notification before processing it
This commit is contained in:
parent
21afef0128
commit
e676b723e9
2 changed files with 18 additions and 12 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pushbits/server/internal/authentication"
|
||||
|
@ -45,23 +44,17 @@ type NotificationHandler struct {
|
|||
// @Failure 500,404,403 ""
|
||||
// @Router /message [post]
|
||||
func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
|
||||
var notification model.Notification
|
||||
application := authentication.GetApplication(ctx)
|
||||
log.Printf("Sending notification for application %s.", application.Name)
|
||||
|
||||
var notification model.Notification
|
||||
if err := ctx.Bind(¬ification); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
application := authentication.GetApplication(ctx)
|
||||
log.Printf("Sending notification for application %s.", application.Name)
|
||||
|
||||
notification.ApplicationID = application.ID
|
||||
if strings.TrimSpace(notification.Title) == "" {
|
||||
notification.Title = application.Name
|
||||
}
|
||||
notification.Date = time.Now()
|
||||
notification.Sanitize(application)
|
||||
|
||||
messageID, err := h.DP.SendNotification(application, ¬ification)
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return
|
||||
}
|
||||
|
@ -86,8 +79,9 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
|
|||
// @Router /message/{message_id} [DELETE]
|
||||
func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
|
||||
application := authentication.GetApplication(ctx)
|
||||
id, err := getMessageID(ctx)
|
||||
log.Printf("Deleting notification for application %s.", application.Name)
|
||||
|
||||
id, err := getMessageID(ctx)
|
||||
if success := successOrAbort(ctx, http.StatusUnprocessableEntity, err); !success {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -16,6 +17,17 @@ type Notification struct {
|
|||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
// Sanitize sets explicit defaults for a notification.
|
||||
func (n *Notification) Sanitize(application *Application) {
|
||||
n.ID = ""
|
||||
n.UrlEncodedID = ""
|
||||
n.ApplicationID = application.ID
|
||||
if strings.TrimSpace(n.Title) == "" {
|
||||
n.Title = application.Name
|
||||
}
|
||||
n.Date = time.Now()
|
||||
}
|
||||
|
||||
// DeleteNotification holds information like the message ID of a deletion notification.
|
||||
type DeleteNotification struct {
|
||||
ID string `json:"id" form:"id"`
|
||||
|
|
Loading…
Add table
Reference in a new issue