website/pkg/controller/forum/subscribe.go
Noah Petherbridge ed4a9f8c89 User Forums: Newest Tab, Moderators
* The "Newest" tab of the forum is updated with new filter options.
  * Which forums: All, Official, Community, My List
  * Show: By threads, All posts
  * The option for "Which forums" is saved in the user's preferences and set as
    their default on future visits, similar to the Site Gallery "Whose photos"
    option.
  * So users can subscribe to their favorite forums and always get their latest
    posts easily while filtering out the rest.
* Forum Moderators
  * Add the ability to add and remove moderators for your forum.
  * Users are notified when they are added as a moderator.
  * Moderators can opt themselves out by unfollowing the forum.
* ForumMembership: add unique constraint on user_id,forum_id.
2024-08-21 21:53:35 -07:00

76 lines
2.0 KiB
Go

package forum
import (
"net/http"
"code.nonshy.com/nonshy/website/pkg/log"
"code.nonshy.com/nonshy/website/pkg/models"
"code.nonshy.com/nonshy/website/pkg/session"
"code.nonshy.com/nonshy/website/pkg/templates"
)
// Subscribe to a forum, adding it to your bookmark list.
func Subscribe() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Parse the path parameters
var (
fragment = r.FormValue("fragment")
forum *models.Forum
intent = r.FormValue("intent")
)
// Look up the forum by its fragment.
if found, err := models.ForumByFragment(fragment); err != nil {
templates.NotFoundPage(w, r)
return
} else {
forum = found
}
// Get the current user.
currentUser, err := session.CurrentUser(r)
if err != nil {
session.FlashError(w, r, "Couldn't get current user: %s", err)
templates.Redirect(w, "/")
return
}
// Is it a private forum?
if forum.Private && !currentUser.IsAdmin {
templates.NotFoundPage(w, r)
return
}
switch intent {
case "follow":
_, err := models.CreateForumMembership(currentUser, forum)
if err != nil {
session.FlashError(w, r, "Couldn't follow this forum: %s", err)
} else {
session.Flash(w, r, "You have added %s to your forum list.", forum.Title)
}
case "unfollow":
fm, err := models.GetForumMembership(currentUser, forum)
if err == nil {
// Were we a moderator previously? If so, revoke the notification about it.
if fm.IsModerator {
if err := models.RemoveSpecificNotification(currentUser.ID, models.NotificationForumModerator, "forums", forum.ID); err != nil {
log.Error("User unsubscribed from forum and couldn't remove their moderator notification: %s", err)
}
}
err = fm.Delete()
if err != nil {
session.FlashError(w, r, "Couldn't delete your forum membership: %s", err)
}
}
session.Flash(w, r, "You have removed %s from your forum list.", forum.Title)
default:
session.Flash(w, r, "Unknown intent.")
}
templates.Redirect(w, "/f/"+fragment)
})
}