hostas2/public/api/v1/actor.php

129 lines
3.7 KiB
PHP
Raw Normal View History

2024-09-01 18:54:41 +00:00
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/api/v1/database.php');
function get_actor_or_exit($conn, string $preferred_username) {
$actor = sql_fetch_actor($conn, $preferred_username);
if (!$actor) {
http_response_code(404);
die();
}
return $actor;
}
function get_actor(string $preferred_username) {
$public_key = file_get_contents(HOSTAS_PUBKEY_PATH);
$conn = new SQLite3(HOSTAS_DATABASE_PATH);
$actor = get_actor_or_exit($conn, $preferred_username);
$activity_representation = array(
'@context' => HOSTAS_CONTEXT,
'id' => $actor['id'],
'type' => 'Person',
'preferredUsername' => $actor['preferredUsername'],
'name' => $actor['name'],
'summary' => $actor['summary'],
'url' => $actor['url'],
'icon' => array(
'type' => 'Image',
'url' => $actor['icon'],
),
'publicKey' => array(
'id' => "{$actor['id']}#main-key",
'owner' => $actor['id'],
'publicKeyPem' => $public_key,
),
'inbox' => "https://" . HOSTAS_DOMAIN . "/api/v1/actor/$preferred_username/inbox",
'outbox' => "https://" . HOSTAS_DOMAIN . "/api/v1/actor/$preferred_username/outbox",
);
header('Content-Type: application/activity+json');
echo json_encode($activity_representation);
$conn->close();
die();
}
function get_actor_outbox(string $preferred_username) {
header('Content-Type: application/activity+json');
$conn = new SQLite3(HOSTAS_DATABASE_PATH);
$actor = get_actor_or_exit($conn, $preferred_username);
$create_activities_result = prepare_and_execute($conn,
"select object.id as object_id, object.type as object_type,
activity.actor as activity_actor, post.id as post_id,
post.published as post_published, post.type as post_type,
post.content as post_content
from object
join activity on activity.objectId = object.id
join object as post on activity.object = post.id
where activity.actor = :actor_id
order by object.published",
/*
"select object.id as object_id, object.type as object_type,
post.id as post_id, post.type as post.type, post.published as post_published,
post.url as post_url, post.content as post_content
from object
join activity on activity.objectId = object.id
join object as post on activity.object = post.id
where activity.actor = :actor_id
order by object.published",
*/
array(':actor_id' => $actor['id'])
);
$total_items = 0;
$ordered_items = array();
while ($entry = $create_activities_result->fetchArray()) {
$total_items += 1;
array_push($ordered_items, array(
'id' => $entry['object_id'],
'type' => $entry['object_type'],
'actor' => $entry['activity_actor'],
'published' => date(DATE_ISO8601, $entry['post_published']),
'cc' => array('https://www.w3.org/ns/activitystreams#Public'),
'object' => array(
'id' => $entry['post_id'],
'type'=> $entry['post_type'],
'published' => date(DATE_ISO8601, $entry['post_published']),
'url' => $entry['post_id'],
'attributedTo' => $actor['id'],
'cc' => array('https://www.w3.org/ns/activitystreams#Public'),
'content' => $entry['post_content'],
),
));
}
echo json_encode(array(
'@context' => HOSTAS_CONTEXT,
'id' => "https://" . HOSTAS_DOMAIN . "/api/v1/actor/$preferred_username/outbox",
'type' => 'OrderedCollection',
'totalItems' => $total_items,
'orderedItems' => $ordered_items,
));
die();
}
function get_actor_inbox(string $objectId) {
die();
}
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (sizeof(REQUEST_PATH) === 3) {
if (REQUEST_PATH[2] === 'inbox') get_actor_inbox(REQUEST_PATH[1]);
else if (REQUEST_PATH[2] === 'outbox') get_actor_outbox(REQUEST_PATH[1]);
}
get_actor(REQUEST_PATH[1]);
break;
default:
http_response_code(405);
die();
}