Move all message sending to mautrix

This commit is contained in:
cubicroot 2022-02-09 20:38:24 +01:00
parent e48cbae59b
commit 855e20978e
2 changed files with 17 additions and 16 deletions

View file

@ -65,8 +65,8 @@ func Create(homeserver, username, password string, formatting configuration.Form
func (d *Dispatcher) Close() { func (d *Dispatcher) Close() {
log.Printf("Logging out.") log.Printf("Logging out.")
d.client.Logout() d.mautrixClient.Logout()
d.client.ClearCredentials() d.mautrixClient.ClearCredentials()
log.Printf("Successfully logged out.") log.Printf("Successfully logged out.")
} }

View file

@ -7,11 +7,12 @@ import (
"strings" "strings"
"github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown"
"github.com/matrix-org/gomatrix"
"github.com/pushbits/server/internal/model" "github.com/pushbits/server/internal/model"
"github.com/pushbits/server/internal/pberrors" "github.com/pushbits/server/internal/pberrors"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id" "maunium.net/go/mautrix/id"
mId "maunium.net/go/mautrix/id"
) )
// MessageFormat is a matrix message format // MessageFormat is a matrix message format
@ -99,7 +100,7 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot
newBody := fmt.Sprintf("<del>%s</del>\n- deleted", oldBody) newBody := fmt.Sprintf("<del>%s</del>\n- deleted", oldBody)
newFormattedBody := fmt.Sprintf("<del>%s</del><br>- deleted", oldFormattedBody) newFormattedBody := fmt.Sprintf("<del>%s</del><br>- deleted", oldFormattedBody)
_, err = d.replaceMessage(a, newBody, newFormattedBody, deleteMessage.ID, oldBody, oldFormattedBody) _, err = d.replaceMessage(a, newBody, newFormattedBody, deleteMessage.ID.String(), oldBody, oldFormattedBody)
if err != nil { if err != nil {
return err return err
@ -177,25 +178,25 @@ func (d *Dispatcher) coloredText(color string, text string) string {
} }
// Searches in the messages list for the given id // Searches in the messages list for the given id
func (d *Dispatcher) getMessage(a *model.Application, id string) (gomatrix.Event, error) { func (d *Dispatcher) getMessage(a *model.Application, id string) (*event.Event, error) {
start := "" start := ""
end := "" end := ""
maxPages := 10 // maximum pages to request (10 messages per page) maxPages := 10 // maximum pages to request (10 messages per page)
for i := 0; i < maxPages; i++ { for i := 0; i < maxPages; i++ {
messages, _ := d.client.Messages(a.MatrixID, start, end, 'b', 10) messages, _ := d.mautrixClient.Messages(mId.RoomID(a.MatrixID), start, end, 'b', nil, 10)
for _, event := range messages.Chunk { for _, event := range messages.Chunk {
if event.ID == id { if event.ID.String() == id {
return event, nil return event, nil
} }
} }
start = messages.End start = messages.End
} }
return gomatrix.Event{}, pberrors.ErrorMessageNotFound return &event.Event{}, pberrors.ErrorMessageNotFound
} }
// Replaces the content of a matrix message // 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) { func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedBody string, messageID string, oldBody, oldFormattedBody string) (*mautrix.RespSendEvent, error) {
newMessage := NewContent{ newMessage := NewContent{
Body: newBody, Body: newBody,
FormattedBody: newFormattedBody, FormattedBody: newFormattedBody,
@ -217,7 +218,7 @@ func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedB
Format: MessageFormatHTML, Format: MessageFormatHTML,
} }
sendEvent, err := d.client.SendMessageEvent(a.MatrixID, "m.room.message", replaceEvent) sendEvent, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &replaceEvent)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -228,7 +229,7 @@ func (d *Dispatcher) replaceMessage(a *model.Application, newBody, newFormattedB
} }
// Sends a notification in response to another matrix message event // 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) { func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody string, respondMessage *event.Event) (*mautrix.RespSendEvent, error) {
oldBody, oldFormattedBody, err := bodiesFromMessage(respondMessage) oldBody, oldFormattedBody, err := bodiesFromMessage(respondMessage)
if err != nil { if err != nil {
@ -247,19 +248,19 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
} }
notificationReply := make(map[string]string) notificationReply := make(map[string]string)
notificationReply["event_id"] = respondMessage.ID notificationReply["event_id"] = respondMessage.ID.String()
notificationRelation := RelatesTo{ notificationRelation := RelatesTo{
InReplyTo: notificationReply, InReplyTo: notificationReply,
} }
notificationEvent.RelatesTo = notificationRelation notificationEvent.RelatesTo = notificationRelation
return d.client.SendMessageEvent(a.MatrixID, "m.room.message", notificationEvent) return d.mautrixClient.SendMessageEvent(id.RoomID(a.MatrixID), event.EventMessage, &notificationEvent)
} }
// Extracts body and formatted body from a matrix message event // Extracts body and formatted body from a matrix message event
func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err error) { func bodiesFromMessage(message *event.Event) (body, formattedBody string, err error) {
if val, ok := message.Content["body"]; ok { if val, ok := message.Content.Raw["body"]; ok {
body, ok := val.(string) body, ok := val.(string)
if !ok { if !ok {
@ -272,7 +273,7 @@ func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err
return "", "", pberrors.ErrorMessageNotFound return "", "", pberrors.ErrorMessageNotFound
} }
if val, ok := message.Content["formatted_body"]; ok { if val, ok := message.Content.Raw["formatted_body"]; ok {
body, ok := val.(string) body, ok := val.(string)
if !ok { if !ok {
return "", "", pberrors.ErrorMessageNotFound return "", "", pberrors.ErrorMessageNotFound