From a21454ba5d21ce702b1562a1162265dcb57aa000 Mon Sep 17 00:00:00 2001 From: Azareal Date: Sat, 18 Jan 2020 18:12:00 +1000 Subject: [PATCH] Cascade like deletions properly when deleting topics. --- common/topic.go | 29 +++++++++++++++++++++++++++-- common/user.go | 22 ---------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/common/topic.go b/common/topic.go index 8979dcea..f16c3ee9 100644 --- a/common/topic.go +++ b/common/topic.go @@ -328,12 +328,33 @@ func (t *Topic) Unlike(uid int) error { return nil } +func handleLikedTopicReplies(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() +} + // TODO: Use a transaction here func (t *Topic) Delete() error { creator, err := Users.Get(t.CreatedBy) if err == nil { - wcount := WordCount(t.Content) - err = creator.DecreasePostStats(wcount, true) + err = creator.DecreasePostStats(WordCount(t.Content), true) if err != nil { return err } @@ -355,6 +376,10 @@ func (t *Topic) Delete() error { if err != nil { return err } + err = handleLikedTopicReplies(t.ID) + if err != nil { + return err + } _, err = topicStmts.deleteActivitySubs.Exec(t.ID) if err != nil { return err diff --git a/common/user.go b/common/user.go index f8e955b3..5915f24f 100644 --- a/common/user.go +++ b/common/user.go @@ -330,28 +330,6 @@ 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() {