mirror of
https://github.com/elyby/chrly.git
synced 2025-05-31 14:11:51 +05:30
Rewrite mojang textures provider module, cleanup its implementation of events emitter, statsd and etc.
This commit is contained in:
@@ -12,6 +12,7 @@ func New(basePath string) (*Filesystem, error) {
|
||||
return &Filesystem{path: basePath}, nil
|
||||
}
|
||||
|
||||
// Deprecated
|
||||
type Filesystem struct {
|
||||
path string
|
||||
}
|
||||
|
||||
@@ -217,64 +217,76 @@ func removeByUsername(ctx context.Context, conn radix.Conn, username string) err
|
||||
return conn.Do(ctx, radix.Cmd(nil, "EXEC"))
|
||||
}
|
||||
|
||||
func (db *Redis) GetUuid(username string) (string, bool, error) {
|
||||
func (db *Redis) GetUuidForMojangUsername(username string) (string, string, error) {
|
||||
var uuid string
|
||||
var found bool
|
||||
foundUsername := username
|
||||
err := db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
||||
var err error
|
||||
uuid, found, err = findMojangUuidByUsername(ctx, conn, username)
|
||||
uuid, foundUsername, err = findMojangUuidByUsername(ctx, conn, username)
|
||||
|
||||
return err
|
||||
}))
|
||||
|
||||
return uuid, found, err
|
||||
return uuid, foundUsername, err
|
||||
}
|
||||
|
||||
func findMojangUuidByUsername(ctx context.Context, conn radix.Conn, username string) (string, bool, error) {
|
||||
func findMojangUuidByUsername(ctx context.Context, conn radix.Conn, username string) (string, string, error) {
|
||||
key := strings.ToLower(username)
|
||||
var result string
|
||||
err := conn.Do(ctx, radix.Cmd(&result, "HGET", mojangUsernameToUuidKey, key))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if result == "" {
|
||||
return "", false, nil
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
parts := strings.Split(result, ":")
|
||||
partsCnt := len(parts)
|
||||
// https://github.com/elyby/chrly/issues/28
|
||||
if len(parts) < 2 {
|
||||
if partsCnt < 2 {
|
||||
err = conn.Do(ctx, radix.Cmd(nil, "HDEL", mojangUsernameToUuidKey, key))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return "", false, fmt.Errorf("got unexpected response from the mojangUsernameToUuid hash: \"%s\"", result)
|
||||
return "", "", fmt.Errorf("got unexpected response from the mojangUsernameToUuid hash: \"%s\"", result)
|
||||
}
|
||||
|
||||
timestamp, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||
var casedUsername, uuid, rawTimestamp string
|
||||
if partsCnt == 2 { // Legacy, when original username wasn't stored
|
||||
casedUsername = username
|
||||
uuid = parts[0]
|
||||
rawTimestamp = parts[1]
|
||||
} else {
|
||||
casedUsername = parts[0]
|
||||
uuid = parts[1]
|
||||
rawTimestamp = parts[2]
|
||||
}
|
||||
|
||||
timestamp, _ := strconv.ParseInt(rawTimestamp, 10, 64)
|
||||
storedAt := time.Unix(timestamp, 0)
|
||||
if storedAt.Add(time.Hour * 24 * 30).Before(now()) {
|
||||
err = conn.Do(ctx, radix.Cmd(nil, "HDEL", mojangUsernameToUuidKey, key))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return "", false, nil
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
return parts[0], true, nil
|
||||
return uuid, casedUsername, nil
|
||||
}
|
||||
|
||||
func (db *Redis) StoreUuid(username string, uuid string) error {
|
||||
func (db *Redis) StoreMojangUuid(username string, uuid string) error {
|
||||
return db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
||||
return storeMojangUuid(ctx, conn, username, uuid)
|
||||
}))
|
||||
}
|
||||
|
||||
func storeMojangUuid(ctx context.Context, conn radix.Conn, username string, uuid string) error {
|
||||
value := uuid + ":" + strconv.FormatInt(now().Unix(), 10)
|
||||
value := fmt.Sprintf("%s:%s:%d", username, uuid, now().Unix())
|
||||
err := conn.Do(ctx, radix.Cmd(nil, "HSET", mojangUsernameToUuidKey, strings.ToLower(username), value))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -328,15 +328,28 @@ func (suite *redisTestSuite) TestRemoveSkinByUsername() {
|
||||
|
||||
func (suite *redisTestSuite) TestGetUuid() {
|
||||
suite.RunSubTest("exists record", func() {
|
||||
suite.cmd("HSET",
|
||||
"hash:mojang-username-to-uuid",
|
||||
"mock",
|
||||
fmt.Sprintf("%s:%s:%d", "MoCk", "d3ca513eb3e14946b58047f2bd3530fd", time.Now().Unix()),
|
||||
)
|
||||
|
||||
uuid, username, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal("MoCk", username)
|
||||
suite.Require().Equal("d3ca513eb3e14946b58047f2bd3530fd", uuid)
|
||||
})
|
||||
|
||||
suite.RunSubTest("exists record (legacy data)", func() {
|
||||
suite.cmd("HSET",
|
||||
"hash:mojang-username-to-uuid",
|
||||
"mock",
|
||||
fmt.Sprintf("%s:%d", "d3ca513eb3e14946b58047f2bd3530fd", time.Now().Unix()),
|
||||
)
|
||||
|
||||
uuid, found, err := suite.Redis.GetUuid("Mock")
|
||||
suite.Require().Nil(err)
|
||||
suite.Require().True(found)
|
||||
uuid, username, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal("Mock", username)
|
||||
suite.Require().Equal("d3ca513eb3e14946b58047f2bd3530fd", uuid)
|
||||
})
|
||||
|
||||
@@ -344,19 +357,19 @@ func (suite *redisTestSuite) TestGetUuid() {
|
||||
suite.cmd("HSET",
|
||||
"hash:mojang-username-to-uuid",
|
||||
"mock",
|
||||
fmt.Sprintf(":%d", time.Now().Unix()),
|
||||
fmt.Sprintf("%s::%d", "MoCk", time.Now().Unix()),
|
||||
)
|
||||
|
||||
uuid, found, err := suite.Redis.GetUuid("Mock")
|
||||
suite.Require().Nil(err)
|
||||
suite.Require().True(found)
|
||||
suite.Require().Empty("", uuid)
|
||||
uuid, username, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal("MoCk", username)
|
||||
suite.Require().Empty(uuid)
|
||||
})
|
||||
|
||||
suite.RunSubTest("not exists record", func() {
|
||||
uuid, found, err := suite.Redis.GetUuid("Mock")
|
||||
suite.Require().Nil(err)
|
||||
suite.Require().False(found)
|
||||
uuid, username, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Empty(username)
|
||||
suite.Require().Empty(uuid)
|
||||
})
|
||||
|
||||
@@ -364,13 +377,13 @@ func (suite *redisTestSuite) TestGetUuid() {
|
||||
suite.cmd("HSET",
|
||||
"hash:mojang-username-to-uuid",
|
||||
"mock",
|
||||
fmt.Sprintf("%s:%d", "d3ca513eb3e14946b58047f2bd3530fd", time.Now().Add(-1*time.Hour*24*31).Unix()),
|
||||
fmt.Sprintf("%s:%s:%d", "MoCk", "d3ca513eb3e14946b58047f2bd3530fd", time.Now().Add(-1*time.Hour*24*31).Unix()),
|
||||
)
|
||||
|
||||
uuid, found, err := suite.Redis.GetUuid("Mock")
|
||||
uuid, username, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Empty(uuid)
|
||||
suite.Require().False(found)
|
||||
suite.Require().Nil(err)
|
||||
suite.Require().Empty(username)
|
||||
|
||||
resp := suite.cmd("HGET", "hash:mojang-username-to-uuid", "mock")
|
||||
suite.Require().Empty(resp, "should cleanup expired records")
|
||||
@@ -383,9 +396,9 @@ func (suite *redisTestSuite) TestGetUuid() {
|
||||
"corrupted value",
|
||||
)
|
||||
|
||||
uuid, found, err := suite.Redis.GetUuid("Mock")
|
||||
uuid, found, err := suite.Redis.GetUuidForMojangUsername("Mock")
|
||||
suite.Require().Empty(uuid)
|
||||
suite.Require().False(found)
|
||||
suite.Require().Empty(found)
|
||||
suite.Require().Error(err, "Got unexpected response from the mojangUsernameToUuid hash: \"corrupted value\"")
|
||||
|
||||
resp := suite.cmd("HGET", "hash:mojang-username-to-uuid", "mock")
|
||||
@@ -399,11 +412,11 @@ func (suite *redisTestSuite) TestStoreUuid() {
|
||||
return time.Date(2020, 04, 21, 02, 10, 16, 0, time.UTC)
|
||||
}
|
||||
|
||||
err := suite.Redis.StoreUuid("Mock", "d3ca513eb3e14946b58047f2bd3530fd")
|
||||
err := suite.Redis.StoreMojangUuid("Mock", "d3ca513eb3e14946b58047f2bd3530fd")
|
||||
suite.Require().Nil(err)
|
||||
|
||||
resp := suite.cmd("HGET", "hash:mojang-username-to-uuid", "mock")
|
||||
suite.Require().Equal(resp, "d3ca513eb3e14946b58047f2bd3530fd:1587435016")
|
||||
suite.Require().Equal(resp, "Mock:d3ca513eb3e14946b58047f2bd3530fd:1587435016")
|
||||
})
|
||||
|
||||
suite.RunSubTest("store empty uuid", func() {
|
||||
@@ -411,11 +424,11 @@ func (suite *redisTestSuite) TestStoreUuid() {
|
||||
return time.Date(2020, 04, 21, 02, 10, 16, 0, time.UTC)
|
||||
}
|
||||
|
||||
err := suite.Redis.StoreUuid("Mock", "")
|
||||
err := suite.Redis.StoreMojangUuid("Mock", "")
|
||||
suite.Require().Nil(err)
|
||||
|
||||
resp := suite.cmd("HGET", "hash:mojang-username-to-uuid", "mock")
|
||||
suite.Require().Equal(resp, ":1587435016")
|
||||
suite.Require().Equal(resp, "Mock::1587435016")
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user