proof of concept

This commit is contained in:
Cubicroot 2021-06-02 17:46:04 +02:00
parent 6c69be7d34
commit 5be204dc19
4 changed files with 65 additions and 0 deletions

View file

@ -19,6 +19,7 @@ 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) error
SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error
} }
// NotificationHandler holds information for processing requests about notifications. // NotificationHandler holds information for processing requests about notifications.
@ -51,3 +52,11 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
ctx.JSON(http.StatusOK, &notification) ctx.JSON(http.StatusOK, &notification)
} }
func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
application := authentication.GetApplication(ctx)
n := model.DeleteNotification{}
h.DP.SendDeleteNotification(application, &n)
}

View file

@ -1,6 +1,7 @@
package dispatcher package dispatcher
import ( import (
"encoding/json"
"fmt" "fmt"
"html" "html"
"log" "log"
@ -10,6 +11,16 @@ import (
"github.com/pushbits/server/internal/model" "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. // 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) error {
log.Printf("Sending notification to room %s.", a.MatrixID) 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 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 // HTML-formats the title
func (d *Dispatcher) getFormattedTitle(n *model.Notification) string { func (d *Dispatcher) getFormattedTitle(n *model.Notification) string {
trimmedTitle := strings.TrimSpace(n.Title) trimmedTitle := strings.TrimSpace(n.Title)

View file

@ -14,3 +14,14 @@ type Notification struct {
Extras map[string]interface{} `json:"extras,omitempty" form:"-" query:"-"` Extras map[string]interface{} `json:"extras,omitempty" form:"-" query:"-"`
Date time.Time `json:"date"` 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"`
}

View file

@ -47,6 +47,8 @@ func Create(debug bool, cm *credentials.Manager, db *database.Database, dp *disp
r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification) r.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification)
r.GET("/test", auth.RequireApplicationToken(), notificationHandler.DeleteNotification)
userGroup := r.Group("/user") userGroup := r.Group("/user")
userGroup.Use(auth.RequireAdmin()) userGroup.Use(auth.RequireAdmin())
{ {