StoryExpire.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $headers = HttpSignature::sign($profile, $url, $activity);
  106. yield function() use ($client, $url, $headers, $payload) {
  107. return $client->postAsync($url, [
  108. 'curl' => [
  109. CURLOPT_HTTPHEADER => $headers,
  110. CURLOPT_POSTFIELDS => $payload,
  111. CURLOPT_HEADER => true
  112. ]
  113. ]);
  114. };
  115. }
  116. };
  117. $pool = new Pool($client, $requests($audience), [
  118. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  119. 'fulfilled' => function ($response, $index) {
  120. },
  121. 'rejected' => function ($reason, $index) {
  122. }
  123. ]);
  124. $promise = $pool->promise();
  125. $promise->wait();
  126. }
  127. protected function handleRemoteExpiry()
  128. {
  129. $story = $this->story;
  130. $story->active = false;
  131. $story->save();
  132. $path = $story->path;
  133. if(Storage::exists($path) == true) {
  134. Storage::delete($path);
  135. }
  136. $story->views()->delete();
  137. $story->delete();
  138. }
  139. }