2018-06-01 03:14:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\RemoteFollowPipeline;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Jobs\ImageOptimizePipeline\ImageThumbnail;
|
|
|
|
use App\Jobs\StatusPipeline\NewStatusPipeline;
|
|
|
|
use App\Media;
|
|
|
|
use App\Status;
|
2018-06-01 03:14:15 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2018-08-28 03:07:36 +00:00
|
|
|
use Illuminate\Http\File;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Log;
|
|
|
|
use Storage;
|
|
|
|
use Zttp\Zttp;
|
2019-04-03 19:24:27 +00:00
|
|
|
use App\Util\ActivityPub\Helpers;
|
2018-06-01 03:14:15 +00:00
|
|
|
|
|
|
|
class RemoteFollowImportRecent implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
protected $actor;
|
|
|
|
protected $profile;
|
|
|
|
protected $outbox;
|
|
|
|
protected $mediaCount;
|
|
|
|
protected $cursor;
|
|
|
|
protected $nextUrl;
|
|
|
|
protected $supported;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($actorObject, $profile)
|
|
|
|
{
|
|
|
|
$this->actor = $actorObject;
|
|
|
|
$this->profile = $profile;
|
|
|
|
$this->cursor = 1;
|
|
|
|
$this->mediaCount = 0;
|
|
|
|
$this->supported = [
|
|
|
|
'image/jpg',
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
2018-08-28 03:07:36 +00:00
|
|
|
'image/gif',
|
2018-06-01 03:14:15 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$outbox = $this->fetchOutbox();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchOutbox($url = false)
|
|
|
|
{
|
|
|
|
$url = ($url == false) ? $this->actor['outbox'] : $url;
|
2019-04-03 19:24:27 +00:00
|
|
|
if(Helpers::validateUrl($url) == false) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-01 03:14:15 +00:00
|
|
|
$response = Zttp::withHeaders([
|
2019-06-06 01:21:19 +00:00
|
|
|
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
|
2018-06-01 03:14:15 +00:00
|
|
|
])->get($url);
|
|
|
|
|
|
|
|
$this->outbox = $response->json();
|
|
|
|
$this->parseOutbox($this->outbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parseOutbox($outbox)
|
|
|
|
{
|
|
|
|
$types = ['OrderedCollection', 'OrderedCollectionPage'];
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (isset($outbox['totalItems']) && $outbox['totalItems'] < 1) {
|
2018-06-01 03:14:15 +00:00
|
|
|
// Skip remote fetch, not enough posts
|
|
|
|
Log::info('not enough items');
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-06-01 03:14:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (isset($outbox['type']) && in_array($outbox['type'], $types)) {
|
2018-06-01 03:14:15 +00:00
|
|
|
Log::info('handle ordered collection');
|
|
|
|
$this->handleOrderedCollection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleOrderedCollection()
|
|
|
|
{
|
|
|
|
$outbox = $this->outbox;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (!isset($outbox['next']) && !isset($outbox['first']['next']) && $this->cursor !== 1) {
|
2018-06-01 03:14:15 +00:00
|
|
|
$this->cursor = 40;
|
|
|
|
$outbox['next'] = false;
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($outbox['type'] == 'OrderedCollectionPage') {
|
2018-06-01 03:14:15 +00:00
|
|
|
$this->nextUrl = $outbox['next'];
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (isset($outbox['first']) && !is_array($outbox['first'])) {
|
2018-06-01 03:14:15 +00:00
|
|
|
// Mastodon detected
|
|
|
|
Log::info('Mastodon detected...');
|
|
|
|
$this->nextUrl = $outbox['first'];
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-06-01 03:14:15 +00:00
|
|
|
return $this->fetchOutbox($this->nextUrl);
|
|
|
|
} else {
|
|
|
|
// Pleroma detected.
|
|
|
|
$this->nextUrl = isset($outbox['next']) ? $outbox['next'] : (isset($outbox['first']['next']) ? $outbox['first']['next'] : $outbox['next']);
|
|
|
|
Log::info('Checking ordered items...');
|
|
|
|
$orderedItems = isset($outbox['orderedItems']) ? $outbox['orderedItems'] : $outbox['first']['orderedItems'];
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
foreach ($orderedItems as $item) {
|
2018-06-01 03:14:15 +00:00
|
|
|
Log::info('Parsing items...');
|
|
|
|
$parsed = $this->parseObject($item);
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($parsed !== 0) {
|
2018-06-01 03:14:15 +00:00
|
|
|
Log::info('Found media!');
|
|
|
|
$this->importActivity($item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($this->cursor < 40 && $this->mediaCount < 9) {
|
2018-06-01 03:14:15 +00:00
|
|
|
$this->cursor++;
|
|
|
|
$this->mediaCount++;
|
|
|
|
$this->fetchOutbox($this->nextUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parseObject($parsed)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($parsed['type'] !== 'Create') {
|
2018-06-01 03:14:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity = $parsed['object'];
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (isset($activity['attachment']) && !empty($activity['attachment'])) {
|
2018-06-01 03:14:15 +00:00
|
|
|
return $this->detectSupportedMedia($activity['attachment']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function detectSupportedMedia($attachments)
|
|
|
|
{
|
|
|
|
$supported = $this->supported;
|
|
|
|
$count = 0;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
foreach ($attachments as $media) {
|
2018-06-01 03:14:15 +00:00
|
|
|
$mime = $media['mediaType'];
|
|
|
|
$count = in_array($mime, $supported) ? ($count + 1) : $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function importActivity($activity)
|
|
|
|
{
|
|
|
|
$profile = $this->profile;
|
|
|
|
$supported = $this->supported;
|
|
|
|
$attachments = $activity['object']['attachment'];
|
|
|
|
$caption = str_limit($activity['object']['content'], 125);
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if (Status::whereUrl($activity['id'])->count() !== 0) {
|
2018-06-01 03:14:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
$status = new Status();
|
2018-06-01 03:14:15 +00:00
|
|
|
$status->profile_id = $profile->id;
|
|
|
|
$status->url = $activity['id'];
|
|
|
|
$status->local = false;
|
|
|
|
$status->caption = strip_tags($caption);
|
|
|
|
$status->created_at = Carbon::parse($activity['published']);
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
foreach ($attachments as $media) {
|
|
|
|
Log::info($media['mediaType'].' - '.$media['url']);
|
2018-06-01 03:14:15 +00:00
|
|
|
$url = $media['url'];
|
|
|
|
$mime = $media['mediaType'];
|
2018-08-28 03:07:36 +00:00
|
|
|
if (!in_array($mime, $supported)) {
|
|
|
|
Log::info('Invalid media, skipping. '.$mime);
|
2018-06-01 03:14:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-04-04 04:50:48 +00:00
|
|
|
if (Helpers::validateUrl($url) == false) {
|
|
|
|
Log::info('Skipping invalid attachment URL: ' . $url);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-06-01 03:14:15 +00:00
|
|
|
$count++;
|
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($count === 1) {
|
2018-06-01 03:14:15 +00:00
|
|
|
$status->save();
|
|
|
|
}
|
|
|
|
$this->importMedia($url, $mime, $status);
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
Log::info(count($attachments).' media found...');
|
2018-06-01 03:14:15 +00:00
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
if ($count !== 0) {
|
2018-06-01 03:14:15 +00:00
|
|
|
NewStatusPipeline::dispatch($status, $status->media->first());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function importMedia($url, $mime, $status)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
$user = $this->profile;
|
|
|
|
$monthHash = hash('sha1', date('Y').date('m'));
|
|
|
|
$userHash = hash('sha1', $user->id.(string) $user->created_at);
|
|
|
|
$storagePath = "public/m/{$monthHash}/{$userHash}";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$info = pathinfo($url);
|
2018-11-04 03:22:45 +00:00
|
|
|
$url = str_replace(' ', '%20', $url);
|
2018-08-28 03:07:36 +00:00
|
|
|
$img = file_get_contents($url);
|
|
|
|
$file = '/tmp/'.str_random(12).$info['basename'];
|
|
|
|
file_put_contents($file, $img);
|
|
|
|
$path = Storage::putFile($storagePath, new File($file), 'public');
|
|
|
|
|
|
|
|
$media = new Media();
|
|
|
|
$media->status_id = $status->id;
|
|
|
|
$media->profile_id = $status->profile_id;
|
|
|
|
$media->user_id = null;
|
|
|
|
$media->media_path = $path;
|
|
|
|
$media->size = 0;
|
|
|
|
$media->mime = $mime;
|
|
|
|
$media->save();
|
|
|
|
|
|
|
|
ImageThumbnail::dispatch($media);
|
2018-06-01 03:14:15 +00:00
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-01 03:14:15 +00:00
|
|
|
}
|