Cascade like deletions properly when deleting topics.

This commit is contained in:
Azareal 2020-01-18 18:12:00 +10:00
parent ec7b304c08
commit a21454ba5d
2 changed files with 27 additions and 24 deletions

View File

@ -328,12 +328,33 @@ func (t *Topic) Unlike(uid int) error {
return nil 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 // TODO: Use a transaction here
func (t *Topic) Delete() error { func (t *Topic) Delete() error {
creator, err := Users.Get(t.CreatedBy) creator, err := Users.Get(t.CreatedBy)
if err == nil { if err == nil {
wcount := WordCount(t.Content) err = creator.DecreasePostStats(WordCount(t.Content), true)
err = creator.DecreasePostStats(wcount, true)
if err != nil { if err != nil {
return err return err
} }
@ -355,6 +376,10 @@ func (t *Topic) Delete() error {
if err != nil { if err != nil {
return err return err
} }
err = handleLikedTopicReplies(t.ID)
if err != nil {
return err
}
_, err = topicStmts.deleteActivitySubs.Exec(t.ID) _, err = topicStmts.deleteActivitySubs.Exec(t.ID)
if err != nil { if err != nil {
return err return err

View File

@ -330,28 +330,6 @@ func (u *User) DeletePosts() error {
defer TopicListThaw.Thaw() defer TopicListThaw.Thaw()
defer u.CacheRemove() 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] updatedForums := make(map[int]int) // forum[count]
tc := Topics.GetCache() tc := Topics.GetCache()
for rows.Next() { for rows.Next() {