This commit is contained in:
parent
09c4f13f2c
commit
2d962d18d2
16 changed files with 520 additions and 89 deletions
66
server/commands.go
Normal file
66
server/commands.go
Normal 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
|
||||
}
|
Reference in a new issue