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 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
dist
.DS_Store
node_modules
30 changes: 13 additions & 17 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,26 +330,18 @@ func (p *Plugin) runPopCommand(args []string, extra *model.CommandArgs) (bool, e
}

func (p *Plugin) runRemoveCommand(args []string, extra *model.CommandArgs) (bool, error) {
message := strings.Join(args, " ")
listID := MyListKey
issueList, err := p.listManager.GetIssueList(extra.UserId, listID)
if err != nil {
return false, err
}

var issueID string
for _, issue := range issueList {
if issue.Message == message {
issueID = issue.ID
}
if args[0] != "--todo" {
thoratvinod marked this conversation as resolved.
Show resolved Hide resolved
p.postCommandResponse(extra, "Invalid command provided")
return false, nil
}

if issueID == "" {
p.postCommandResponse(extra, "Can't find the todo in list")
todoID := args[1]
if todoID == "" {
p.postCommandResponse(extra, "Empty todoID provided")
return false, nil
}

issue, foreignID, _, _, err := p.listManager.RemoveIssue(extra.UserId, issueID)
issue, foreignID, _, _, err := p.listManager.RemoveIssue(extra.UserId, todoID)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -497,8 +489,8 @@ 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", "", "Removes the given Todo")
remove.AddTextArgument("E.g. be average", "[message]", "")
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")
Expand Down Expand Up @@ -527,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"
)
39 changes: 39 additions & 0 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 @@ -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 marhsal issues list to json err=" + err.Error())
p.handleErrorWithCode(w, http.StatusInternalServerError, "Unable marhsal issues list to json", err)
thoratvinod marked this conversation as resolved.
Show resolved Hide resolved
return
}

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