mirror of
https://code.castopod.org/adaures/castopod.git
synced 2024-09-27 20:21:59 +02:00
fix(activitypub): add conditions for possibly missing actor properties + add user-agent to requests
This commit is contained in:
parent
3a0a20d59c
commit
8fbf948fbb
@ -27,15 +27,9 @@ class ActivityRequest
|
||||
protected ?Activity $activity = null;
|
||||
|
||||
/**
|
||||
* @var array<string, string[]>
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $options = [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Accept' => 'application/activity+json',
|
||||
// TODO: outgoing and incoming requests
|
||||
],
|
||||
];
|
||||
protected array $options = [];
|
||||
|
||||
public function __construct(string $uri, ?string $activityPayload = null)
|
||||
{
|
||||
@ -45,12 +39,21 @@ class ActivityRequest
|
||||
$this->request->setBody($activityPayload);
|
||||
}
|
||||
|
||||
$this->options = [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Accept' => 'application/activity+json',
|
||||
'User-Agent' => 'Castopod/' . CP_VERSION . '; +' . base_url('', 'https'),
|
||||
// TODO: outgoing and incoming requests
|
||||
],
|
||||
];
|
||||
|
||||
$this->uri = new URI($uri);
|
||||
}
|
||||
|
||||
public function post(): void
|
||||
{
|
||||
// send Message to Fediverse instance
|
||||
// outgoing message to Fediverse instance
|
||||
$this->request->post((string) $this->uri, $this->options);
|
||||
}
|
||||
|
||||
@ -80,7 +83,7 @@ class ActivityRequest
|
||||
$digest = 'SHA-256=' . base64_encode($this->getBodyDigest());
|
||||
$contentType = $this->options['headers']['Content-Type'];
|
||||
$contentLength = (string) strlen($this->request->getBody());
|
||||
$userAgent = 'Castopod';
|
||||
$userAgent = 'Castopod/' . CP_VERSION . '; +' . base_url('', 'https');
|
||||
|
||||
$plainText = "(request-target): post {$path}\nhost: {$host}\ndate: {$date}\ndigest: {$digest}\ncontent-type: {$contentType}\ncontent-length: {$contentLength}\nuser-agent: {$userAgent}";
|
||||
|
||||
|
@ -108,12 +108,13 @@ class ActorController extends Controller
|
||||
if ($replyToPost !== null) {
|
||||
// TODO: strip content from html to retrieve message
|
||||
// remove all html tags and reconstruct message with mentions?
|
||||
extract_text_from_html($payload->object->content);
|
||||
$message = get_message_from_object($payload->object);
|
||||
|
||||
$reply = new Post([
|
||||
'uri' => $payload->object->id,
|
||||
'actor_id' => $payloadActor->id,
|
||||
'in_reply_to_id' => $replyToPost->id,
|
||||
'message' => $payload->object->content,
|
||||
'message' => $message,
|
||||
'published_at' => Time::parse($payload->object->published),
|
||||
]);
|
||||
}
|
||||
|
@ -261,7 +261,7 @@ if (! function_exists('create_actor_from_uri')) {
|
||||
$newActor->public_key = $actorPayload->publicKey->publicKeyPem;
|
||||
$newActor->private_key = null;
|
||||
$newActor->display_name = $actorPayload->name;
|
||||
$newActor->summary = $actorPayload->summary;
|
||||
$newActor->summary = property_exists($actorPayload, 'summary') ? $actorPayload->summary : null;
|
||||
if (property_exists($actorPayload, 'icon')) {
|
||||
$newActor->avatar_image_url = $actorPayload->icon->url;
|
||||
$newActor->avatar_image_mimetype = $actorPayload->icon->mediaType;
|
||||
@ -272,8 +272,8 @@ if (! function_exists('create_actor_from_uri')) {
|
||||
$newActor->cover_image_mimetype = $actorPayload->image->mediaType;
|
||||
}
|
||||
$newActor->inbox_url = $actorPayload->inbox;
|
||||
$newActor->outbox_url = $actorPayload->outbox;
|
||||
$newActor->followers_url = $actorPayload->followers;
|
||||
$newActor->outbox_url = property_exists($actorPayload, 'outbox') ? $actorPayload->outbox : null;
|
||||
$newActor->followers_url = property_exists($actorPayload, 'followers') ? $actorPayload->followers : null;
|
||||
|
||||
if (! ($newActorId = model('ActorModel')->insert($newActor, true))) {
|
||||
return null;
|
||||
@ -307,6 +307,36 @@ if (! function_exists('extract_text_from_html')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('get_message_from_object')) {
|
||||
/**
|
||||
* Gets the message from content, if no content key is present, checks for content in contentMap
|
||||
*
|
||||
* TODO: store multiple languages, convert markdown
|
||||
*
|
||||
* @return string|false
|
||||
*/
|
||||
function get_message_from_object(stdClass $object): string | false
|
||||
{
|
||||
if (property_exists($object, 'content')) {
|
||||
extract_text_from_html($object->content);
|
||||
return $object->content;
|
||||
}
|
||||
|
||||
$message = '';
|
||||
if (property_exists($object, 'contentMap')) {
|
||||
// TODO: update message to be json? (include all languages?)
|
||||
if (property_exists($object->contentMap, 'en')) {
|
||||
extract_text_from_html($object->contentMap->en);
|
||||
$message = $object->contentMap->en;
|
||||
} else {
|
||||
$message = current($object->contentMap);
|
||||
}
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('linkify')) {
|
||||
/**
|
||||
* Turn all link elements in clickable links. Transforms urls and handles
|
||||
|
Loading…
Reference in New Issue
Block a user