2024-08-21 04:26:53 +00:00
|
|
|
package forum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-08-22 04:53:35 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/log"
|
2024-08-21 04:26:53 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch intent {
|
|
|
|
case "follow":
|
2024-08-22 06:06:06 +00:00
|
|
|
// Is it a private forum?
|
|
|
|
if forum.Private && !currentUser.IsAdmin {
|
|
|
|
templates.NotFoundPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
_, 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 {
|
2024-08-22 04:53:35 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:26:53 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|