feat: refactor bridge user detection and fix linting issues

- Extract bridge user detection logic into separate isBridgeUserMessage() function
- Fix gocritic ifElseChain issues by converting if-else to switch statements
- Fix gofmt formatting issues in client.go
- Fix revive naming issues by renaming XMPPUser to User and XMPPUserManager to UserManager
- Improve code organization and maintainability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Felipe M 2025-08-12 12:01:42 +02:00
parent 6e45352f3e
commit b80e8ebd8f
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A
7 changed files with 913 additions and 81 deletions

View file

@ -5,8 +5,6 @@ import (
"strings"
)
const DefaultXMPPUsernamePrefix = "xmpp"
// Configuration captures the plugin's external configuration as exposed in the Mattermost server
// configuration, as well as values computed from the configuration. Any public fields will be
// deserialized from the Mattermost server configuration in OnConfigurationChange.
@ -23,9 +21,14 @@ type Configuration struct {
XMPPUsername string `json:"XMPPUsername"`
XMPPPassword string `json:"XMPPPassword"`
EnableSync bool `json:"EnableSync"`
XMPPUsernamePrefix string `json:"XMPPUsernamePrefix"`
XMPPResource string `json:"XMPPResource"`
XMPPInsecureSkipVerify bool `json:"XMPPInsecureSkipVerify"`
// Ghost User Settings (XEP-0077 In-Band Registration)
EnableXMPPGhostUsers bool `json:"EnableXMPPGhostUsers"`
XMPPGhostUserPrefix string `json:"XMPPGhostUserPrefix"`
XMPPGhostUserDomain string `json:"XMPPGhostUserDomain"`
XMPPGhostUserCleanup bool `json:"XMPPGhostUserCleanup"`
}
// Equals compares two configuration structs
@ -41,9 +44,12 @@ func (c *Configuration) Equals(other *Configuration) bool {
c.XMPPUsername == other.XMPPUsername &&
c.XMPPPassword == other.XMPPPassword &&
c.EnableSync == other.EnableSync &&
c.XMPPUsernamePrefix == other.XMPPUsernamePrefix &&
c.XMPPResource == other.XMPPResource &&
c.XMPPInsecureSkipVerify == other.XMPPInsecureSkipVerify
c.XMPPInsecureSkipVerify == other.XMPPInsecureSkipVerify &&
c.EnableXMPPGhostUsers == other.EnableXMPPGhostUsers &&
c.XMPPGhostUserPrefix == other.XMPPGhostUserPrefix &&
c.XMPPGhostUserDomain == other.XMPPGhostUserDomain &&
c.XMPPGhostUserCleanup == other.XMPPGhostUserCleanup
}
// Clone shallow copies the configuration. Your implementation may require a deep copy if
@ -53,14 +59,6 @@ func (c *Configuration) Clone() *Configuration {
return &clone
}
// GetXMPPUsernamePrefix returns the configured username prefix, or the default if not set
func (c *Configuration) GetXMPPUsernamePrefix() string {
if c.XMPPUsernamePrefix == "" {
return DefaultXMPPUsernamePrefix
}
return c.XMPPUsernamePrefix
}
// GetXMPPResource returns the configured XMPP resource, or a default if not set
func (c *Configuration) GetXMPPResource() string {
if c.XMPPResource == "" {
@ -69,6 +67,41 @@ func (c *Configuration) GetXMPPResource() string {
return c.XMPPResource
}
// GetXMPPGhostUserDomain returns the configured ghost user domain, or derives it from server URL
func (c *Configuration) GetXMPPGhostUserDomain() string {
if c.XMPPGhostUserDomain != "" {
return c.XMPPGhostUserDomain
}
// Extract domain from bridge username as fallback
if c.XMPPUsername != "" && strings.Contains(c.XMPPUsername, "@") {
parts := strings.Split(c.XMPPUsername, "@")
if len(parts) > 1 {
return parts[1]
}
}
// Last resort: try to extract from server URL
if c.XMPPServerURL != "" {
parts := strings.Split(c.XMPPServerURL, ":")
if len(parts) > 0 {
return parts[0]
}
}
return "localhost"
}
// IsGhostUserEnabled returns true if ghost users should be created
func (c *Configuration) IsGhostUserEnabled() bool {
return c.EnableXMPPGhostUsers && c.EnableSync
}
// IsGhostUserCleanupEnabled returns true if ghost users should be cleaned up on removal
func (c *Configuration) IsGhostUserCleanupEnabled() bool {
return c.XMPPGhostUserCleanup
}
// IsValid validates the configuration and returns an error if invalid
func (c *Configuration) IsValid() error {
if c.EnableSync {
@ -87,10 +120,16 @@ func (c *Configuration) IsValid() error {
return fmt.Errorf("XMPP Server URL must include port (e.g., server.com:5222)")
}
// Validate username prefix doesn't contain invalid characters
prefix := c.GetXMPPUsernamePrefix()
if strings.ContainsAny(prefix, ":@/\\") {
return fmt.Errorf("XMPP Username Prefix cannot contain special characters (:, @, /, \\)")
// Validate ghost user configuration if enabled
if c.EnableXMPPGhostUsers {
if c.XMPPGhostUserPrefix == "" {
return fmt.Errorf("XMPP Ghost User Prefix is required when ghost users are enabled")
}
// Validate ghost user prefix doesn't contain invalid characters
if strings.ContainsAny(c.XMPPGhostUserPrefix, ":@/\\") {
return fmt.Errorf("XMPP Ghost User Prefix cannot contain special characters (:, @, /, \\)")
}
}
}