Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 9555d8e8dbc5b988f3cc53b89131f969bcd75017 Author: Qais Patankar <qaisjp@gmail.com> Date: Wed Jun 24 23:25:10 2020 +0000 Add sane RemoteNickFormat default for API (#1157) diff --git a/bridge/api/api.go b/bridge/api/api.go index 9a3f117..38d1a4b 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -107 +107 @@ import (   "github.com/42wim/matterbridge/bridge/config"   "github.com/labstack/echo/v4"   "github.com/labstack/echo/v4/middleware" - "github.com/zfjagann/golang-ring" + ring "github.com/zfjagann/golang-ring"  )    type API struct { @@ -416 +4113 @@ func New(cfg *bridge.Config) bridge.Bridger {   return key == b.GetString("Token"), nil   }))   } + + // Set RemoteNickFormat to a sane default + if !b.IsKeySet("RemoteNickFormat") { + b.Log.Debugln("RemoteNickFormat is unset, defaulting to \"{NICK}\"") + b.Config.Config.Viper().Set(b.GetConfigKey("RemoteNickFormat"), "{NICK}") + } +   e.GET("/api/health", b.handleHealthcheck)   e.GET("/api/messages", b.handleMessages)   e.GET("/api/stream", b.handleStream) diff --git a/bridge/bridge.go b/bridge/bridge.go index eec2bfa..ef71f97 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -868 +8616 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map   return nil  }   +func (b *Bridge) GetConfigKey(key string) string { + return b.Account + "." + key +} + +func (b *Bridge) IsKeySet(key string) bool { + return b.Config.IsKeySet(b.GetConfigKey(key)) || b.Config.IsKeySet("general."+key) +} +  func (b *Bridge) GetBool(key string) bool { - val, ok := b.Config.GetBool(b.Account + "." + key) + val, ok := b.Config.GetBool(b.GetConfigKey(key))   if !ok {   val, _ = b.Config.GetBool("general." + key)   } @@ -957 +1037 @@ func (b *Bridge) GetBool(key string) bool {  }    func (b *Bridge) GetInt(key string) int { - val, ok := b.Config.GetInt(b.Account + "." + key) + val, ok := b.Config.GetInt(b.GetConfigKey(key))   if !ok {   val, _ = b.Config.GetInt("general." + key)   } @@ -1037 +1117 @@ func (b *Bridge) GetInt(key string) int {  }    func (b *Bridge) GetString(key string) string { - val, ok := b.Config.GetString(b.Account + "." + key) + val, ok := b.Config.GetString(b.GetConfigKey(key))   if !ok {   val, _ = b.Config.GetString("general." + key)   } @@ -1117 +1197 @@ func (b *Bridge) GetString(key string) string {  }    func (b *Bridge) GetStringSlice(key string) []string { - val, ok := b.Config.GetStringSlice(b.Account + "." + key) + val, ok := b.Config.GetStringSlice(b.GetConfigKey(key))   if !ok {   val, _ = b.Config.GetStringSlice("general." + key)   } @@ -1197 +1277 @@ func (b *Bridge) GetStringSlice(key string) []string {  }    func (b *Bridge) GetStringSlice2D(key string) [][]string { - val, ok := b.Config.GetStringSlice2D(b.Account + "." + key) + val, ok := b.Config.GetStringSlice2D(b.GetConfigKey(key))   if !ok {   val, _ = b.Config.GetStringSlice2D("general." + key)   } diff --git a/bridge/config/config.go b/bridge/config/config.go index 59d7d4b..d98c942 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -2196 +2197 @@ type BridgeValues struct {  type Config interface {   Viper() *viper.Viper   BridgeValues() *BridgeValues + IsKeySet(key string) bool   GetBool(key string) (bool, bool)   GetInt(key string) (int, bool)   GetString(key string) (string, bool) @@ -3036 +30412 @@ func (c *config) Viper() *viper.Viper {   return c.v  }   +func (c *config) IsKeySet(key string) bool { + c.RLock() + defer c.RUnlock() + return c.v.IsSet(key) +} +  func (c *config) GetBool(key string) (bool, bool) {   c.RLock()   defer c.RUnlock() @@ -3626 +36911 @@ type TestConfig struct {   Overrides map[string]interface{}  }   +func (c *TestConfig) IsKeySet(key string) bool { + _, ok := c.Overrides[key] + return ok || c.Config.IsKeySet(key) +} +  func (c *TestConfig) GetBool(key string) (bool, bool) {   val, ok := c.Overrides[key]   if ok {