From 483e5a2db30305c516db30ef44b8268058081044 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 21 Dec 2023 14:37:16 -0800 Subject: [PATCH] Photo edit/delete fixes, lazy load images --- pkg/controller/photo/edit_delete.go | 55 ++++++++++++++++------------ web/templates/account/dashboard.html | 2 +- web/templates/forum/thread.html | 2 +- web/templates/photo/gallery.html | 4 +- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/pkg/controller/photo/edit_delete.go b/pkg/controller/photo/edit_delete.go index 055c7ec..aff8058 100644 --- a/pkg/controller/photo/edit_delete.go +++ b/pkg/controller/photo/edit_delete.go @@ -42,9 +42,21 @@ func Edit() http.HandlerFunc { } // Do we have permission for this photo? - if photo.UserID != currentUser.ID && !currentUser.IsAdmin { - templates.ForbiddenPage(w, r) - return + if photo.UserID != currentUser.ID { + if !currentUser.IsAdmin { + templates.ForbiddenPage(w, r) + return + } + + // Find the owner of this photo and assume currentUser is them for the remainder + // of this controller. + if user, err := models.GetUser(photo.UserID); err != nil { + session.FlashError(w, r, "Couldn't get the owner User for this photo!") + templates.Redirect(w, "/") + return + } else { + currentUser = user + } } // Are we saving the changes? @@ -119,15 +131,6 @@ func Edit() http.HandlerFunc { models.RemoveNotification("photos", photo.ID) } - // Whose photo gallery to redirect to? if admin editing a user's photo, - // go back to the owner's gallery instead of our own. - if photo.UserID != currentUser.ID { - if owner, err := models.GetUser(photo.UserID); err == nil { - templates.Redirect(w, "/photo/u/"+owner.Username) - return - } - } - // Return the user to their gallery. templates.Redirect(w, "/photo/u/"+currentUser.Username) return @@ -177,9 +180,21 @@ func Delete() http.HandlerFunc { } // Do we have permission for this photo? - if photo.UserID != currentUser.ID && !currentUser.IsAdmin { - templates.ForbiddenPage(w, r) - return + if photo.UserID != currentUser.ID { + if !currentUser.IsAdmin { + templates.ForbiddenPage(w, r) + return + } + + // Find the owner of this photo and assume currentUser is them for the remainder + // of this controller. + if user, err := models.GetUser(photo.UserID); err != nil { + session.FlashError(w, r, "Couldn't get the owner User for this photo!") + templates.Redirect(w, "/") + return + } else { + currentUser = user + } } // Confirm deletion? @@ -224,17 +239,9 @@ func Delete() http.HandlerFunc { session.Flash(w, r, "Photo deleted!") - // Whose photo gallery to redirect to? if admin editing a user's photo, - // go back to the owner's gallery instead of our own. - if photo.UserID != currentUser.ID { - if owner, err := models.GetUser(photo.UserID); err == nil { - templates.Redirect(w, "/photo/u/"+owner.Username) - return - } - } - // Return the user to their gallery. templates.Redirect(w, "/photo/u/"+currentUser.Username) + return } var vars = map[string]interface{}{ diff --git a/web/templates/account/dashboard.html b/web/templates/account/dashboard.html index d94d106..f4072f7 100644 --- a/web/templates/account/dashboard.html +++ b/web/templates/account/dashboard.html @@ -520,7 +520,7 @@ {{else}} - + {{end}} diff --git a/web/templates/forum/thread.html b/web/templates/forum/thread.html index 6bf74a1..6c2a34e 100644 --- a/web/templates/forum/thread.html +++ b/web/templates/forum/thread.html @@ -220,7 +220,7 @@ {{else}}
- +
{{end}} {{end}} diff --git a/web/templates/photo/gallery.html b/web/templates/photo/gallery.html index 4b5fac5..42df558 100644 --- a/web/templates/photo/gallery.html +++ b/web/templates/photo/gallery.html @@ -463,7 +463,7 @@ {{else}} - + {{end}} @@ -575,7 +575,7 @@ - + {{end}}