mirror of
https://github.com/pushbits/server.git
synced 2025-05-04 04:36:28 +02:00
completely move to mautrix
This commit is contained in:
parent
855e20978e
commit
bb5755cfd3
3 changed files with 26 additions and 45 deletions
|
@ -5,8 +5,10 @@ import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/pushbits/server/internal/model"
|
"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 {
|
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) {
|
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)
|
log.Printf("Registering application %s, notifications will be relayed to user %s.\n", name, user)
|
||||||
|
|
||||||
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
|
resp, err := d.mautrixClient.CreateRoom(&mautrix.ReqCreateRoom{
|
||||||
Invite: []string{user},
|
Visibility: "private",
|
||||||
|
Invite: []mId.UserID{mId.UserID(user)},
|
||||||
IsDirect: true,
|
IsDirect: true,
|
||||||
Name: name,
|
Name: name,
|
||||||
Preset: "private_chat",
|
Preset: "private_chat",
|
||||||
Topic: buildRoomTopic(id),
|
Topic: buildRoomTopic(id),
|
||||||
Visibility: "private",
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return "", 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.
|
// DeregisterApplication deletes a channel for an application.
|
||||||
func (d *Dispatcher) DeregisterApplication(a *model.Application, u *model.User) error {
|
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)
|
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.
|
// 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)
|
log.Print(err)
|
||||||
return 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)
|
log.Print(err)
|
||||||
return 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 {
|
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)
|
log.Print(err)
|
||||||
return 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.
|
// IsOrphan checks if the user is still connected to the channel.
|
||||||
func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error) {
|
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 {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -104,7 +106,7 @@ func (d *Dispatcher) IsOrphan(a *model.Application, u *model.User) (bool, error)
|
||||||
found := false
|
found := false
|
||||||
|
|
||||||
for userID := range resp.Joined {
|
for userID := range resp.Joined {
|
||||||
found = found || (userID == u.MatrixID)
|
found = found || (userID.String() == u.MatrixID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return !found, nil
|
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.
|
// RepairApplication re-invites the user to the channel.
|
||||||
func (d *Dispatcher) RepairApplication(a *model.Application, u *model.User) error {
|
func (d *Dispatcher) RepairApplication(a *model.Application, u *model.User) error {
|
||||||
_, err := d.client.InviteUser(a.MatrixID, &gomatrix.ReqInviteUser{
|
_, err := d.mautrixClient.InviteUser(mId.RoomID(a.MatrixID), &mautrix.ReqInviteUser{
|
||||||
UserID: u.MatrixID,
|
UserID: mId.UserID(u.MatrixID),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -3,7 +3,6 @@ package dispatcher
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/matrix-org/gomatrix"
|
|
||||||
"github.com/pushbits/server/internal/configuration"
|
"github.com/pushbits/server/internal/configuration"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
@ -15,7 +14,6 @@ var (
|
||||||
|
|
||||||
// Dispatcher holds information for sending notifications to clients.
|
// Dispatcher holds information for sending notifications to clients.
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
client *gomatrix.Client // TODO get rid of this client as a dependency
|
|
||||||
mautrixClient *mautrix.Client
|
mautrixClient *mautrix.Client
|
||||||
formatting configuration.Formatting
|
formatting configuration.Formatting
|
||||||
}
|
}
|
||||||
|
@ -24,24 +22,6 @@ type Dispatcher struct {
|
||||||
func Create(homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
|
func Create(homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
|
||||||
log.Println("Setting up dispatcher.")
|
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, "", "")
|
matrixClient, err := mautrix.NewClient(homeserver, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -58,7 +38,7 @@ func Create(homeserver, username, password string, formatting configuration.Form
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Dispatcher{client: client, formatting: formatting, mautrixClient: matrixClient}, nil
|
return &Dispatcher{formatting: formatting, mautrixClient: matrixClient}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the dispatcher connection.
|
// Close closes the dispatcher connection.
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/pushbits/server/internal/pberrors"
|
"github.com/pushbits/server/internal/pberrors"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
"maunium.net/go/mautrix/id"
|
|
||||||
mId "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,
|
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
|
return evt.EventID.String(), err
|
||||||
}
|
}
|
||||||
|
@ -255,7 +254,7 @@ func (d *Dispatcher) respondToMessage(a *model.Application, body, formattedBody
|
||||||
}
|
}
|
||||||
notificationEvent.RelatesTo = notificationRelation
|
notificationEvent.RelatesTo = notificationRelation
|
||||||
|
|
||||||
return d.mautrixClient.SendMessageEvent(id.RoomID(a.MatrixID), event.EventMessage, ¬ificationEvent)
|
return d.mautrixClient.SendMessageEvent(mId.RoomID(a.MatrixID), event.EventMessage, ¬ificationEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extracts body and formatted body from a matrix message event
|
// Extracts body and formatted body from a matrix message event
|
||||||
|
|
Loading…
Add table
Reference in a new issue