2022-08-11 03:59:59 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
2022-08-14 05:44:57 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/session"
|
2022-08-11 03:59:59 +00:00
|
|
|
"git.kirsle.net/apps/gosocial/pkg/templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-14 05:44:57 +00:00
|
|
|
// Get the current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get CurrentUser: %s", err)
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 03:59:59 +00:00
|
|
|
// Find this user.
|
|
|
|
user, err := models.FindUser(username)
|
|
|
|
if err != nil {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-11 03:59:59 +00:00
|
|
|
vars := map[string]interface{}{
|
2022-08-14 05:44:57 +00:00
|
|
|
"User": user,
|
|
|
|
"IsFriend": models.FriendStatus(currentUser.ID, 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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|