mirror of
https://github.com/pushbits/server.git
synced 2025-04-30 10:46:55 +02:00
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
"github.com/pushbits/server/internal/configuration"
|
|
)
|
|
|
|
var (
|
|
loginType = "m.login.password"
|
|
)
|
|
|
|
// The Database interface for encapsulating database access.
|
|
type Database interface {
|
|
}
|
|
|
|
// Dispatcher holds information for sending notifications to clients.
|
|
type Dispatcher struct {
|
|
db Database
|
|
client *gomatrix.Client
|
|
formatting configuration.Formatting
|
|
}
|
|
|
|
// Create instanciates a dispatcher connection.
|
|
func Create(db Database, homeserver, username, password string, formatting configuration.Formatting) (*Dispatcher, error) {
|
|
log.Println("Setting up dispatcher.")
|
|
|
|
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)
|
|
|
|
return &Dispatcher{client: client, formatting: formatting}, nil
|
|
}
|
|
|
|
// Close closes the dispatcher connection.
|
|
func (d *Dispatcher) Close() {
|
|
log.Printf("Logging out.")
|
|
|
|
d.client.Logout()
|
|
d.client.ClearCredentials()
|
|
|
|
log.Printf("Successfully logged out.")
|
|
}
|