feat(db/userstore): add avatar, email, bgColor to user profile

This commit is contained in:
hexxa 2022-03-20 17:16:39 +08:00 committed by Hexxa
parent 5f31cfc867
commit 817ced61de
14 changed files with 69 additions and 110 deletions

View file

@ -3,6 +3,7 @@ package db
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"github.com/ihexxa/quickshare/src/db/sitestore" "github.com/ihexxa/quickshare/src/db/sitestore"
) )
@ -44,6 +45,18 @@ type Preferences struct {
LanPackURL string `json:"lanPackURL"` LanPackURL string `json:"lanPackURL"`
Lan string `json:"lan"` Lan string `json:"lan"`
Theme string `json:"theme"` Theme string `json:"theme"`
Avatar string `json:"avatar"`
Email string `json:"email"`
}
func ComparePreferences(p1, p2 *Preferences) bool {
return p1.CSSURL == p2.CSSURL &&
p1.LanPackURL == p2.LanPackURL &&
p1.Lan == p2.Lan &&
p1.Theme == p2.Theme &&
p1.Avatar == p2.Avatar &&
p1.Email == p2.Email &&
reflect.DeepEqual(p1.Bg, p2.Bg)
} }
type User struct { type User struct {

View file

@ -59,109 +59,6 @@ type FileInfoStore struct {
boltdb boltdbpvd.BoltProvider boltdb boltdbpvd.BoltProvider
} }
func migrate(fi *FileInfoStore) error {
ver := "v0"
schemaVer, ok := fi.store.GetStringIn(InitNs, SchemaVerKey)
if ok {
ver = schemaVer
}
switch ver {
case "v0":
// add ShareID to FileInfos
infoStrs, err := fi.store.ListStringsIn(db.InfoNs)
if err != nil {
return err
}
type FileInfoV0 struct {
IsDir bool `json:"isDir"`
Shared bool `json:"shared"`
Sha1 string `json:"sha1"`
}
infoV0 := &FileInfoV0{}
for itemPath, infoStr := range infoStrs {
err = json.Unmarshal([]byte(infoStr), infoV0)
if err != nil {
return fmt.Errorf("list sharing error: %w", err)
}
shareID := ""
if infoV0.IsDir && infoV0.Shared {
dirShareID, err := fi.getShareID(itemPath)
if err != nil {
return err
}
shareID = dirShareID
err = fi.store.SetStringIn(db.ShareIDNs, shareID, itemPath)
if err != nil {
return err
}
}
newInfo := &db.FileInfo{
IsDir: infoV0.IsDir,
Shared: infoV0.Shared,
ShareID: shareID,
Sha1: infoV0.Sha1,
}
if err = fi.SetInfo(itemPath, newInfo); err != nil {
return err
}
}
err = fi.store.SetStringIn(InitNs, SchemaVerKey, SchemaV1)
if err != nil {
return err
}
case "v1":
// add size to file info
infoStrs, err := fi.store.ListStringsIn(db.InfoNs)
if err != nil {
return err
}
type FileInfoV1 struct {
IsDir bool `json:"isDir"`
Shared bool `json:"shared"`
Sha1 string `json:"sha1"`
ShareID string `json:"shareID"` // for short url
}
infoV1 := &FileInfoV1{}
for itemPath, infoStr := range infoStrs {
err = json.Unmarshal([]byte(infoStr), infoV1)
if err != nil {
return fmt.Errorf("list sharing error: %w", err)
}
newInfo := &db.FileInfo{
IsDir: infoV1.IsDir,
Shared: infoV1.Shared,
ShareID: infoV1.ShareID,
Sha1: infoV1.Sha1,
Size: 0, // need to run an async task to refresh this
}
if err = fi.SetInfo(itemPath, newInfo); err != nil {
return err
}
}
err = fi.store.SetStringIn(InitNs, SchemaVerKey, db.SchemaV2)
if err != nil {
return err
}
case db.SchemaV2:
// no need to migrate
default:
return fmt.Errorf("file info: unknown schema version (%s)", ver)
}
return nil
}
func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) { func NewFileInfoStore(store kvstore.IKVStore) (*FileInfoStore, error) {
var err error var err error
for _, nsName := range []string{ for _, nsName := range []string{

View file

@ -41,6 +41,7 @@ type BgConfig struct {
Repeat string `json:"repeat" yaml:"repeat"` Repeat string `json:"repeat" yaml:"repeat"`
Position string `json:"position" yaml:"position"` Position string `json:"position" yaml:"position"`
Align string `json:"align" yaml:"align"` Align string `json:"align" yaml:"align"`
BgColor string `json:"bgColor" yaml:"bgColor"`
} }
type SiteConfig struct { type SiteConfig struct {

View file

@ -22,6 +22,7 @@ func TestSiteStore(t *testing.T) {
Repeat: "no-repeat", Repeat: "no-repeat",
Position: "fixed", Position: "fixed",
Align: "center", Align: "center",
BgColor: "#ccc",
}, },
}, },
} }
@ -62,6 +63,7 @@ func TestSiteStore(t *testing.T) {
Repeat: "", Repeat: "",
Position: "", Position: "",
Align: "", Align: "",
BgColor: "",
}, },
}, },
}) })

