Don't create a poll if no options are set.

This commit is contained in:
Azareal 2019-06-15 22:26:37 +10:00
parent d95268f069
commit 7a27534153

View File

@ -366,7 +366,9 @@ func CreateTopicSubmit(w http.ResponseWriter, r *http.Request, user c.User) c.Ro
var pollInputItems = make(map[int]string) var pollInputItems = make(map[int]string)
for key, values := range r.Form { for key, values := range r.Form {
for _, value := range values { for _, value := range values {
if strings.HasPrefix(key, "pollinputitem[") { if !strings.HasPrefix(key, "pollinputitem[") {
continue
}
halves := strings.Split(key, "[") halves := strings.Split(key, "[")
if len(halves) != 2 { if len(halves) != 2 {
return c.LocalError("Malformed pollinputitem", w, r, user) 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 break
} }
} }
}
} }
} }
// Make sure the indices are sequential to avoid out of bounds issues if len(pollInputItems) > 0 {
var seqPollInputItems = make(map[int]string) // Make sure the indices are sequential to avoid out of bounds issues
for i := 0; i < len(pollInputItems); i++ { var seqPollInputItems = make(map[int]string)
seqPollInputItems[i] = pollInputItems[i] for i := 0; i < len(pollInputItems); i++ {
} seqPollInputItems[i] = pollInputItems[i]
}
pollType := 0 // Basic single choice pollType := 0 // Basic single choice
_, err := c.Polls.Create(topic, pollType, seqPollInputItems) _, err := c.Polls.Create(topic, pollType, seqPollInputItems)
if err != nil { 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? 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?
}
} }
} }