78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
|
package photo
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"regexp"
|
||
|
|
||
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
||
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
||
|
"git.kirsle.net/apps/gosocial/pkg/session"
|
||
|
"git.kirsle.net/apps/gosocial/pkg/templates"
|
||
|
)
|
||
|
|
||
|
var UserPhotosRegexp = regexp.MustCompile(`^/photo/u/([^@]+?)$`)
|
||
|
|
||
|
// UserPhotos controller (/photo/u/:username) to view a user's gallery or manage if it's yourself.
|
||
|
func UserPhotos() http.HandlerFunc {
|
||
|
tmpl := templates.Must("photo/user_photos.html")
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// Query params.
|
||
|
var (
|
||
|
viewStyle = r.FormValue("view") // cards (default), full
|
||
|
)
|
||
|
if viewStyle != "full" {
|
||
|
viewStyle = "cards"
|
||
|
}
|
||
|
|
||
|
// Parse the username out of the URL parameters.
|
||
|
var username string
|
||
|
m := UserPhotosRegexp.FindStringSubmatch(r.URL.Path)
|
||
|
if m != nil {
|
||
|
username = m[1]
|
||
|
}
|
||
|
|
||
|
// Find this user.
|
||
|
user, err := models.FindUser(username)
|
||
|
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 isOwnPhotos = currentUser.ID == user.ID
|
||
|
|
||
|
// What set of visibilities to query?
|
||
|
visibility := []models.PhotoVisibility{models.PhotoPublic}
|
||
|
if isOwnPhotos || currentUser.IsAdmin {
|
||
|
visibility = append(visibility, models.PhotoFriends, models.PhotoPrivate)
|
||
|
}
|
||
|
|
||
|
// Get the page of photos.
|
||
|
pager := &models.Pagination{
|
||
|
Page: 1,
|
||
|
PerPage: 8,
|
||
|
Sort: "created_at desc",
|
||
|
}
|
||
|
pager.ParsePage(r)
|
||
|
log.Error("Pager: %+v", pager)
|
||
|
photos, err := models.PaginateUserPhotos(user.ID, visibility, pager)
|
||
|
|
||
|
var vars = map[string]interface{}{
|
||
|
"IsOwnPhotos": currentUser.ID == user.ID,
|
||
|
"User": user,
|
||
|
"Photos": photos,
|
||
|
"Pager": pager,
|
||
|
"ViewStyle": viewStyle,
|
||
|
}
|
||
|
|
||
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|