View file

@ -40,11 +40,14 @@ var (
Repeat: "no-repeat", Repeat: "no-repeat",
Position: "center", Position: "center",
Align: "fixed", Align: "fixed",
BgColor: "#ccc",
}, },
CSSURL: "", CSSURL: "",
LanPackURL: "", LanPackURL: "",
Lan: "en_US", Lan: "en_US",
Theme: "light", Theme: "light",
Avatar: "",
Email: "",
} }
) )
@ -99,10 +102,12 @@ func NewKVUserStore(store kvstore.IKVStore) (*KVUserStore, error) {
} }
} }
return &KVUserStore{ usStore := &KVUserStore{
store: store, store: store,
mtx: &sync.RWMutex{}, mtx: &sync.RWMutex{},
}, nil }
return usStore, nil
} }
func (us *KVUserStore) Init(rootName, rootPwd string) error { func (us *KVUserStore) Init(rootName, rootPwd string) error {

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/ihexxa/quickshare/src/db" "github.com/ihexxa/quickshare/src/db"
"github.com/ihexxa/quickshare/src/db/sitestore"
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd" "github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
) )
@ -37,6 +38,9 @@ func TestUserStores(t *testing.T) {
if root.Quota.DownloadSpeedLimit != defaultDownloadSpeedLimit { if root.Quota.DownloadSpeedLimit != defaultDownloadSpeedLimit {
t.Fatalf("incorrect root DownloadSpeedLimit") t.Fatalf("incorrect root DownloadSpeedLimit")
} }
if !db.ComparePreferences(root.Preferences, &DefaultPreferences) {
t.Fatalf("incorrect preference %v %v", root.Preferences, DefaultPreferences)
}
visitor, err := store.GetUser(1) visitor, err := store.GetUser(1)
if err != nil { if err != nil {
@ -60,6 +64,9 @@ func TestUserStores(t *testing.T) {
if visitor.Quota.DownloadSpeedLimit != visitorDownloadSpeedLimit { if visitor.Quota.DownloadSpeedLimit != visitorDownloadSpeedLimit {
t.Fatalf("incorrect visitor DownloadSpeedLimit") t.Fatalf("incorrect visitor DownloadSpeedLimit")
} }
if !db.ComparePreferences(visitor.Preferences, &DefaultPreferences) {
t.Fatalf("incorrect preference")
}
id, name1 := uint64(2), "test_user1" id, name1 := uint64(2), "test_user1"
pwd1, pwd2 := "666", "888" pwd1, pwd2 := "666", "888"
@ -178,6 +185,26 @@ func TestUserStores(t *testing.T) {
t.Fatalf("used space not matched %d %d", user.UsedSpace, usedIncr-usedDecr) t.Fatalf("used space not matched %d %d", user.UsedSpace, usedIncr-usedDecr)
} }
newPrefer := &db.Preferences{
Bg: &sitestore.BgConfig{
Url: "/url",
Repeat: "repeat",
Position: "pos",
Align: "fixed",
BgColor: "#333",
},
CSSURL: "/cssurl",
LanPackURL: "lanPackURL",
Lan: "zhCN",
Theme: "dark",
Avatar: "/avatar",
Email: "foo@gmail.com",
}
err = store.SetPreferences(id, newPrefer)
if err != nil {
t.Fatal(err)
}
user, err = store.GetUserByName(name1) user, err = store.GetUserByName(name1)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -200,6 +227,9 @@ func TestUserStores(t *testing.T) {
if user.Quota.DownloadSpeedLimit != downLimit2 { if user.Quota.DownloadSpeedLimit != downLimit2 {
t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit) t.Fatalf("down limit not matched %d %d", downLimit2, user.Quota.DownloadSpeedLimit)
} }
if !db.ComparePreferences(user.Preferences, newPrefer) {
t.Fatalf("preferences not matched %v %v", user.Preferences, newPrefer)
}
err = store.DelUser(id) err = store.DelUser(id)
if err != nil { if err != nil {

View file

@ -128,6 +128,7 @@ func DefaultConfigStruct() *Config {
Repeat: "repeat", Repeat: "repeat",
Position: "fixed", Position: "fixed",
Align: "center", Align: "center",
BgColor: "#ccc",
}, },
}, },
}, },

