From e697e09050e2deed60159b864d50ea2b4de8bcbb Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Thu, 19 Oct 2023 23:59:17 +0800 Subject: [PATCH] Feat: use hash version --- internal/op/room.go | 3 ++- internal/op/rooms.go | 4 ++-- internal/op/user.go | 4 +++- internal/op/users.go | 5 +++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/internal/op/room.go b/internal/op/room.go index c9fa202..a6b3c90 100644 --- a/internal/op/room.go +++ b/internal/op/room.go @@ -2,6 +2,7 @@ package op import ( "errors" + "hash/crc32" "net/url" "sync/atomic" "time" @@ -298,7 +299,7 @@ func (r *Room) SetPassword(password string) error { if err != nil { return err } - atomic.AddUint32(&r.version, 1) + atomic.StoreUint32(&r.version, crc32.ChecksumIEEE(hashedPassword)) } r.HashedPassword = hashedPassword return db.SetRoomHashedPassword(r.ID, hashedPassword) diff --git a/internal/op/rooms.go b/internal/op/rooms.go index 48b1ca5..5945b28 100644 --- a/internal/op/rooms.go +++ b/internal/op/rooms.go @@ -2,7 +2,7 @@ package op import ( "errors" - "math/rand" + "hash/crc32" "sync/atomic" "github.com/synctv-org/synctv/internal/db" @@ -31,7 +31,7 @@ func WithVersion(version uint32) RoomConf { func initRoom(room *model.Room, conf ...RoomConf) (*Room, error) { r := &Room{ Room: *room, - version: rand.Uint32(), + version: crc32.ChecksumIEEE(room.HashedPassword), current: newCurrent(), } for _, c := range conf { diff --git a/internal/op/user.go b/internal/op/user.go index 4b12c7f..e6398b2 100644 --- a/internal/op/user.go +++ b/internal/op/user.go @@ -2,6 +2,7 @@ package op import ( "errors" + "hash/crc32" "sync/atomic" "github.com/synctv-org/synctv/internal/db" @@ -62,6 +63,7 @@ func (u *User) SetPassword(password string) error { } } u.HashedPassword = hashedPassword - atomic.AddUint32(&u.version, 1) + + atomic.StoreUint32(&u.version, crc32.ChecksumIEEE(u.HashedPassword)) return db.SetUserHashedPassword(u.ID, hashedPassword) } diff --git a/internal/op/users.go b/internal/op/users.go index 3ffb610..94faded 100644 --- a/internal/op/users.go +++ b/internal/op/users.go @@ -1,6 +1,7 @@ package op import ( + "hash/crc32" "time" "github.com/bluele/gcache" @@ -26,7 +27,7 @@ func GetUserById(id uint) (*User, error) { u2 := &User{ User: *u, - version: 1, + version: crc32.ChecksumIEEE(u.HashedPassword), } return u2, userCache.SetWithExpire(id, u2, time.Hour) @@ -64,7 +65,7 @@ func CreateUser(username, password string) (*User, error) { u2 := &User{ User: *u, - version: 1, + version: crc32.ChecksumIEEE(u.HashedPassword), } return u2, userCache.SetWithExpire(u.ID, u2, time.Hour)