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 { func (r *Reply) Delete() error {
creator, err := Users.Get(r.CreatedBy) creator, err := Users.Get(r.CreatedBy)
if err == nil { if err == nil {
wcount := WordCount(r.Content) err = creator.DecreasePostStats(WordCount(r.Content), false)
err = creator.DecreasePostStats(wcount, false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -149,6 +149,7 @@ type UserStmts struct {
deletePosts *sql.Stmt deletePosts *sql.Stmt
deleteProfilePosts *sql.Stmt deleteProfilePosts *sql.Stmt
deleteReplyPosts *sql.Stmt deleteReplyPosts *sql.Stmt
getLikedRepliesOfTopic *sql.Stmt
} }
var userStmts UserStmts var userStmts UserStmts
@ -189,6 +190,7 @@ func init() {
deletePosts: acc.Select("topics").Columns("tid,parentID").Where("createdBy=?").Prepare(), deletePosts: acc.Select("topics").Columns("tid,parentID").Where("createdBy=?").Prepare(),
deleteProfilePosts: acc.Select("users_replies").Columns("rid").Where("createdBy=?").Prepare(), deleteProfilePosts: acc.Select("users_replies").Columns("rid").Where("createdBy=?").Prepare(),
deleteReplyPosts: acc.Select("replies").Columns("rid,tid").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() return acc.FirstError()
}) })
@ -328,6 +330,28 @@ 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() {
@ -350,6 +374,10 @@ func (u *User) DeletePosts() error {
if err != nil { if err != nil {
return err return err
} }
err = handleLikedTopicReplies(tid)
if err != nil {
return err
}
_, err = topicStmts.deleteActivitySubs.Exec(tid) _, err = topicStmts.deleteActivitySubs.Exec(tid)
if err != nil { if err != nil {
return err return err
@ -391,8 +419,7 @@ func (u *User) DeletePosts() error {
} }
// TODO: Remove alerts. // TODO: Remove alerts.
} }
err = rows.Err() if err = rows.Err(); err != nil {
if err != nil {
return err 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...? // ? - 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 { if err == nil {
err = replyCreator.DecreasePostStats(c.WordCount(reply.Content), false) err = creator.DecreasePostStats(c.WordCount(reply.Content), false)
if err != nil { if err != nil {
return c.InternalErrorJSQ(err, w, r, js) return c.InternalErrorJSQ(err, w, r, js)
} }