View file

@ -119,6 +119,7 @@ func TestLoadCfg(t *testing.T) {
Repeat: "1", Repeat: "1",
Position: "1", Position: "1",
Align: "1", Align: "1",
BgColor: "1",
}, },
}, },
}, },
@ -184,6 +185,7 @@ func TestLoadCfg(t *testing.T) {
Repeat: "4", Repeat: "4",
Position: "4", Position: "4",
Align: "4", Align: "4",
BgColor: "4",
}, },
}, },
}, },
@ -249,6 +251,7 @@ func TestLoadCfg(t *testing.T) {
Repeat: "4", Repeat: "4",
Position: "4", Position: "4",
Align: "4", Align: "4",
BgColor: "4",
}, },
}, },
}, },
@ -314,6 +317,7 @@ func TestLoadCfg(t *testing.T) {
Repeat: "no-repeat", Repeat: "no-repeat",
Position: "top", Position: "top",
Align: "scroll", Align: "scroll",
BgColor: "", // the schema of the config from db is old
}, },
}, },
}, },
@ -349,7 +353,7 @@ func TestLoadCfg(t *testing.T) {
} }
if !Equal(gotCfg, expectCfg) { if !Equal(gotCfg, expectCfg) {
t.Fatal("cfgs are not identical") t.Fatalf("%d, cfgs are not identical", i)
} }
} }
} }

View file

@ -170,6 +170,7 @@ func initDeps(cfg gocfg.ICfg) *depidx.Deps {
Repeat: cfg.StringOr("Site.ClientCfg.Bg.Repeat", "repeat"), Repeat: cfg.StringOr("Site.ClientCfg.Bg.Repeat", "repeat"),
Position: cfg.StringOr("Site.ClientCfg.Bg.Position", "fixed"), Position: cfg.StringOr("Site.ClientCfg.Bg.Position", "fixed"),
Align: cfg.StringOr("Site.ClientCfg.Bg.Align", "center"), Align: cfg.StringOr("Site.ClientCfg.Bg.Align", "center"),
BgColor: cfg.StringOr("Site.ClientCfg.Bg.BgColor", "#ccc"),
}, },
}, },
}) })

View file

@ -79,10 +79,11 @@ func TestSettingsHandlers(t *testing.T) {
SiteName: "quickshare", SiteName: "quickshare",
SiteDesc: "quickshare", SiteDesc: "quickshare",
Bg: &sitestore.BgConfig{ Bg: &sitestore.BgConfig{
Url: "", Url: "url",
Repeat: "", Repeat: "repeat",
Position: "", Position: "center",
Align: "", Align: "fixed",
BgColor: "#ccc",
}, },
}, },
} }

View file

@ -453,6 +453,7 @@ func TestUsersHandlers(t *testing.T) {
Repeat: "no-repeat", Repeat: "no-repeat",
Position: "center", Position: "center",
Align: "fixed", Align: "fixed",
BgColor: "#ccc",
}, },
CSSURL: "/cssurl", CSSURL: "/cssurl",
LanPackURL: "/lanpack", LanPackURL: "/lanpack",
@ -463,6 +464,7 @@ func TestUsersHandlers(t *testing.T) {
Repeat: "no-repeat2", Repeat: "no-repeat2",
Position: "center2", Position: "center2",
Align: "fixed2", Align: "fixed2",
BgColor: "#333",
}, },
CSSURL: "/cssurl2", CSSURL: "/cssurl2",
LanPackURL: "/lanpack2", LanPackURL: "/lanpack2",

View file

@ -46,6 +46,7 @@ site:
repeat: "1" repeat: "1"
position: "1" position: "1"
align: "1" align: "1"
bgColor: "1"
db: db:
dbPath: "1" dbPath: "1"

View file

@ -46,5 +46,6 @@ site:
repeat: "4" repeat: "4"
position: "4" position: "4"
align: "4" align: "4"
bgColor: "4"
db: db:
dbPath: "4" dbPath: "4"

Binary file not shown.