mirror of
https://github.com/pushbits/server.git
synced 2025-06-01 18:22:02 +02:00
Initialize repository
This commit is contained in:
commit
1d758fcfd0
28 changed files with 1107 additions and 0 deletions
29
dispatcher/application.go
Normal file
29
dispatcher/application.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
// RegisterApplication creates a new channel for the application.
|
||||
func (d *Dispatcher) RegisterApplication(name, 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{
|
||||
Visibility: "private",
|
||||
Name: name,
|
||||
Invite: []string{user},
|
||||
Preset: "private_chat",
|
||||
IsDirect: true,
|
||||
})
|
||||
|
||||
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
|
||||
}
|
58
dispatcher/dispatcher.go
Normal file
58
dispatcher/dispatcher.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/eikendev/pushbits/model"
|
||||
|
||||
"github.com/matrix-org/gomatrix"
|
||||
)
|
||||
|
||||
var (
|
||||
loginType = "m.login.password"
|
||||
)
|
||||
|
||||
// The Database interface for encapsulating database access.
|
||||
type Database interface {
|
||||
UpdateApplication(application *model.Application) error
|
||||
}
|
||||
|
||||
// Dispatcher holds information for sending notifications to clients.
|
||||
type Dispatcher struct {
|
||||
db Database
|
||||
client *gomatrix.Client
|
||||
}
|
||||
|
||||
// Create instanciates a dispatcher connection.
|
||||
// TODO: Call JoinedRooms() to validate room mappings on startup.
|
||||
// TODO: ForgetRoom() for unused rooms.
|
||||
// TODO: InviteUser() if the user is no longer in the room.
|
||||
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()
|
||||
}
|
19
dispatcher/notification.go
Normal file
19
dispatcher/notification.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package dispatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/eikendev/pushbits/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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue