added command with interactive dialog
Some checks failed
ci / plugin-ci (push) Has been cancelled

This commit is contained in:
Felipe M 2024-08-08 15:52:29 +02:00
parent 09c4f13f2c
commit 2d962d18d2
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
16 changed files with 520 additions and 89 deletions

66
server/commands.go Normal file
View file

@ -0,0 +1,66 @@
package main
import (
"strings"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
)
func (p *Plugin) getCommand() *model.Command {
return &model.Command{
Trigger: "removeattachments",
DisplayName: "Remove Attachments",
Description: "Remove all attachments from a post",
AutoComplete: false, // Hide from autocomplete
}
}
func (p *Plugin) createCommandResponse(message string) *model.CommandResponse {
return &model.CommandResponse{
Text: message,
}
}
func (p *Plugin) createErrorCommandResponse(errorMessage string) *model.CommandResponse {
return &model.CommandResponse{
Text: "Can't remove attachments: " + errorMessage,
}
}
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
commandSplit := strings.Split(args.Command, " ")
// Do not provide command output since it's going to be triggered from the frontend
if len(commandSplit) != 2 {
return p.createErrorCommandResponse("Invalid number of arguments, use `/removeattachments [postID]`."), nil
}
postID := commandSplit[1]
// Check if the post ID is a valid ID
if !model.IsValidId(postID) {
return p.createErrorCommandResponse("Invalid post ID"), nil
}
// Check if the user has permissions to remove attachments from the post
if errReason := p.userHasRemovePermissionsToPost(args.UserId, args.ChannelId, postID); errReason != "" {
return p.createErrorCommandResponse(errReason), nil
}
// Create an interactive dialog to confirm the action
if err := p.API.OpenInteractiveDialog(model.OpenDialogRequest{
TriggerId: args.TriggerId,
URL: "/plugins/" + manifest.Id + "/api/v1/remove_attachments?post_id=" + postID,
Dialog: model.Dialog{
Title: "Remove Attachments",
IntroductionText: "Are you sure you want to remove all attachments from this post?",
SubmitLabel: "Remove",
},
}); err != nil {
return p.createCommandResponse(err.Error()), nil
}
// Return nothing, let the dialog/api handle the response
return &model.CommandResponse{}, nil
}