mirror of
https://github.com/pushbits/server.git
synced 2025-06-07 05:02:04 +02:00
Some plugins check for token length. Since PushBits uses longer tokens by default for better security, these plugins are incompatible. With this patch, users can decide if they want an application to have a short token, so that said plugins can talk to PushBits again.
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package dispatcher
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/pushbits/server/internal/model"
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
)
|
|
|
|
func buildRoomTopic(id uint) string {
|
|
return fmt.Sprintf("Application %d", id)
|
|
}
|
|
|
|
// 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)
|
|
|
|
response, err := d.client.CreateRoom(&gomatrix.ReqCreateRoom{
|
|
Invite: []string{user},
|
|
IsDirect: true,
|
|
Name: name,
|
|
Preset: "private_chat",
|
|
Topic: buildRoomTopic(id),
|
|
Visibility: "private",
|
|
})
|
|
|
|
if err != nil {
|
|
log.Print(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.Print(err)
|
|
return err
|
|
}
|
|
|
|
if _, err := d.client.LeaveRoom(a.MatrixID); err != nil {
|
|
log.Print(err)
|
|
return err
|
|
}
|
|
|
|
if _, err := d.client.ForgetRoom(a.MatrixID); err != nil {
|
|
log.Print(err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *Dispatcher) sendRoomEvent(roomID, eventType string, content interface{}) error {
|
|
if _, err := d.client.SendStateEvent(roomID, eventType, "", content); err != nil {
|
|
log.Print(err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateApplication updates a channel for an application.
|
|
func (d *Dispatcher) UpdateApplication(a *model.Application, u *model.User) error {
|
|
log.Printf("Updating application %s (ID %d) with Matrix ID %s.\n", a.Name, a.ID, a.MatrixID)
|
|
|
|
content := map[string]interface{}{
|
|
"name": a.Name,
|
|
}
|
|
|
|
if err := d.sendRoomEvent(a.MatrixID, "m.room.name", content); err != nil {
|
|
return err
|
|
}
|
|
|
|
content = map[string]interface{}{
|
|
"topic": buildRoomTopic(a.ID),
|
|
}
|
|
|
|
if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|