feat: implement centralized channel mapping management

Adds OnChannelMappingDeleted method to BridgeManager for centralized
cleanup of channel mappings across all bridge types. Updates slash
commands to use centralized management and fixes method naming
inconsistencies.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Felipe M 2025-08-01 18:18:10 +02:00
parent 5d143808a3
commit 2e13d96dce
No known key found for this signature in database
GPG key ID: 52E5D65FCF99808A
7 changed files with 480 additions and 43 deletions

View file

@ -7,6 +7,7 @@ import (
"time"
"github.com/mattermost/mattermost-plugin-bridge-xmpp/server/bridge"
mattermostbridge "github.com/mattermost/mattermost-plugin-bridge-xmpp/server/bridge/mattermost"
xmppbridge "github.com/mattermost/mattermost-plugin-bridge-xmpp/server/bridge/xmpp"
"github.com/mattermost/mattermost-plugin-bridge-xmpp/server/command"
"github.com/mattermost/mattermost-plugin-bridge-xmpp/server/config"
@ -43,6 +44,10 @@ type Plugin struct {
// remoteID is the identifier returned by RegisterPluginForSharedChannels
remoteID string
// botUserID is the ID of the bot user created for this plugin
botUserID string
// backgroundJob is the scheduled job that runs periodically to perform background tasks.
backgroundJob *cluster.Job
// configurationLock synchronizes access to the configuration.
@ -71,6 +76,12 @@ func (p *Plugin) OnActivate() error {
cfg := p.getConfiguration()
p.logger.LogDebug("Loaded configuration in OnActivate", "config", cfg)
// Register the plugin for shared channels
if err := p.registerForSharedChannels(); err != nil {
p.logger.LogError("Failed to register for shared channels", "error", err)
return fmt.Errorf("failed to register for shared channels: %w", err)
}
// Initialize bridge manager
p.bridgeManager = bridge.NewManager(p.logger)
@ -118,6 +129,10 @@ func (p *Plugin) OnDeactivate() error {
}
}
if err := p.API.UnregisterPluginForSharedChannels(manifest.Id); err != nil {
p.API.LogError("Failed to unregister plugin for shared channels", "err", err)
}
return nil
}
@ -143,19 +158,63 @@ func (p *Plugin) initXMPPClient() {
func (p *Plugin) initBridges(cfg config.Configuration) error {
// Create and register XMPP bridge
bridge := xmppbridge.NewBridge(
xmppBridge := xmppbridge.NewBridge(
p.logger,
p.API,
p.kvstore,
&cfg,
)
if err := p.bridgeManager.RegisterBridge("xmpp", bridge); err != nil {
if err := p.bridgeManager.RegisterBridge("xmpp", xmppBridge); err != nil {
return fmt.Errorf("failed to register XMPP bridge: %w", err)
}
// Create and register Mattermost bridge
mattermostBridge := mattermostbridge.NewBridge(
p.logger,
p.API,
p.kvstore,
&cfg,
)
if err := p.bridgeManager.RegisterBridge("mattermost", mattermostBridge); err != nil {
return fmt.Errorf("failed to register Mattermost bridge: %w", err)
}
p.logger.LogInfo("Bridge instances created and registered successfully")
return nil
}
func (p *Plugin) registerForSharedChannels() error {
botUserID, err := p.API.EnsureBotUser(&model.Bot{
Username: "mattermost-bridge",
DisplayName: "Mattermost Bridge",
Description: "Mattermost Bridge Bot",
})
if err != nil {
return fmt.Errorf("failed to ensure bot user: %w", err)
}
p.botUserID = botUserID
opts := model.RegisterPluginOpts{
Displayname: "XMPP-Bridge",
PluginID: manifest.Id,
CreatorID: botUserID,
AutoShareDMs: false,
AutoInvited: false,
}
remoteID, appErr := p.API.RegisterPluginForSharedChannels(opts)
if appErr != nil {
return fmt.Errorf("failed to register plugin for shared channels: %w", appErr)
}
// Store the remote ID for use in sync operations
p.remoteID = remoteID
p.logger.LogInfo("Successfully registered plugin for shared channels", "remote_id", remoteID)
return nil
}
// See https://developers.mattermost.com/extend/plugins/server/reference/