[CACHE] Filter notes by scope in pagedStream

This currently does not return a fixed number of notes per page. Fixing this is left as an exercise to the reader
This commit is contained in:
Hugo Sales 2021-09-21 16:28:54 +01:00
parent 14c173df7a
commit 879f54c772
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 32 additions and 16 deletions

View File

@ -35,7 +35,7 @@ parameters:
cache:
early_recompute: 0.95
notice_max_count: 128
max_note_count: 128
adapters:
default: redis://redis

View File

@ -22,6 +22,8 @@
namespace App\Core;
use App\Core\DB\DB;
use App\Entity\Actor;
use App\Entity\LocalUser;
use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ConfigurationException;
@ -242,30 +244,39 @@ abstract class Cache
}
/**
* @param null|callable(int $offset, int $lenght): Note[] $getter
* Create a cached stream of Notes, paged
*
* Note: the number of notes per page may not always be the same,
* because of scoping. This would make this even more complicated
* and is left as an exercise to the reader :^)
* TODO Ensure same number of notes per page
*
* @return Note[]
*/
public static function pagedStream(string $key, ?callable $getter = null, ?string $query = null, array $query_args = null, int $page = 1, ?int $per_page = null, string $pool = 'default', ?int $max_count = null, float $beta = 1.0)
public static function pagedStream(string $key, string $query, array $query_args, LocalUser|Actor|null $actor = null, int $page = 1, ?int $per_page = null, string $pool = 'default', ?int $max_count = null, float $beta = 1.0): array
{
// TODO scope
$max_count = $max_count ?? Common::config('cache', 'max_note_count');
if ($per_page > $max_count) {
throw new \InvalidArgumentException;
}
if (is_null($per_page)) {
$per_page = Common::config('streams', 'notes_per_page');
}
if (!is_null($max_count) && $per_page > $max_count || !(is_null($getter) ^ is_null($query))) {
throw new \InvalidArgumentException;
}
$filter_scope = fn (Note $n) => $n->isVisibleTo($actor);
if (!is_callable($getter)) {
$getter = function (int $offset, int $lenght) use ($query, $query_args) {
return DB::dql($query, $query_args, options: ['offset' => $offset, 'limit' => $lenght]);
};
}
$getter = function (int $offset, int $lenght) use ($query, $query_args) {
return DB::dql($query, $query_args, options: ['offset' => $offset, 'limit' => $lenght]);
};
$requested_left = $offset = $per_page * ($page - 1);
$requested_right = $requested_left + $per_page;
[$stored_left, $stored_right] = F\map(explode(':', self::get("{$key}-bounds", fn () => "{$requested_left}:{$requested_right}")), fn (string $v) => (int) $v);
$lenght = $stored_right - $stored_left;
[$stored_left, $stored_right] = F\map(
explode(':', self::get("{$key}-bounds", fn () => "{$requested_left}:{$requested_right}")),
fn (string $v) => (int) $v
);
$lenght = $stored_right - $stored_left;
if (!is_null($max_count) && $lenght > $max_count) {
$lenght = $max_count;
@ -276,9 +287,14 @@ abstract class Cache
$res = $getter($stored_left, $stored_right);
self::setList($key, value: $res, pool: $pool, max_count: $max_count, beta: $beta);
self::set("{$key}-bounds", "{$stored_left}:{$stored_right}");
return $res;
return F\filter($res, $filter_scope);
}
return self::getList($key, fn () => $getter($requested_left, $lenght), max_count: $max_count, left: $requested_left, right: $requested_right, beta: $beta);
return F\filter(
self::getList(
$key,
fn () => $getter($requested_left, $lenght), max_count: $max_count, left: $requested_left, right: $requested_right, beta: $beta),
$filter_scope
);
}
}