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 {
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)
}

View File

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

View File

@ -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)

View File

@ -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
}

View File

@ -7,8 +7,8 @@
<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>

11
web.go
View File

@ -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)
}