1
0

StoryExpire.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Jobs\StoryPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Storage;
  10. use App\Story;
  11. use League\Fractal;
  12. use League\Fractal\Serializer\ArraySerializer;
  13. use App\Transformer\ActivityPub\Verb\DeleteStory;
  14. use App\Util\ActivityPub\Helpers;
  15. use GuzzleHttp\Pool;
  16. use GuzzleHttp\Client;
  17. use GuzzleHttp\Promise;
  18. use App\Util\ActivityPub\HttpSignature;
  19. use App\Services\FollowerService;
  20. use App\Services\StoryService;
  21. class StoryExpire implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $story;
  25. /**
  26. * Delete the job if its models no longer exist.
  27. *
  28. * @var bool
  29. */
  30. public $deleteWhenMissingModels = true;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(Story $story)
  37. {
  38. $this->story = $story;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $story = $this->story;
  48. if($story->local == false) {
  49. $this->handleRemoteExpiry();
  50. return;
  51. }
  52. if($story->active == false) {
  53. return;
  54. }
  55. if($story->expires_at->gt(now())) {
  56. return;
  57. }
  58. $story->active = false;
  59. $story->save();
  60. $this->rotateMediaPath();
  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. },
  126. 'rejected' => function ($reason, $index) {
  127. }
  128. ]);
  129. $promise = $pool->promise();
  130. $promise->wait();
  131. }
  132. protected function handleRemoteExpiry()
  133. {
  134. $story = $this->story;
  135. $story->active = false;
  136. $story->save();
  137. $path = $story->path;
  138. if(Storage::exists($path) == true) {
  139. Storage::delete($path);
  140. }
  141. $story->views()->delete();
  142. $story->delete();
  143. }
  144. }