add endpoint for delete

This commit is contained in:
Cubicroot 2021-06-06 19:30:25 +02:00
parent 3834e90527
commit b392ea1b44
6 changed files with 65 additions and 34 deletions

View file

@ -20,6 +20,17 @@ func getID(ctx *gin.Context) (uint, error) {
return id, nil 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) { func getApplication(ctx *gin.Context, db Database) (*model.Application, error) {
id, err := getID(ctx) id, err := getID(ctx)
if err != nil { if err != nil {

View file

@ -8,6 +8,10 @@ type idInURI struct {
ID uint `uri:"id" binding:"required"` 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. // RequireIDInURI returns a Gin middleware which requires an ID to be supplied in the URI of the request.
func RequireIDInURI() gin.HandlerFunc { func RequireIDInURI() gin.HandlerFunc {
return func(ctx *gin.Context) { return func(ctx *gin.Context) {
@ -20,3 +24,16 @@ func RequireIDInURI() gin.HandlerFunc {
ctx.Set("id", requestModel.ID) 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

@ -18,8 +18,8 @@ type NotificationDatabase interface {
// The NotificationDispatcher interface for relaying notifications. // The NotificationDispatcher interface for relaying notifications.
type NotificationDispatcher interface { type NotificationDispatcher interface {
SendNotification(a *model.Application, n *model.Notification) error SendNotification(a *model.Application, n *model.Notification) (id string, err error)
SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error DeleteNotification(a *model.Application, n *model.DeleteNotification) error
} }
// NotificationHandler holds information for processing requests about notifications. // NotificationHandler holds information for processing requests about notifications.
@ -39,24 +39,36 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
application := authentication.GetApplication(ctx) application := authentication.GetApplication(ctx)
log.Printf("Sending notification for application %s.", application.Name) log.Printf("Sending notification for application %s.", application.Name)
notification.ID = 0
notification.ApplicationID = application.ID notification.ApplicationID = application.ID
if strings.TrimSpace(notification.Title) == "" { if strings.TrimSpace(notification.Title) == "" {
notification.Title = application.Name notification.Title = application.Name
} }
notification.Date = time.Now() 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 return
} }
notification.ID = messageID
ctx.JSON(http.StatusOK, &notification) 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) { func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
application := authentication.GetApplication(ctx) application := authentication.GetApplication(ctx)
id, err := getMessageID(ctx)
n := model.DeleteNotification{} if success := successOrAbort(ctx, http.StatusUnprocessableEntity, err); !success {
return
}
h.DP.SendDeleteNotification(application, &n) n := model.DeleteNotification{
ID: id,
Date: time.Now(),
}
h.DP.DeleteNotification(application, &n)
} }

View file

@ -1,7 +1,6 @@
package dispatcher package dispatcher
import ( import (
"encoding/json"
"fmt" "fmt"
"html" "html"
"log" "log"
@ -11,7 +10,7 @@ import (
"github.com/pushbits/server/internal/model" "github.com/pushbits/server/internal/model"
) )
type ExampleEvent struct { type ReplyEvent struct {
Body string `json:"body"` Body string `json:"body"`
Msgtype string `json:"msgtype"` Msgtype string `json:"msgtype"`
RelatesTo RelatesTo `json:"m.relates_to,omitempty"` RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
@ -22,7 +21,7 @@ type RelatesTo struct {
} }
// SendNotification sends a notification to the specified user. // SendNotification sends a notification to the specified user.
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) error { func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) (id string, err error) {
log.Printf("Sending notification to room %s.", a.MatrixID) log.Printf("Sending notification to room %s.", a.MatrixID)
plainMessage := strings.TrimSpace(n.Message) plainMessage := strings.TrimSpace(n.Message)
@ -33,41 +32,37 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage) text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
formattedText := fmt.Sprintf("%s %s", title, message) formattedText := fmt.Sprintf("%s %s", title, message)
_, err := d.client.SendFormattedText(a.MatrixID, text, formattedText) respSendEvent, err := d.client.SendFormattedText(a.MatrixID, text, formattedText)
return err return respSendEvent.EventID, err
} }
func (d *Dispatcher) SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error { // DeleteNotification sends a notification to the specified user that another notificaion is deleted
func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNotification) error {
log.Printf("Sending delete notification to room %s", a.MatrixID) log.Printf("Sending delete notification to room %s", a.MatrixID)
event := ExampleEvent{ event := ReplyEvent{
Body: "Testmessage", Body: "<i>This message got deleted.</i>",
Msgtype: "m.text", Msgtype: "m.text",
} }
irt := make(map[string]string) irt := make(map[string]string)
irt["event_id"] = n.ID
irt["event_id"] = "$uf5OLKPaefHTZhc2lxSIY7If7pLFcNHcMZLbMfS-7qw"
rt := RelatesTo{ rt := RelatesTo{
InReplyTo: irt, InReplyTo: irt,
} }
event.RelatesTo = rt event.RelatesTo = rt
_, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", event) _, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", event)
if err != nil { return err
log.Println(err)
}
messages, _ := d.client.Messages(a.MatrixID, "", "", 'b', 10) /*
messages, _ := d.client.Messages(a.MatrixID, "", "", 'b', 10)
js, _ := json.Marshal(messages) js, _ := json.Marshal(messages)
log.Println(string(js)) log.Println(string(js))
*/
return nil
} }
// HTML-formats the title // HTML-formats the title

View file

@ -6,7 +6,7 @@ import (
// Notification holds information like the message, the title, and the priority of a notification. // Notification holds information like the message, the title, and the priority of a notification.
type Notification struct { type Notification struct {
ID uint `json:"id"` ID string `json:"id"`
ApplicationID uint `json:"appid"` ApplicationID uint `json:"appid"`
Message string `json:"message" form:"message" query:"message" binding:"required"` Message string `json:"message" form:"message" query:"message" binding:"required"`
Title string `json:"title" form:"title" query:"title"` Title string `json:"title" form:"title" query:"title"`
@ -17,11 +17,6 @@ type Notification struct {
// DeleteNotification holds information like the message, the reply to message id and the priority of a deletion notification. // DeleteNotification holds information like the message, the reply to message id and the priority of a deletion notification.
type DeleteNotification struct { type DeleteNotification struct {
ID uint `json:"id"` ID string `json:"id" form:"id"`
DeleteID uint `json:"deleteid"` Date time.Time `json:"date"`
ApplicationID uint `json:"appid"`
Message string `json:"message" form:"message" query:"message" binding:"required"`
Priority int `json:"priority" form:"priority" query:"priority"`
Extras map[string]interface{} `json:"extras,omitempty" form:"-" query:"-"`
Date time.Time `json:"date"`
} }

View file

@ -46,6 +46,7 @@ func Create(debug bool, cm *credentials.Manager, db *database.Database, dp *disp
r.GET("/health", healthHandler.Health) r.GET("/health", healthHandler.Health)
r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification) r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification)
r.DELETE("/message/:messageid", api.RequireIDInURI(), auth.RequireApplicationToken(), notificationHandler.DeleteNotification)
r.GET("/test", auth.RequireApplicationToken(), notificationHandler.DeleteNotification) r.GET("/test", auth.RequireApplicationToken(), notificationHandler.DeleteNotification)