RemoteFollowImportRecent.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Jobs\RemoteFollowPipeline;
  3. use Zttp\Zttp;
  4. use Log, Storage;
  5. use Carbon\Carbon;
  6. use Illuminate\Http\File;
  7. use App\{Media, Profile, Status};
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Contracts\Queue\ShouldQueue;
  12. use Illuminate\Foundation\Bus\Dispatchable;
  13. use App\Jobs\StatusPipeline\NewStatusPipeline;
  14. class RemoteFollowImportRecent implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $actor;
  18. protected $profile;
  19. protected $outbox;
  20. protected $mediaCount;
  21. protected $cursor;
  22. protected $nextUrl;
  23. protected $supported;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct($actorObject, $profile)
  30. {
  31. $this->actor = $actorObject;
  32. $this->profile = $profile;
  33. $this->cursor = 1;
  34. $this->mediaCount = 0;
  35. $this->supported = [
  36. 'image/jpg',
  37. 'image/jpeg',
  38. 'image/png',
  39. 'image/gif'
  40. ];
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. $outbox = $this->fetchOutbox();
  50. }
  51. public function fetchOutbox($url = false)
  52. {
  53. Log::info(json_encode($url));
  54. $url = ($url == false) ? $this->actor['outbox'] : $url;
  55. $response = Zttp::withHeaders([
  56. 'User-Agent' => 'PixelFedBot v0.1 - https://pixelfed.org'
  57. ])->get($url);
  58. $this->outbox = $response->json();
  59. $this->parseOutbox($this->outbox);
  60. }
  61. public function parseOutbox($outbox)
  62. {
  63. $types = ['OrderedCollection', 'OrderedCollectionPage'];
  64. if(isset($outbox['totalItems']) && $outbox['totalItems'] < 1) {
  65. // Skip remote fetch, not enough posts
  66. Log::info('not enough items');
  67. return;
  68. }
  69. if(isset($outbox['type']) && in_array($outbox['type'], $types)) {
  70. Log::info('handle ordered collection');
  71. $this->handleOrderedCollection();
  72. }
  73. }
  74. public function handleOrderedCollection()
  75. {
  76. $outbox = $this->outbox;
  77. if(!isset($outbox['next']) && !isset($outbox['first']['next']) && $this->cursor !== 1) {
  78. $this->cursor = 40;
  79. $outbox['next'] = false;
  80. }
  81. if($outbox['type'] == 'OrderedCollectionPage') {
  82. $this->nextUrl = $outbox['next'];
  83. }
  84. if(isset($outbox['first']) && !is_array($outbox['first'])) {
  85. // Mastodon detected
  86. Log::info('Mastodon detected...');
  87. $this->nextUrl = $outbox['first'];
  88. return $this->fetchOutbox($this->nextUrl);
  89. } else {
  90. // Pleroma detected.
  91. $this->nextUrl = isset($outbox['next']) ? $outbox['next'] : (isset($outbox['first']['next']) ? $outbox['first']['next'] : $outbox['next']);
  92. Log::info('Checking ordered items...');
  93. $orderedItems = isset($outbox['orderedItems']) ? $outbox['orderedItems'] : $outbox['first']['orderedItems'];
  94. }
  95. foreach($orderedItems as $item) {
  96. Log::info('Parsing items...');
  97. $parsed = $this->parseObject($item);
  98. if($parsed !== 0) {
  99. Log::info('Found media!');
  100. $this->importActivity($item);
  101. }
  102. }
  103. if($this->cursor < 40 && $this->mediaCount < 9) {
  104. $this->cursor++;
  105. $this->mediaCount++;
  106. $this->fetchOutbox($this->nextUrl);
  107. }
  108. }
  109. public function parseObject($parsed)
  110. {
  111. if($parsed['type'] !== 'Create') {
  112. return 0;
  113. }
  114. $activity = $parsed['object'];
  115. if(isset($activity['attachment']) && !empty($activity['attachment'])) {
  116. return $this->detectSupportedMedia($activity['attachment']);
  117. }
  118. }
  119. public function detectSupportedMedia($attachments)
  120. {
  121. $supported = $this->supported;
  122. $count = 0;
  123. foreach($attachments as $media) {
  124. $mime = $media['mediaType'];
  125. $count = in_array($mime, $supported) ? ($count + 1) : $count;
  126. }
  127. return $count;
  128. }
  129. public function importActivity($activity)
  130. {
  131. $profile = $this->profile;
  132. $supported = $this->supported;
  133. $attachments = $activity['object']['attachment'];
  134. $caption = str_limit($activity['object']['content'], 125);
  135. if(Status::whereUrl($activity['id'])->count() !== 0) {
  136. return true;
  137. }
  138. $status = new Status;
  139. $status->profile_id = $profile->id;
  140. $status->url = $activity['id'];
  141. $status->local = false;
  142. $status->caption = strip_tags($caption);
  143. $status->created_at = Carbon::parse($activity['published']);
  144. $count = 0;
  145. foreach($attachments as $media) {
  146. Log::info($media['mediaType'] . ' - ' . $media['url']);
  147. $url = $media['url'];
  148. $mime = $media['mediaType'];
  149. if(!in_array($mime, $supported)) {
  150. Log::info('Invalid media, skipping. ' . $mime);
  151. continue;
  152. }
  153. $count++;
  154. if($count === 1) {
  155. $status->save();
  156. }
  157. $this->importMedia($url, $mime, $status);
  158. }
  159. Log::info(count($attachments) . ' media found...');
  160. if($count !== 0) {
  161. NewStatusPipeline::dispatch($status, $status->media->first());
  162. }
  163. }
  164. public function importMedia($url, $mime, $status)
  165. {
  166. $user = $this->profile;
  167. $monthHash = hash('sha1', date('Y') . date('m'));
  168. $userHash = hash('sha1', $user->id . (string) $user->created_at);
  169. $storagePath = "public/m/{$monthHash}/{$userHash}";
  170. try {
  171. $info = pathinfo($url);
  172. $img = file_get_contents($url);
  173. $file = '/tmp/' . str_random(12) . $info['basename'];
  174. file_put_contents($file, $img);
  175. $path = Storage::putFile($storagePath, new File($file), 'public');
  176. $media = new Media;
  177. $media->status_id = $status->id;
  178. $media->profile_id = $status->profile_id;
  179. $media->user_id = null;
  180. $media->media_path = $path;
  181. $media->size = 0;
  182. $media->mime = $mime;
  183. $media->save();
  184. return true;
  185. } catch (Exception $e) {
  186. return false;
  187. }
  188. }
  189. }