aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Timeline/Timeline.php
blob: 095160984d2e79b57a7b28ffa1d4ce895b1ce5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace Digitigrade\Timeline;

use Digitigrade\Model\Note;
use Digitigrade\Model\UserAccount;

abstract class Timeline {
    // TODO: currently making this number up. probably should be modifiable somewhere
    private const RESULTS_PER_PAGE = 20;

    /**
     * Gets a pageful of items to be included in the timeline
     * @param int $page page number to return (0-indexed)
     * @return TimelineItem[]
     */
    public function getPage(int $page = 0): array {
        return $this->getItemsFiltered(self::RESULTS_PER_PAGE, self::RESULTS_PER_PAGE * $page);
    }

    /**
     * @return TimelineItem[]
     */
    private function getItemsFiltered(int $limit, int $offset): array {
        $user = UserAccount::findByCurrentSession();
        $items = $this->getItems($limit, $offset);
        if ($user == null)
            return $items;
        // filter out notes from blocked users
        return array_filter($items, function (TimelineItem $item) use ($user) {
            $obj = $item->getObject();
            if ($obj instanceof Note && $user->actor->blocks($obj->author)) {
                return false;
            }
            return true;
        });
    }

    /**
     * Gets a selection of items to be included in the timeline
     * @param int $limit max number of items to return
     * @param int $offset number of items to skip at first
     * @return TimelineItem[]
     */
    abstract protected function getItems(int $limit, int $offset): array;
}