website/pkg/controller/account/user_note.go
Noah Petherbridge 481bd0ae61 Deactivate Account; Friends Lists on Profiles
* Add a way for users to temporarily deactivate their accounts, in a
  recoverable way should they decide to return later.
* A deactivated account may log in but have limited options: to
  reactivate their account, permanently delete it, or log out.
* Fix several bugs around the display of comments, messages and
  forum threads for disabled, banned, or blocked users:
  * Messages (inbox and sentbox) will be hidden and the unread indicator
    will not count unread messages the user can't access.
  * Comments on photos and forum posts are hidden, and top-level threads
    on the "Newest" tab will show "[unavailable]" for their text and
    username.
  * Your historical notifications will hide users who are blocked, banned
    or disabled.
* Add a "Friends" tab to user profile pages, to see other users' friends.
  * The page is Certification Required so non-cert users can't easily
    discover any members on the site.
2023-10-22 15:02:24 -07:00

162 lines
4.3 KiB
Go

package account
import (
"net/http"
"net/url"
"regexp"
"code.nonshy.com/nonshy/website/pkg/config"
"code.nonshy.com/nonshy/website/pkg/middleware"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/templates"
)
var NotesURLRegexp = regexp.MustCompile(`^/notes/u/([^@]+?)$`)
// User notes page (/notes/u/username)
func UserNotes() http.HandlerFunc {
tmpl := templates.Must("account/user_notes.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Parse the username out of the URL parameters.
var username string
m := NotesURLRegexp.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
}
// Get the current user.
currentUser, err := session.CurrentUser(r)
if err != nil {
session.FlashError(w, r, "You must be signed in to view this page.")
templates.Redirect(w, "/login?next="+url.QueryEscape(r.URL.String()))
return
}
// Is the site under a Maintenance Mode restriction?
if middleware.MaintenanceMode(currentUser, w, r) {
return
}
// Banned or disabled? Only admin can view then.
if user.Status != models.UserStatusActive && !currentUser.IsAdmin {
templates.NotFoundPage(w, r)
return
}
// Is either one blocking?
if models.IsBlocking(currentUser.ID, user.ID) && !currentUser.IsAdmin {
templates.NotFoundPage(w, r)
return
}
// Look up our current note about this person.
var myNote = models.GetNoteBetweenUsers(currentUser, user)
// Are we submitting a note?
if r.Method == http.MethodPost {
var message = r.FormValue("message")
// Update & save our note.
if message == "" {
// Delete it.
if err := myNote.Delete(); err != nil && err != models.ErrNoUserNoteToDelete {
session.FlashError(w, r, "Error deleting your note: %s", err)
} else if err == nil {
session.Flash(w, r, "Your note was deleted!")
}
} else {
// Update it.
myNote.Message = message
if err := myNote.Save(); err != nil {
session.FlashError(w, r, "Error saving your note: %s", err)
} else {
session.Flash(w, r, "Your notes have been saved!")
}
}
templates.Redirect(w, r.URL.Path)
return
}
// Admin view: paginate their feedback & reports.
var (
feedback = []*models.Feedback{}
otherNotes = []*models.UserNote{}
userMap = models.UserMap{}
notePager = &models.Pagination{
Page: 1,
PerPage: config.PageSizeAdminUserNotes,
Sort: "updated_at desc",
}
fbPager = &models.Pagination{
Page: 1,
PerPage: config.PageSizeAdminFeedbackNotesPage,
Sort: "created_at desc",
}
)
notePager.ParsePage(r)
fbPager.ParsePage(r)
if currentUser.IsAdmin {
// Paginate all notes written about this user.
if on, err := models.PaginateUserNotes(user, notePager); err != nil {
session.FlashError(w, r, "Paginating user notes: %s", err)
} else {
otherNotes = on
}
// Paginate feedback & reports.
if fb, err := models.PaginateFeedbackAboutUser(user, fbPager); err != nil {
session.FlashError(w, r, "Paginating feedback on this user: %s", err)
} else {
feedback = fb
}
// Map user IDs for the Feedback Reply-To line and the Note Givers for the paginated notes.
var userIDs = []uint64{}
for _, p := range feedback {
if p.UserID > 0 {
userIDs = append(userIDs, p.UserID)
}
}
for _, p := range otherNotes {
if p.UserID > 0 {
userIDs = append(userIDs, p.UserID)
}
}
if um, err := models.MapUsers(currentUser, userIDs); err != nil {
session.FlashError(w, r, "Couldn't map user IDs: %s", err)
} else {
userMap = um
}
}
vars := map[string]interface{}{
"User": user,
"PhotoCount": models.CountPhotosICanSee(user, currentUser),
"NoteCount": models.CountNotesAboutUser(currentUser, user),
"FriendCount": models.CountFriends(user.ID),
"MyNote": myNote,
// Admin concerns.
"Feedback": feedback,
"FeedbackPager": fbPager,
"OtherNotes": otherNotes,
"NotePager": notePager,
"UserMap": userMap,
}
if err := tmpl.Execute(w, r, vars); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}