From 5be204dc19ae7ea2db74eeb18ec5194e4dadf834 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Wed, 2 Jun 2021 17:46:04 +0200 Subject: [PATCH] proof of concept --- internal/api/notification.go | 9 ++++++ internal/dispatcher/notification.go | 43 +++++++++++++++++++++++++++++ internal/model/notification.go | 11 ++++++++ internal/router/router.go | 2 ++ 4 files changed, 65 insertions(+) diff --git a/internal/api/notification.go b/internal/api/notification.go index bfb8af0..b4a2100 100644 --- a/internal/api/notification.go +++ b/internal/api/notification.go @@ -19,6 +19,7 @@ type NotificationDatabase interface { // The NotificationDispatcher interface for relaying notifications. type NotificationDispatcher interface { SendNotification(a *model.Application, n *model.Notification) error + SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error } // NotificationHandler holds information for processing requests about notifications. @@ -51,3 +52,11 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) { ctx.JSON(http.StatusOK, ¬ification) } + +func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) { + application := authentication.GetApplication(ctx) + + n := model.DeleteNotification{} + + h.DP.SendDeleteNotification(application, &n) +} diff --git a/internal/dispatcher/notification.go b/internal/dispatcher/notification.go index 2a59a6a..0eb15c3 100644 --- a/internal/dispatcher/notification.go +++ b/internal/dispatcher/notification.go @@ -1,6 +1,7 @@ package dispatcher import ( + "encoding/json" "fmt" "html" "log" @@ -10,6 +11,16 @@ import ( "github.com/pushbits/server/internal/model" ) +type ExampleEvent struct { + Body string `json:"body"` + Msgtype string `json:"msgtype"` + RelatesTo RelatesTo `json:"m.relates_to,omitempty"` +} + +type RelatesTo struct { + InReplyTo map[string]string `json:"m.in_reply_to"` +} + // SendNotification sends a notification to the specified user. func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) error { log.Printf("Sending notification to room %s.", a.MatrixID) @@ -27,6 +38,38 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio return err } +func (d *Dispatcher) SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error { + log.Printf("Sending delete notification to room %s", a.MatrixID) + event := ExampleEvent{ + Body: "Testmessage", + Msgtype: "m.text", + } + + irt := make(map[string]string) + + irt["event_id"] = "$uf5OLKPaefHTZhc2lxSIY7If7pLFcNHcMZLbMfS-7qw" + + rt := RelatesTo{ + InReplyTo: irt, + } + + event.RelatesTo = rt + + _, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", event) + + if err != nil { + log.Println(err) + } + + messages, _ := d.client.Messages(a.MatrixID, "", "", 'b', 10) + + js, _ := json.Marshal(messages) + + log.Println(string(js)) + + return nil +} + // HTML-formats the title func (d *Dispatcher) getFormattedTitle(n *model.Notification) string { trimmedTitle := strings.TrimSpace(n.Title) diff --git a/internal/model/notification.go b/internal/model/notification.go index ef8d6ed..0b229cf 100644 --- a/internal/model/notification.go +++ b/internal/model/notification.go @@ -14,3 +14,14 @@ type Notification struct { Extras map[string]interface{} `json:"extras,omitempty" form:"-" query:"-"` Date time.Time `json:"date"` } + +// DeleteNotification holds information like the message, the reply to message id and the priority of a deletion notification. +type DeleteNotification struct { + ID uint `json:"id"` + DeleteID uint `json:"deleteid"` + 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"` +} diff --git a/internal/router/router.go b/internal/router/router.go index 587c256..9dd5e87 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -47,6 +47,8 @@ func Create(debug bool, cm *credentials.Manager, db *database.Database, dp *disp r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification) + r.GET("/test", auth.RequireApplicationToken(), notificationHandler.DeleteNotification) + userGroup := r.Group("/user") userGroup.Use(auth.RequireAdmin()) {