convert user options to json before we add any more

This commit is contained in:
Ted Unangst 2019-10-29 01:00:38 -04:00
parent a324b33294
commit 36d7ebe8ae
6 changed files with 64 additions and 17 deletions

View File

@ -43,7 +43,10 @@ func userfromrow(row *sql.Row) (*WhatAbout, error) {
} }
if user.ID > 0 { if user.ID > 0 {
user.URL = fmt.Sprintf("https://%s/%s/%s", serverName, userSep, user.Name) 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 { } else {
user.URL = fmt.Sprintf("https://%s/%s", serverName, user.Name) user.URL = fmt.Sprintf("https://%s/%s", serverName, user.Name)
} }

View File

@ -33,10 +33,14 @@ type WhatAbout struct {
About string About string
Key string Key string
URL string URL string
SkinnyCSS bool Options UserOptions
SecKey *rsa.PrivateKey SecKey *rsa.PrivateKey
} }
type UserOptions struct {
SkinnyCSS bool `json:",omitempty"`
}
type KeyInfo struct { type KeyInfo struct {
keyname string keyname string
seckey *rsa.PrivateKey seckey *rsa.PrivateKey

View File

@ -24,7 +24,7 @@ import (
"time" "time"
) )
var myVersion = 30 var myVersion = 31
func doordie(db *sql.DB, s string, args ...interface{}) { func doordie(db *sql.DB, s string, args ...interface{}) {
_, err := db.Exec(s, args...) _, err := db.Exec(s, args...)
@ -317,6 +317,43 @@ func upgradedb() {
doordie(db, "update config set value = 30 where key = 'dbversion'") doordie(db, "update config set value = 30 where key = 'dbversion'")
fallthrough fallthrough
case 30: 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: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)

View File

@ -303,7 +303,7 @@ func createuser(db *sql.DB, r *bufio.Reader) error {
return err return err
} }
about := "what about me?" 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 { if err != nil {
return err return err
} }

View File

@ -7,8 +7,8 @@
<input type="hidden" name="CSRF" value="{{ .UserCSRF }}"> <input type="hidden" name="CSRF" value="{{ .UserCSRF }}">
<p>about me: <p>about me:
<p><textarea name="whatabout">{{ .User.About }}</textarea> <p><textarea name="whatabout">{{ .User.About }}</textarea>
<p><span><label for="skinny">skinny layout:</label> <p><label class="button" for="skinny">skinny layout:</label>
<input tabindex=1 type="checkbox" id="skinny" name="skinny" value="skinny" {{ if .User.SkinnyCSS }}checked{{ end }}><span></span></span> <input tabindex=1 type="checkbox" id="skinny" name="skinny" value="skinny" {{ if .User.Options.SkinnyCSS }}checked{{ end }}><span></span>
<p><button>update settings</button> <p><button>update settings</button>
</form> </form>
</div> </div>

11
web.go
View File

@ -53,7 +53,7 @@ func getuserstyle(u *login.UserInfo) template.CSS {
return "" return ""
} }
user, _ := butwhatabout(u.Username) user, _ := butwhatabout(u.Username)
if user.SkinnyCSS { if user.Options.SkinnyCSS {
return "main { max-width: 700px; }" return "main { max-width: 700px; }"
} }
return "" return ""
@ -929,11 +929,14 @@ func saveuser(w http.ResponseWriter, r *http.Request) {
whatabout := r.FormValue("whatabout") whatabout := r.FormValue("whatabout")
u := login.GetUserInfo(r) u := login.GetUserInfo(r)
db := opendatabase() db := opendatabase()
options := "" var options UserOptions
if r.FormValue("skinny") == "skinny" { 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 { if err != nil {
log.Printf("error bouting what: %s", err) log.Printf("error bouting what: %s", err)
} }