From 7a275341535b265aa5e928f37c4839306e094e8b Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 15 Jun 2019 22:26:37 +1000 Subject: [PATCH] Don't create a poll if no options are set. --- routes/topic.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/routes/topic.go b/routes/topic.go index 08a626fc..7bfd2007 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -366,7 +366,9 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.Ro var pollInputItems = make(map[int]string) for key, values := range r.Form { for _, value := range values { - if strings.HasPrefix(key, "pollinputitem[") { + if !strings.HasPrefix(key, "pollinputitem[") { + continue + } halves := strings.Split(key, "[") if len(halves) != 2 { return c.LocalError("Malformed pollinputitem", w, r, user) @@ -387,20 +389,21 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.Ro break } } - } } } - // Make sure the indices are sequential to avoid out of bounds issues - var seqPollInputItems = make(map[int]string) - for i := 0; i < len(pollInputItems); i++ { - seqPollInputItems[i] = pollInputItems[i] - } + if len(pollInputItems) > 0 { + // Make sure the indices are sequential to avoid out of bounds issues + var seqPollInputItems = make(map[int]string) + for i := 0; i < len(pollInputItems); i++ { + seqPollInputItems[i] = pollInputItems[i] + } - pollType := 0 // Basic single choice - _, err := c.Polls.Create(topic, pollType, seqPollInputItems) - if err != nil { - return c.LocalError("Failed to add poll to topic", w, r, user) // TODO: Might need to be an internal error as it could leave phantom polls? + pollType := 0 // Basic single choice + _, err := c.Polls.Create(topic, pollType, seqPollInputItems) + if err != nil { + return c.LocalError("Failed to add poll to topic", w, r, user) // TODO: Might need to be an internal error as it could leave phantom polls? + } } }