From fd0ff31d3e12c3d10ec009503c31092fa4420442 Mon Sep 17 00:00:00 2001 From: Noah Date: Thu, 20 Oct 2022 21:51:53 -0700 Subject: [PATCH] Update delete_user to handle CommentPhotos --- pkg/controller/forum/new_post.go | 14 ++++++++++ pkg/models/comment_photo.go | 25 ++++++++++++++++++ pkg/models/deletion/delete_user.go | 42 ++++++++++++++++++++++++++++++ pkg/session/session.go | 2 +- 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/pkg/controller/forum/new_post.go b/pkg/controller/forum/new_post.go index 1e95046..278cab7 100644 --- a/pkg/controller/forum/new_post.go +++ b/pkg/controller/forum/new_post.go @@ -137,6 +137,19 @@ func NewPost() http.HandlerFunc { // Are we DELETING this comment? if isDelete { + // Is there a photo attachment? Remove it, too. + if commentPhoto != nil { + if err := photo.Delete(commentPhoto.Filename); err != nil { + session.FlashError(w, r, "Error removing the photo from disk: %s", err) + } + + if err := commentPhoto.Delete(); err != nil { + session.FlashError(w, r, "Couldn't remove photo from DB: %s", err) + } else { + commentPhoto = nil + } + } + if err := thread.DeleteReply(comment); err != nil { session.FlashError(w, r, "Error deleting your post: %s", err) } else { @@ -206,6 +219,7 @@ func NewPost() http.HandlerFunc { // Create the PhotoComment. If we don't have a Comment ID yet, let it be empty. ptmpl := models.CommentPhoto{ Filename: filename, + UserID: currentUser.ID, } if comment != nil { ptmpl.CommentID = comment.ID diff --git a/pkg/models/comment_photo.go b/pkg/models/comment_photo.go index db60d16..09512bb 100644 --- a/pkg/models/comment_photo.go +++ b/pkg/models/comment_photo.go @@ -7,6 +7,7 @@ import ( // CommentPhoto table associates a photo attachment to a (forum) comment. type CommentPhoto struct { ID uint64 `gorm:"primaryKey"` + UserID uint64 `gorm:"index"` CommentID uint64 `gorm:"index"` Filename string Filesize int64 @@ -19,6 +20,7 @@ type CommentPhoto struct { func CreateCommentPhoto(tmpl CommentPhoto) (*CommentPhoto, error) { p := &CommentPhoto{ CommentID: tmpl.CommentID, + UserID: tmpl.UserID, Filename: tmpl.Filename, } @@ -43,6 +45,29 @@ func (c *Comment) GetPhotos() ([]*CommentPhoto, error) { return mapping.Get(c.ID), nil } +/* +PaginateUserCommentPhotos gets a page of all CommentPhotos by the user. +*/ +func PaginateUserCommentPhotos(userID uint64, pager *Pagination) ([]*CommentPhoto, error) { + var p = []*CommentPhoto{} + + query := DB.Where( + "user_id = ?", + userID, + ).Order( + pager.Sort, + ) + + // Get the total count. + query.Model(&CommentPhoto{}).Count(&pager.Total) + + result := query.Offset( + pager.GetOffset(), + ).Limit(pager.PerPage).Find(&p) + + return p, result.Error +} + // CommentPhotoMap maps comment IDs to CommentPhotos. type CommentPhotoMap map[uint64][]*CommentPhoto diff --git a/pkg/models/deletion/delete_user.go b/pkg/models/deletion/delete_user.go index edc69ad..fc6a659 100644 --- a/pkg/models/deletion/delete_user.go +++ b/pkg/models/deletion/delete_user.go @@ -18,6 +18,9 @@ func DeleteUser(user *models.User) error { Fn func(uint64) error } + // Blank out the user's profile photo ID to avoid conflict removing their picture. + user.RemoveProfilePhoto() + var todo = []remover{ {"Notifications", DeleteNotifications}, {"Likes", DeleteLikes}, @@ -26,6 +29,7 @@ func DeleteUser(user *models.User) error { {"Subscriptions", DeleteSubscriptions}, {"Photos", DeleteUserPhotos}, {"Certification Photo", DeleteCertification}, + {"Comment Photos", DeleteUserCommentPhotos}, {"Messages", DeleteUserMessages}, {"Friends", DeleteFriends}, {"Profile Fields", DeleteProfile}, @@ -84,6 +88,44 @@ func DeleteUserPhotos(userID uint64) error { return nil } +// DeleteUserCommentPhotos scrubs data for deleting a user. +func DeleteUserCommentPhotos(userID uint64) error { + log.Error("DeleteUser: BEGIN DeleteUserCommentPhotos(%d)", userID) + + // Deeply scrub all user photos. + pager := &models.Pagination{ + Page: 1, + PerPage: 20, + Sort: "comment_photos.id", + } + + for { + photos, err := models.PaginateUserCommentPhotos( + userID, + pager, + ) + + if err != nil { + return err + } + + if len(photos) == 0 { + break + } + + for _, item := range photos { + log.Warn("DeleteUserCommentPhotos(%d): remove file %s", userID, item.Filename) + photo.Delete(item.Filename) + if err := item.Delete(); err != nil { + return err + } + } + } + + log.Error("DeleteUser: END DeleteUserPhotos(%d)", userID) + return nil +} + // DeleteCertification scrubs data for deleting a user. func DeleteCertification(userID uint64) error { log.Error("DeleteUser: DeleteCertification(%d)", userID) diff --git a/pkg/session/session.go b/pkg/session/session.go index 395bab9..0458261 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -153,7 +153,7 @@ func Flash(w http.ResponseWriter, r *http.Request, msg string, args ...interface // FlashError adds a transient error message to the session. func FlashError(w http.ResponseWriter, r *http.Request, msg string, args ...interface{}) { sess := Get(r) - sess.Errors = append(sess.Flashes, fmt.Sprintf(msg, args...)) + sess.Errors = append(sess.Errors, fmt.Sprintf(msg, args...)) sess.Save(w) }