Opt: use sort uuid

pull/31/head
zijiren233 2 years ago
parent 1fc33cf9ba
commit f8de130b57

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

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

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

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

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

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

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

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

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

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

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

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

Loading…
Cancel
Save