stop storing poll vote ips when anti-cheat is off

remove the PollStore.CastVote method
This commit is contained in:
Azareal 2020-02-19 15:17:12 +10:00
parent e351de0266
commit 1191076c5b
3 changed files with 43 additions and 45 deletions

View File

@ -13,7 +13,7 @@ type Poll struct {
ParentID int ParentID int
ParentTable string ParentTable string
Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points
//AntiCheat bool // Apply various mitigations for cheating AntiCheat bool // Apply various mitigations for cheating
// GroupPower map[gid]points // The number of points a group can spend in this poll, defaults to 1 // GroupPower map[gid]points // The number of points a group can spend in this poll, defaults to 1
Options map[int]string Options map[int]string
@ -22,8 +22,22 @@ type Poll struct {
VoteCount int VoteCount int
} }
// TODO: Use a transaction for this?
// TODO: Add a voters table with castAt / IP data and only populate it when poll anti-cheat is on
func (p *Poll) CastVote(optionIndex, uid int, ip string) error { func (p *Poll) CastVote(optionIndex, uid int, ip string) error {
return Polls.CastVote(optionIndex, p.ID, uid, ip) // TODO: Move the query into a pollStmts rather than having it in the store if Config.DisablePollIP || !p.AntiCheat {
ip = ""
}
_, err := pollStmts.addVote.Exec(p.ID, uid, optionIndex, ip)
if err != nil {
return err
}
_, err = pollStmts.incVoteCount.Exec(p.ID)
if err != nil {
return err
}
_, err = pollStmts.incVoteCountForOption.Exec(optionIndex, p.ID)
return err
} }
func (p *Poll) Delete() error { func (p *Poll) Delete() error {
@ -44,6 +58,9 @@ func (p *Poll) Copy() Poll {
} }
type PollStmts struct { type PollStmts struct {
addVote *sql.Stmt
incVoteCount *sql.Stmt
incVoteCountForOption *sql.Stmt
deletePoll *sql.Stmt deletePoll *sql.Stmt
deletePollOptions *sql.Stmt deletePollOptions *sql.Stmt
deletePollVotes *sql.Stmt deletePollVotes *sql.Stmt
@ -51,8 +68,12 @@ type PollStmts struct {
func init() { func init() {
DbInits.Add(func(acc *qgen.Accumulator) error { DbInits.Add(func(acc *qgen.Accumulator) error {
p := "polls"
pollStmts = PollStmts{ pollStmts = PollStmts{
deletePoll: acc.Delete("polls").Where("pollID=?").Prepare(), addVote: acc.Insert("polls_votes").Columns("pollID,uid,option,castAt,ip").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(),
incVoteCount: acc.Update(p).Set("votes = votes + 1").Where("pollID=?").Prepare(),
incVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option=? AND pollID=?").Prepare(),
deletePoll: acc.Delete(p).Where("pollID=?").Prepare(),
deletePollOptions: acc.Delete("polls_options").Where("pollID=?").Prepare(), deletePollOptions: acc.Delete("polls_options").Where("pollID=?").Prepare(),
deletePollVotes: acc.Delete("polls_votes").Where("pollID=?").Prepare(), deletePollVotes: acc.Delete("polls_votes").Where("pollID=?").Prepare(),
} }

View File

@ -27,7 +27,6 @@ type PollStore interface {
Get(id int) (*Poll, error) Get(id int) (*Poll, error)
Exists(id int) bool Exists(id int) bool
Create(parent Pollable, pollType int, pollOptions map[int]string) (int, error) Create(parent Pollable, pollType int, pollOptions map[int]string) (int, error)
CastVote(optionIndex, pollID, uid int, ip string) error
Reload(id int) error Reload(id int) error
//Count() int //Count() int
@ -42,9 +41,6 @@ type DefaultPollStore struct {
exists *sql.Stmt exists *sql.Stmt
createPoll *sql.Stmt createPoll *sql.Stmt
createPollOption *sql.Stmt createPollOption *sql.Stmt
addVote *sql.Stmt
incVoteCount *sql.Stmt
incVoteCountForOption *sql.Stmt
delete *sql.Stmt delete *sql.Stmt
//count *sql.Stmt //count *sql.Stmt
} }
@ -55,16 +51,14 @@ func NewDefaultPollStore(cache PollCache) (*DefaultPollStore, error) {
cache = NewNullPollCache() cache = NewNullPollCache()
} }
// TODO: Add an admin version of registerStmt with more flexibility? // TODO: Add an admin version of registerStmt with more flexibility?
p := "polls"
return &DefaultPollStore{ return &DefaultPollStore{
cache: cache, cache: cache,
get: acc.Select("polls").Columns("parentID, parentTable, type, options, votes").Where("pollID = ?").Prepare(), get: acc.Select(p).Columns("parentID, parentTable, type, options, votes").Where("pollID=?").Prepare(),
exists: acc.Select("polls").Columns("pollID").Where("pollID = ?").Prepare(), exists: acc.Select(p).Columns("pollID").Where("pollID=?").Prepare(),
createPoll: acc.Insert("polls").Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(), createPoll: acc.Insert(p).Columns("parentID, parentTable, type, options").Fields("?,?,?,?").Prepare(),
createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(), createPollOption: acc.Insert("polls_options").Columns("pollID, option, votes").Fields("?,?,0").Prepare(),
addVote: acc.Insert("polls_votes").Columns("pollID,uid,option,castAt,ip").Fields("?,?,?,UTC_TIMESTAMP(),?").Prepare(), //count: acc.SimpleCount(p, "", ""),
incVoteCount: acc.Update("polls").Set("votes = votes + 1").Where("pollID = ?").Prepare(),
incVoteCountForOption: acc.Update("polls_options").Set("votes = votes + 1").Where("option=? AND pollID=?").Prepare(),
//count: acc.SimpleCount("polls", "", ""),
}, acc.FirstError() }, acc.FirstError()
} }
@ -211,23 +205,6 @@ func (s *DefaultPollStore) unpackOptionsMap(rawOptions map[int]string) []PollOpt
return options return options
} }
// TODO: Use a transaction for this?
func (s *DefaultPollStore) CastVote(optionIndex, pollID, uid int, ip string) error {
if Config.DisablePollIP {
ip = ""
}
_, err := s.addVote.Exec(pollID, uid, optionIndex, ip)
if err != nil {
return err
}
_, err = s.incVoteCount.Exec(pollID)
if err != nil {
return err
}
_, err = s.incVoteCountForOption.Exec(optionIndex, pollID)
return err
}
// TODO: Use a transaction for this // TODO: Use a transaction for this
func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map[int]string) (id int, err error) { func (s *DefaultPollStore) Create(parent Pollable, pollType int, pollOptions map[int]string) (id int, err error) {
pollOptionsTxt, err := json.Marshal(pollOptions) pollOptionsTxt, err := json.Marshal(pollOptions)