Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added command for removing any todo #207

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo
handler = p.runListCommand
case "pop":
handler = p.runPopCommand
case "remove":
handler = p.runRemoveCommand
case "send":
handler = p.runSendCommand
case "settings":
Expand Down Expand Up @@ -327,6 +329,53 @@ func (p *Plugin) runPopCommand(args []string, extra *model.CommandArgs) (bool, e
return false, nil
}

func (p *Plugin) runRemoveCommand(args []string, extra *model.CommandArgs) (bool, error) {
if len(args) < 2 || args[0] != "--todo" {
p.postCommandResponse(extra, "Invalid command provided")
return false, nil
}

todoID := args[1]
if todoID == "" {
p.postCommandResponse(extra, "Empty todoID provided")
return false, nil
}

issue, foreignID, _, _, err := p.listManager.RemoveIssue(extra.UserId, todoID)
if err != nil {
return false, err
}

userName := p.listManager.GetUserName(extra.UserId)

if foreignID != "" {
p.sendRefreshEvent(foreignID, []string{OutListKey})

message := fmt.Sprintf("@%s removed a Todo you sent: %s", userName, issue.Message)
p.PostBotDM(foreignID, message)
}

p.sendRefreshEvent(extra.UserId, []string{MyListKey})

responseMessage := fmt.Sprintf("Removed given Todo (%s).", issue.Message)

replyMessage := fmt.Sprintf("@%s removed a todo attached to this thread", userName)
p.postReplyIfNeeded(issue.PostID, replyMessage, issue.Message)

issues, err := p.listManager.GetIssueList(extra.UserId, MyListKey)
if err != nil {
p.API.LogError(err.Error())
p.postCommandResponse(extra, responseMessage)
return false, nil
}

responseMessage += listHeaderMessage
responseMessage += issuesListToString(issues)
p.postCommandResponse(extra, responseMessage)

return false, nil
}

func (p *Plugin) runSettingsCommand(args []string, extra *model.CommandArgs) (bool, error) {
const (
on = "on"
Expand Down Expand Up @@ -418,7 +467,7 @@ func (p *Plugin) runSettingsCommand(args []string, extra *model.CommandArgs) (bo
}

func getAutocompleteData() *model.AutocompleteData {
todo := model.NewAutocompleteData("todo", "[command]", "Available commands: list, add, pop, send, settings, help")
todo := model.NewAutocompleteData("todo", "[command]", "Available commands: list, add, pop, remove, send, settings, help")

add := model.NewAutocompleteData("add", "[message]", "Adds a Todo")
add.AddTextArgument("E.g. be awesome", "[message]", "")
Expand All @@ -440,6 +489,10 @@ func getAutocompleteData() *model.AutocompleteData {
pop := model.NewAutocompleteData("pop", "", "Removes the Todo issue at the top of the list")
todo.AddCommand(pop)

remove := model.NewAutocompleteData("remove", "--todo id", "Removes the given Todo")
remove.AddNamedDynamicListArgument("todo", "--todo todoID", getAutocompletePath(AutocompletePathRemoveTodoSuggestions), true)
todo.AddCommand(remove)

send := model.NewAutocompleteData("send", "[user] [todo]", "Sends a Todo to a specified user")
send.AddTextArgument("Whom to send", "[@awesomePerson]", "")
send.AddTextArgument("Todo message", "[message]", "")
Expand All @@ -466,3 +519,7 @@ func getAutocompleteData() *model.AutocompleteData {
todo.AddCommand(help)
return todo
}

func getAutocompletePath(path string) string {
return "plugins/" + manifest.Id + "/autocomplete" + path
}
6 changes: 6 additions & 0 deletions server/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

const (
AutocompletePath = "/autocomplete"
AutocompletePathRemoveTodoSuggestions = "/getRemoveTodoSuggestions"
)
43 changes: 41 additions & 2 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
p.handleEdit(w, r)
case "/change_assignment":
p.handleChangeAssignment(w, r)
case AutocompletePath + AutocompletePathRemoveTodoSuggestions:
p.getRemoveTodoSuggestions(w, r)
default:
http.NotFound(w, r)
}
Expand Down Expand Up @@ -316,8 +318,8 @@ func (p *Plugin) handleList(w http.ResponseWriter, r *http.Request) {

issuesJSON, err := json.Marshal(issues)
if err != nil {
p.API.LogError("Unable marhsal issues list to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable marhsal issues list to json", err)
p.API.LogError("Unable marshal issues list to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable marshal issues list to json", err)
return
}

Expand Down Expand Up @@ -669,3 +671,40 @@ func (p *Plugin) handleErrorWithCode(w http.ResponseWriter, code int, errTitle s
})
_, _ = w.Write(b)
}

func (p *Plugin) getRemoveTodoSuggestions(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
listID := MyListKey
issues, err := p.listManager.GetIssueList(userID, listID)
if err != nil {
p.API.LogError("Unable to get issues for user err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable to get issues for user", err)
return
}

out := []model.AutocompleteListItem{}
for _, issue := range issues {
s := model.AutocompleteListItem{
Item: issue.ID,
Hint: issue.Message,
HelpText: issue.Description,
}
out = append(out, s)
}

outJSON, err := json.Marshal(out)
if err != nil {
p.API.LogError("Unable marshal issues list to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable marshal issues list to json", err)
return
}

_, err = w.Write(outJSON)
if err != nil {
p.API.LogError("Unable to write json response err=" + err.Error())
}
}