diff options
| author | winter | 2024-12-23 16:48:58 +0000 |
|---|---|---|
| committer | winter | 2024-12-23 16:48:58 +0000 |
| commit | 094b1d010253f1a9a114303d823739e513834742 (patch) | |
| tree | be6a420fb695c4ab45b4e257dfb84e8cbcbadaa4 | |
| parent | 3272de09352d1583ea817187d13a7a548e360296 (diff) | |
better timeline system
| -rw-r--r-- | Digitigrade/Timeline.php | 29 | ||||
| -rw-r--r-- | Digitigrade/Timeline/GlobalTimeline.php | 18 | ||||
| -rw-r--r-- | routes/global_timeline.php | 13 |
3 files changed, 53 insertions, 7 deletions
diff --git a/Digitigrade/Timeline.php b/Digitigrade/Timeline.php new file mode 100644 index 0000000..67708ab --- /dev/null +++ b/Digitigrade/Timeline.php @@ -0,0 +1,29 @@ +<?php +namespace Digitigrade; + +use Digitigrade\Model\Note; + +abstract class Timeline { + // TODO: currently making this number up. probably should be modifiable somewhere + private const RESULTS_PER_PAGE = 50; + + /** + * Gets a pageful of note IDs to be included in this timeline + * @param int $limit maximum number of results to return + * @param int $offset number of results to skip (i.e. results from previous pages) + * @return int[] + */ + protected abstract function getNoteIds(int $limit, int $offset): array; + + /** + * Gets a pageful of notes that are included in this timeline + * @param int $page page number to return (0-indexed) + * @return Note[] + */ + public function getNotes(int $page = 0): array { + $ids = $this->getNoteIds(self::RESULTS_PER_PAGE, self::RESULTS_PER_PAGE * $page); + return array_map(function (int $id) { + return Note::find($id); + }, $ids); + } +}
\ No newline at end of file diff --git a/Digitigrade/Timeline/GlobalTimeline.php b/Digitigrade/Timeline/GlobalTimeline.php new file mode 100644 index 0000000..b495174 --- /dev/null +++ b/Digitigrade/Timeline/GlobalTimeline.php @@ -0,0 +1,18 @@ +<?php +namespace Digitigrade\Timeline; + +use Digitigrade\Db; +use Digitigrade\Timeline; + +class GlobalTimeline extends Timeline { + protected function getNoteIds(int $limit, int $offset): array { + $db = Db::getInstance()->getPdo(); + $stmt = $db->prepare(<<<END + SELECT note.id FROM note LEFT JOIN note_privacy ON note_privacy.note_id = note.id + WHERE note_privacy.scope = 'public' AND note_privacy.indexable = true + ORDER BY note.created DESC LIMIT ? OFFSET ? + END); + $stmt->execute([$limit, $offset]); + return $stmt->fetchAll(\PDO::FETCH_COLUMN, 0); + } +}
\ No newline at end of file diff --git a/routes/global_timeline.php b/routes/global_timeline.php index 55f64e1..c32419c 100644 --- a/routes/global_timeline.php +++ b/routes/global_timeline.php @@ -1,11 +1,10 @@ <?php -use Digitigrade\Model\Note; use Digitigrade\Router; +use Digitigrade\Timeline\GlobalTimeline; Router::getInstance()->mount('/feed/global', function (array $args) { - // this is really rubbish as it doesn't put them in order, doesn't check visibility/indexable, etc - $posts = Note::findAllWhere('deleted = false', []); + $notes = (new GlobalTimeline())->getNotes(); ?> <!DOCTYPE html> <html lang="en"> @@ -16,11 +15,11 @@ Router::getInstance()->mount('/feed/global', function (array $args) { <body> <h1>Global timeline</h1> - <?php foreach ($posts as $p): ?> + <?php foreach ($notes as $n): ?> <div style="margin: 8px; padding: 8px; border: 1px solid"> - <b><?= $p->author->displayName ?></b> <small><?= $p->author->uri ?></small><br> - <p><?= $p->plainContent ?></p> - <small><?= $p->created->format('c') ?></small> + <b><?= $n->author->displayName ?></b> <small><?= $n->author->uri ?></small><br> + <p><?= $n->plainContent ?></p> + <small><?= $n->created->format('c') ?></small> </div> <?php endforeach; ?> </body> |
