2018-01-27 07:30:44 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-10-06 00:34:09 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2018-01-27 07:30:44 +00:00
|
|
|
)
|
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func PollVote(w http.ResponseWriter, r *http.Request, user *c.User, sPollID string) c.RouteError {
|
2018-01-27 07:30:44 +00:00
|
|
|
pollID, err := strconv.Atoi(sPollID)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The provided PollID is not a valid number.", w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
poll, err := c.Polls.Get(pollID)
|
2018-01-27 07:30:44 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The poll you tried to vote for doesn't exist.", w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
var topic *c.Topic
|
2018-01-27 07:30:44 +00:00
|
|
|
if poll.ParentTable == "replies" {
|
2019-04-19 06:36:26 +00:00
|
|
|
reply, err := c.Rstore.Get(poll.ParentID)
|
2018-01-27 07:30:44 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The parent post doesn't exist.", w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
topic, err = c.Topics.Get(reply.ParentID)
|
2018-01-27 07:30:44 +00:00
|
|
|
} else if poll.ParentTable == "topics" {
|
2019-04-19 06:36:26 +00:00
|
|
|
topic, err = c.Topics.Get(poll.ParentID)
|
2018-01-27 07:30:44 +00:00
|
|
|
} else {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(errors.New("Unknown parentTable for poll"), w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The parent topic doesn't exist.", w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add hooks to make use of headerLite
|
2020-03-18 09:21:34 +00:00
|
|
|
_, ferr := c.SimpleForumUserCheck(w, r, user, topic.ParentID)
|
2018-01-27 07:30:44 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ViewTopic {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NoPermissions(w, r, user)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
optionIndex, err := strconv.Atoi(r.PostFormValue("poll_option_input"))
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("Malformed input", w, r, user)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
2019-12-31 21:57:54 +00:00
|
|
|
err = poll.CastVote(optionIndex, user.ID, user.GetIP())
|
2018-01-27 07:30:44 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-27 07:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/topic/"+strconv.Itoa(topic.ID), http.StatusSeeOther)
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-28 14:30:24 +00:00
|
|
|
|
2020-03-18 09:21:34 +00:00
|
|
|
func PollResults(w http.ResponseWriter, r *http.Request, user *c.User, sPollID string) c.RouteError {
|
2018-11-12 09:23:36 +00:00
|
|
|
//log.Print("in PollResults")
|
2018-01-28 14:30:24 +00:00
|
|
|
pollID, err := strconv.Atoi(sPollID)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The provided PollID is not a valid number.", w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
poll, err := c.Polls.Get(pollID)
|
2018-01-28 14:30:24 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.PreError("The poll you tried to vote for doesn't exist.", w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Abstract this
|
2020-02-09 10:00:08 +00:00
|
|
|
rows, err := qgen.NewAcc().Select("polls_options").Columns("votes").Where("pollID=?").Orderby("option ASC").Query(poll.ID)
|
2018-01-28 14:30:24 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2019-10-06 00:34:09 +00:00
|
|
|
optionList := ""
|
2018-01-28 14:30:24 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var votes int
|
|
|
|
err := rows.Scan(&votes)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|
|
|
|
optionList += strconv.Itoa(votes) + ","
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-01-28 14:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement a version of this which doesn't rely so much on sequential order
|
|
|
|
if len(optionList) > 0 {
|
|
|
|
optionList = optionList[:len(optionList)-1]
|
|
|
|
}
|
|
|
|
w.Write([]byte("[" + optionList + "]"))
|
|
|
|
return nil
|
|
|
|
}
|