StatusDelete.php 5.7 KB

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