From f8de130b57f304c0b9fc68389bea719fbde917b3 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Thu, 2 Nov 2023 13:41:57 +0800 Subject: [PATCH] Opt: use sort uuid --- internal/db/room.go | 2 +- internal/db/user.go | 2 +- internal/model/model_test.go | 68 ------------------------------------ internal/model/movie.go | 3 +- internal/model/room.go | 4 +-- internal/model/user.go | 4 +-- internal/op/rooms.go | 2 +- server/middlewares/auth.go | 6 ++-- server/model/movie.go | 6 ++-- server/model/room.go | 4 +-- server/model/user.go | 2 +- utils/utils.go | 14 ++++++++ 12 files changed, 31 insertions(+), 86 deletions(-) delete mode 100644 internal/model/model_test.go diff --git a/internal/db/room.go b/internal/db/room.go index 4cce077..b8b1e72 100644 --- a/internal/db/room.go +++ b/internal/db/room.go @@ -66,7 +66,7 @@ func CreateRoom(name, password string, conf ...CreateRoomConfig) (*model.Room, e } func GetRoomByID(id string) (*model.Room, error) { - if len(id) != 36 { + if len(id) != 32 { return nil, errors.New("room id is not 32 bit") } r := &model.Room{} diff --git a/internal/db/user.go b/internal/db/user.go index 1a2e3be..4f983c4 100644 --- a/internal/db/user.go +++ b/internal/db/user.go @@ -123,7 +123,7 @@ func GetUserByIDOrUsernameLike(idOrUsername string, scopes ...func(*gorm.DB) *go } func GetUserByID(id string) (*model.User, error) { - if len(id) != 36 { + if len(id) != 32 { return nil, errors.New("user id is not 32 bit") } u := &model.User{} diff --git a/internal/model/model_test.go b/internal/model/model_test.go deleted file mode 100644 index 07f4885..0000000 --- a/internal/model/model_test.go +++ /dev/null @@ -1,68 +0,0 @@ -package model_test - -import ( - "testing" - - "github.com/glebarez/sqlite" - "github.com/synctv-org/synctv/internal/model" - "gorm.io/gorm" -) - -func TestAutoMigrate(t *testing.T) { - // db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) - db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - err = db.AutoMigrate(new(model.Movie), new(model.Room), new(model.User), new(model.RoomUserRelation)) - if err != nil { - t.Fatal(err) - } -} - -func TestCreateRoom(t *testing.T) { - db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - room := model.Room{ - Name: "test", - HashedPassword: nil, - } - err = db.Create(&room).Error - if err != nil { - t.Fatal(err) - } -} - -func TestCreateUser(t *testing.T) { - db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - user := model.User{ - Username: "user1", - GroupUserRelations: []model.RoomUserRelation{}, - } - err = db.Create(&user).Error - if err != nil { - t.Fatal(err) - } -} - -func TestAddUserToRoom(t *testing.T) { - db, err := gorm.Open(sqlite.Open("./sqlite.db"), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - ur := model.RoomUserRelation{ - UserID: "1", - RoomID: "1", - Role: model.RoomRoleUser, - Permissions: model.DefaultPermissions, - } - err = db.Create(&ur).Error - if err != nil { - t.Fatal(err) - } -} diff --git a/internal/model/movie.go b/internal/model/movie.go index 8e69bd8..e6092f7 100644 --- a/internal/model/movie.go +++ b/internal/model/movie.go @@ -6,7 +6,6 @@ import ( "net/url" "time" - "github.com/google/uuid" "github.com/synctv-org/synctv/internal/conf" "github.com/synctv-org/synctv/utils" "gorm.io/gorm" @@ -24,7 +23,7 @@ type Movie struct { func (m *Movie) BeforeCreate(tx *gorm.DB) error { if m.ID == "" { - m.ID = uuid.NewString() + m.ID = utils.SortUUID() } return nil } diff --git a/internal/model/room.go b/internal/model/room.go index f9ef5d1..93cd722 100644 --- a/internal/model/room.go +++ b/internal/model/room.go @@ -3,7 +3,7 @@ package model import ( "time" - "github.com/google/uuid" + "github.com/synctv-org/synctv/utils" "github.com/zijiren233/stream" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" @@ -33,7 +33,7 @@ type Room struct { func (r *Room) BeforeCreate(tx *gorm.DB) error { if r.ID == "" { - r.ID = uuid.NewString() + r.ID = utils.SortUUID() } return nil } diff --git a/internal/model/user.go b/internal/model/user.go index 132df17..555d358 100644 --- a/internal/model/user.go +++ b/internal/model/user.go @@ -5,7 +5,7 @@ import ( "math/rand" "time" - "github.com/google/uuid" + "github.com/synctv-org/synctv/utils" "gorm.io/gorm" ) @@ -39,7 +39,7 @@ func (u *User) BeforeCreate(tx *gorm.DB) error { u.Username = fmt.Sprintf("%s#%d", u.Username, rand.Intn(9999)) } if u.ID == "" { - u.ID = uuid.NewString() + u.ID = utils.SortUUID() } return nil } diff --git a/internal/op/rooms.go b/internal/op/rooms.go index f4cd983..486ecc5 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -108,7 +108,7 @@ func LoadRoomByID(id string) (*Room, error) { } func LoadOrInitRoomByID(id string) (*Room, error) { - if len(id) != 36 { + if len(id) != 32 { return nil, errors.New("room id is not 32 bit") } i, loaded := roomCache.Load(id) diff --git a/server/middlewares/auth.go b/server/middlewares/auth.go index fdc6d76..a9fda12 100644 --- a/server/middlewares/auth.go +++ b/server/middlewares/auth.go @@ -64,11 +64,11 @@ func AuthRoom(Authorization string) (*op.User, *op.Room, error) { return nil, nil, err } - if len(claims.RoomId) != 36 { + if len(claims.RoomId) != 32 { return nil, nil, ErrAuthFailed } - if len(claims.UserId) != 36 { + if len(claims.UserId) != 32 { return nil, nil, ErrAuthFailed } @@ -94,7 +94,7 @@ func AuthUser(Authorization string) (*op.User, error) { return nil, err } - if len(claims.UserId) != 36 { + if len(claims.UserId) != 32 { return nil, ErrAuthFailed } diff --git a/server/model/movie.go b/server/model/movie.go index e9503ec..af936d6 100644 --- a/server/model/movie.go +++ b/server/model/movie.go @@ -68,7 +68,7 @@ func (i *IdReq) Decode(ctx *gin.Context) error { } func (i *IdReq) Validate() error { - if len(i.Id) != 36 { + if len(i.Id) != 32 { return ErrId } return nil @@ -106,7 +106,7 @@ func (i *IdsReq) Validate() error { return ErrEmptyIds } for _, v := range i.Ids { - if len(v) != 36 { + if len(v) != 32 { return ErrId } } @@ -123,7 +123,7 @@ func (s *SwapMovieReq) Decode(ctx *gin.Context) error { } func (s *SwapMovieReq) Validate() error { - if len(s.Id1) != 36 || len(s.Id2) != 36 { + if len(s.Id1) != 32 || len(s.Id2) != 32 { return ErrId } return nil diff --git a/server/model/room.go b/server/model/room.go index e5b6612..c552949 100644 --- a/server/model/room.go +++ b/server/model/room.go @@ -83,7 +83,7 @@ func (l *LoginRoomReq) Decode(ctx *gin.Context) error { } func (l *LoginRoomReq) Validate() error { - if len(l.RoomId) != 36 { + if len(l.RoomId) != 32 { return ErrEmptyRoomName } @@ -116,7 +116,7 @@ func (r *RoomIDReq) Decode(ctx *gin.Context) error { } func (r *RoomIDReq) Validate() error { - if len(r.Id) != 36 { + if len(r.Id) != 32 { return ErrEmptyRoomName } diff --git a/server/model/user.go b/server/model/user.go index 6cb5e67..5937326 100644 --- a/server/model/user.go +++ b/server/model/user.go @@ -86,7 +86,7 @@ func (u *UserIDReq) Decode(ctx *gin.Context) error { } func (u *UserIDReq) Validate() error { - if len(u.ID) != 36 { + if len(u.ID) != 32 { return errors.New("id is required") } return nil diff --git a/utils/utils.go b/utils/utils.go index 7c1ba18..0ed7ab7 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "encoding/hex" "fmt" "math/rand" "net" @@ -13,11 +14,17 @@ import ( "sync" "sync/atomic" + "github.com/google/uuid" "github.com/synctv-org/synctv/cmd/flags" + "github.com/zijiren233/stream" yamlcomment "github.com/zijiren233/yaml-comment" "gopkg.in/yaml.v3" ) +func init() { + uuid.EnableRandPool() +} + var ( letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") noRedirectHttpClient = &http.Client{ @@ -265,3 +272,10 @@ func OptFilePath(filePath *string) { func LIKE(s string) string { return fmt.Sprintf("%%%s%%", s) } + +func SortUUID() string { + src := uuid.New() + dst := make([]byte, 32) + hex.Encode(dst, src[:]) + return stream.BytesToString(dst) +}