Update delete_user to handle CommentPhotos

This commit is contained in:
Noah 2022-10-20 21:51:53 -07:00
parent f1e12f344e
commit fd0ff31d3e
4 changed files with 82 additions and 1 deletions

View File

@ -137,6 +137,19 @@ func NewPost() http.HandlerFunc {
// Are we DELETING this comment? // Are we DELETING this comment?
if isDelete { 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 { if err := thread.DeleteReply(comment); err != nil {
session.FlashError(w, r, "Error deleting your post: %s", err) session.FlashError(w, r, "Error deleting your post: %s", err)
} else { } 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. // Create the PhotoComment. If we don't have a Comment ID yet, let it be empty.
ptmpl := models.CommentPhoto{ ptmpl := models.CommentPhoto{
Filename: filename, Filename: filename,
UserID: currentUser.ID,
} }
if comment != nil { if comment != nil {
ptmpl.CommentID = comment.ID ptmpl.CommentID = comment.ID

View File

@ -7,6 +7,7 @@ import (
// CommentPhoto table associates a photo attachment to a (forum) comment. // CommentPhoto table associates a photo attachment to a (forum) comment.
type CommentPhoto struct { type CommentPhoto struct {
ID uint64 `gorm:"primaryKey"` ID uint64 `gorm:"primaryKey"`
UserID uint64 `gorm:"index"`
CommentID uint64 `gorm:"index"` CommentID uint64 `gorm:"index"`
Filename string Filename string
Filesize int64 Filesize int64
@ -19,6 +20,7 @@ type CommentPhoto struct {
func CreateCommentPhoto(tmpl CommentPhoto) (*CommentPhoto, error) { func CreateCommentPhoto(tmpl CommentPhoto) (*CommentPhoto, error) {
p := &CommentPhoto{ p := &CommentPhoto{
CommentID: tmpl.CommentID, CommentID: tmpl.CommentID,
UserID: tmpl.UserID,
Filename: tmpl.Filename, Filename: tmpl.Filename,
} }
@ -43,6 +45,29 @@ func (c *Comment) GetPhotos() ([]*CommentPhoto, error) {
return mapping.Get(c.ID), nil 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. // CommentPhotoMap maps comment IDs to CommentPhotos.
type CommentPhotoMap map[uint64][]*CommentPhoto type CommentPhotoMap map[uint64][]*CommentPhoto

View File

@ -18,6 +18,9 @@ func DeleteUser(user *models.User) error {
Fn func(uint64) error Fn func(uint64) error
} }
// Blank out the user's profile photo ID to avoid conflict removing their picture.
user.RemoveProfilePhoto()
var todo = []remover{ var todo = []remover{
{"Notifications", DeleteNotifications}, {"Notifications", DeleteNotifications},
{"Likes", DeleteLikes}, {"Likes", DeleteLikes},
@ -26,6 +29,7 @@ func DeleteUser(user *models.User) error {
{"Subscriptions", DeleteSubscriptions}, {"Subscriptions", DeleteSubscriptions},
{"Photos", DeleteUserPhotos}, {"Photos", DeleteUserPhotos},
{"Certification Photo", DeleteCertification}, {"Certification Photo", DeleteCertification},
{"Comment Photos", DeleteUserCommentPhotos},
{"Messages", DeleteUserMessages}, {"Messages", DeleteUserMessages},
{"Friends", DeleteFriends}, {"Friends", DeleteFriends},
{"Profile Fields", DeleteProfile}, {"Profile Fields", DeleteProfile},
@ -84,6 +88,44 @@ func DeleteUserPhotos(userID uint64) error {
return nil 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. // DeleteCertification scrubs data for deleting a user.
func DeleteCertification(userID uint64) error { func DeleteCertification(userID uint64) error {
log.Error("DeleteUser: DeleteCertification(%d)", userID) log.Error("DeleteUser: DeleteCertification(%d)", userID)

View File

@ -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. // FlashError adds a transient error message to the session.
func FlashError(w http.ResponseWriter, r *http.Request, msg string, args ...interface{}) { func FlashError(w http.ResponseWriter, r *http.Request, msg string, args ...interface{}) {
sess := Get(r) sess := Get(r)
sess.Errors = append(sess.Flashes, fmt.Sprintf(msg, args...)) sess.Errors = append(sess.Errors, fmt.Sprintf(msg, args...))
sess.Save(w) sess.Save(w)
} }