feat: implement sync and sync-reset commands for shared channel management
- Add GetRemoteID() method to Bridge interface for cursor operations - Update bridge constructors to accept and store remoteID parameter - Implement executeSyncCommand handler for forcing shared channel sync - Implement executeSyncResetCommand handler for resetting sync cursor - Add command registration for 'sync' and 'sync-reset' subcommands - Enhance command handler with direct plugin API access for shared channel operations - Add comprehensive validation and error handling for unmapped channels - Support both SyncSharedChannel and UpdateSharedChannelCursor API methods 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
5d81ca2154
commit
d21dcd2dd1
5 changed files with 139 additions and 6 deletions
|
@ -26,6 +26,7 @@ type mattermostBridge struct {
|
|||
kvstore kvstore.KVStore
|
||||
userManager pluginModel.BridgeUserManager
|
||||
botUserID string // Bot user ID for posting messages
|
||||
remoteID string // Remote ID for shared channels
|
||||
|
||||
// Message handling
|
||||
messageHandler *mattermostMessageHandler
|
||||
|
@ -47,13 +48,14 @@ type mattermostBridge struct {
|
|||
}
|
||||
|
||||
// NewBridge creates a new Mattermost bridge
|
||||
func NewBridge(log logger.Logger, api plugin.API, kvstore kvstore.KVStore, cfg *config.Configuration, botUserID string) pluginModel.Bridge {
|
||||
func NewBridge(log logger.Logger, api plugin.API, kvstore kvstore.KVStore, cfg *config.Configuration, botUserID, remoteID string) pluginModel.Bridge {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
b := &mattermostBridge{
|
||||
logger: log,
|
||||
api: api,
|
||||
kvstore: kvstore,
|
||||
botUserID: botUserID,
|
||||
remoteID: remoteID,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
channelMappings: make(map[string]string),
|
||||
|
@ -395,3 +397,8 @@ func (b *mattermostBridge) GetMessageHandler() pluginModel.MessageHandler {
|
|||
func (b *mattermostBridge) GetUserResolver() pluginModel.UserResolver {
|
||||
return b.userResolver
|
||||
}
|
||||
|
||||
// GetRemoteID returns the remote ID used for shared channels registration
|
||||
func (b *mattermostBridge) GetRemoteID() string {
|
||||
return b.remoteID
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ type xmppBridge struct {
|
|||
kvstore kvstore.KVStore
|
||||
bridgeClient *xmppClient.Client // Main bridge XMPP client connection
|
||||
userManager pluginModel.BridgeUserManager
|
||||
remoteID string // Remote ID for shared channels
|
||||
|
||||
// Message handling
|
||||
messageHandler *xmppMessageHandler
|
||||
|
@ -51,7 +52,7 @@ type xmppBridge struct {
|
|||
}
|
||||
|
||||
// NewBridge creates a new XMPP bridge
|
||||
func NewBridge(log logger.Logger, api plugin.API, kvstore kvstore.KVStore, cfg *config.Configuration) pluginModel.Bridge {
|
||||
func NewBridge(log logger.Logger, api plugin.API, kvstore kvstore.KVStore, cfg *config.Configuration, remoteID string) pluginModel.Bridge {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
b := &xmppBridge{
|
||||
logger: log,
|
||||
|
@ -63,6 +64,7 @@ func NewBridge(log logger.Logger, api plugin.API, kvstore kvstore.KVStore, cfg *
|
|||
config: cfg,
|
||||
userManager: bridge.NewUserManager("xmpp", log),
|
||||
incomingMessages: make(chan *pluginModel.DirectionalMessage, defaultMessageBufferSize),
|
||||
remoteID: remoteID,
|
||||
}
|
||||
|
||||
// Initialize handlers after bridge is created
|
||||
|
@ -617,3 +619,8 @@ func (b *xmppBridge) GetMessageHandler() pluginModel.MessageHandler {
|
|||
func (b *xmppBridge) GetUserResolver() pluginModel.UserResolver {
|
||||
return b.userResolver
|
||||
}
|
||||
|
||||
// GetRemoteID returns the remote ID used for shared channels registration
|
||||
func (b *xmppBridge) GetRemoteID() string {
|
||||
return b.remoteID
|
||||
}
|
||||
|
|
|
@ -6,11 +6,13 @@ import (
|
|||
|
||||
pluginModel "github.com/mattermost/mattermost-plugin-bridge-xmpp/server/model"
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
"github.com/mattermost/mattermost/server/public/pluginapi"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
client *pluginapi.Client
|
||||
api plugin.API
|
||||
bridgeManager pluginModel.BridgeManager
|
||||
}
|
||||
|
||||
|
@ -22,7 +24,7 @@ type Command interface {
|
|||
const xmppBridgeCommandTrigger = "xmppbridge"
|
||||
|
||||
// Register all your slash commands in the NewCommandHandler function.
|
||||
func NewCommandHandler(client *pluginapi.Client, bridgeManager pluginModel.BridgeManager) Command {
|
||||
func NewCommandHandler(client *pluginapi.Client, api plugin.API, bridgeManager pluginModel.BridgeManager) Command {
|
||||
// Register XMPP bridge command
|
||||
xmppBridgeData := model.NewAutocompleteData(xmppBridgeCommandTrigger, "", "Manage XMPP bridge")
|
||||
mapSubcommand := model.NewAutocompleteData("map", "[room_jid]", "Map current channel to XMPP room")
|
||||
|
@ -35,11 +37,17 @@ func NewCommandHandler(client *pluginapi.Client, bridgeManager pluginModel.Bridg
|
|||
statusSubcommand := model.NewAutocompleteData("status", "", "Show bridge connection status")
|
||||
xmppBridgeData.AddCommand(statusSubcommand)
|
||||
|
||||
syncSubcommand := model.NewAutocompleteData("sync", "", "Force sync with shared channels if channel is mapped")
|
||||
xmppBridgeData.AddCommand(syncSubcommand)
|
||||
|
||||
syncResetSubcommand := model.NewAutocompleteData("sync-reset", "", "Reset sync cursor for channel if mapped")
|
||||
xmppBridgeData.AddCommand(syncResetSubcommand)
|
||||
|
||||
err := client.SlashCommand.Register(&model.Command{
|
||||
Trigger: xmppBridgeCommandTrigger,
|
||||
AutoComplete: true,
|
||||
AutoCompleteDesc: "Manage XMPP bridge mappings",
|
||||
AutoCompleteHint: "[map|unmap|status]",
|
||||
AutoCompleteHint: "[map|unmap|status|sync|sync-reset]",
|
||||
AutocompleteData: xmppBridgeData,
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -48,6 +56,7 @@ func NewCommandHandler(client *pluginapi.Client, bridgeManager pluginModel.Bridg
|
|||
|
||||
return &Handler{
|
||||
client: client,
|
||||
api: api,
|
||||
bridgeManager: bridgeManager,
|
||||
}
|
||||
}
|
||||
|
@ -85,6 +94,8 @@ func (c *Handler) executeXMPPBridgeCommand(args *model.CommandArgs) *model.Comma
|
|||
- ` + "`/xmppbridge map <room_jid>`" + ` - Map current channel to XMPP room
|
||||
- ` + "`/xmppbridge unmap`" + ` - Unmap current channel from XMPP room
|
||||
- ` + "`/xmppbridge status`" + ` - Show bridge connection status
|
||||
- ` + "`/xmppbridge sync`" + ` - Force sync with shared channels if channel is mapped
|
||||
- ` + "`/xmppbridge sync-reset`" + ` - Reset sync cursor for channel if mapped
|
||||
|
||||
**Example:**
|
||||
` + "`/xmppbridge map general@conference.example.com`",
|
||||
|
@ -99,6 +110,10 @@ func (c *Handler) executeXMPPBridgeCommand(args *model.CommandArgs) *model.Comma
|
|||
return c.executeUnmapCommand(args)
|
||||
case "status":
|
||||
return c.executeStatusCommand(args)
|
||||
case "sync":
|
||||
return c.executeSyncCommand(args)
|
||||
case "sync-reset":
|
||||
return c.executeSyncResetCommand(args)
|
||||
default:
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
|
@ -284,6 +299,105 @@ func (c *Handler) isSystemAdmin(userID string) bool {
|
|||
}
|
||||
|
||||
// formatMappingError provides user-friendly error messages for mapping operations
|
||||
func (c *Handler) executeSyncCommand(args *model.CommandArgs) *model.CommandResponse {
|
||||
channelID := args.ChannelId
|
||||
|
||||
// Get the XMPP bridge to check if channel is mapped
|
||||
bridge, err := c.bridgeManager.GetBridge("xmpp")
|
||||
if err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "❌ XMPP bridge is not available. Please check the plugin configuration.",
|
||||
}
|
||||
}
|
||||
|
||||
// Check if channel is mapped to XMPP
|
||||
roomJID, err := bridge.GetChannelMapping(channelID)
|
||||
if err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("Error checking channel mapping: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
if roomJID == "" {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "❌ This channel is not mapped to any XMPP room. Use `/xmppbridge map <room_jid>` to create a mapping first.",
|
||||
}
|
||||
}
|
||||
|
||||
// Force sync with shared channels
|
||||
if err := c.api.SyncSharedChannel(channelID); err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("❌ Failed to sync channel: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("✅ Successfully triggered sync for channel mapped to XMPP room: `%s`", roomJID),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Handler) executeSyncResetCommand(args *model.CommandArgs) *model.CommandResponse {
|
||||
channelID := args.ChannelId
|
||||
|
||||
// Get the XMPP bridge to check if channel is mapped
|
||||
bridge, err := c.bridgeManager.GetBridge("xmpp")
|
||||
if err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "❌ XMPP bridge is not available. Please check the plugin configuration.",
|
||||
}
|
||||
}
|
||||
|
||||
// Check if channel is mapped to XMPP
|
||||
roomJID, err := bridge.GetChannelMapping(channelID)
|
||||
if err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("Error checking channel mapping: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
if roomJID == "" {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "❌ This channel is not mapped to any XMPP room. Use `/xmppbridge map <room_jid>` to create a mapping first.",
|
||||
}
|
||||
}
|
||||
|
||||
// Get remoteID from bridge for cursor operations
|
||||
remoteID := bridge.GetRemoteID()
|
||||
if remoteID == "" {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: "❌ Bridge remote ID not available. Cannot reset sync cursor.",
|
||||
}
|
||||
}
|
||||
|
||||
// Create empty cursor to reset to beginning
|
||||
emptyCursor := model.GetPostsSinceForSyncCursor{
|
||||
LastPostUpdateAt: 1,
|
||||
LastPostCreateAt: 1,
|
||||
}
|
||||
|
||||
// Reset sync cursor using UpdateSharedChannelCursor
|
||||
if err := c.api.UpdateSharedChannelCursor(channelID, remoteID, emptyCursor); err != nil {
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("❌ Failed to reset sync cursor: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CommandResponse{
|
||||
ResponseType: model.CommandResponseTypeEphemeral,
|
||||
Text: fmt.Sprintf("✅ Successfully reset sync cursor for channel mapped to XMPP room: `%s`", roomJID),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Handler) formatMappingError(operation, roomJID string, err error) *model.CommandResponse {
|
||||
errorMsg := err.Error()
|
||||
|
||||
|
|
|
@ -161,6 +161,9 @@ type Bridge interface {
|
|||
SendMessage(msg *BridgeMessage) error
|
||||
GetMessageHandler() MessageHandler
|
||||
GetUserResolver() UserResolver
|
||||
|
||||
// GetRemoteID returns the remote ID used for shared channels registration
|
||||
GetRemoteID() string
|
||||
}
|
||||
|
||||
// BridgeUser represents a user connected to any bridge service
|
||||
|
|
|
@ -82,7 +82,7 @@ func (p *Plugin) OnActivate() error {
|
|||
return fmt.Errorf("failed to initialize bridges: %w", err)
|
||||
}
|
||||
|
||||
p.commandClient = command.NewCommandHandler(p.client, p.bridgeManager)
|
||||
p.commandClient = command.NewCommandHandler(p.client, p.API, p.bridgeManager)
|
||||
|
||||
// Start the bridge manager (this starts message routing)
|
||||
if err := p.bridgeManager.Start(); err != nil {
|
||||
|
@ -148,6 +148,7 @@ func (p *Plugin) initBridges(cfg config.Configuration) error {
|
|||
p.API,
|
||||
p.kvstore,
|
||||
&cfg,
|
||||
p.remoteID,
|
||||
)
|
||||
|
||||
if err := p.bridgeManager.RegisterBridge("xmpp", xmppBridge); err != nil {
|
||||
|
@ -161,6 +162,7 @@ func (p *Plugin) initBridges(cfg config.Configuration) error {
|
|||
p.kvstore,
|
||||
&cfg,
|
||||
p.botUserID,
|
||||
"mattermost",
|
||||
)
|
||||
|
||||
if err := p.bridgeManager.RegisterBridge("mattermost", mattermostBridge); err != nil {
|
||||
|
@ -188,7 +190,7 @@ func (p *Plugin) registerForSharedChannels() error {
|
|||
PluginID: manifest.Id,
|
||||
CreatorID: botUserID,
|
||||
AutoShareDMs: false,
|
||||
AutoInvited: true,
|
||||
AutoInvited: false,
|
||||
}
|
||||
|
||||
remoteID, appErr := p.API.RegisterPluginForSharedChannels(opts)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue