diff --git a/modules/Fediverse/ActivityRequest.php b/modules/Fediverse/ActivityRequest.php index bfd7c37d93591359a96616ce9fdb7a2a8fa8dc4d..aad8d48a8d2f592bf8595db273654cd08cfb6cb1 100644 --- a/modules/Fediverse/ActivityRequest.php +++ b/modules/Fediverse/ActivityRequest.php @@ -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}"; diff --git a/modules/Fediverse/Controllers/ActorController.php b/modules/Fediverse/Controllers/ActorController.php index d28e64f039dd5ac7dae0350740081bdac793194c..43fe5a37f9ef577b0a97b330a8115704c28ac0f1 100644 --- a/modules/Fediverse/Controllers/ActorController.php +++ b/modules/Fediverse/Controllers/ActorController.php @@ -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), ]); } diff --git a/modules/Fediverse/Helpers/fediverse_helper.php b/modules/Fediverse/Helpers/fediverse_helper.php index 302e4ceb303c50f423df5524651be0dc0a7f2294..178f4d2e6696a22dac5ccf0ad2a24f63de9f3998 100644 --- a/modules/Fediverse/Helpers/fediverse_helper.php +++ b/modules/Fediverse/Helpers/fediverse_helper.php @@ -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