StatusDelete.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\AccountInterstitial;
  4. use App\Bookmark;
  5. use App\CollectionItem;
  6. use App\DirectMessage;
  7. use App\Jobs\MediaPipeline\MediaDeletePipeline;
  8. use App\Like;
  9. use App\Media;
  10. use App\MediaTag;
  11. use App\Mention;
  12. use App\Notification;
  13. use App\Report;
  14. use App\Services\CollectionService;
  15. use App\Services\NotificationService;
  16. use App\Services\StatusService;
  17. use App\Status;
  18. use App\StatusArchived;
  19. use App\StatusHashtag;
  20. use App\StatusView;
  21. use App\Transformer\ActivityPub\Verb\DeleteNote;
  22. use App\Util\ActivityPub\HttpSignature;
  23. use Cache;
  24. use GuzzleHttp\Client;
  25. use GuzzleHttp\Pool;
  26. use Illuminate\Bus\Queueable;
  27. use Illuminate\Contracts\Queue\ShouldQueue;
  28. use Illuminate\Foundation\Bus\Dispatchable;
  29. use Illuminate\Queue\InteractsWithQueue;
  30. use Illuminate\Queue\SerializesModels;
  31. use League\Fractal;
  32. use League\Fractal\Serializer\ArraySerializer;
  33. class StatusDelete implements ShouldQueue
  34. {
  35. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  36. protected $status;
  37. /**
  38. * Delete the job if its models no longer exist.
  39. *
  40. * @var bool
  41. */
  42. public $deleteWhenMissingModels = true;
  43. public $timeout = 900;
  44. public $tries = 2;
  45. /**
  46. * Create a new job instance.
  47. *
  48. * @return void
  49. */
  50. public function __construct(Status $status)
  51. {
  52. $this->status = $status;
  53. }
  54. /**
  55. * Execute the job.
  56. *
  57. * @return void
  58. */
  59. public function handle()
  60. {
  61. $status = $this->status;
  62. $profile = $this->status->profile;
  63. StatusService::del($status->id, true);
  64. if ($profile) {
  65. if (in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
  66. $profile->status_count = $profile->status_count - 1;
  67. $profile->save();
  68. }
  69. }
  70. Cache::forget('pf:atom:user-feed:by-id:'.$status->profile_id);
  71. if ((bool) config_cache('federation.activitypub.enabled') == true) {
  72. return $this->fanoutDelete($status);
  73. } else {
  74. return $this->unlinkRemoveMedia($status);
  75. }
  76. }
  77. public function unlinkRemoveMedia($status)
  78. {
  79. Media::whereStatusId($status->id)
  80. ->get()
  81. ->each(function ($media) {
  82. MediaDeletePipeline::dispatch($media);
  83. });
  84. if ($status->in_reply_to_id) {
  85. $parent = Status::findOrFail($status->in_reply_to_id);
  86. $parent->reply_count--;
  87. $parent->save();
  88. StatusService::del($parent->id);
  89. }
  90. Bookmark::whereStatusId($status->id)->delete();
  91. CollectionItem::whereObjectType('App\Status')
  92. ->whereObjectId($status->id)
  93. ->get()
  94. ->each(function ($col) {
  95. CollectionService::removeItem($col->collection_id, $col->object_id);
  96. $col->delete();
  97. });
  98. $dms = DirectMessage::whereStatusId($status->id)->get();
  99. foreach ($dms as $dm) {
  100. $not = Notification::whereItemType('App\DirectMessage')
  101. ->whereItemId($dm->id)
  102. ->first();
  103. if ($not) {
  104. NotificationService::del($not->profile_id, $not->id);
  105. $not->forceDeleteQuietly();
  106. }
  107. $dm->delete();
  108. }
  109. Like::whereStatusId($status->id)->delete();
  110. $mediaTags = MediaTag::where('status_id', $status->id)->get();
  111. foreach ($mediaTags as $mtag) {
  112. $not = Notification::whereItemType('App\MediaTag')
  113. ->whereItemId($mtag->id)
  114. ->first();
  115. if ($not) {
  116. NotificationService::del($not->profile_id, $not->id);
  117. $not->forceDeleteQuietly();
  118. }
  119. $mtag->delete();
  120. }
  121. Mention::whereStatusId($status->id)->forceDelete();
  122. Notification::whereItemType('App\Status')
  123. ->whereItemId($status->id)
  124. ->forceDelete();
  125. Report::whereObjectType('App\Status')
  126. ->whereObjectId($status->id)
  127. ->delete();
  128. StatusArchived::whereStatusId($status->id)->delete();
  129. StatusHashtag::whereStatusId($status->id)->delete();
  130. StatusView::whereStatusId($status->id)->delete();
  131. Status::whereInReplyToId($status->id)->update(['in_reply_to_id' => null]);
  132. AccountInterstitial::where('item_type', 'App\Status')
  133. ->where('item_id', $status->id)
  134. ->delete();
  135. $status->delete();
  136. return 1;
  137. }
  138. public function fanoutDelete($status)
  139. {
  140. $profile = $status->profile;
  141. if (! $profile) {
  142. return;
  143. }
  144. $audience = $status->profile->getAudienceInbox();
  145. $fractal = new Fractal\Manager();
  146. $fractal->setSerializer(new ArraySerializer());
  147. $resource = new Fractal\Resource\Item($status, new DeleteNote());
  148. $activity = $fractal->createData($resource)->toArray();
  149. $this->unlinkRemoveMedia($status);
  150. $payload = json_encode($activity);
  151. $client = new Client([
  152. 'timeout' => config('federation.activitypub.delivery.timeout'),
  153. ]);
  154. $version = config('pixelfed.version');
  155. $appUrl = config('app.url');
  156. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  157. $requests = function ($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  158. foreach ($audience as $url) {
  159. $headers = HttpSignature::sign($profile, $url, $activity, [
  160. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  161. 'User-Agent' => $userAgent,
  162. ]);
  163. yield function () use ($client, $url, $headers, $payload) {
  164. return $client->postAsync($url, [
  165. 'curl' => [
  166. CURLOPT_HTTPHEADER => $headers,
  167. CURLOPT_POSTFIELDS => $payload,
  168. CURLOPT_HEADER => true,
  169. ],
  170. ]);
  171. };
  172. }
  173. };
  174. $pool = new Pool($client, $requests($audience), [
  175. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  176. 'fulfilled' => function ($response, $index) {
  177. },
  178. 'rejected' => function ($reason, $index) {
  179. },
  180. ]);
  181. $promise = $pool->promise();
  182. $promise->wait();
  183. return 1;
  184. }
  185. }