danog/MadelineProto

Redis Settings overrides database number and password when URI contains these values

xdimedrolx opened this issue · 3 comments

There is an issue in the getOrmSettings method of the Redis class where if the URI contains a database number or password, these values are overridden by the default values set in the class properties

RedisConfig::fromUri($this->uri)->withDatabase($this->database)->withPassword($this->password),

Make sure you provide the redis settings every time you pass a settings object to MadelineProto

I initialize it like this:

$redis = new danog\MadelineProto\Settings\Database\Redis();
$redis->setUri('redis://:secret@localhost:6379/2');

$settings = new \danog\MadelineProto\Settings();
$settings->setDb($redis);

$madelineProto = new \danog\MadelineProto\API('session.22986706', $settings);

I expect that the password "secret" and database 2 will be used.
However, the connection occurs without a password and to database 1.

To fix this, I did the following:

public function getOrmSettings(): Settings
{
    $config = RedisConfig::fromUri($this->uri);
    if ($this->database !== 0) {
        $config->withDatabase($this->database);
    }
    if ($this->password !== '') {
        $config->withPassword($this->password);
    }

    return new RedisSettings(
        $config,
        match ($this->serializer) {
            SerializerType::IGBINARY => new Igbinary,
            SerializerType::SERIALIZE => new Native,
            null => null
        },
        $this->cacheTtl,
    );
}

I can create a pull request if this solution is correct.

This is an issue with the amphp/redis repo, please reopen the issue/PR there.