...

Source file src/codeberg.org/tslocum/sriracha/internal/server/server_preference.go

Documentation: codeberg.org/tslocum/sriracha/internal/server

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  	"slices"
     6  	"strings"
     7  
     8  	"codeberg.org/tslocum/sriracha/internal/database"
     9  	. "codeberg.org/tslocum/sriracha/util"
    10  )
    11  
    12  func (s *Server) servePreference(data *templateData, db *database.DB, w http.ResponseWriter, r *http.Request) {
    13  	data.Template = "manage_preference"
    14  	if r.Method == http.MethodPost {
    15  		switch FormString(r, "action") {
    16  		case "style":
    17  			var style string
    18  			if FormString(r, "style") == "burichan" || FormString(r, "style") == "sriracha" {
    19  				style = FormString(r, "style")
    20  			}
    21  			db.UpdateAccountStyle(data.Account.ID, style)
    22  
    23  			http.Redirect(w, r, "/sriracha/preference/", http.StatusFound)
    24  			return
    25  		case "locale":
    26  			locale := FormString(r, "locale")
    27  			if locale != "" && !slices.Contains(s.opt.LocalesSorted, locale) {
    28  				locale = ""
    29  			}
    30  			db.UpdateAccountLocale(data.Account.ID, locale)
    31  
    32  			http.Redirect(w, r, "/sriracha/preference/", http.StatusFound)
    33  			return
    34  		case "password":
    35  			oldPass := r.FormValue("old")
    36  			newPass := r.FormValue("new")
    37  			confirmPass := r.FormValue("confirmation")
    38  			if strings.TrimSpace(oldPass) == "" || strings.TrimSpace(newPass) == "" || strings.TrimSpace(confirmPass) == "" {
    39  				data.ManageError("All fields are required")
    40  				return
    41  			}
    42  
    43  			if newPass != confirmPass {
    44  				data.ManageError("New passwords do not match")
    45  				return
    46  			}
    47  
    48  			match := db.LoginAccount(data.Account.Username, oldPass)
    49  			if match == nil {
    50  				data.ManageError("Current password is incorrect")
    51  				return
    52  			}
    53  
    54  			db.UpdateAccountPassword(match.ID, newPass)
    55  
    56  			http.Redirect(w, r, "/sriracha/", http.StatusFound)
    57  			return
    58  		}
    59  	}
    60  }
    61  

View as plain text