Restructure project layout

This commit is contained in:
eikendev 2021-01-16 16:56:49 +01:00
parent a49db216d5
commit 9a4a096526
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
32 changed files with 35 additions and 35 deletions

View file

@ -0,0 +1,62 @@
package dispatcher
import (
"fmt"
"log"
"github.com/pushbits/server/internal/model"
"github.com/matrix-org/gomatrix"
)
// RegisterApplication creates a channel for an application.
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)
topic := fmt.Sprintf("Application %d, Token %s", id, token)
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
Invite: []string{user},
IsDirect: true,
Name: name,
Preset: "private_chat",
Topic: topic,
Visibility: "private",
})
if err != nil {
log.Fatal(err)
return "", err
}
log.Printf("Application %s is now relayed to room with ID %s.\n", name, response.RoomID)
return response.RoomID, 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,
}
if _, err := d.client.KickUser(a.MatrixID, kickUser); err != nil {
log.Fatal(err)
return err
}
if _, err := d.client.LeaveRoom(a.MatrixID); err != nil {
log.Fatal(err)
return err
}
if _, err := d.client.ForgetRoom(a.MatrixID); err != nil {
log.Fatal(err)
return err
}
return nil
}

View file

@ -0,0 +1,52 @@
package dispatcher
import (
"log"
"github.com/matrix-org/gomatrix"
)
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
}
// Create instanciates a dispatcher connection.
func Create(db Database, homeserver, username, password string) (*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}, nil
}
// Close closes the dispatcher connection.
func (d *Dispatcher) Close() {
log.Printf("Logging out.\n")
d.client.Logout()
d.client.ClearCredentials()
}

View file

@ -0,0 +1,19 @@
package dispatcher
import (
"fmt"
"log"
"github.com/pushbits/server/internal/model"
)
// SendNotification sends a notification to the specified user.
func (d *Dispatcher) SendNotification(a *model.Application, n *model.Notification) error {
log.Printf("Sending notification to room %s.\n", a.MatrixID)
text := fmt.Sprintf("%s\n\n%s", n.Title, n.Message)
_, err := d.client.SendText(a.MatrixID, text)
return err
}