pixelfed/app/Jobs/RemoteFollowPipeline/RemoteFollowPipeline.php

106 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2018-06-01 03:14:15 +00:00
<?php
namespace App\Jobs\RemoteFollowPipeline;
2018-08-28 03:07:36 +00:00
use App\Jobs\AvatarPipeline\CreateAvatar;
2018-06-01 03:14:15 +00:00
use App\{Profile};
use GuzzleHttp\Client;
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Zttp\Zttp;
2018-06-01 03:14:15 +00:00
class RemoteFollowPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $url;
protected $follower;
protected $response;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($follower, $url)
{
$this->follower = $follower;
$this->url = $url;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$follower = $this->follower;
$url = $this->url;
2018-08-28 03:07:36 +00:00
if (Profile::whereRemoteUrl($url)->count() !== 0) {
2018-06-01 03:14:15 +00:00
return true;
}
$this->discover($url);
2018-08-28 03:07:36 +00:00
2018-06-01 03:14:15 +00:00
return true;
}
public function discover($url)
{
$context = new Context([
2018-08-28 03:07:36 +00:00
'keys' => ['examplekey' => 'secret-key-here'],
2018-06-01 03:14:15 +00:00
'algorithm' => 'hmac-sha256',
2018-08-28 03:07:36 +00:00
'headers' => ['(request-target)', 'date'],
2018-06-01 03:14:15 +00:00
]);
$handlerStack = GuzzleHttpSignatures::defaultHandlerFromContext($context);
$client = new Client(['handler' => $handlerStack]);
$response = Zttp::withHeaders([
2018-08-28 03:07:36 +00:00
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
2018-06-01 03:14:15 +00:00
])->get($url);
$this->response = $response->json();
$this->storeProfile();
}
public function storeProfile()
{
$res = $this->response;
$domain = parse_url($res['url'], PHP_URL_HOST);
$username = $res['preferredUsername'];
$remoteUsername = "@{$username}@{$domain}";
2018-08-28 03:07:36 +00:00
$profile = new Profile();
2018-06-01 03:14:15 +00:00
$profile->user_id = null;
$profile->domain = $domain;
$profile->username = $remoteUsername;
$profile->name = $res['name'];
2018-11-05 00:20:48 +00:00
$profile->bio = Purify::clean($res['summary']);
2018-06-01 03:14:15 +00:00
$profile->sharedInbox = $res['endpoints']['sharedInbox'];
$profile->remote_url = $res['url'];
$profile->save();
RemoteFollowImportRecent::dispatch($this->response, $profile);
CreateAvatar::dispatch($profile);
}
public function sendActivity()
{
$res = $this->response;
$url = $res['inbox'];
2022-12-12 23:30:48 +00:00
$activity = Zttp::withHeaders(['Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'])->post($url, [
2018-08-28 03:07:36 +00:00
'type' => 'Follow',
'object' => $this->follower->url(),
2018-06-01 03:14:15 +00:00
]);
}
}