Cascade poll deletions properly when deleting topics normally or via delete all posts.

This commit is contained in:
Azareal 2020-01-20 16:58:22 +10:00
parent 5b6607b92c
commit 32bb2edfe4
4 changed files with 82 additions and 28 deletions

61
common/poll.go Normal file
View File

@ -0,0 +1,61 @@
package common
import (
"database/sql"
qgen "github.com/Azareal/Gosora/query_gen"
)
var pollStmts PollStmts
type Poll struct {
ID int
ParentID int
ParentTable string
Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points
//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
Options map[int]string
Results map[int]int // map[optionIndex]points
QuickOptions []PollOption // TODO: Fix up the template transpiler so we don't need to use this hack anymore
VoteCount int
}
func (p *Poll) CastVote(optionIndex int, 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
}
func (p *Poll) Delete() error {
_, err := pollStmts.deletePollVotes.Exec(p.ID)
if err != nil {
return err
}
_, err = pollStmts.deletePollOptions.Exec(p.ID)
if err != nil {
return err
}
_, err = pollStmts.deletePoll.Exec(p.ID)
return err
}
func (p *Poll) Copy() Poll {
return *p
}
type PollStmts struct {
deletePoll *sql.Stmt
deletePollOptions *sql.Stmt
deletePollVotes *sql.Stmt
}
func init() {
DbInits.Add(func(acc *qgen.Accumulator) error {
pollStmts = PollStmts{
deletePoll: acc.Delete("polls").Where("pollID=?").Prepare(),
deletePollOptions: acc.Delete("polls_options").Where("pollID=?").Prepare(),
deletePollVotes: acc.Delete("polls_votes").Where("pollID=?").Prepare(),
}
return acc.FirstError()
})
}

View File

@ -12,28 +12,6 @@ import (
var Polls PollStore var Polls PollStore
type Poll struct {
ID int
ParentID int
ParentTable string
Type int // 0: Single choice, 1: Multiple choice, 2: Multiple choice w/ points
//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
Options map[int]string
Results map[int]int // map[optionIndex]points
QuickOptions []PollOption // TODO: Fix up the template transpiler so we don't need to use this hack anymore
VoteCount int
}
func (p *Poll) CastVote(optionIndex int, 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
}
func (p *Poll) Copy() Poll {
return *p
}
type PollOption struct { type PollOption struct {
ID int ID int
Value string Value string

View File

@ -385,8 +385,17 @@ func (t *Topic) Delete() error {
return err return err
} }
_, err = topicStmts.deleteActivity.Exec(t.ID) _, err = topicStmts.deleteActivity.Exec(t.ID)
if err != nil {
return err return err
} }
if t.Poll > 0 {
err = (&Poll{ID:t.Poll}).Delete()
if err != nil {
return err
}
}
return nil
}
// TODO: Write tests for this // TODO: Write tests for this
func (t *Topic) Update(name, content string) error { func (t *Topic) Update(name, content string) error {

View File

@ -187,7 +187,7 @@ func init() {
scheduleAvatarResize: acc.Insert("users_avatar_queue").Columns("uid").Fields("?").Prepare(), scheduleAvatarResize: acc.Insert("users_avatar_queue").Columns("uid").Fields("?").Prepare(),
deletePosts: acc.Select("topics").Columns("tid,parentID").Where("createdBy=?").Prepare(), deletePosts: acc.Select("topics").Columns("tid,parentID,poll").Where("createdBy=?").Prepare(),
deleteProfilePosts: acc.Select("users_replies").Columns("rid").Where("createdBy=?").Prepare(), deleteProfilePosts: acc.Select("users_replies").Columns("rid").Where("createdBy=?").Prepare(),
deleteReplyPosts: acc.Select("replies").Columns("rid,tid").Where("createdBy=?").Prepare(), deleteReplyPosts: acc.Select("replies").Columns("rid,tid").Where("createdBy=?").Prepare(),
getLikedRepliesOfTopic: acc.Select("replies").Columns("rid").Where("tid=? AND likeCount>0").Prepare(), getLikedRepliesOfTopic: acc.Select("replies").Columns("rid").Where("tid=? AND likeCount>0").Prepare(),
@ -333,8 +333,8 @@ func (u *User) DeletePosts() error {
updatedForums := make(map[int]int) // forum[count] updatedForums := make(map[int]int) // forum[count]
tc := Topics.GetCache() tc := Topics.GetCache()
for rows.Next() { for rows.Next() {
var tid, parentID int var tid, parentID, poll int
err := rows.Scan(&tid, &parentID) err := rows.Scan(&tid, &parentID, &poll)
if err != nil { if err != nil {
return err return err
} }
@ -364,6 +364,12 @@ func (u *User) DeletePosts() error {
if err != nil { if err != nil {
return err return err
} }
if poll > 0 {
err = (&Poll{ID:poll}).Delete()
if err != nil {
return err
}
}
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
return err return err