ActivityPubDeliveryService.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use App\Profile;
  4. use App\Util\ActivityPub\Helpers;
  5. use App\Util\ActivityPub\HttpSignature;
  6. class ActivityPubDeliveryService
  7. {
  8. public $sender;
  9. public $to;
  10. public $payload;
  11. public static function queue()
  12. {
  13. return new self;
  14. }
  15. public function from($profile)
  16. {
  17. $this->sender = $profile;
  18. return $this;
  19. }
  20. public function to(string $url)
  21. {
  22. $this->to = $url;
  23. return $this;
  24. }
  25. public function payload($payload)
  26. {
  27. $this->payload = $payload;
  28. return $this;
  29. }
  30. public function send()
  31. {
  32. return $this->queueDelivery();
  33. }
  34. protected function queueDelivery()
  35. {
  36. abort_if(!$this->sender || !$this->to || !$this->payload, 400);
  37. abort_if(!Helpers::validateUrl($this->to), 400);
  38. abort_if($this->sender->domain != null || $this->sender->status != null, 400);
  39. $body = $this->payload;
  40. $payload = json_encode($body);
  41. $version = config('pixelfed.version');
  42. $appUrl = config('app.url');
  43. $headers = HttpSignature::sign($this->sender, $this->to, $body, [
  44. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  45. 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
  46. ]);
  47. $ch = curl_init($this->to);
  48. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  51. curl_setopt($ch, CURLOPT_HEADER, true);
  52. curl_exec($ch);
  53. }
  54. }