completely move to mautrix

This commit is contained in:
cubicroot 2022-02-10 20:20:16 +01:00
parent 855e20978e
commit bb5755cfd3
3 changed files with 26 additions and 45 deletions

View file

@ -5,8 +5,10 @@ import (
"log"
"github.com/pushbits/server/internal/model"
"maunium.net/go/mautrix"
"github.com/matrix-org/gomatrix"
"maunium.net/go/mautrix/event"
mId "maunium.net/go/mautrix/id"
)
func buildRoomTopic(id uint) string {
@ -17,44 +19,44 @@ func buildRoomTopic(id uint) string {
func (d *Dispatcher) RegisterApplication(id uint, name, token, user string) (string, error) {
log.Printf("Registering application %s, notifications will be relayed to user %s.\n", name, user)
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
Invite: []string{user},
resp, err := d.mautrixClient.CreateRoom(&mautrix.ReqCreateRoom{
Visibility: "private",
Invite: []mId.UserID{mId.UserID(user)},
IsDirect: true,
Name: name,
Preset: "private_chat",
Topic: buildRoomTopic(id),
Visibility: "private",
})
if err != nil {
log.Print(err)
return "", err
}
log.Printf("Application %s is now relayed to room with ID %s.\n", name, response.RoomID)
log.Printf("Application %s is now relayed to room with ID %s.\n", name, resp.RoomID.String())
return response.RoomID, err
return resp.RoomID.String(), err
}
// DeregisterApplication deletes a channel for an application.
func (d *Dispatcher) DeregisterApplication(a *model.Application, u *model.User) error {
log.Printf("Deregistering application %s (ID %d) with Matrix ID %s.\n", a.Name, a.ID, a.MatrixID)
kickUser := &gomatrix.ReqKickUser{
Reason: "This application was deleted",
UserID: u.MatrixID,
}
// The user might have left the channel, but we can still try to remove them.
if _, err := d.client.KickUser(a.MatrixID, kickUser); err != nil {
log.Print(err)
}
if _, err := d.client.LeaveRoom(a.MatrixID); err != nil {
if _, err := d.mautrixClient.KickUser(mId.RoomID(a.MatrixID), &mautrix.ReqKickUser{
Reason: "This application was deleted",
UserID: mId.UserID(a.MatrixID),
}); err != nil {
log.Print(err)
return err
}
if _, err := d.client.ForgetRoom(a.MatrixID); err != nil {
if _, err := d.mautrixClient.LeaveRoom(mId.RoomID(a.MatrixID)); err != nil {
log.Print(err)
return err
}
if _, err := d.mautrixClient.ForgetRoom(mId.RoomID(a.MatrixID)); err != nil {
log.Print(err)
return err
}
@ -63,7 +65,7 @@ func (d *Dispatcher) DeregisterApplication(a *model.Application, u *model.User)
}
func (d *Dispatcher) sendRoomEvent(roomID, eventType string, content interface{}) error {
if _, err := d.client.SendStateEvent(roomID, eventType, "", content); err != nil {
if _, err := d.mautrixClient.SendStateEvent(mId.RoomID(roomID), event.NewEventType(eventType), "", content); err != nil {
log.Print(err)
return err
}
@ -96,7 +98,7 @@ func (d *Dispatcher) UpdateApplication(a *model.Application) error {
// IsOrphan checks if the user is still connected to the channel.
func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error) {
resp, err := d.client.JoinedMembers(a.MatrixID)
resp, err := d.mautrixClient.JoinedMembers(mId.RoomID(a.MatrixID))
if err != nil {
return false, err
}
@ -104,7 +106,7 @@ func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error)
found := false
for userID := range resp.Joined {
found = found || (userID == u.MatrixID)
found = found || (userID.String() == u.MatrixID)
}
return !found, nil
@ -112,8 +114,8 @@ func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error)
// RepairApplication re-invites the user to the channel.
func (d *Dispatcher) RepairApplication(a *model.Application, u *model.User) error {
_, err := d.client.InviteUser(a.MatrixID, &gomatrix.ReqInviteUser{
UserID: u.MatrixID,
_, err := d.mautrixClient.InviteUser(mId.RoomID(a.MatrixID), &mautrix.ReqInviteUser{
UserID: mId.UserID(u.MatrixID),
})
if err != nil {
return err

View file

@ -3,7 +3,6 @@ package dispatcher
import (
"log"
"github.com/matrix-org/gomatrix"
"github.com/pushbits/server/internal/configuration"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
@ -15,7 +14,6 @@ var (
// Dispatcher holds information for sending notifications to clients.
type Dispatcher struct {
client *gomatrix.Client // TODO get rid of this client as a dependency
mautrixClient *mautrix.Client
formatting configuration.Formatting
}
@ -24,24 +22,6 @@ type Dispatcher struct {
func Create(homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
log.Println("Setting up dispatcher.")
// TODO remove from here
client, err := gomatrix.NewClient(homeserver, "", "")
if err != nil {
return nil, err
}
response, err := client.Login(&gomatrix.ReqLogin{
Type: loginType,
User: username,
Password: password,
})
if err != nil {
return nil, err
}
client.SetCredentials(response.UserID, response.AccessToken)
// To here
matrixClient, err := mautrix.NewClient(homeserver, "", "")
if err != nil {
return nil, err
@ -58,7 +38,7 @@ func Create(homeserver, username, password string, formatting configuration.Form
return nil, err
}
return &Dispatcher{client: client, formatting: formatting, mautrixClient: matrixClient}, nil
return &Dispatcher{formatting: formatting, mautrixClient: matrixClient}, nil
}
// Close closes the dispatcher connection.

View file

@ -11,7 +11,6 @@ import (
"github.com/pushbits/server/internal/pberrors"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
mId "maunium.net/go/mautrix/id"
)
@ -71,7 +70,7 @@ func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notificatio
Format: MessageFormatHTML,
}
evt, err := d.mautrixClient.SendMessageEvent(id.RoomID(a.MatrixID), event.EventMessage, &messageEvent)
evt, err := d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &messageEvent)
return evt.EventID.String(), err
}
@ -255,7 +254,7 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
}
notificationEvent.RelatesTo = notificationRelation
return d.mautrixClient.SendMessageEvent(id.RoomID(a.MatrixID), event.EventMessage, &notificationEvent)
return d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, &notificationEvent)
}
// Extracts body and formatted body from a matrix message event