package mutelist import ( "net/http" "strings" "code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/templates" ) // Muted User list: view the list of muted accounts. func MuteList() http.HandlerFunc { tmpl := templates.Must("account/mute_list.html") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { currentUser, err := session.CurrentUser(r) if err != nil { session.FlashError(w, r, "Unexpected error: could not get currentUser.") templates.Redirect(w, "/") return } // Get our mutelist. pager := &models.Pagination{ PerPage: config.PageSizeMuteList, Sort: "updated_at desc", } pager.ParsePage(r) muted, err := models.PaginateMuteList(currentUser, pager) if err != nil { session.FlashError(w, r, "Couldn't paginate mute list: %s", err) templates.Redirect(w, "/") return } var vars = map[string]interface{}{ "MutedUsers": muted, "Pager": pager, } if err := tmpl.Execute(w, r, vars); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }) } // AddUser to manually add someone to your mute list. func AddUser() http.HandlerFunc { tmpl := templates.Must("account/mute_list_add.html") return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Query parameters. var ( username = strings.ToLower(r.FormValue("username")) next = r.FormValue("next") context = models.MutedUserContext(r.FormValue("context")) listName = "Site Gallery" // TODO: more as contexts are added ) // Validate the Next URL. if !strings.HasPrefix(next, "/") { next = "/users/muted" } // Validate acceptable contexts. if !models.IsValidMuteUserContext(context) { session.FlashError(w, r, "Unsupported mute context.") templates.Redirect(w, next) return } // Get the target user. user, err := models.FindUser(username) if err != nil { session.FlashError(w, r, "User Not Found") templates.Redirect(w, next) return } vars := map[string]interface{}{ "User": user, "Next": next, "Context": context, "MuteListName": listName, } if err := tmpl.Execute(w, r, vars); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }) } // MuteUser controller: POST endpoint to add a mute. func MuteUser() http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Form fields var ( username = strings.ToLower(r.PostFormValue("username")) next = r.PostFormValue("next") context = models.MutedUserContext(r.PostFormValue("context")) listName = "Site Gallery" // TODO: more as contexts are added unmute = r.PostFormValue("unmute") == "true" ) // Validate the Next URL. if !strings.HasPrefix(next, "/") { next = "/users/muted" } // Validate acceptable contexts. if !models.IsValidMuteUserContext(context) { session.FlashError(w, r, "Unsupported mute context.") templates.Redirect(w, "/") return } // Get the current user. currentUser, err := session.CurrentUser(r) if err != nil { session.FlashError(w, r, "Couldn't get CurrentUser: %s", err) templates.Redirect(w, "/") return } // Get the target user. user, err := models.FindUser(username) if err != nil { session.FlashError(w, r, "User Not Found") templates.Redirect(w, next) return } // Unmuting? if unmute { if err := models.RemoveMutedUser(currentUser.ID, user.ID, context); err != nil { session.FlashError(w, r, "Couldn't unmute this user: %s.", err) } else { session.Flash(w, r, "You have removed %s from your %s mute list.", user.Username, listName) // Log the change. models.LogDeleted(currentUser, nil, "muted_users", user.ID, "Unmuted user "+user.Username+" from "+listName+".", nil) } templates.Redirect(w, next) return } // Can't mute yourself. if currentUser.ID == user.ID { session.FlashError(w, r, "You can't mute yourself!") templates.Redirect(w, next) return } // Mute the target user. if err := models.AddMutedUser(currentUser.ID, user.ID, context); err != nil { session.FlashError(w, r, "Couldn't mute this user: %s.", err) } else { session.Flash(w, r, "You have added %s to your %s mute list.", user.Username, listName) // Log the change. models.LogCreated(currentUser, "muted_users", user.ID, "Mutes user "+user.Username+" on list "+listName+".") } templates.Redirect(w, next) }) }