mirror of
https://github.com/pushbits/server.git
synced 2025-05-21 04:46:36 +02:00
proof of concept
This commit is contained in:
parent
6c69be7d34
commit
5be204dc19
4 changed files with 65 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue