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
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 {
ID int
Value string

View File

@ -226,12 +226,12 @@ func init() {
hasLikedTopic: acc.Select("likes").Columns("targetItem").Where("sentBy=? and targetItem=? and targetType='topics'").Prepare(),
createLike: acc.Insert("likes").Columns("weight, targetItem, targetType, sentBy, createdAt").Fields("?,?,?,?,UTC_TIMESTAMP()").Prepare(),
addLikesToTopic: acc.Update(t).Set("likeCount=likeCount+?").Where("tid = ?").Prepare(),
delete: acc.Delete(t).Where("tid = ?").Prepare(),
delete: acc.Delete(t).Where("tid=?").Prepare(),
deleteLikesForTopic: acc.Delete("likes").Where("targetItem=? AND targetType='topics'").Prepare(),
deleteActivity: acc.Delete("activity_stream").Where("elementID=? AND elementType='topic'").Prepare(),
deleteActivitySubs: acc.Delete("activity_subscriptions").Where("targetID=? AND targetType='topic'").Prepare(),
edit: acc.Update(t).Set("title=?,content=?,parsed_content=?").Where("tid=?").Prepare(), // TODO: Only run the content update bits on non-polls, does this matter?
setPoll: acc.Update(t).Set("poll = ?").Where("tid = ? AND poll = 0").Prepare(),
setPoll: acc.Update(t).Set("poll=?").Where("tid=? AND poll=0").Prepare(),
createAction: acc.Insert("replies").Columns("tid, actionType, ipaddress, createdBy, createdAt, lastUpdated, content, parsed_content").Fields("?,?,?,?,UTC_TIMESTAMP(),UTC_TIMESTAMP(),'',''").Prepare(),
getTopicUser: acc.SimpleLeftJoin("topics AS t", "users AS u", "t.title, t.content, t.createdBy, t.createdAt, t.lastReplyAt, t.lastReplyBy, t.lastReplyID, t.is_closed, t.sticky, t.parentID, t.ipaddress, t.views, t.postCount, t.likeCount, t.attachCount,t.poll, u.name, u.avatar, u.group, u.level", "t.createdBy = u.uid", "tid = ?", "", ""),
@ -385,7 +385,16 @@ func (t *Topic) Delete() error {
return err
}
_, err = topicStmts.deleteActivity.Exec(t.ID)
return err
if err != nil {
return err
}
if t.Poll > 0 {
err = (&Poll{ID:t.Poll}).Delete()
if err != nil {
return err
}
}
return nil
}
// TODO: Write tests for this

View File

@ -187,7 +187,7 @@ func init() {
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(),
deleteReplyPosts: acc.Select("replies").Columns("rid,tid").Where("createdBy=?").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]
tc := Topics.GetCache()
for rows.Next() {
var tid, parentID int
err := rows.Scan(&tid, &parentID)
var tid, parentID, poll int
err := rows.Scan(&tid, &parentID, &poll)
if err != nil {
return err
}
@ -364,6 +364,12 @@ func (u *User) DeletePosts() error {
if err != nil {
return err
}
if poll > 0 {
err = (&Poll{ID:poll}).Delete()
if err != nil {
return err
}
}
}
if err = rows.Err(); err != nil {
return err