Noah
aa8d719fc4
* Add ability to (un)subscribe from comment threads on Forums and Photos. * Creating a forum post, replying to a post or adding a comment to a photo automatically subscribes you to be notified when somebody else adds a comment to the thing later. * At the top of each comment thread is a link to disable or re-enable your subscription. You can join a subscription without even needing to comment. If you click to disable notifications, they stay disabled even if you add another comment later.
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
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
|
|
}
|
|
}
|
|
}
|
|
|
|
// Find the photo's owner.
|
|
user, err := models.GetUser(photo.UserID)
|
|
if err != nil {
|
|
templates.NotFoundPage(w, r)
|
|
return
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
var isOwnPhoto = currentUser.ID == user.ID
|
|
|
|
// Is either one blocking?
|
|
if models.IsBlocking(currentUser.ID, user.ID) && !currentUser.IsAdmin {
|
|
templates.NotFoundPage(w, r)
|
|
return
|
|
}
|
|
|
|
// Is this user private and we're not friends?
|
|
var (
|
|
areFriends = models.AreFriends(user.ID, currentUser.ID)
|
|
isPrivate = user.Visibility == models.UserVisibilityPrivate && !areFriends
|
|
)
|
|
if isPrivate && !currentUser.IsAdmin && !isOwnPhoto {
|
|
session.FlashError(w, r, "This user's profile page and photo gallery are private.")
|
|
templates.Redirect(w, "/u/"+user.Username)
|
|
return
|
|
}
|
|
|
|
// 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.
|
|
comments, err := models.ListComments("photos", photo.ID)
|
|
if err != nil {
|
|
log.Error("Couldn't list comments for photo %d: %s", photo.ID, err)
|
|
}
|
|
|
|
// Is the current user subscribed to notifications on this thread?
|
|
_, isSubscribed := models.IsSubscribed(currentUser, "photos", photo.ID)
|
|
|
|
var vars = map[string]interface{}{
|
|
"IsOwnPhoto": currentUser.ID == user.ID,
|
|
"User": user,
|
|
"Photo": photo,
|
|
"LikeMap": likeMap,
|
|
"CommentMap": commentMap,
|
|
"Comments": comments,
|
|
"IsSubscribed": isSubscribed,
|
|
}
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
})
|
|
}
|