diff --git a/cmd/pushbits/main.go b/cmd/pushbits/main.go index d866405..db6d4bb 100644 --- a/cmd/pushbits/main.go +++ b/cmd/pushbits/main.go @@ -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) } diff --git a/config.example.yml b/config.example.yml index 813f08f..21235ed 100644 --- a/config.example.yml +++ b/config.example.yml @@ -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 diff --git a/internal/api/application.go b/internal/api/application.go index f10db59..dc117ee 100644 --- a/internal/api/application.go +++ b/internal/api/application.go @@ -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 } diff --git a/internal/api/interfaces.go b/internal/api/interfaces.go index 4bc72b1..0de2a32 100644 --- a/internal/api/interfaces.go +++ b/internal/api/interfaces.go @@ -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. diff --git a/internal/configuration/configuration.go b/internal/configuration/configuration.go index 5d50f38..f57e341 100644 --- a/internal/configuration/configuration.go +++ b/internal/configuration/configuration.go @@ -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 { diff --git a/internal/database/database.go b/internal/database/database.go index f1482fb..43da6d7 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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 } diff --git a/internal/database/interfaces.go b/internal/database/interfaces.go index 7d4a70c..07feab2 100644 --- a/internal/database/interfaces.go +++ b/internal/database/interfaces.go @@ -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 } diff --git a/internal/dispatcher/application.go b/internal/dispatcher/application.go index 4d17ee3..cceb471 100644 --- a/internal/dispatcher/application.go +++ b/internal/dispatcher/application.go @@ -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 diff --git a/tests/mockups/dispatcher.go b/tests/mockups/dispatcher.go index 4441414..c21af04 100644 --- a/tests/mockups/dispatcher.go +++ b/tests/mockups/dispatcher.go @@ -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 }