2018-01-27 07:30:44 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
2018-01-27 07:30:44 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 01:04:07 +00:00
|
|
|
func PollVote(w http.ResponseWriter, r *http.Request, u *c.User, sPollID string) c.RouteError {
|
2022-02-21 03:32:53 +00:00
|
|
|
pollID, err := strconv.Atoi(sPollID)
|
|
|
|
if err != nil {
|
|
|
|
return c.PreError("The provided PollID is not a valid number.", w, r)
|
|
|
|
}
|
|
|
|
poll, err := c.Polls.Get(pollID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.PreError("The poll you tried to vote for doesn't exist.", w, r)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
var topic *c.Topic
|
|
|
|
if poll.ParentTable == "replies" {
|
|
|
|
reply, err := c.Rstore.Get(poll.ParentID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.PreError("The parent post doesn't exist.", w, r)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
topic, err = c.Topics.Get(reply.ParentID)
|
|
|
|
} else if poll.ParentTable == "topics" {
|
|
|
|
topic, err = c.Topics.Get(poll.ParentID)
|
|
|
|
} else {
|
|
|
|
return c.InternalError(errors.New("Unknown parentTable for poll"), w, r)
|
|
|
|
}
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.PreError("The parent topic doesn't exist.", w, r)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
// TODO: Add hooks to make use of headerLite
|
|
|
|
_, ferr := c.SimpleForumUserCheck(w, r, u, topic.ParentID)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !u.Perms.ViewTopic {
|
|
|
|
return c.NoPermissions(w, r, u)
|
|
|
|
}
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
optIndex, err := strconv.Atoi(r.PostFormValue("poll_option_input"))
|
|
|
|
if err != nil {
|
|
|
|
return c.LocalError("Malformed input", w, r, u)
|
|
|
|
}
|
|
|
|
err = poll.CastVote(optIndex, u.ID, u.GetIP())
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-01-27 07:30:44 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
http.Redirect(w, r, "/topic/"+strconv.Itoa(topic.ID), http.StatusSeeOther)
|
|
|
|
return nil
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
2018-01-28 14:30:24 +00:00
|
|
|
|
2020-12-18 01:04:07 +00:00
|
|
|
func PollResults(w http.ResponseWriter, r *http.Request, u *c.User, sPollID string) c.RouteError {
|
2022-02-21 03:32:53 +00:00
|
|
|
//log.Print("in PollResults")
|
|
|
|
pollID, err := strconv.Atoi(sPollID)
|
|
|
|
if err != nil {
|
|
|
|
return c.PreError("The provided PollID is not a valid number.", w, r)
|
|
|
|
}
|
|
|
|
poll, err := c.Polls.Get(pollID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.PreError("The poll you tried to vote for doesn't exist.", w, r)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
2018-01-28 14:30:24 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
// TODO: Implement a version of this which doesn't rely so much on sequential order
|
|
|
|
var ob bytes.Buffer
|
|
|
|
ob.WriteRune('[')
|
|
|
|
var i int
|
|
|
|
e := poll.Resultsf(func(votes int) error {
|
|
|
|
if i != 0 {
|
|
|
|
ob.WriteRune(',')
|
|
|
|
}
|
|
|
|
ob.WriteString(strconv.Itoa(votes))
|
|
|
|
i++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if e != nil && e != sql.ErrNoRows {
|
|
|
|
return c.InternalError(e, w, r)
|
|
|
|
}
|
|
|
|
ob.WriteRune(']')
|
|
|
|
w.Write(ob.Bytes())
|
2018-01-28 14:30:24 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
return nil
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|