convert user options to json before we add any more
Ted Unangst tedu@tedunangst.com
Tue, 29 Oct 2019 01:00:38 -0400
6 files changed,
64 insertions(+),
17 deletions(-)
M
database.go
→
database.go
@@ -43,7 +43,10 @@ return nil, err
} if user.ID > 0 { user.URL = fmt.Sprintf("https://%s/%s/%s", serverName, userSep, user.Name) - user.SkinnyCSS = strings.Contains(options, " skinny ") + err = unjsonify(options, &user.Options) + if err != nil { + log.Printf("error processing user options: %s", err) + } } else { user.URL = fmt.Sprintf("https://%s/%s", serverName, user.Name) }
M
honk.go
→
honk.go
@@ -27,14 +27,18 @@ "time"
) type WhatAbout struct { - ID int64 - Name string - Display string - About string - Key string - URL string - SkinnyCSS bool - SecKey *rsa.PrivateKey + ID int64 + Name string + Display string + About string + Key string + URL string + Options UserOptions + SecKey *rsa.PrivateKey +} + +type UserOptions struct { + SkinnyCSS bool `json:",omitempty"` } type KeyInfo struct {
M
upgradedb.go
→
upgradedb.go
@@ -24,7 +24,7 @@ "strings"
"time" ) -var myVersion = 30 +var myVersion = 31 func doordie(db *sql.DB, s string, args ...interface{}) { _, err := db.Exec(s, args...)@@ -317,6 +317,43 @@ doordie(db, "update honkers set owner = xid")
doordie(db, "update config set value = 30 where key = 'dbversion'") fallthrough case 30: + tx, err := db.Begin() + if err != nil { + log.Fatal(err) + } + rows, err := tx.Query("select userid, options from users") + if err != nil { + log.Fatal(err) + } + m := make(map[int64]string) + for rows.Next() { + var userid int64 + var options string + err = rows.Scan(&userid, &options) + if err != nil { + log.Fatal(err) + } + var uo UserOptions + uo.SkinnyCSS = strings.Contains(options, " skinny ") + m[userid], err = jsonify(uo) + if err != nil { + log.Fatal(err) + } + } + rows.Close() + for u, o := range m { + _, err = tx.Exec("update users set options = ? where userid = ?", o, u) + if err != nil { + log.Fatal(err) + } + } + err = tx.Commit() + if err != nil { + log.Fatal(err) + } + doordie(db, "update config set value = 31 where key = 'dbversion'") + fallthrough + case 31: default: log.Fatalf("can't upgrade unknown version %d", dbversion)
M
util.go
→
util.go
@@ -303,7 +303,7 @@ if err != nil {
return err } about := "what about me?" - _, err = db.Exec("insert into users (username, displayname, about, hash, pubkey, seckey, options) values (?, ?, ?, ?, ?, ?, ?)", name, name, about, hash, pubkey, seckey, "") + _, err = db.Exec("insert into users (username, displayname, about, hash, pubkey, seckey, options) values (?, ?, ?, ?, ?, ?, ?)", name, name, about, hash, pubkey, seckey, "{}") if err != nil { return err }
M
views/account.html
→
views/account.html
@@ -7,8 +7,8 @@ <form id="aboutform" action="/saveuser" method="POST">
<input type="hidden" name="CSRF" value="{{ .UserCSRF }}"> <p>about me: <p><textarea name="whatabout">{{ .User.About }}</textarea> -<p><span><label for="skinny">skinny layout:</label> -<input tabindex=1 type="checkbox" id="skinny" name="skinny" value="skinny" {{ if .User.SkinnyCSS }}checked{{ end }}><span></span></span> +<p><label class="button" for="skinny">skinny layout:</label> +<input tabindex=1 type="checkbox" id="skinny" name="skinny" value="skinny" {{ if .User.Options.SkinnyCSS }}checked{{ end }}><span></span> <p><button>update settings</button> </form> </div>
M
web.go
→
web.go
@@ -53,7 +53,7 @@ if u == nil {
return "" } user, _ := butwhatabout(u.Username) - if user.SkinnyCSS { + if user.Options.SkinnyCSS { return "main { max-width: 700px; }" } return ""@@ -929,11 +929,14 @@ func saveuser(w http.ResponseWriter, r *http.Request) {
whatabout := r.FormValue("whatabout") u := login.GetUserInfo(r) db := opendatabase() - options := "" + var options UserOptions if r.FormValue("skinny") == "skinny" { - options += " skinny " + options.SkinnyCSS = true + } + j, err := jsonify(options) + if err == nil { + _, err = db.Exec("update users set about = ?, options = ? where username = ?", whatabout, j, u.Username) } - _, err := db.Exec("update users set about = ?, options = ? where username = ?", whatabout, options, u.Username) if err != nil { log.Printf("error bouting what: %s", err) }