Add option to control repair behavior

This commit is contained in:
eikendev 2022-12-04 23:41:22 +01:00
parent 38b615a05d
commit 77765e77a9
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
9 changed files with 49 additions and 22 deletions

View file

@ -77,7 +77,7 @@ func main() {
setupCleanup(db, dp)
err = db.RepairChannels(dp)
err = db.RepairChannels(dp, &c.RepairBehavior)
if err != nil {
log.L.Fatal(err)
}

View file

@ -70,3 +70,9 @@ alertmanager:
annotationtitle: title
# The name of the entry in the alerts annotations or labels that should be used for the message
annotationmessage: message
repairbehavior:
# Reset the room's name to what was initially set by PushBits.
resetroomname: true
# Reset the room's topic to what was initially set by PushBits.
resetroomtopic: true

View file

@ -5,6 +5,7 @@ import (
"net/http"
"github.com/pushbits/server/internal/authentication"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model"
@ -104,7 +105,7 @@ func (h *ApplicationHandler) updateApplication(ctx *gin.Context, a *model.Applic
return err
}
err = h.DP.UpdateApplication(a)
err = h.DP.UpdateApplication(a, &configuration.RepairBehavior{ResetRoomName: true, ResetRoomTopic: true})
if success := SuccessOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err
}

View file

@ -1,6 +1,7 @@
package api
import (
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/model"
)
@ -28,7 +29,7 @@ type Database interface {
type Dispatcher interface {
RegisterApplication(id uint, name, token, user string) (string, error)
DeregisterApplication(a *model.Application, u *model.User) error
UpdateApplication(a *model.Application) error
UpdateApplication(a *model.Application, behavior *configuration.RepairBehavior) error
}
// The CredentialsManager interface for updating credentials.

View file

@ -39,6 +39,12 @@ type Alertmanager struct {
AnnotationMessage string `default:"message"`
}
// RepairBehavior holds information on how repair applications.
type RepairBehavior struct {
ResetRoomName bool `default:"true"`
ResetRoomTopic bool `default:"true"`
}
// Configuration holds values that can be configured by the user.
type Configuration struct {
Debug bool `default:"false"`
@ -60,9 +66,10 @@ type Configuration struct {
Security struct {
CheckHIBP bool `default:"false"`
}
Crypto CryptoConfig
Formatting Formatting
Alertmanager Alertmanager
Crypto CryptoConfig
Formatting Formatting
Alertmanager Alertmanager
RepairBehavior RepairBehavior
}
func configFiles() []string {

View file

@ -8,6 +8,7 @@ import (
"time"
"github.com/pushbits/server/internal/authentication/credentials"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model"
@ -111,7 +112,7 @@ func (d *Database) Populate(name, password, matrixID string) error {
}
// RepairChannels resets channels that have been modified by a user.
func (d *Database) RepairChannels(dp Dispatcher) error {
func (d *Database) RepairChannels(dp Dispatcher, behavior *configuration.RepairBehavior) error {
log.L.Print("Repairing application channels.")
users, err := d.GetUsers()
@ -130,7 +131,7 @@ func (d *Database) RepairChannels(dp Dispatcher) error {
for _, application := range applications {
application := application // See https://stackoverflow.com/a/68247837
if err := dp.UpdateApplication(&application); err != nil {
if err := dp.UpdateApplication(&application, behavior); err != nil {
return err
}

View file

@ -1,13 +1,14 @@
package database
import (
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/model"
)
// The Dispatcher interface for constructing and destructing channels.
type Dispatcher interface {
DeregisterApplication(a *model.Application, u *model.User) error
UpdateApplication(a *model.Application) error
UpdateApplication(a *model.Application, behavior *configuration.RepairBehavior) error
IsOrphan(a *model.Application, u *model.User) (bool, error)
RepairApplication(a *model.Application, u *model.User) error
}

View file

@ -3,6 +3,7 @@ package dispatcher
import (
"fmt"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model"
@ -74,23 +75,31 @@ func (d *Dispatcher) sendRoomEvent(roomID, eventType string, content interface{}
}
// UpdateApplication updates a channel for an application.
func (d *Dispatcher) UpdateApplication(a *model.Application) error {
func (d *Dispatcher) UpdateApplication(a *model.Application, behavior *configuration.RepairBehavior) error {
log.L.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 behavior.ResetRoomName {
content := map[string]interface{}{
"name": a.Name,
}
if err := d.sendRoomEvent(a.MatrixID, "m.room.name", content); err != nil {
return err
}
} else {
log.L.Debugf("Not reseting room name as per configuration.\n")
}
if err := d.sendRoomEvent(a.MatrixID, "m.room.name", content); err != nil {
return err
}
if behavior.ResetRoomTopic {
content := map[string]interface{}{
"topic": buildRoomTopic(a.ID),
}
content = map[string]interface{}{
"topic": buildRoomTopic(a.ID),
}
if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil {
return err
if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil {
return err
}
} else {
log.L.Debugf("Not reseting room topic as per configuration.\n")
}
return nil

View file

@ -3,6 +3,7 @@ package mockups
import (
"fmt"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/model"
)
@ -17,7 +18,7 @@ func (d *MockDispatcher) DeregisterApplication(a *model.Application, u *model.Us
return nil
}
func (d *MockDispatcher) UpdateApplication(a *model.Application) error {
func (d *MockDispatcher) UpdateApplication(a *model.Application, behavior *configuration.RepairBehavior) error {
return nil
}