1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 08:18:59 +01:00
This commit is contained in:
Daniil Gentili 2021-09-05 19:28:22 +02:00
parent 63b744aead
commit b700c2088a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
8 changed files with 112 additions and 8 deletions

View File

@ -20,6 +20,7 @@
*/
use danog\MadelineProto\API;
use danog\MadelineProto\Db\DbArray;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings;
@ -51,6 +52,21 @@ class MyEventHandler extends EventHandler
* @var int|string Username or ID of bot admin
*/
const ADMIN = "danogentili"; // Change this
/**
* List of properties automatically stored in database (MySQL, Postgres, redis or memory).
* @see https://docs.madelineproto.xyz/docs/DATABASE.html
* @var array
*/
protected static array $dbProperties = [
'dataStoredOnDb' => 'array'
];
/**
* @var DbArray<array>
*/
protected $dataStoredOnDb;
/**
* Get peer(s) where to report errors.
*
@ -71,6 +87,7 @@ class MyEventHandler extends EventHandler
{
return $this->onUpdateNewMessage($update);
}
/**
* Handle updates from users.
*
@ -89,6 +106,31 @@ class MyEventHandler extends EventHandler
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame' && $update['message']['media']['_'] !== 'messageMediaWebPage') {
yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]);
}
// You can also use the built-in MadelineProto MySQL async driver!
// Can be anything serializable, an array, an int, an object
$myData = [];
// Use the isset method to check whether some data exists in the database
if (yield $this->dataStoredOnDb->isset('yourKey')) {
// Always yield when fetching data
$myData = yield $this->dataStoredOnDb['yourKey'];
}
$this->dataStoredOnDb['yourKey'] = $myData + ['moreStuff' => 'yay'];
$this->dataStoredOnDb['otherKey'] = 0;
unset($this->dataStoredOnDb['otherKey']);
$this->logger("Count: ".(yield $this->dataStoredOnDb->count()));
// You can even use an async iterator to iterate over the data
$iterator = $this->dataStoredOnDb->getIterator();
while (yield $iterator->advance()) {
[$key, $value] = $iterator->getCurrent();
$this->logger($key);
$this->logger($value);
}
}
}

View File

@ -2,6 +2,7 @@
namespace danog\MadelineProto\Db;
use Amp\Iterator;
use Amp\Producer;
use Amp\Promise;
@ -73,9 +74,9 @@ interface DbArray extends DbType, \ArrayAccess, \Countable
/**
* Get iterator.
*
* @return Producer<array{0: string|int, 1: T}>
* @return Iterator<array{0: string|int, 1: T}>
*/
public function getIterator(): Producer;
public function getIterator(): Iterator;
/**
* @deprecated

View File

@ -2,6 +2,7 @@
namespace danog\MadelineProto\Db;
use Amp\Iterator;
use Amp\Producer;
use Amp\Promise;
use Amp\Success;
@ -83,7 +84,7 @@ class MemoryArray extends \ArrayIterator implements DbArray
return new Success();
}
public function getIterator(): Producer
public function getIterator(): Iterator
{
return new Producer(function (callable $emit) {
foreach ($this as $key => $value) {

View File

@ -2,6 +2,7 @@
namespace danog\MadelineProto\Db;
use Amp\Iterator;
use Amp\Producer;
use Amp\Promise;
use Amp\Redis\Redis as RedisRedis;
@ -209,7 +210,7 @@ class RedisArray extends DriverArray
});
}
public function getIterator(): Producer
public function getIterator(): Iterator
{
return new Producer(function (callable $emit) {
$request = $this->db->scan($this->itKey());

View File

@ -2,6 +2,7 @@
namespace danog\MadelineProto\Db;
use Amp\Iterator;
use Amp\Producer;
use Amp\Promise;
use Amp\Sql\CommandResult;
@ -47,7 +48,7 @@ abstract class SqlArray extends DriverArray
abstract protected function getValue(array $row);
public function getIterator(): Producer
public function getIterator(): Iterator
{
return new Producer(function (callable $emit) {
$request = yield from $this->executeRaw($this->getSqlQuery(self::SQL_ITERATE));

View File

@ -242,6 +242,53 @@ class Connection extends SettingsAbstract
/**
* Set protocol identifier.
*
* Available MTProto transport protocols (smaller overhead is better):
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\AbridgedStream`: Lightest protocol available
* * Overhead: Very small
* * Minimum envelope length: 1 byte (length)
* * Maximum envelope length: 4 bytes (length)
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\IntermediateStream`: I guess they like having multiple protocols
* * Overhead: small
* * Minimum envelope length: 4 bytes (length)
* * Maximum envelope length: 4 bytes (length)
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\IntermediatePaddedStream`: Padded version of the intermediate protocol, to use with obfuscation enabled to bypass ISP blocks
* * Overhead: small-medium
* * Minimum envelope length: random
* * Maximum envelope length: random
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\FullStream`: The basic MTProto transport protocol
* * Overhead: medium
* * Minimum envelope length: 12 bytes (length+seqno+crc)
* * Maximum envelope length: 12 bytes (length+seqno+crc)
* * Pros:
* * Initial integrity check with crc32
* * Transport sequence number check
*
* * Cons:
* * Initial integrity check with crc32 is not that useful since the TCP protocol already uses it internally
* * Transport sequence number check is also not that useful since transport sequence numbers are not encrypted and thus cannot be used to avoid replay attacks, and MadelineProto already uses MTProto sequence numbers and message ids for that.
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\HttpStream`: MTProto over HTTP for browsers and webhosts
* * Overhead: medium
* * Pros:
* * Can be used on restricted webhosts or browsers
* * Cons:
* * Very big envelope length
*
* * `\danog\MadelineProto\Stream\MTProtoTransport\HttpsStream`: MTProto over HTTPS for browsers and webhosts, very secure
* * Overhead: high
* * Pros:
* * Can be used on restricted webhosts or browsers
* * Provides an additional layer of security by trasmitting data over TLS
* * Integrity checks with HMAC built into TLS
* * Sequence number checks built into TLS
* * Cons:
* * Very big envelope length
* * Requires an additional round of encryption
*
* @param class-string<MTProtoBufferInterface> $protocol Protocol identifier
*
* @return self
@ -489,7 +536,7 @@ class Connection extends SettingsAbstract
return $this;
}
/**
* Get whether to use the obfuscated protocol.
* Get whether to use the obfuscated protocol: useful to bypass ISP blocks.
*
* @return bool
*/
@ -499,7 +546,7 @@ class Connection extends SettingsAbstract
}
/**
* Set whether to use the obfuscated protocol.
* Set whether to use the obfuscated protocol: useful to bypass ISP blocks.
*
* @param bool $obfuscated Whether to use the obfuscated protocol.
*
@ -547,7 +594,12 @@ class Connection extends SettingsAbstract
}
/**
* Set transport identifier.
* Sets the transport protocol to use when connecting to telegram.
* Not supported by HTTP and HTTPS protocols, obfuscation must be enabled.
*
* * `danog\MadelineProto\Stream\Transport`: Default TCP transport
* * `danog\MadelineProto\Stream\WsTransport`: Plain websocket transport
* * `danog\MadelineProto\Stream\WssTransport`: TLS websocket transport
*
* @param class-string<RawStreamInterface> $transport Transport identifier.
*

View File

@ -58,6 +58,10 @@ abstract class DatabaseAbstract extends SettingsDatabaseAbstract
* Set for how long to keep records in memory after last read, for cached backends.
*
* The cache TTL identifier can be a string like '+5 minutes'.
* When data is retrieved from a database it is stored in memory.
* This helps to reduce latency, improve speed and reduce mysql/postgres/redis load.
* Data will be removed from the cache if last access was more than this amount of time.
* Clean up is done once per minute.
*
* @param int|string $cacheTtl For how long to keep records in memory after last read, for cached backends.
*

View File

@ -4,6 +4,8 @@ namespace danog\MadelineProto\Settings\Database;
/**
* MySQL backend settings.
*
* MariaDb 10.2+ or Mysql 5.6+ required.
*/
class Mysql extends SqlAbstract
{