StoryExpire.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Jobs\StoryPipeline;
  3. use App\Services\FollowerService;
  4. use App\Services\StoryIndexService;
  5. use App\Services\StoryService;
  6. use App\Story;
  7. use App\Transformer\ActivityPub\Verb\DeleteStory;
  8. use App\Util\ActivityPub\HttpSignature;
  9. use GuzzleHttp\Client;
  10. use GuzzleHttp\Pool;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Foundation\Bus\Dispatchable;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. use Illuminate\Queue\SerializesModels;
  16. use League\Fractal;
  17. use League\Fractal\Serializer\ArraySerializer;
  18. use Storage;
  19. class StoryExpire implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $story;
  23. /**
  24. * Delete the job if its models no longer exist.
  25. *
  26. * @var bool
  27. */
  28. public $deleteWhenMissingModels = true;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(Story $story)
  35. {
  36. $this->story = $story;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $story = $this->story;
  46. if ($story->local == false) {
  47. $this->handleRemoteExpiry();
  48. return;
  49. }
  50. if ($story->active == false) {
  51. return;
  52. }
  53. if ($story->expires_at->gt(now())) {
  54. return;
  55. }
  56. $story->active = false;
  57. $story->save();
  58. $this->rotateMediaPath();
  59. $index = app(StoryIndexService::class);
  60. $index->removeStory($story->id, $story->profile_id);
  61. $this->fanoutExpiry();
  62. StoryService::delLatest($story->profile_id);
  63. }
  64. protected function rotateMediaPath()
  65. {
  66. $story = $this->story;
  67. $date = date('Y').date('m');
  68. $old = $story->path;
  69. $base = "story_archives/{$story->profile_id}/{$date}/";
  70. $paths = explode('/', $old);
  71. $path = array_pop($paths);
  72. $newPath = $base.$path;
  73. if (Storage::exists($old) == true) {
  74. $dir = implode('/', $paths);
  75. Storage::move($old, $newPath);
  76. Storage::delete($old);
  77. $story->bearcap_token = null;
  78. $story->path = $newPath;
  79. $story->save();
  80. Storage::deleteDirectory($dir);
  81. }
  82. }
  83. protected function fanoutExpiry()
  84. {
  85. $story = $this->story;
  86. $profile = $story->profile;
  87. if ($story->local == false || $story->remote_url) {
  88. return;
  89. }
  90. $audience = FollowerService::softwareAudience($story->profile_id, 'pixelfed');
  91. if (empty($audience)) {
  92. // Return on profiles with no remote followers
  93. return;
  94. }
  95. $fractal = new Fractal\Manager;
  96. $fractal->setSerializer(new ArraySerializer);
  97. $resource = new Fractal\Resource\Item($story, new DeleteStory);
  98. $activity = $fractal->createData($resource)->toArray();
  99. $payload = json_encode($activity);
  100. $client = new Client([
  101. 'timeout' => config('federation.activitypub.delivery.timeout'),
  102. ]);
  103. $requests = function ($audience) use ($client, $activity, $profile, $payload) {
  104. foreach ($audience as $url) {
  105. $version = config('pixelfed.version');
  106. $appUrl = config('app.url');
  107. $headers = HttpSignature::sign($profile, $url, $activity, [
  108. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  109. 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
  110. ]);
  111. yield function () use ($client, $url, $headers, $payload) {
  112. return $client->postAsync($url, [
  113. 'curl' => [
  114. CURLOPT_HTTPHEADER => $headers,
  115. CURLOPT_POSTFIELDS => $payload,
  116. CURLOPT_HEADER => true,
  117. ],
  118. ]);
  119. };
  120. }
  121. };
  122. $pool = new Pool($client, $requests($audience), [
  123. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  124. 'fulfilled' => function ($response, $index) {},
  125. 'rejected' => function ($reason, $index) {},
  126. ]);
  127. $promise = $pool->promise();
  128. $promise->wait();
  129. }
  130. protected function handleRemoteExpiry()
  131. {
  132. $story = $this->story;
  133. $story->active = false;
  134. $story->save();
  135. $index = app(StoryIndexService::class);
  136. $index->removeStory($story->id, $story->profile_id);
  137. $path = $story->path;
  138. if (Storage::exists($path) == true) {
  139. Storage::delete($path);
  140. }
  141. $story->views()->delete();
  142. $story->delete();
  143. }
  144. }