Skip to content

Commit e18d6c6

Browse files
committed
Clear database on logout
1 parent 1ed27c8 commit e18d6c6

7 files changed

Lines changed: 62 additions & 11 deletions

File tree

src/APIWrapper.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,21 @@ public function getIpcPath(): string
9898
/**
9999
* Serialize session.
100100
*/
101-
public function serialize(): bool
101+
public function serialize(): void
102102
{
103103
if ($this->API === null) {
104-
return false;
104+
return;
105105
}
106106
if ($this->API instanceof Client) {
107-
return false;
107+
return;
108108
}
109109
$this->API->waitForInit();
110110
$API = $this->API;
111111

112112
if ($API->getAuthorization() === API::LOGGED_OUT) {
113-
return false;
113+
$API->deleteSession();
114+
$this->session->delete();
115+
return;
114116
}
115117

116118
$this->session->serialize(
@@ -123,7 +125,6 @@ public function serialize(): bool
123125
if (!Magic::$suspendPeriodicLogging) {
124126
Logger::log('Saved session!');
125127
}
126-
return true;
127128
}
128129

129130
/**

src/EventHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
use Revolt\EventLoop;
4141
use Webmozart\Assert\Assert;
4242

43+
use function Amp\async;
4344
use function Amp\File\isDirectory;
4445
use function Amp\File\isFile;
4546
use function Amp\File\listFiles;
47+
use function Amp\Future\await;
4648

4749
/**
4850
* Event handler.
@@ -112,6 +114,17 @@ final public function internalSaveDbProperties(): void
112114
{
113115
$this->privateInternalSaveDbProperties();
114116
}
117+
/**
118+
* @internal Do not use manually.
119+
*/
120+
final public function internalClearDbProperties(): void
121+
{
122+
$f = [];
123+
foreach ($this->properties as $property) {
124+
$f []= async($property->clear(...));
125+
}
126+
await($f);
127+
}
115128

116129
private static bool $includingPlugins = false;
117130
/**

src/MTProto.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ public static function giveInstanceBySession(string $session): MTProto
427427
return self::$references[$session];
428428
}
429429

430+
/** @internal */
431+
public function deleteSession(): void
432+
{
433+
$this->getDbAutoProperties();
434+
/** @psalm-suppress TypeDoesNotContainType */
435+
if (!isset($this->sessionDb) || $this->sessionDb instanceof MemoryArray) {
436+
return;
437+
}
438+
439+
$db = [async($this->sessionDb->clear(...))];
440+
if ($this->referenceDatabase) {
441+
$db []= async($this->referenceDatabase->clear(...));
442+
}
443+
$db []= async($this->minDatabase->clear(...));
444+
$db []= async($this->peerDatabase->clearAll(...));
445+
foreach ($this->properties as $property) {
446+
$db []= async($property->clear(...));
447+
}
448+
if (isset($this->event_handler_instance)) {
449+
$db []= async($this->event_handler_instance->internalClearDbProperties(...));
450+
}
451+
await($db);
452+
}
453+
430454
/**
431455
* Serialize session, returning object to serialize to db.
432456
*
@@ -497,9 +521,6 @@ public static function serializeAll(): void
497521
if (self::$references) {
498522
Logger::log('Prompting final serialization (SHUTDOWN)...');
499523
foreach (self::$references as $instance) {
500-
if ($instance->loginState->getState()->state === API::LOGGED_OUT) {
501-
continue;
502-
}
503524
$instance->wrapper->serialize();
504525
}
505526
Logger::log('Done final serialization (SHUTDOWN)!');

src/MTProtoTools/MinDatabase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public function init(): void
106106
}
107107
});
108108
}
109+
public function clear(): void
110+
{
111+
$this->db->clear();
112+
}
109113
#[\Override]
110114
public function getMethodAfterResponseDeserializationCallbacks(): array
111115
{

src/MTProtoTools/PeerDatabase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ public function init(): void
152152
});
153153
}
154154

155+
public function clearAll(): void
156+
{
157+
$this->db->clear();
158+
$this->fullDb->clear();
159+
}
160+
155161
public function getFull(int $id): ?array
156162
{
157163
$result = $this->fullDb[$id];

src/MTProtoTools/ReferenceDatabase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function init(): void
119119
EventLoop::queue($this->flush(...), $key);
120120
}
121121
}
122+
public function clear(): void
123+
{
124+
$this->db->clear();
125+
}
122126
private function flush(string $location): void
123127
{
124128
if (!isset($this->pendingDb[$location])) {

src/SessionPaths.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ public function delete(): void
131131
{
132132
if (file_exists($this->sessionDirectoryPath)) {
133133
foreach (scandir($this->sessionDirectoryPath) as $f) {
134-
if ($f === '.' || $f === '..') {
134+
if ($f === '.' || $f === '..' || !str_ends_with($f, '.php')) {
135135
continue;
136136
}
137-
unlink($this->sessionDirectoryPath.DIRECTORY_SEPARATOR.$f);
137+
try {
138+
unlink($this->sessionDirectoryPath.DIRECTORY_SEPARATOR.$f);
139+
} catch (\Throwable) {
140+
}
138141
}
139-
rmdir($this->sessionDirectoryPath);
140142
}
141143
}
142144
/**

0 commit comments

Comments
 (0)