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 } switch intent { case "follow": // Is it a private forum? if forum.Private && !currentUser.IsAdmin { templates.NotFoundPage(w, r) return } _, 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) }) }