68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
|
package forum
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"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 {
|
||
|
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)
|
||
|
})
|
||
|
}
|