Provide a channel header menu action and /cleanup-channel slash command so system administrators can remove all messages from a channel, with admin checks enforced on both the web app and server. Configure CI and release workflows for the Forgejo runner and signed plugin bundles. Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/pluginapi"
|
|
)
|
|
|
|
const cleanupCommandTrigger = "cleanup-channel"
|
|
|
|
type Handler struct {
|
|
client *pluginapi.Client
|
|
cleanup func(userID, channelID string) (int, error)
|
|
}
|
|
|
|
type Command interface {
|
|
Handle(args *model.CommandArgs) (*model.CommandResponse, error)
|
|
}
|
|
|
|
func NewCommandHandler(client *pluginapi.Client, cleanup func(userID, channelID string) (int, error)) Command {
|
|
err := client.SlashCommand.Register(&model.Command{
|
|
Trigger: cleanupCommandTrigger,
|
|
AutoComplete: false,
|
|
AutoCompleteDesc: "Remove all messages from the current channel (system admins only)",
|
|
})
|
|
if err != nil {
|
|
client.Log.Error("Failed to register command", "error", err)
|
|
}
|
|
|
|
return &Handler{
|
|
client: client,
|
|
cleanup: cleanup,
|
|
}
|
|
}
|
|
|
|
func (c *Handler) Handle(args *model.CommandArgs) (*model.CommandResponse, error) {
|
|
fields := strings.Fields(args.Command)
|
|
if len(fields) == 0 {
|
|
return ephemeralResponse("Empty command"), nil
|
|
}
|
|
|
|
trigger := strings.TrimPrefix(fields[0], "/")
|
|
if trigger != cleanupCommandTrigger {
|
|
return ephemeralResponse(fmt.Sprintf("Unknown command: %s", args.Command)), nil
|
|
}
|
|
|
|
if !c.client.User.HasPermissionTo(args.UserId, model.PermissionManageSystem) {
|
|
return ephemeralResponse("This command is only available to system administrators."), nil
|
|
}
|
|
|
|
deleted, err := c.cleanup(args.UserId, args.ChannelId)
|
|
if err != nil {
|
|
c.client.Log.Error("Failed to cleanup channel via slash command", "channel_id", args.ChannelId, "error", err)
|
|
return ephemeralResponse("Failed to remove messages from this channel."), nil
|
|
}
|
|
|
|
return ephemeralResponse(fmt.Sprintf("Removed %d message(s) from this channel.", deleted)), nil
|
|
}
|
|
|
|
func ephemeralResponse(text string) *model.CommandResponse {
|
|
return &model.CommandResponse{
|
|
ResponseType: model.CommandResponseTypeEphemeral,
|
|
Text: text,
|
|
}
|
|
}
|