Merge pull request #30 from CubicrootXYZ/delete-messages

Add delete endpoint for messages
This commit is contained in:
Raphael Eikenberg 2021-06-19 10:02:37 +02:00 committed by GitHub
commit 26e9669eb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 275 additions and 7 deletions

View file

@ -142,6 +142,18 @@ curl \
HTML-Content might not be fully rendered in your Matrix-Client - see the corresponding [Matrix specs](https://spec.matrix.org/unstable/client-server-api/#mroommessage-msgtypes). This also holds for Markdown, as it is transfered to the corresponding HTML-syntax.
### Deleting a Message
You can delete a message, this will send a notification in response to the original message informing you that the message is "deleted".
You need the message ID for deleting a message. As it might contain characters not valid in uris we provide an additional `id_url_encoded` field for messages, use that value for deleting a message.
```bash
curl \
--request DELETE \
"https://pushbits.example.com/message/${MESSAGE_ID}?token=$PB_TOKEN"
```
## 👮 Acknowledgments
The idea for this software and most parts of the initial source are heavily inspired by [Gotify](https://gotify.net/).

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,8 +11,14 @@ import (
func successOrAbort(ctx *gin.Context, code int, err error) bool {
if err != nil {
// 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
}

View file

@ -7,11 +7,50 @@ import (
"strings"
"github.com/gomarkdown/markdown"
"github.com/matrix-org/gomatrix"
"github.com/pushbits/server/internal/api"
"github.com/pushbits/server/internal/model"
)
// MessageFormat is a matrix message format
type MessageFormat string
// MsgType is a matrix msgtype
type MsgType string
// Define matrix constants
const (
MessageFormatHTML = MessageFormat("org.matrix.custom.html")
MsgTypeText = MsgType("m.text")
)
// MessageEvent is the content of a matrix message event
type MessageEvent struct {
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
MsgType MsgType `json:"msgtype"`
RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
Format MessageFormat `json:"format"`
NewContent NewContent `json:"m.new_content,omitempty"`
}
// RelatesTo holds information about relations to other message events
type RelatesTo struct {
InReplyTo map[string]string `json:"m.in_reply_to,omitempty"`
RelType string `json:"rel_type,omitempty"`
EventID string `json:"event_id,omitempty"`
}
// NewContent holds information about an updated message event
type NewContent struct {
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
MsgType MsgType `json:"msgtype"`
Format MessageFormat `json:"format"`
}
// 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)
plainMessage := strings.TrimSpace(n.Message)
@ -22,7 +61,42 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
text := fmt.Sprintf("%s\n\n%s", plainTitle, plainMessage)
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 respSendEvent.EventID, err
}
// 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)
var oldFormattedBody string
var oldBody string
// get the message we want to delete
deleteMessage, err := d.getMessage(a, n.ID)
if err != nil {
log.Println(err)
return api.ErrorMessageNotFound
}
oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage)
if err != nil {
return err
}
// update the message with strikethrough
newBody := fmt.Sprintf("<del>%s</del>\n- deleted", oldBody)
newFormattedBody := fmt.Sprintf("<del>%s</del><br>- deleted", oldFormattedBody)
_, err = d.replaceMessage(a, newBody, newFormattedBody, deleteMessage.ID, oldBody, oldFormattedBody)
if err != nil {
return err
}
_, err = d.respondToMessage(a, "This message got deleted", "<i>This message got deleted.</i>", deleteMessage)
return err
}
@ -92,3 +166,111 @@ func (d *Dispatcher) coloredText(color string, text string) string {
return "<font data-mx-color='" + color + "'>" + text + "</font>"
}
// Searches in the messages list for the given id
func (d *Dispatcher) getMessage(a *model.Application, id string) (gomatrix.Event, error) {
start := ""
end := ""
maxPages := 10 // maximum pages to request (10 messages per page)
for i := 0; i < maxPages; i++ {
messages, _ := d.client.Messages(a.MatrixID, start, end, 'b', 10)
for _, event := range messages.Chunk {
if event.ID == id {
return event, nil
}
}
start = messages.End
}
return gomatrix.Event{}, api.ErrorMessageNotFound
}
// Replaces the content of a matrix message
func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedBody string, messageID string, oldBody, oldFormattedBody string) (*gomatrix.RespSendEvent, error) {
newMessage := NewContent{
Body: newBody,
FormattedBody: newFormattedBody,
MsgType: MsgTypeText,
Format: MessageFormatHTML,
}
replaceRelation := RelatesTo{
RelType: "m.replace",
EventID: messageID,
}
replaceEvent := MessageEvent{
Body: oldBody,
FormattedBody: oldFormattedBody,
MsgType: MsgTypeText,
NewContent: newMessage,
RelatesTo: replaceRelation,
Format: MessageFormatHTML,
}
sendEvent, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", replaceEvent)
if err != nil {
log.Println(err)
return nil, err
}
return sendEvent, nil
}
// Sends a notification in response to another matrix message event
func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody string, respondMessage gomatrix.Event) (*gomatrix.RespSendEvent, error) {
oldBody, oldFormattedBody, err := bodiesFromMessage(respondMessage)
if err != nil {
return nil, err
}
// formatting according to https://matrix.org/docs/spec/client_server/latest#fallbacks-and-event-representation
newFormattedBody := fmt.Sprintf("<mx-reply><blockquote><a href='https://matrix.to/#/%s/%s'>In reply to</a> <a href='https://matrix.to/#/%s'>%s</a><br />%s</blockquote>\n</mx-reply>%s", respondMessage.RoomID, respondMessage.ID, respondMessage.Sender, respondMessage.Sender, oldFormattedBody, formattedBody)
newBody := fmt.Sprintf("> <%s>%s\n\n%s", respondMessage.Sender, oldBody, body)
notificationEvent := MessageEvent{
FormattedBody: newFormattedBody,
Body: newBody,
MsgType: MsgTypeText,
Format: MessageFormatHTML,
}
notificationReply := make(map[string]string)
notificationReply["event_id"] = respondMessage.ID
notificationRelation := RelatesTo{
InReplyTo: notificationReply,
}
notificationEvent.RelatesTo = notificationRelation
return d.client.SendMessageEvent(a.MatrixID, "m.room.message", notificationEvent)
}
// Extracts body and formatted body from a matrix message event
func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err error) {
if val, ok := message.Content["body"]; ok {
body, ok := val.(string)
if !ok {
return "", "", api.ErrorMessageNotFound
}
formattedBody = body
} else {
return "", "", api.ErrorMessageNotFound
}
if val, ok := message.Content["formatted_body"]; ok {
body, ok := val.(string)
if !ok {
return "", "", api.ErrorMessageNotFound
}
formattedBody = body
}
return body, formattedBody, nil
}

View file

@ -6,7 +6,8 @@ import (
// Notification holds information like the message, the title, and the priority of a notification.
type Notification struct {
ID uint `json:"id"`
ID string `json:"id"`
UrlEncodedID string `json:"id_url_encoded"`
ApplicationID uint `json:"appid"`
Message string `json:"message" form:"message" query:"message" binding:"required"`
Title string `json:"title" form:"title" query:"title"`
@ -14,3 +15,9 @@ type Notification struct {
Extras map[string]interface{} `json:"extras,omitempty" form:"-" query:"-"`
Date time.Time `json:"date"`
}
// DeleteNotification holds information like the message ID of a deletion notification.
type DeleteNotification struct {
ID string `json:"id" form:"id"`
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.POST("/message", auth.RequireApplicationToken(), notificationHandler.CreateNotification)
r.DELETE("/message/:messageid", api.RequireMessageIDInURI(), auth.RequireApplicationToken(), notificationHandler.DeleteNotification)
userGroup := r.Group("/user")
userGroup.Use(auth.RequireAdmin())