pushbits/internal/dispatcher/dispatcher.go
Cubicroot 2ff9f5972a adopt to review
change formatting options to separate struct and clean up
2021-05-02 15:37:54 +02:00

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.")
}