diff options
author | Arno Richter <oelna@oelna.de> | 2023-08-16 14:52:58 +0200 |
---|---|---|
committer | Arno Richter <oelna@oelna.de> | 2023-08-16 14:52:58 +0200 |
commit | 66c6658bac8b0e99b59e3b9f4eb285f38bcebcf5 (patch) | |
tree | fa54ab21d4c6122df124459030dd5c6af723f1af /lib/activitypub-outbox.php | |
parent | ff2858b6ea8f586daa95e51ae21315f86cc5ded5 (diff) | |
download | microblog-66c6658bac8b0e99b59e3b9f4eb285f38bcebcf5.tar.gz microblog-66c6658bac8b0e99b59e3b9f4eb285f38bcebcf5.tar.bz2 microblog-66c6658bac8b0e99b59e3b9f4eb285f38bcebcf5.zip |
huge update to implement first version of activitypub support. closes #16. AP and subdir hosting are incompatible!
Diffstat (limited to 'lib/activitypub-outbox.php')
-rw-r--r-- | lib/activitypub-outbox.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/activitypub-outbox.php b/lib/activitypub-outbox.php new file mode 100644 index 0000000..242c695 --- /dev/null +++ b/lib/activitypub-outbox.php @@ -0,0 +1,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; |