Cascade like deletions properly when using delete all posts.

This commit is contained in:
Azareal 2020-01-18 17:24:51 +10:00
parent 7ec2037f5f
commit ec7b304c08
3 changed files with 38 additions and 12 deletions

View File

@ -124,8 +124,7 @@ func (r *Reply) Like(uid int) (err error) {
func (r *Reply) Delete() error {
creator, err := Users.Get(r.CreatedBy)
if err == nil {
wcount := WordCount(r.Content)
err = creator.DecreasePostStats(wcount, false)
err = creator.DecreasePostStats(WordCount(r.Content), false)
if err != nil {
return err
}

View File

@ -146,9 +146,10 @@ type UserStmts struct {
scheduleAvatarResize *sql.Stmt
deletePosts *sql.Stmt
deleteProfilePosts *sql.Stmt
deleteReplyPosts *sql.Stmt
deletePosts *sql.Stmt
deleteProfilePosts *sql.Stmt
deleteReplyPosts *sql.Stmt
getLikedRepliesOfTopic *sql.Stmt
}
var userStmts UserStmts
@ -186,9 +187,10 @@ func init() {
scheduleAvatarResize: acc.Insert("users_avatar_queue").Columns("uid").Fields("?").Prepare(),
deletePosts: acc.Select("topics").Columns("tid,parentID").Where("createdBy=?").Prepare(),
deleteProfilePosts: acc.Select("users_replies").Columns("rid").Where("createdBy=?").Prepare(),
deleteReplyPosts: acc.Select("replies").Columns("rid,tid").Where("createdBy=?").Prepare(),
deletePosts: acc.Select("topics").Columns("tid,parentID").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(),
}
return acc.FirstError()
})
@ -328,6 +330,28 @@ func (u *User) DeletePosts() error {
defer TopicListThaw.Thaw()
defer u.CacheRemove()
handleLikedTopicReplies := func(tid int) error {
rows, err := userStmts.getLikedRepliesOfTopic.Query(tid)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var rid int
err := rows.Scan(&rid)
if err != nil {
return err
}
_, err = replyStmts.deleteLikesForReply.Exec(rid)
if err != nil {
return err
}
}
return rows.Err()
}
updatedForums := make(map[int]int) // forum[count]
tc := Topics.GetCache()
for rows.Next() {
@ -350,6 +374,10 @@ func (u *User) DeletePosts() error {
if err != nil {
return err
}
err = handleLikedTopicReplies(tid)
if err != nil {
return err
}
_, err = topicStmts.deleteActivitySubs.Exec(tid)
if err != nil {
return err
@ -391,8 +419,7 @@ func (u *User) DeletePosts() error {
}
// TODO: Remove alerts.
}
err = rows.Err()
if err != nil {
if err = rows.Err(); err != nil {
return err
}

View File

@ -327,9 +327,9 @@ func ReplyDeleteSubmit(w http.ResponseWriter, r *http.Request, user c.User, srid
}
// ? - What happens if an error fires after a redirect...?
/*replyCreator, err := c.Users.Get(reply.CreatedBy)
/*creator, err := c.Users.Get(reply.CreatedBy)
if err == nil {
err = replyCreator.DecreasePostStats(c.WordCount(reply.Content), false)
err = creator.DecreasePostStats(c.WordCount(reply.Content), false)
if err != nil {
return c.InternalErrorJSQ(err, w, r, js)
}