2022-08-27 02:50:33 +00:00
|
|
|
package photo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
// View photo controller to see the comment thread.
|
|
|
|
func View() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("photo/permalink.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Required query param: the photo ID.
|
|
|
|
var photo *models.Photo
|
|
|
|
if idStr := r.FormValue("id"); idStr == "" {
|
|
|
|
session.FlashError(w, r, "Missing photo ID parameter.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if idInt, err := strconv.Atoi(idStr); err != nil {
|
|
|
|
session.FlashError(w, r, "Invalid ID parameter.")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if found, err := models.GetPhoto(uint64(idInt)); err != nil {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
photo = found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the current user in case they are viewing their own page.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Unexpected error: couldn't get CurrentUser")
|
|
|
|
}
|
|
|
|
|
2024-09-26 05:46:33 +00:00
|
|
|
// Find the photo's owner.
|
|
|
|
user, err := models.GetUser(photo.UserID)
|
|
|
|
if err != nil {
|
2022-08-27 02:50:33 +00:00
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-26 05:46:33 +00:00
|
|
|
if ok, err := photo.CanBeSeenBy(currentUser); !ok {
|
|
|
|
log.Error("Photo %d can't be seen by %s: %s", photo.ID, currentUser.Username, err)
|
|
|
|
session.FlashError(w, r, "Photo Not Found")
|
|
|
|
templates.Redirect(w, "/")
|
2022-09-08 04:18:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-27 02:50:33 +00:00
|
|
|
// Get Likes information about these photos.
|
|
|
|
likeMap := models.MapLikes(currentUser, "photos", []uint64{photo.ID})
|
|
|
|
commentMap := models.MapCommentCounts("photos", []uint64{photo.ID})
|
|
|
|
|
|
|
|
// Get all the comments.
|
2023-09-17 06:07:32 +00:00
|
|
|
comments, err := models.ListComments(currentUser, "photos", photo.ID)
|
2022-08-27 02:50:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't list comments for photo %d: %s", photo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-08-30 03:00:15 +00:00
|
|
|
// Get the like map for these comments.
|
|
|
|
commentIDs := []uint64{}
|
|
|
|
for _, com := range comments {
|
|
|
|
commentIDs = append(commentIDs, com.ID)
|
|
|
|
}
|
|
|
|
commentLikeMap := models.MapLikes(currentUser, "comments", commentIDs)
|
|
|
|
|
2023-09-14 04:28:38 +00:00
|
|
|
// Get the summary of WHO liked this picture.
|
2023-09-18 05:28:21 +00:00
|
|
|
likeExample, likeRemainder, err := models.WhoLikes(currentUser, "photos", photo.ID)
|
2023-09-14 04:28:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("WhoLikes(photo %d): %s", photo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-09-09 04:52:50 +00:00
|
|
|
// Populate the user relationships in these comments.
|
|
|
|
models.SetUserRelationshipsInComments(currentUser, comments)
|
|
|
|
|
2022-08-27 18:42:48 +00:00
|
|
|
// Is the current user subscribed to notifications on this thread?
|
|
|
|
_, isSubscribed := models.IsSubscribed(currentUser, "photos", photo.ID)
|
|
|
|
|
2024-09-26 05:46:33 +00:00
|
|
|
// Mark this photo as "Viewed" by the user.
|
2024-09-28 19:45:20 +00:00
|
|
|
if err := photo.View(currentUser); err != nil {
|
2024-09-26 05:46:33 +00:00
|
|
|
log.Error("Update photo(%d) views: %s", photo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-08-27 02:50:33 +00:00
|
|
|
var vars = map[string]interface{}{
|
2022-08-30 03:00:15 +00:00
|
|
|
"IsOwnPhoto": currentUser.ID == user.ID,
|
|
|
|
"User": user,
|
|
|
|
"Photo": photo,
|
|
|
|
"LikeMap": likeMap,
|
|
|
|
"CommentMap": commentMap,
|
|
|
|
"Comments": comments,
|
|
|
|
"CommentLikeMap": commentLikeMap,
|
|
|
|
"IsSubscribed": isSubscribed,
|
2023-09-14 04:28:38 +00:00
|
|
|
|
|
|
|
// Details on who likes the photo.
|
|
|
|
"LikeExample": likeExample,
|
|
|
|
"LikeRemainder": likeRemainder,
|
|
|
|
"LikeTableName": "photos",
|
|
|
|
"LikeTableID": photo.ID,
|
2022-08-27 02:50:33 +00:00
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|