Merge pull request #59 from pushbits/repairbehavior

Add option to control repair behavior
This commit is contained in:
Raphael Eikenberg 2022-12-05 21:09:23 +01:00 committed by GitHub
commit 7087cc6df9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 22 deletions

View file

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

View file

@ -70,3 +70,9 @@ alertmanager:
annotationtitle: title annotationtitle: title
# The name of the entry in the alerts annotations or labels that should be used for the message # The name of the entry in the alerts annotations or labels that should be used for the message
annotationmessage: 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" "net/http"
"github.com/pushbits/server/internal/authentication" "github.com/pushbits/server/internal/authentication"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log" "github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model" "github.com/pushbits/server/internal/model"
@ -104,7 +105,7 @@ func (h *ApplicationHandler) updateApplication(ctx *gin.Context, a *model.Applic
return err 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 { if success := SuccessOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err return err
} }

View file

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

View file

@ -39,6 +39,12 @@ type Alertmanager struct {
AnnotationMessage string `default:"message"` 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. // Configuration holds values that can be configured by the user.
type Configuration struct { type Configuration struct {
Debug bool `default:"false"` Debug bool `default:"false"`
@ -60,9 +66,10 @@ type Configuration struct {
Security struct { Security struct {
CheckHIBP bool `default:"false"` CheckHIBP bool `default:"false"`
} }
Crypto CryptoConfig Crypto CryptoConfig
Formatting Formatting Formatting Formatting
Alertmanager Alertmanager Alertmanager Alertmanager
RepairBehavior RepairBehavior
} }
func configFiles() []string { func configFiles() []string {

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/pushbits/server/internal/authentication/credentials" "github.com/pushbits/server/internal/authentication/credentials"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log" "github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model" "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. // 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.") log.L.Print("Repairing application channels.")
users, err := d.GetUsers() users, err := d.GetUsers()
@ -130,7 +131,7 @@ func (d *Database) RepairChannels(dp Dispatcher) error {
for _, application := range applications { for _, application := range applications {
application := application // See https://stackoverflow.com/a/68247837 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 return err
} }

View file

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

View file

@ -3,6 +3,7 @@ package dispatcher
import ( import (
"fmt" "fmt"
"github.com/pushbits/server/internal/configuration"
"github.com/pushbits/server/internal/log" "github.com/pushbits/server/internal/log"
"github.com/pushbits/server/internal/model" "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. // 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) log.L.Printf("Updating application %s (ID %d) with Matrix ID %s.\n", a.Name, a.ID, a.MatrixID)
content := map[string]interface{}{ if behavior.ResetRoomName {
"name": a.Name, 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 { if behavior.ResetRoomTopic {
return err content := map[string]interface{}{
} "topic": buildRoomTopic(a.ID),
}
content = map[string]interface{}{ if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil {
"topic": buildRoomTopic(a.ID), return err
} }
} else {
if err := d.sendRoomEvent(a.MatrixID, "m.room.topic", content); err != nil { log.L.Debugf("Not reseting room topic as per configuration.\n")
return err
} }
return nil return nil

View file

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