1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 06:59:01 +01:00
This commit is contained in:
Daniil Gentili 2023-08-14 22:19:23 +02:00
parent d3fa21feb6
commit 105ed6b9e5
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 153 additions and 119 deletions

View File

@ -861,7 +861,7 @@ final class MTProto implements TLCallback, LoggerGetter
if (!isset($this->TL)) {
$this->TL = new TL($this);
$callbacks = [$this, $this->referenceDatabase];
$callbacks = [$this, $this->referenceDatabase, $this->peerDatabase];
if (!($this->authorization['user']['bot'] ?? false)) {
$callbacks[] = $this->minDatabase;
}

View File

@ -20,6 +20,8 @@ declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools;
use Amp\Sync\LocalKeyedMutex;
use Amp\Sync\LocalMutex;
use danog\MadelineProto\Db\DbArray;
use danog\MadelineProto\Db\DbPropertiesTrait;
use danog\MadelineProto\Exception;
@ -69,10 +71,12 @@ final class MinDatabase implements TLCallback
'db' => ['innerMadelineProto' => true],
];
private LocalKeyedMutex $localMutex;
public function __construct(MTProto $API)
{
$this->API = $API;
$this->v = self::V;
$this->localMutex = new LocalKeyedMutex;
}
public function __destruct()
{
@ -81,6 +85,10 @@ final class MinDatabase implements TLCallback
{
return ['db', 'pendingDb', 'API', 'v'];
}
public function __wakeup()
{
$this->localMutex = new LocalKeyedMutex;
}
public function init(): void
{
$this->initDb($this->API);
@ -200,10 +208,17 @@ final class MinDatabase implements TLCallback
if (!isset($this->pendingDb[$id])) {
return;
}
$pending = $this->pendingDb[$id];
unset($this->pendingDb[$id]);
$lock = $this->localMutex->acquire((string) $id);
try {
if (!isset($this->pendingDb[$id])) {
return;
}
if ($this->API->peerDatabase->get($id)['min'] ?? true) {
$this->db[$id] = $pending;
$this->db[$id] = $this->pendingDb[$id];
}
} finally {
unset($this->pendingDb[$id]);
$lock->release();
}
}
public function populateFrom(array $object)

View File

@ -20,6 +20,7 @@ declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools;
use Amp\Sync\LocalKeyedMutex;
use Amp\Sync\LocalMutex;
use AssertionError;
use danog\MadelineProto\Db\DbArray;
@ -92,9 +93,11 @@ final class PeerDatabase implements TLCallback
];
private LocalMutex $decacheMutex;
private LocalKeyedMutex $mutex;
public function __construct(private MTProto $API)
{
$this->decacheMutex = new LocalMutex;
$this->mutex = new LocalKeyedMutex;
}
public function __sleep()
{
@ -103,6 +106,7 @@ final class PeerDatabase implements TLCallback
public function __wakeup(): void
{
$this->decacheMutex = new LocalMutex;
$this->mutex = new LocalKeyedMutex;
}
public function init(): void
{
@ -182,7 +186,12 @@ final class PeerDatabase implements TLCallback
}
public function clear(int $id): void
{
unset($this->pendingDb[$id], $this->db[$id]);
if ($id < 0) {
$this->processChat($id);
} else {
$this->processUser($id);
}
unset($this->db[$id]);
}
/**
@ -369,8 +378,11 @@ final class PeerDatabase implements TLCallback
return;
}
$user = $this->pendingDb[$id];
unset($this->pendingDb[$id]);
$lock = $this->mutex->acquire((string) $id);
try {
if (!isset($this->pendingDb[$id])) {
return;
}
$existingChat = $this->db[$user['id']];
if (!isset($user['access_hash']) && !($user['min'] ?? false)) {
if (isset($existingChat['access_hash'])) {
@ -420,6 +432,10 @@ final class PeerDatabase implements TLCallback
$this->API->minDatabase->clearPeer($user['id']);
}
}
} finally {
unset($this->pendingDb[$id]);
$lock->release();
}
}
public function addChatBlocking(int $chat): void
{
@ -462,8 +478,9 @@ final class PeerDatabase implements TLCallback
if (!isset($this->pendingDb[$id])) {
return;
}
$lock = $this->mutex->acquire((string) $id);
try {
$chat = $this->pendingDb[$id];
unset($this->pendingDb[$id]);
if ($chat['_'] === 'chat'
|| $chat['_'] === 'chatEmpty'
|| $chat['_'] === 'chatForbidden'
@ -541,6 +558,10 @@ final class PeerDatabase implements TLCallback
$this->API->minDatabase->clearPeer($bot_api_id);
}
}
} finally {
unset($this->pendingDb[$id]);
$lock->release();
}
}
/**
* @internal

View File

@ -252,8 +252,6 @@ if (!getenv('GITHUB_SHA') && stripos(($MadelineProto->readline('Do you want to m
if (!getenv('TEST_USERNAME')) {
throw new Exception('No TEST_USERNAME environment variable was provided!');
}
/*$MadelineProto->refreshPeerCache(\getenv('TEST_USERNAME'));
$MadelineProto->refreshFullPeerCache(\getenv('TEST_USERNAME'));*/
$mention = $MadelineProto->getInfo(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id
$mention = $mention['user_id']; // Selects only the numeric user id
$media = [];