website/pkg/controller/account/dashboard.go

109 lines
3.2 KiB
Go
Raw Permalink Normal View History

package account
import (
"net/http"
2022-08-26 04:21:46 +00:00
"code.nonshy.com/nonshy/website/pkg/config"
2023-08-20 02:11:33 +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"
)
// User dashboard or landing page (/me).
func Dashboard() http.HandlerFunc {
tmpl := templates.Must("account/dashboard.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
currentUser, err := session.CurrentUser(r)
if err != nil {
http.Error(w, "Couldn't get currentUser", http.StatusInternalServerError)
return
}
// Mark all notifications read?
if r.Method == http.MethodPost {
switch r.FormValue("intent") {
case "read-notifications":
if err := models.MarkNotificationsRead(currentUser); err != nil {
session.FlashError(w, r, "Error marking your notifications as read: %s", err)
} else {
session.Flash(w, r, "All of your notifications have been marked as 'read!'")
}
case "clear-all":
if err := models.ClearAllNotifications(currentUser); err != nil {
session.FlashError(w, r, "Error clearing your notifications: %s", err)
} else {
session.Flash(w, r, "All of your notifications have been cleared!")
}
default:
session.FlashError(w, r, "Unknown intent.")
}
templates.Redirect(w, r.URL.Path)
return
}
// Get our notifications.
pager := &models.Pagination{
Page: 1,
PerPage: config.PageSizeDashboardNotifications,
Sort: "created_at desc",
}
pager.ParsePage(r)
notifs, err := models.PaginateNotifications(currentUser, pager)
if err != nil {
session.FlashError(w, r, "Couldn't get your notifications: %s", err)
}
// Map our notifications.
notifMap := models.MapNotifications(notifs)
models.SetUserRelationshipsInNotifications(currentUser, notifs)
2023-06-16 02:06:16 +00:00
// Map likes for in-line like buttons on (other peoples) photos.
// NOTE: comments can be trickier since the Notification.table_name='photos' if the comment is on a photo,
// hard to create a LikesMap for the specific comment ID.
var photoIDs = []uint64{}
for _, notif := range notifs {
if notif.TableName == "photos" {
photoIDs = append(photoIDs, notif.TableID)
}
}
// Restricted profile warnings.
var (
isShyUser = currentUser.IsShy()
photoTypes = currentUser.DistinctPhotoTypes()
_, hasPublic = photoTypes[models.PhotoPublic]
)
2023-08-20 02:11:33 +00:00
// Geolocation/Who's Nearby: if the current user uses GeoIP, update
// their coordinates now.
2023-08-20 04:09:23 +00:00
myLocation, err := models.RefreshGeoIP(currentUser.ID, r)
if err != nil {
2023-08-20 02:11:33 +00:00
log.Error("RefreshGeoIP: %s", err)
}
var vars = map[string]interface{}{
"Notifications": notifs,
"NotifMap": notifMap,
"Pager": pager,
// Show a warning to 'restricted' profiles who are especially private.
"IsShyUser": isShyUser,
"HasPublicPhoto": hasPublic,
2023-06-16 02:06:16 +00:00
"PhotoLikeMap": models.MapLikes(currentUser, "photos", photoIDs),
2023-08-20 04:09:23 +00:00
// Who's Nearby stats.
"MyLocation": myLocation,
2023-09-19 00:22:50 +00:00
// Check 2FA enabled status for new feature announcement.
"TwoFactorEnabled": models.Get2FA(currentUser.ID).Enabled,
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}