Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=8.0",
"ext-pdo": "*",
"ext-curl": "*",
"utopia-php/database": "0.28.*"
"utopia-php/database": "dev-increment as 0.28.99"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
Expand Down
41 changes: 25 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ services:
- abuse
depends_on:
- mysql
volumes:
- ./phpunit.xml:/code/phpunit.xml
- ./src:/code/src
- ./tests:/code/tests

networks:
abuse:
82 changes: 45 additions & 37 deletions src/Abuse/Adapters/TimeLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Utopia\Database\Document;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Limit;
use Utopia\Database\Exception\Structure;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
Expand Down Expand Up @@ -39,9 +38,9 @@ class TimeLimit implements Adapter
protected int $limit = 0;

/**
* @var int|null
* @var Document|null
*/
protected ?int $count = null;
protected ?Document $document = null;

/**
* @var array
Expand All @@ -64,7 +63,6 @@ public function __construct(string $key, int $limit, int $seconds, Database $db)
}

/**
* @throws Limit
* @throws Duplicate
* @throws Exception|\Exception
*/
Expand Down Expand Up @@ -183,26 +181,16 @@ protected function count(string $key, string $datetime): int
return 0;
}

if (!\is_null($this->count)) { // Get fetched result
return $this->count;
if (!\is_null($this->document)) { // Get fetched result
return $this->document['count'];
}

$result = Authorization::skip(function () use ($key, $datetime) {
return $this->db->find(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
});

if (\count($result) === 1) {
$result = $result[0]->getAttribute('count', 0);
} else {
$result = 0;
$this->document = $this->getDocument($key, $datetime);
if (\is_null($this->document)) {
return 0;
}

$this->count = (int)$result;

return $this->count;
return $this->document->getAttribute('count', 0);
}

/**
Expand All @@ -214,32 +202,27 @@ protected function count(string $key, string $datetime): int
*/
protected function hit(string $key, string $datetime): void
{
if (0 == $this->limit) { // No limit no point for counting
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed because it is checked in he check() method

return;
}

Authorization::skip(function () use ($datetime, $key) {
$data = $this->db->findOne(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
$data = $this->getDocument($key, $datetime);
$data = null;
Authorization::skip(function () use ($datetime, $key, $data) {

if ($data === false) {
$data = [
if (empty($data)) {
$document = new Document([
'$permissions' => [],
'key' => $key,
'time' => $datetime,
'count' => 1,
'$collection' => TimeLimit::COLLECTION,
];
$this->db->createDocument(TimeLimit::COLLECTION, new Document($data));
]);

$this->db->createDocument(TimeLimit::COLLECTION, $document);
$this->document = $document;

} else {
$data->setAttribute('count', $data->getAttribute('count', 0) + 1);
$this->db->updateDocument(TimeLimit::COLLECTION, $data->getId(), $data);
$this->db->increaseDocumentAttribute(TimeLimit::COLLECTION, $data->getId(),'count');
$this->document['count']++;
}
});

$this->count++;
}

/**
Expand Down Expand Up @@ -348,4 +331,29 @@ public function time(): string
{
return $this->time;
}

/**
* Return the unique document from database
* @param $key
* @param $datetime
* @return Document|null
* @throws \Exception
*/
protected function getDocument($key, $datetime): ?Document
{
if(!empty($this->document)){
return $this->document;
}

$result = Authorization::skip(function () use ($key, $datetime) {
return $this->db->findOne(TimeLimit::COLLECTION, [
Query::equal('key', [$key]),
Query::equal('time', [$datetime]),
]);
});

$this->document = $result === false ? null :$result;
return $this->document;
}

}