website/pkg/controller/friend/friends.go

67 lines
1.8 KiB
Go
Raw Normal View History

2022-08-14 05:44:57 +00:00
package friend
import (
"net/http"
2022-08-26 04:21:46 +00:00
"code.nonshy.com/nonshy/website/pkg/config"
2023-10-23 02:57:18 +00:00
"code.nonshy.com/nonshy/website/pkg/log"
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-14 05:44:57 +00:00
)
// Friends list and pending friend request endpoint.
func Friends() http.HandlerFunc {
tmpl := templates.Must("friend/friends.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
view = r.FormValue("view")
isRequests = view == "requests"
isPending = view == "pending"
2023-10-23 02:57:18 +00:00
isIgnored = view == "ignored"
)
2022-08-14 05:44:57 +00:00
currentUser, err := session.CurrentUser(r)
if err != nil {
session.FlashError(w, r, "Unexpected error: could not get currentUser.")
templates.Redirect(w, "/")
return
}
// Get our friends.
pager := &models.Pagination{
PerPage: config.PageSizeFriends,
Sort: "updated_at desc",
}
pager.ParsePage(r)
2023-10-23 02:57:18 +00:00
friends, err := models.PaginateFriends(currentUser, isRequests, isPending, isIgnored, pager)
2022-08-14 05:44:57 +00:00
if err != nil {
session.FlashError(w, r, "Couldn't paginate friends: %s", err)
templates.Redirect(w, "/")
return
}
// Inject relationship booleans.
models.SetUserRelationships(currentUser, friends)
2023-10-23 02:57:18 +00:00
// Ignored friend request count.
ignoredFriendCount, err := models.CountIgnoredFriendRequests(currentUser.ID)
if err != nil {
log.Error("Ignored Friend Request Count (%s): %s", currentUser.Username, err)
}
2022-08-14 05:44:57 +00:00
var vars = map[string]interface{}{
2023-10-23 02:57:18 +00:00
"IsRequests": isRequests,
"IsPending": isPending,
"IsIgnored": isIgnored,
"Friends": friends,
"IgnoredFriendCount": ignoredFriendCount,
"Pager": pager,
2022-08-14 05:44:57 +00:00
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}