2022-08-11 03:59:59 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-08-27 04:32:26 +00:00
|
|
|
"net/url"
|
2022-08-11 03:59:59 +00:00
|
|
|
"regexp"
|
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-11 03:59:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ProfileRegexp = regexp.MustCompile(`^/u/([^@]+?)$`)
|
|
|
|
|
|
|
|
// User profile page (/u/username)
|
|
|
|
func Profile() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("account/profile.html")
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Parse the username out of the URL parameters.
|
|
|
|
var username string
|
|
|
|
m := ProfileRegexp.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m != nil {
|
|
|
|
username = m[1]
|
|
|
|
}
|
|
|
|
|
2022-08-27 04:32:26 +00:00
|
|
|
// Find this user.
|
|
|
|
user, err := models.FindUser(username)
|
2022-08-14 05:44:57 +00:00
|
|
|
if err != nil {
|
2022-08-27 04:32:26 +00:00
|
|
|
templates.NotFoundPage(w, r)
|
2022-08-14 05:44:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-30 03:00:15 +00:00
|
|
|
// Forcing an external view? (preview of logged-out profile view for visibility=external accounts)
|
|
|
|
if r.FormValue("view") == "external" {
|
|
|
|
vars := map[string]interface{}{
|
|
|
|
"User": user,
|
|
|
|
"IsPrivate": true,
|
|
|
|
"IsExternalView": true,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current user (if logged in). If not, check for external view.
|
2022-08-27 04:32:26 +00:00
|
|
|
currentUser, err := session.CurrentUser(r)
|
2022-08-11 03:59:59 +00:00
|
|
|
if err != nil {
|
2022-08-27 04:32:26 +00:00
|
|
|
// The viewer is not logged in, bail now with the basic profile page. If this
|
2022-08-30 03:00:15 +00:00
|
|
|
// user doesn't allow external viewers, redirect to login page.
|
|
|
|
if user.Visibility != models.UserVisibilityExternal {
|
2022-08-27 04:32:26 +00:00
|
|
|
session.FlashError(w, r, "You must be signed in to view this page.")
|
|
|
|
templates.Redirect(w, "/login?next="+url.QueryEscape(r.URL.String()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]interface{}{
|
|
|
|
"User": user,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-08-11 03:59:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 00:29:39 +00:00
|
|
|
var isSelf = currentUser.ID == user.ID
|
|
|
|
|
2022-08-14 23:27:57 +00:00
|
|
|
// Banned or disabled? Only admin can view then.
|
|
|
|
if user.Status != models.UserStatusActive && !currentUser.IsAdmin {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
// Is either one blocking?
|
|
|
|
if models.IsBlocking(currentUser.ID, user.ID) && !currentUser.IsAdmin {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 00:29:39 +00:00
|
|
|
// Are they friends? And/or is this user private?
|
|
|
|
var (
|
|
|
|
isFriend = models.FriendStatus(currentUser.ID, user.ID)
|
|
|
|
isPrivate = !currentUser.IsAdmin && !isSelf && user.Visibility == models.UserVisibilityPrivate && isFriend != "approved"
|
|
|
|
)
|
|
|
|
|
2022-08-25 04:17:34 +00:00
|
|
|
// Get Likes for this profile.
|
|
|
|
likeMap := models.MapLikes(currentUser, "users", []uint64{user.ID})
|
|
|
|
|
2022-08-11 03:59:59 +00:00
|
|
|
vars := map[string]interface{}{
|
2022-08-26 02:58:43 +00:00
|
|
|
"User": user,
|
|
|
|
"LikeMap": likeMap,
|
|
|
|
"IsFriend": isFriend,
|
|
|
|
"IsPrivate": isPrivate,
|
|
|
|
"PhotoCount": models.CountPhotos(user.ID),
|
2022-08-11 03:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|