From ec7b304c085344fd161a2ce07a1b2ce47695b67d Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 18 Jan 2020 17:24:51 +1000 Subject: [PATCH] Cascade like deletions properly when using delete all posts. --- common/reply.go | 3 +-- common/user.go | 43 +++++++++++++++++++++++++++++++++++-------- routes/reply.go | 4 ++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/common/reply.go b/common/reply.go index 1ee072ab..f4d76880 100644 --- a/common/reply.go +++ b/common/reply.go @@ -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 } diff --git a/common/user.go b/common/user.go index b295d41e..f8e955b3 100644 --- a/common/user.go +++ b/common/user.go @@ -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 } diff --git a/routes/reply.go b/routes/reply.go index 2757c87b..8ec2bc8d 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -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) }