flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 6c98078eb024bc7a15bdcc7b33ab0314148e1134
parent 345c439f7abc16601227fb4bef930f34ad5ab287
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  9 Jan 2021 14:41:07 -0800

Add domain to form

Also fix a bug where you cant update email (oops)

Diffstat:
Mdb.go | 20+++++++++++---------
Mhttp.go | 18++++++++++++++----
Mtemplates/me.html | 4++--
3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/db.go b/db.go @@ -71,12 +71,14 @@ func fileFromPath(fullPath string) File { } type User struct { - Username string - Email string - Active bool - Admin bool - CreatedAt int // timestamp - Reference string + Username string + Email string + Active bool + Admin bool + CreatedAt int // timestamp + Reference string + Domain string + DomainEnabled bool } func getActiveUserNames() ([]string, error) { @@ -99,8 +101,8 @@ func getActiveUserNames() ([]string, error) { func getUserByName(username string) (*User, error) { var user User - row := DB.QueryRow(`SELECT username, email, active, admin, created_at, reference from user WHERE username = ?`, username) - err := row.Scan(&user.Username, &user.Email, &user.Active, &user.Admin, &user.CreatedAt, &user.Reference) + row := DB.QueryRow(`SELECT username, email, active, admin, created_at, reference, domain, domain_enabled from user WHERE username = ?`, username) + err := row.Scan(&user.Username, &user.Email, &user.Active, &user.Admin, &user.CreatedAt, &user.Reference, &user.Domain, &user.DomainEnabled) if err != nil { return nil, err } @@ -180,7 +182,7 @@ func createTablesIfDNE() { active boolean NOT NULL DEFAULT false, admin boolean NOT NULL DEFAULT false, created_at INTEGER DEFAULT (strftime('%s', 'now')), - domain TEXT, + domain TEXT NOT NULL default "", domain_enabled BOOLEAN NOT NULL DEFAULT false ); diff --git a/http.go b/http.go @@ -283,10 +283,10 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { type pageData struct { Config Config AuthUser AuthUser - Email string + MyUser *User Errors []string } - data := pageData{c, user, me.Email, nil} + data := pageData{c, user, me, nil} if r.Method == "GET" { err := t.ExecuteTemplate(w, "me.html", data) @@ -298,10 +298,19 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { newUsername := r.Form.Get("username") errors := []string{} newEmail := r.Form.Get("email") + newDomain := r.Form.Get("domain") newUsername = strings.ToLower(newUsername) var err error + if newDomain != me.Domain { + _, err = DB.Exec("update user set domain = ? where username = ?", newDomain, me.Username) // TODO use transaction + if err != nil { + errors = append(errors, err.Error()) + } else { + log.Printf("Changed domain for %s from %s to %s", authUser, me.Domain, newDomain) + } + } if newEmail != me.Email { - _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Email) + _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Username) if err != nil { // TODO better error not sql errors = append(errors, err.Error()) @@ -325,7 +334,8 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { user = newGetAuthUser(r) data.Errors = errors data.AuthUser = user - data.Email = newEmail + data.MyUser.Email = newEmail + data.MyUser.Domain = newDomain _ = t.ExecuteTemplate(w, "me.html", data) } } diff --git a/templates/me.html b/templates/me.html @@ -16,13 +16,13 @@ </div> <div> <label for="email">Email</label><br> - <input id="email" name="email" size="32" type="text" value="{{.Email}}" /> + <input id="email" name="email" size="32" type="text" value="{{.MyUser.Email}}" /> </div> <div> <details> <summary><label for="domain">Custom domain</label></summary> <em>For more information on setting up a custom domain, see <a href="https://admin.flounder.online/custom-domains.gmi">Custom Domains</a></em> - <input id="domain" name="domain" size="32" type="text" /> enabled/disabled + <input id="domain" name="domain" size="32" type="text" value="{{.MyUser.Domain}}"/> {{if ne .MyUser.Domain ""}}{{if .MyUser.DomainEnabled}}✅{{else}} ❌{{end}}{{end}} </details> </div> <div class="error">{{ range .Errors}}{{.}}<br>{{end}} </div>