From 36d7ebe8ae896dac351b71997524e971444fad2a Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 29 Oct 2019 01:00:38 -0400 Subject: [PATCH] convert user options to json before we add any more --- database.go | 5 ++++- honk.go | 20 ++++++++++++-------- upgradedb.go | 39 ++++++++++++++++++++++++++++++++++++++- util.go | 2 +- views/account.html | 4 ++-- web.go | 11 +++++++---- 6 files changed, 64 insertions(+), 17 deletions(-) diff --git a/database.go b/database.go index 20b69de..d1c2945 100644 --- a/database.go +++ b/database.go @@ -43,7 +43,10 @@ func userfromrow(row *sql.Row) (*WhatAbout, error) { } 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) } diff --git a/honk.go b/honk.go index b147ef5..1245132 100644 --- a/honk.go +++ b/honk.go @@ -27,14 +27,18 @@ import ( ) 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 { diff --git a/upgradedb.go b/upgradedb.go index 4bb2dc5..d43babc 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -24,7 +24,7 @@ import ( "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 @@ func upgradedb() { 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) diff --git a/util.go b/util.go index db12bb1..acf5e9e 100644 --- a/util.go +++ b/util.go @@ -303,7 +303,7 @@ func createuser(db *sql.DB, r *bufio.Reader) error { 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 } diff --git a/views/account.html b/views/account.html index 0523d12..460325e 100644 --- a/views/account.html +++ b/views/account.html @@ -7,8 +7,8 @@

about me:

-

- +

+

diff --git a/web.go b/web.go index 127a6a9..f6a7d35 100644 --- a/web.go +++ b/web.go @@ -53,7 +53,7 @@ func getuserstyle(u *login.UserInfo) template.CSS { 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) }