aboutsummaryrefslogtreecommitdiff
path: root/lib/activitypub-followers.php
blob: bc21ab58aa8d1e4bd05bc5159a261c9061e0bd38 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

if(!$config['activitypub']) exit('ActivityPub is disabled via config file.');

// get total amount
$statement = $db->prepare('SELECT COUNT(id) as total FROM followers WHERE follower_actor IS NOT NULL');
$statement->execute();
$followers_total = $statement->fetchAll(PDO::FETCH_ASSOC);
$followers_total = (!empty($followers_total)) ? $followers_total[0]['total'] : 0;

if(!isset($_GET['page'])):

	$output = [
		'@context' => 'https://www.w3.org/ns/activitystreams',
		'id' => $config['url'].'/followers',
		'type' => 'OrderedCollection',
		'totalItems' => $followers_total,
		'first' => $config['url'].'/followers?page=1',
	];

	header('Content-Type: application/ld+json');
	echo(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
else:

	// get items
	$items_per_page = 12; // mastodon default?

	// pagination
	$current_page = (isset($_GET['page']) && is_numeric($_GET['page'])) ? (int) $_GET['page'] : 1;
	$total_pages = ceil($followers_total / $items_per_page);
	$offset = ($current_page-1)*$items_per_page;

	if($current_page < 1 || $current_page > $total_pages) {
		http_response_code(404);
		header('Content-Type: application/ld+json');
		die('{}');
	}

	$statement = $db->prepare('SELECT follower_actor FROM followers WHERE follower_actor IS NOT NULL ORDER BY follower_added ASC LIMIT :limit OFFSET :page');
	$statement->bindValue(':limit', $items_per_page, PDO::PARAM_INT);
	$statement->bindValue(':page', $offset, PDO::PARAM_INT);
	$statement->execute();
	$followers = $statement->fetchAll(PDO::FETCH_ASSOC);

	$ordered_items = [];
	if(!empty($followers)) {
		$ordered_items = array_column($followers, 'follower_actor');
	}

	$output = [
		'@context' => 'https://www.w3.org/ns/activitystreams',
		'id' => $config['url'].'/followers?page='.$current_page,
		'type' => 'OrderedCollectionPage',
		'totalItems' => $followers_total,
		'partOf' => $config['url'].'/followers'
	];

	if($current_page > 1) {
		$output['prev'] = $config['url'].'/followers?page='.($current_page-1);
	}

	if($current_page < $total_pages) {
		$output['next'] = $config['url'].'/followers?page='.($current_page+1);
	}

	$output['orderedItems'] = $ordered_items;

	header('Content-Type: application/ld+json');
	echo(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
endif;