aboutsummaryrefslogtreecommitdiff
path: root/lib/activitypub-outbox.php
blob: 242c695f145b01eecb3bef4c14da55500ab15842 (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
71
72
<?php

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

$posts_per_page = 20; // 20 is mastodon default?
$posts_total = db_posts_count(); // get total amount of posts
$total_pages = ceil($posts_total / $posts_per_page);

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

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

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

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

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

	$posts = db_select_posts(NOW, $posts_per_page, 'desc', $offset);

	$ordered_items = [];
	if(!empty($posts)) {
		foreach ($posts as $post) {

			$item = [];
			$item['id'] = $config['url'].'/'.$post['id'].'/json';
			$item['type'] = 'Create';
			$item['actor'] = $config['url'].'/actor';
			$item['published'] = gmdate('Y-m-d\TH:i:s\Z', $post['post_timestamp']);
			$item['to'] = ['https://www.w3.org/ns/activitystreams#Public'];
			$item['cc'] = [$config['url'].'/followers'];
			$item['object'] = $config['url'].'/'.$post['id'].'/';

			$ordered_items[] = $item;
		}
	}

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

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

	if($current_page < $total_pages) {
		$output['next'] = $config['url'].'/outbox?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;