Merge branch 'master' into testing

This commit is contained in:
Cubicroot 2021-06-20 17:24:14 +02:00 committed by GitHub
commit 5f5d941bc4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 333 additions and 41 deletions

View file

@ -20,6 +20,17 @@ func getID(ctx *gin.Context) (uint, error) {
return id, nil
}
func getMessageID(ctx *gin.Context) (string, error) {
id, ok := ctx.MustGet("messageid").(string)
if !ok {
err := errors.New("an error occured while retrieving messageID from context")
ctx.AbortWithError(http.StatusInternalServerError, err)
return "", err
}
return id, nil
}
func getApplication(ctx *gin.Context, db Database) (*model.Application, error) {
id, err := getID(ctx)
if err != nil {

5
internal/api/errors.go Normal file
View file

@ -0,0 +1,5 @@
package api
import "errors"
var ErrorMessageNotFound = errors.New("message not found")

View file

@ -8,6 +8,10 @@ type idInURI struct {
ID uint `uri:"id" binding:"required"`
}
type messageIdInURI struct {
MessageID string `uri:"messageid" binding:"required"`
}
// RequireIDInURI returns a Gin middleware which requires an ID to be supplied in the URI of the request.
func RequireIDInURI() gin.HandlerFunc {
return func(ctx *gin.Context) {
@ -20,3 +24,16 @@ func RequireIDInURI() gin.HandlerFunc {
ctx.Set("id", requestModel.ID)
}
}
// RequireMessageIDInURI returns a Gin middleware which requires an messageID to be supplied in the URI of the request.
func RequireMessageIDInURI() gin.HandlerFunc {
return func(ctx *gin.Context) {
var requestModel messageIdInURI
if err := ctx.BindUri(&requestModel); err != nil {
return
}
ctx.Set("messageid", requestModel.MessageID)
}
}

View file

@ -3,6 +3,7 @@ package api
import (
"log"
"net/http"
"net/url"
"strings"
"time"
@ -18,7 +19,8 @@ type NotificationDatabase interface {
// The NotificationDispatcher interface for relaying notifications.
type NotificationDispatcher interface {
SendNotification(a *model.Application, n *model.Notification) error
SendNotification(a *model.Application, n *model.Notification) (id string, err error)
DeleteNotification(a *model.Application, n *model.DeleteNotification) error
}
// NotificationHandler holds information for processing requests about notifications.
@ -38,16 +40,41 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
application := authentication.GetApplication(ctx)
log.Printf("Sending notification for application %s.", application.Name)
notification.ID = 0
notification.ApplicationID = application.ID
if strings.TrimSpace(notification.Title) == "" {
notification.Title = application.Name
}
notification.Date = time.Now()
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DP.SendNotification(application, &notification)); !success {
messageID, err := h.DP.SendNotification(application, &notification)
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
return
}
notification.ID = messageID
notification.UrlEncodedID = url.QueryEscape(messageID)
ctx.JSON(http.StatusOK, &notification)
}
// DeleteNotification is used to delete (or mark as deleted) a notification for a user
func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
application := authentication.GetApplication(ctx)
id, err := getMessageID(ctx)
if success := successOrAbort(ctx, http.StatusUnprocessableEntity, err); !success {
return
}
n := model.DeleteNotification{
ID: id,
Date: time.Now(),
}
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DP.DeleteNotification(application, &n)); !success {
return
}
ctx.Status(http.StatusOK)
}

View file

@ -11,7 +11,13 @@ import (
func successOrAbort(ctx *gin.Context, code int, err error) bool {
if err != nil {
ctx.AbortWithError(code, err)
// If we know the error force error code
switch err {
case ErrorMessageNotFound:
ctx.AbortWithError(http.StatusNotFound, err)
default:
ctx.AbortWithError(code, err)
}
}
return err == nil