2018-10-29 01:29:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Util\ActivityPub;
|
|
|
|
|
2019-04-05 05:13:59 +00:00
|
|
|
use DB, Cache, Purify, Storage, Request, Validator;
|
2018-10-29 01:29:44 +00:00
|
|
|
use App\{
|
2019-03-08 06:46:38 +00:00
|
|
|
Activity,
|
|
|
|
Follower,
|
2021-04-21 04:29:52 +00:00
|
|
|
Instance,
|
2019-03-08 06:46:38 +00:00
|
|
|
Like,
|
|
|
|
Media,
|
|
|
|
Notification,
|
|
|
|
Profile,
|
|
|
|
Status
|
2018-10-29 01:29:44 +00:00
|
|
|
};
|
|
|
|
use Zttp\Zttp;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Illuminate\Http\File;
|
|
|
|
use Illuminate\Validation\Rule;
|
|
|
|
use App\Jobs\AvatarPipeline\CreateAvatar;
|
|
|
|
use App\Jobs\RemoteFollowPipeline\RemoteFollowImportRecent;
|
|
|
|
use App\Jobs\ImageOptimizePipeline\{ImageOptimize,ImageThumbnail};
|
|
|
|
use App\Jobs\StatusPipeline\NewStatusPipeline;
|
2021-06-18 08:55:42 +00:00
|
|
|
use App\Jobs\StatusPipeline\StatusReplyPipeline;
|
2022-01-19 07:46:30 +00:00
|
|
|
use App\Jobs\StatusPipeline\StatusTagsPipeline;
|
2018-12-21 19:57:43 +00:00
|
|
|
use App\Util\ActivityPub\HttpSignature;
|
2019-04-05 04:44:08 +00:00
|
|
|
use Illuminate\Support\Str;
|
2021-01-17 19:51:07 +00:00
|
|
|
use App\Services\ActivityPubFetchService;
|
2020-02-01 18:42:24 +00:00
|
|
|
use App\Services\ActivityPubDeliveryService;
|
2022-01-19 06:26:44 +00:00
|
|
|
use App\Services\CustomEmojiService;
|
2021-04-21 05:18:07 +00:00
|
|
|
use App\Services\InstanceService;
|
2020-07-20 14:39:48 +00:00
|
|
|
use App\Services\MediaPathService;
|
2021-01-07 03:34:55 +00:00
|
|
|
use App\Services\MediaStorageService;
|
2021-01-24 20:32:20 +00:00
|
|
|
use App\Jobs\MediaPipeline\MediaStoragePipeline;
|
2021-01-26 05:24:24 +00:00
|
|
|
use App\Jobs\AvatarPipeline\RemoteAvatarFetch;
|
2021-07-25 09:17:49 +00:00
|
|
|
use App\Util\Media\License;
|
2021-08-05 02:29:21 +00:00
|
|
|
use App\Models\Poll;
|
2022-05-21 05:09:25 +00:00
|
|
|
use Illuminate\Contracts\Cache\LockTimeoutException;
|
2018-10-29 01:29:44 +00:00
|
|
|
|
|
|
|
class Helpers {
|
|
|
|
|
|
|
|
public static function validateObject($data)
|
|
|
|
{
|
2019-04-18 05:48:26 +00:00
|
|
|
$verbs = ['Create', 'Announce', 'Like', 'Follow', 'Delete', 'Accept', 'Reject', 'Undo', 'Tombstone'];
|
2018-10-29 01:29:44 +00:00
|
|
|
|
|
|
|
$valid = Validator::make($data, [
|
|
|
|
'type' => [
|
|
|
|
'required',
|
2019-04-18 06:32:27 +00:00
|
|
|
'string',
|
2018-10-29 01:29:44 +00:00
|
|
|
Rule::in($verbs)
|
|
|
|
],
|
|
|
|
'id' => 'required|string',
|
2019-04-18 05:29:22 +00:00
|
|
|
'actor' => 'required|string|url',
|
2018-10-29 01:29:44 +00:00
|
|
|
'object' => 'required',
|
|
|
|
'object.type' => 'required_if:type,Create',
|
2019-04-18 05:29:22 +00:00
|
|
|
'object.attributedTo' => 'required_if:type,Create|url',
|
2018-10-29 01:29:44 +00:00
|
|
|
'published' => 'required_if:type,Create|date'
|
|
|
|
])->passes();
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function verifyAttachments($data)
|
|
|
|
{
|
|
|
|
if(!isset($data['object']) || empty($data['object'])) {
|
|
|
|
$data = ['object'=>$data];
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity = $data['object'];
|
|
|
|
|
2021-05-11 03:11:43 +00:00
|
|
|
$mimeTypes = explode(',', config_cache('pixelfed.media_types'));
|
2019-06-12 19:21:41 +00:00
|
|
|
$mediaTypes = in_array('video/mp4', $mimeTypes) ? ['Document', 'Image', 'Video'] : ['Document', 'Image'];
|
2019-03-08 06:46:38 +00:00
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
// Peertube
|
|
|
|
// $mediaTypes = in_array('video/mp4', $mimeTypes) ? ['Document', 'Image', 'Video', 'Link'] : ['Document', 'Image'];
|
|
|
|
|
2019-03-08 06:46:38 +00:00
|
|
|
if(!isset($activity['attachment']) || empty($activity['attachment'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
// peertube
|
|
|
|
// $attachment = is_array($activity['url']) ?
|
|
|
|
// collect($activity['url'])
|
|
|
|
// ->filter(function($media) {
|
|
|
|
// return $media['type'] == 'Link' && $media['mediaType'] == 'video/mp4';
|
|
|
|
// })
|
|
|
|
// ->take(1)
|
|
|
|
// ->values()
|
|
|
|
// ->toArray()[0] : $activity['attachment'];
|
|
|
|
|
2019-03-08 06:46:38 +00:00
|
|
|
$attachment = $activity['attachment'];
|
2022-03-04 04:55:15 +00:00
|
|
|
|
2019-03-08 06:46:38 +00:00
|
|
|
$valid = Validator::make($attachment, [
|
|
|
|
'*.type' => [
|
|
|
|
'required',
|
|
|
|
'string',
|
|
|
|
Rule::in($mediaTypes)
|
|
|
|
],
|
2019-04-18 05:29:22 +00:00
|
|
|
'*.url' => 'required|url|max:255',
|
2019-03-08 06:46:38 +00:00
|
|
|
'*.mediaType' => [
|
|
|
|
'required',
|
|
|
|
'string',
|
|
|
|
Rule::in($mimeTypes)
|
|
|
|
],
|
2022-03-04 04:55:15 +00:00
|
|
|
'*.name' => 'sometimes|nullable|string|max:255'
|
2019-03-08 06:46:38 +00:00
|
|
|
])->passes();
|
|
|
|
|
|
|
|
return $valid;
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function normalizeAudience($data, $localOnly = true)
|
|
|
|
{
|
|
|
|
if(!isset($data['to'])) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 06:46:38 +00:00
|
|
|
|
2018-10-29 01:29:44 +00:00
|
|
|
$audience = [];
|
|
|
|
$audience['to'] = [];
|
|
|
|
$audience['cc'] = [];
|
|
|
|
$scope = 'private';
|
|
|
|
|
|
|
|
if(is_array($data['to']) && !empty($data['to'])) {
|
|
|
|
foreach ($data['to'] as $to) {
|
|
|
|
if($to == 'https://www.w3.org/ns/activitystreams#Public') {
|
|
|
|
$scope = 'public';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $localOnly ? self::validateLocalUrl($to) : self::validateUrl($to);
|
|
|
|
if($url != false) {
|
|
|
|
array_push($audience['to'], $url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($data['cc']) && !empty($data['cc'])) {
|
|
|
|
foreach ($data['cc'] as $cc) {
|
|
|
|
if($cc == 'https://www.w3.org/ns/activitystreams#Public') {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $localOnly ? self::validateLocalUrl($cc) : self::validateUrl($cc);
|
|
|
|
if($url != false) {
|
|
|
|
array_push($audience['cc'], $url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$audience['scope'] = $scope;
|
|
|
|
return $audience;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function userInAudience($profile, $data)
|
|
|
|
{
|
|
|
|
$audience = self::normalizeAudience($data);
|
|
|
|
$url = $profile->permalink();
|
2019-07-27 05:02:13 +00:00
|
|
|
return in_array($url, $audience['to']) || in_array($url, $audience['cc']);
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateUrl($url)
|
|
|
|
{
|
2020-06-13 06:21:41 +00:00
|
|
|
if(is_array($url)) {
|
|
|
|
$url = $url[0];
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
$hash = hash('sha256', $url);
|
|
|
|
$key = "helpers:url:valid:sha256-{$hash}";
|
|
|
|
$ttl = now()->addMinutes(5);
|
2019-04-02 00:26:15 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
$valid = Cache::remember($key, $ttl, function() use($url) {
|
|
|
|
$localhosts = [
|
|
|
|
'127.0.0.1', 'localhost', '::1'
|
|
|
|
];
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
if(mb_substr($url, 0, 8) !== 'https://') {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-05 01:57:13 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
$valid = filter_var($url, FILTER_VALIDATE_URL);
|
2019-04-05 01:57:13 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
if(!$valid) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-24 04:22:23 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
$host = parse_url($valid, PHP_URL_HOST);
|
|
|
|
|
2021-04-21 04:09:59 +00:00
|
|
|
// if(count(dns_get_record($host, DNS_A | DNS_AAAA)) == 0) {
|
|
|
|
// return false;
|
|
|
|
// }
|
2019-04-05 01:57:13 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
if(config('costar.enabled') == true) {
|
|
|
|
if(
|
2021-04-21 04:09:59 +00:00
|
|
|
(config('costar.domain.block') != null && Str::contains($host, config('costar.domain.block')) == true) ||
|
2020-11-26 07:39:01 +00:00
|
|
|
(config('costar.actor.block') != null && in_array($url, config('costar.actor.block')) == true)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2021-04-30 05:39:38 +00:00
|
|
|
if(app()->environment() === 'production') {
|
|
|
|
$bannedInstances = InstanceService::getBannedDomains();
|
|
|
|
if(in_array($host, $bannedInstances)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-21 04:29:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 05:39:38 +00:00
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
if(in_array($host, $localhosts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-26 07:50:46 +00:00
|
|
|
return $url;
|
2020-11-26 07:39:01 +00:00
|
|
|
});
|
|
|
|
|
2020-11-26 07:50:46 +00:00
|
|
|
return $valid;
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function validateLocalUrl($url)
|
|
|
|
{
|
|
|
|
$url = self::validateUrl($url);
|
2019-04-05 03:26:10 +00:00
|
|
|
if($url == true) {
|
2018-10-29 01:29:44 +00:00
|
|
|
$domain = config('pixelfed.domain.app');
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
|
|
$url = $domain === $host ? $url : false;
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function zttpUserAgent()
|
|
|
|
{
|
2020-04-29 19:31:47 +00:00
|
|
|
$version = config('pixelfed.version');
|
|
|
|
$url = config('app.url');
|
2018-10-29 01:29:44 +00:00
|
|
|
return [
|
2019-08-24 03:28:37 +00:00
|
|
|
'Accept' => 'application/activity+json',
|
2020-04-29 19:31:47 +00:00
|
|
|
'User-Agent' => "(Pixelfed/{$version}; +{$url})",
|
2019-03-08 06:46:38 +00:00
|
|
|
];
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 07:39:01 +00:00
|
|
|
public static function fetchFromUrl($url = false)
|
2018-10-29 01:29:44 +00:00
|
|
|
{
|
2020-11-26 07:39:01 +00:00
|
|
|
if(self::validateUrl($url) == false) {
|
2019-04-02 00:29:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-26 07:39:01 +00:00
|
|
|
|
|
|
|
$hash = hash('sha256', $url);
|
|
|
|
$key = "helpers:url:fetcher:sha256-{$hash}";
|
|
|
|
$ttl = now()->addMinutes(5);
|
|
|
|
|
|
|
|
return Cache::remember($key, $ttl, function() use($url) {
|
2021-01-17 19:51:07 +00:00
|
|
|
$res = ActivityPubFetchService::get($url);
|
|
|
|
$res = json_decode($res, true, 8);
|
2020-11-26 07:39:01 +00:00
|
|
|
if(json_last_error() == JSON_ERROR_NONE) {
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fetchProfileFromUrl($url)
|
|
|
|
{
|
|
|
|
return self::fetchFromUrl($url);
|
|
|
|
}
|
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
public static function pluckval($val)
|
|
|
|
{
|
|
|
|
if(is_string($val)) {
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($val)) {
|
|
|
|
return !empty($val) ? $val[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:46:35 +00:00
|
|
|
public static function statusFirstOrFetch($url, $replyTo = false)
|
2018-10-29 01:29:44 +00:00
|
|
|
{
|
|
|
|
$url = self::validateUrl($url);
|
|
|
|
if($url == false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
|
|
$local = config('pixelfed.domain.app') == $host ? true : false;
|
|
|
|
|
|
|
|
if($local) {
|
|
|
|
$id = (int) last(explode('/', $url));
|
2021-01-07 03:34:55 +00:00
|
|
|
return Status::whereNotIn('scope', ['draft','archived'])->findOrFail($id);
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2021-01-08 03:52:50 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$cached = Status::whereNotIn('scope', ['draft','archived'])
|
|
|
|
->whereUri($url)
|
|
|
|
->orWhere('object_url', $url)
|
|
|
|
->first();
|
2021-01-08 03:52:50 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
if($cached) {
|
|
|
|
return $cached;
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$res = self::fetchFromUrl($url);
|
2021-04-21 04:09:59 +00:00
|
|
|
|
2021-08-05 02:29:21 +00:00
|
|
|
if(!$res || empty($res) || isset($res['error']) || !isset($res['@context']) ) {
|
2021-01-29 04:26:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
if(isset($res['object'])) {
|
|
|
|
$activity = $res;
|
|
|
|
} else {
|
|
|
|
$activity = ['object' => $res];
|
|
|
|
}
|
2019-04-05 04:44:08 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$scope = 'private';
|
2021-04-21 04:09:59 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false;
|
|
|
|
|
|
|
|
if(isset($res['to']) == true) {
|
|
|
|
if(is_array($res['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['to'])) {
|
|
|
|
$scope = 'public';
|
|
|
|
}
|
|
|
|
if(is_string($res['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['to']) {
|
|
|
|
$scope = 'public';
|
2019-04-05 04:44:08 +00:00
|
|
|
}
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2019-04-05 04:44:08 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
if(isset($res['cc']) == true) {
|
|
|
|
if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
}
|
|
|
|
if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) {
|
|
|
|
$scope = 'unlisted';
|
2019-04-05 04:44:08 +00:00
|
|
|
}
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2019-04-05 04:44:08 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
if(config('costar.enabled') == true) {
|
|
|
|
$blockedKeywords = config('costar.keyword.block');
|
|
|
|
if($blockedKeywords !== null) {
|
|
|
|
$keywords = config('costar.keyword.block');
|
|
|
|
foreach($keywords as $kw) {
|
|
|
|
if(Str::contains($res['content'], $kw) == true) {
|
|
|
|
return;
|
2019-04-05 04:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2019-04-05 04:44:08 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$unlisted = config('costar.domain.unlisted');
|
|
|
|
if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) {
|
|
|
|
$unlisted = true;
|
|
|
|
$scope = 'unlisted';
|
|
|
|
} else {
|
|
|
|
$unlisted = false;
|
2019-04-05 04:44:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
$cwDomains = config('costar.domain.cw');
|
|
|
|
if(in_array(parse_url($url, PHP_URL_HOST), $cwDomains) == true) {
|
|
|
|
$cw = true;
|
2021-04-21 04:09:59 +00:00
|
|
|
}
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2021-01-08 03:52:50 +00:00
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
$id = isset($res['id']) ? self::pluckval($res['id']) : self::pluckval($url);
|
2021-01-29 04:26:45 +00:00
|
|
|
$idDomain = parse_url($id, PHP_URL_HOST);
|
|
|
|
$urlDomain = parse_url($url, PHP_URL_HOST);
|
2019-06-16 05:30:12 +00:00
|
|
|
|
2021-01-29 04:26:45 +00:00
|
|
|
if(!self::validateUrl($id)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-26 05:05:47 +00:00
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
$attributedTo = is_string($activity['object']['attributedTo']) ?
|
|
|
|
$activity['object']['attributedTo'] :
|
|
|
|
(is_array($activity['object']['attributedTo']) ?
|
|
|
|
collect($activity['object']['attributedTo'])
|
|
|
|
->filter(function($o) {
|
|
|
|
return $o && isset($o['type']) && $o['type'] == 'Person';
|
|
|
|
})
|
|
|
|
->pluck('id')
|
|
|
|
->first() : null
|
|
|
|
);
|
|
|
|
|
|
|
|
if($attributedTo) {
|
|
|
|
$actorDomain = parse_url($attributedTo, PHP_URL_HOST);
|
|
|
|
if(!self::validateUrl($attributedTo) ||
|
2021-01-31 01:14:04 +00:00
|
|
|
$idDomain !== $actorDomain ||
|
|
|
|
$actorDomain !== $urlDomain
|
|
|
|
)
|
2021-01-29 04:26:45 +00:00
|
|
|
{
|
2020-04-29 19:47:03 +00:00
|
|
|
return;
|
2018-12-26 05:05:47 +00:00
|
|
|
}
|
2021-01-29 04:26:45 +00:00
|
|
|
}
|
2018-12-26 05:05:47 +00:00
|
|
|
|
2021-01-31 01:14:04 +00:00
|
|
|
if($idDomain !== $urlDomain) {
|
2021-01-29 04:26:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
|
2022-03-04 04:55:15 +00:00
|
|
|
$profile = self::profileFirstOrNew($attributedTo);
|
2021-07-09 03:30:59 +00:00
|
|
|
if(isset($activity['object']['inReplyTo']) && !empty($activity['object']['inReplyTo']) || $replyTo == true) {
|
2022-03-04 04:55:15 +00:00
|
|
|
$reply_to = self::statusFirstOrFetch(self::pluckval($activity['object']['inReplyTo']), false);
|
2021-01-29 04:26:45 +00:00
|
|
|
$reply_to = optional($reply_to)->id;
|
|
|
|
} else {
|
|
|
|
$reply_to = null;
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
2022-03-04 04:55:15 +00:00
|
|
|
$ts = self::pluckval($res['published']);
|
2021-02-17 06:39:39 +00:00
|
|
|
|
2021-04-21 05:18:07 +00:00
|
|
|
if($scope == 'public' && in_array($urlDomain, InstanceService::getUnlistedDomains())) {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(in_array($urlDomain, InstanceService::getNsfwDomains())) {
|
|
|
|
$cw = true;
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:39:39 +00:00
|
|
|
$statusLockKey = 'helpers:status-lock:' . hash('sha256', $res['id']);
|
|
|
|
$status = Cache::lock($statusLockKey)
|
|
|
|
->get(function () use(
|
2021-04-21 04:09:59 +00:00
|
|
|
$profile,
|
|
|
|
$res,
|
|
|
|
$url,
|
|
|
|
$ts,
|
|
|
|
$reply_to,
|
|
|
|
$cw,
|
|
|
|
$scope,
|
2021-02-17 06:39:39 +00:00
|
|
|
$id
|
|
|
|
) {
|
2022-01-19 06:26:44 +00:00
|
|
|
|
2021-08-05 02:29:21 +00:00
|
|
|
if($res['type'] === 'Question') {
|
|
|
|
$status = self::storePoll(
|
|
|
|
$profile,
|
|
|
|
$res,
|
|
|
|
$url,
|
|
|
|
$ts,
|
|
|
|
$reply_to,
|
|
|
|
$cw,
|
|
|
|
$scope,
|
|
|
|
$id
|
|
|
|
);
|
|
|
|
return $status;
|
|
|
|
}
|
2022-01-19 07:46:30 +00:00
|
|
|
|
2022-03-17 09:13:51 +00:00
|
|
|
return self::storeStatus($url, $profile, $res);
|
2021-01-29 04:26:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return $status;
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 09:13:51 +00:00
|
|
|
public static function storeStatus($url, $profile, $activity)
|
|
|
|
{
|
|
|
|
$id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($activity['url']);
|
|
|
|
$url = isset($activity['url']) && is_string($activity['url']) ? $activity['url'] : $id;
|
|
|
|
$idDomain = parse_url($id, PHP_URL_HOST);
|
|
|
|
$urlDomain = parse_url($url, PHP_URL_HOST);
|
|
|
|
if(!self::validateUrl($id) || !self::validateUrl($url)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reply_to = self::getReplyTo($activity);
|
|
|
|
|
|
|
|
return DB::transaction(function() use($url, $profile, $activity, $reply_to, $id) {
|
|
|
|
$ts = self::pluckval($activity['published']);
|
2022-05-18 05:25:15 +00:00
|
|
|
$scope = self::getScope($activity, $url);
|
|
|
|
$cw = self::getSensitive($activity, $url);
|
2022-04-06 07:51:40 +00:00
|
|
|
$pid = is_object($profile) ? $profile->id : (is_array($profile) ? $profile['id'] : null);
|
|
|
|
|
|
|
|
if(!$pid) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-17 09:13:51 +00:00
|
|
|
|
|
|
|
$status = new Status;
|
2022-04-06 07:51:40 +00:00
|
|
|
$status->profile_id = $pid;
|
2022-03-17 09:13:51 +00:00
|
|
|
$status->url = $url;
|
|
|
|
$status->uri = $url;
|
|
|
|
$status->object_url = $id;
|
|
|
|
$status->caption = strip_tags($activity['content']);
|
|
|
|
$status->rendered = Purify::clean($activity['content']);
|
2022-05-14 05:13:30 +00:00
|
|
|
$status->created_at = Carbon::parse($ts)->tz('UTC');
|
2022-03-17 09:13:51 +00:00
|
|
|
$status->in_reply_to_id = $reply_to;
|
|
|
|
$status->local = false;
|
|
|
|
$status->is_nsfw = $cw;
|
|
|
|
$status->scope = $scope;
|
|
|
|
$status->visibility = $scope;
|
|
|
|
$status->cw_summary = $cw == true && isset($activity['summary']) ?
|
|
|
|
Purify::clean(strip_tags($activity['summary'])) : null;
|
|
|
|
$status->save();
|
|
|
|
|
|
|
|
if($reply_to == null) {
|
|
|
|
self::importNoteAttachment($activity, $status);
|
|
|
|
} else {
|
|
|
|
StatusReplyPipeline::dispatch($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($activity['tag']) && is_array($activity['tag']) && !empty($activity['tag'])) {
|
|
|
|
StatusTagsPipeline::dispatch($activity, $status);
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-18 05:25:15 +00:00
|
|
|
public static function getSensitive($activity, $url)
|
2022-03-17 09:13:51 +00:00
|
|
|
{
|
|
|
|
$id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($url);
|
|
|
|
$url = isset($activity['url']) ? self::pluckval($activity['url']) : $id;
|
|
|
|
$urlDomain = parse_url($url, PHP_URL_HOST);
|
|
|
|
|
|
|
|
$cw = isset($activity['sensitive']) ? (bool) $activity['sensitive'] : false;
|
|
|
|
|
|
|
|
if(in_array($urlDomain, InstanceService::getNsfwDomains())) {
|
|
|
|
$cw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cw;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getReplyTo($activity)
|
|
|
|
{
|
|
|
|
$reply_to = null;
|
|
|
|
$inReplyTo = isset($activity['inReplyTo']) && !empty($activity['inReplyTo']) ?
|
|
|
|
self::pluckval($activity['inReplyTo']) :
|
|
|
|
false;
|
|
|
|
|
|
|
|
if($inReplyTo) {
|
|
|
|
$reply_to = self::statusFirstOrFetch($inReplyTo);
|
|
|
|
if($reply_to) {
|
|
|
|
$reply_to = optional($reply_to)->id;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$reply_to = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $reply_to;
|
|
|
|
}
|
|
|
|
|
2022-05-18 05:25:15 +00:00
|
|
|
public static function getScope($activity, $url)
|
2022-03-17 09:13:51 +00:00
|
|
|
{
|
|
|
|
$id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($url);
|
|
|
|
$url = isset($activity['url']) ? self::pluckval($activity['url']) : $id;
|
|
|
|
$urlDomain = parse_url($url, PHP_URL_HOST);
|
|
|
|
$scope = 'private';
|
|
|
|
|
|
|
|
if(isset($activity['to']) == true) {
|
|
|
|
if(is_array($activity['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to'])) {
|
|
|
|
$scope = 'public';
|
|
|
|
}
|
|
|
|
if(is_string($activity['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $activity['to']) {
|
|
|
|
$scope = 'public';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($activity['cc']) == true) {
|
|
|
|
if(is_array($activity['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $activity['cc'])) {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
}
|
|
|
|
if(is_string($activity['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $activity['cc']) {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($scope == 'public' && in_array($urlDomain, InstanceService::getUnlistedDomains())) {
|
|
|
|
$scope = 'unlisted';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scope;
|
|
|
|
}
|
|
|
|
|
2021-08-05 02:29:21 +00:00
|
|
|
private static function storePoll($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id)
|
|
|
|
{
|
|
|
|
if(!isset($res['endTime']) || !isset($res['oneOf']) || !is_array($res['oneOf']) || count($res['oneOf']) > 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = collect($res['oneOf'])->map(function($option) {
|
|
|
|
return $option['name'];
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$cachedTallies = collect($res['oneOf'])->map(function($option) {
|
|
|
|
return $option['replies']['totalItems'] ?? 0;
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$status = new Status;
|
|
|
|
$status->profile_id = $profile->id;
|
|
|
|
$status->url = isset($res['url']) ? $res['url'] : $url;
|
|
|
|
$status->uri = isset($res['url']) ? $res['url'] : $url;
|
|
|
|
$status->object_url = $id;
|
|
|
|
$status->caption = strip_tags($res['content']);
|
|
|
|
$status->rendered = Purify::clean($res['content']);
|
2022-05-14 05:13:30 +00:00
|
|
|
$status->created_at = Carbon::parse($ts)->tz('UTC');
|
2021-08-05 02:29:21 +00:00
|
|
|
$status->in_reply_to_id = null;
|
|
|
|
$status->local = false;
|
|
|
|
$status->is_nsfw = $cw;
|
|
|
|
$status->scope = 'draft';
|
|
|
|
$status->visibility = 'draft';
|
|
|
|
$status->cw_summary = $cw == true && isset($res['summary']) ?
|
|
|
|
Purify::clean(strip_tags($res['summary'])) : null;
|
|
|
|
$status->save();
|
|
|
|
|
|
|
|
$poll = new Poll;
|
|
|
|
$poll->status_id = $status->id;
|
|
|
|
$poll->profile_id = $status->profile_id;
|
|
|
|
$poll->poll_options = $options;
|
|
|
|
$poll->cached_tallies = $cachedTallies;
|
|
|
|
$poll->votes_count = array_sum($cachedTallies);
|
|
|
|
$poll->expires_at = now()->parse($res['endTime']);
|
|
|
|
$poll->last_fetched_at = now();
|
|
|
|
$poll->save();
|
|
|
|
|
|
|
|
$status->type = 'poll';
|
|
|
|
$status->scope = $scope;
|
|
|
|
$status->visibility = $scope;
|
|
|
|
$status->save();
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:44:01 +00:00
|
|
|
public static function statusFetch($url)
|
|
|
|
{
|
|
|
|
return self::statusFirstOrFetch($url);
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:29:44 +00:00
|
|
|
public static function importNoteAttachment($data, Status $status)
|
|
|
|
{
|
|
|
|
if(self::verifyAttachments($data) == false) {
|
2022-03-04 04:55:15 +00:00
|
|
|
// \Log::info('importNoteAttachment::failedVerification.', [$data['id']]);
|
2021-07-21 04:17:31 +00:00
|
|
|
$status->viewType();
|
2018-10-29 01:29:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$attachments = isset($data['object']) ? $data['object']['attachment'] : $data['attachment'];
|
2022-03-04 04:55:15 +00:00
|
|
|
// peertube
|
|
|
|
// if(!$attachments) {
|
|
|
|
// $obj = isset($data['object']) ? $data['object'] : $data;
|
|
|
|
// $attachments = is_array($obj['url']) ? $obj['url'] : null;
|
|
|
|
// }
|
2018-10-29 01:29:44 +00:00
|
|
|
$user = $status->profile;
|
2020-07-20 14:39:48 +00:00
|
|
|
$storagePath = MediaPathService::get($user, 2);
|
2021-05-11 03:11:43 +00:00
|
|
|
$allowed = explode(',', config_cache('pixelfed.media_types'));
|
2019-06-12 19:21:41 +00:00
|
|
|
|
2018-10-29 01:29:44 +00:00
|
|
|
foreach($attachments as $media) {
|
|
|
|
$type = $media['mediaType'];
|
|
|
|
$url = $media['url'];
|
2021-01-26 05:24:24 +00:00
|
|
|
$blurhash = isset($media['blurhash']) ? $media['blurhash'] : null;
|
2021-07-25 09:17:49 +00:00
|
|
|
$license = isset($media['license']) ? License::nameToId($media['license']) : null;
|
2018-10-29 01:29:44 +00:00
|
|
|
$valid = self::validateUrl($url);
|
|
|
|
if(in_array($type, $allowed) == false || $valid == false) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-09 18:53:32 +00:00
|
|
|
|
|
|
|
$media = new Media();
|
2021-01-26 05:24:24 +00:00
|
|
|
$media->blurhash = $blurhash;
|
2019-06-09 23:38:29 +00:00
|
|
|
$media->remote_media = true;
|
2019-06-09 18:53:32 +00:00
|
|
|
$media->status_id = $status->id;
|
|
|
|
$media->profile_id = $status->profile_id;
|
|
|
|
$media->user_id = null;
|
2019-07-19 04:08:39 +00:00
|
|
|
$media->media_path = $url;
|
|
|
|
$media->remote_url = $url;
|
2021-07-25 09:17:49 +00:00
|
|
|
if($license) {
|
|
|
|
$media->license = $license;
|
|
|
|
}
|
2019-07-19 04:08:39 +00:00
|
|
|
$media->mime = $type;
|
2021-01-26 05:24:24 +00:00
|
|
|
$media->version = 3;
|
2019-06-09 18:53:32 +00:00
|
|
|
$media->save();
|
2021-01-25 04:13:31 +00:00
|
|
|
|
2021-05-19 04:45:04 +00:00
|
|
|
if(config_cache('pixelfed.cloud_storage') == true) {
|
2021-01-25 04:13:31 +00:00
|
|
|
MediaStoragePipeline::dispatch($media);
|
|
|
|
}
|
2018-10-29 01:29:44 +00:00
|
|
|
}
|
2021-04-21 04:09:59 +00:00
|
|
|
|
2019-06-12 19:21:41 +00:00
|
|
|
$status->viewType();
|
2018-10-29 01:29:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
public static function profileFirstOrNew($url)
|
2018-10-29 01:29:44 +00:00
|
|
|
{
|
2019-03-06 02:52:12 +00:00
|
|
|
$url = self::validateUrl($url);
|
2022-05-21 05:09:25 +00:00
|
|
|
if($url == false) {
|
2019-03-08 06:46:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-05-21 05:09:25 +00:00
|
|
|
|
|
|
|
$host = parse_url($url, PHP_URL_HOST);
|
|
|
|
$local = config('pixelfed.domain.app') == $host ? true : false;
|
|
|
|
|
|
|
|
if($local == true) {
|
|
|
|
$id = last(explode('/', $url));
|
|
|
|
return Profile::whereNull('status')
|
|
|
|
->whereNull('domain')
|
|
|
|
->whereUsername($id)
|
|
|
|
->firstOrFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($profile = Profile::whereRemoteUrl($url)->first()) {
|
|
|
|
if($profile->last_fetched_at->lt(now()->subHours(24))) {
|
|
|
|
return self::profileUpdateOrCreate($url);
|
|
|
|
}
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::profileUpdateOrCreate($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function profileUpdateOrCreate($url)
|
|
|
|
{
|
2021-01-07 07:25:45 +00:00
|
|
|
$hash = base64_encode($url);
|
|
|
|
$key = 'ap:profile:by_url:' . $hash;
|
2022-05-21 05:09:25 +00:00
|
|
|
$lock = Cache::lock($key, 30);
|
|
|
|
$profile = null;
|
2021-01-07 07:25:45 +00:00
|
|
|
|
2022-05-21 05:09:25 +00:00
|
|
|
try {
|
|
|
|
$lock->block(5);
|
2021-01-26 05:24:24 +00:00
|
|
|
|
2021-01-07 07:25:45 +00:00
|
|
|
$res = self::fetchProfileFromUrl($url);
|
|
|
|
if(isset($res['id']) == false) {
|
|
|
|
return;
|
2020-06-13 06:21:41 +00:00
|
|
|
}
|
2021-01-07 07:25:45 +00:00
|
|
|
$domain = parse_url($res['id'], PHP_URL_HOST);
|
|
|
|
if(!isset($res['preferredUsername']) && !isset($res['nickname'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$username = (string) Purify::clean($res['preferredUsername'] ?? $res['nickname']);
|
|
|
|
if(empty($username)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$remoteUsername = $username;
|
|
|
|
$webfinger = "@{$username}@{$domain}";
|
|
|
|
|
|
|
|
abort_if(!self::validateUrl($res['inbox']), 400);
|
|
|
|
abort_if(!self::validateUrl($res['id']), 400);
|
|
|
|
|
2022-05-21 05:19:45 +00:00
|
|
|
$profile = DB::transaction(function() use($domain, $webfinger, $res) {
|
2022-05-21 05:09:25 +00:00
|
|
|
$instance = Instance::updateOrCreate([
|
2021-04-21 04:29:52 +00:00
|
|
|
'domain' => $domain
|
|
|
|
]);
|
2021-09-04 03:21:17 +00:00
|
|
|
if($instance->wasRecentlyCreated == true) {
|
|
|
|
\App\Jobs\InstancePipeline\FetchNodeinfoPipeline::dispatch($instance)->onQueue('low');
|
|
|
|
}
|
2022-05-21 05:09:25 +00:00
|
|
|
|
|
|
|
$profile = Profile::updateOrCreate(
|
|
|
|
[
|
|
|
|
'domain' => strtolower($domain),
|
|
|
|
'username' => Purify::clean($webfinger),
|
|
|
|
'remote_url' => $res['id'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => isset($res['name']) ? Purify::clean($res['name']) : 'user',
|
|
|
|
'bio' => isset($res['summary']) ? Purify::clean($res['summary']) : null,
|
|
|
|
'sharedInbox' => isset($res['endpoints']) && isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : null,
|
|
|
|
'inbox_url' => $res['inbox'],
|
|
|
|
'outbox_url' => isset($res['outbox']) ? $res['outbox'] : null,
|
|
|
|
'public_key' => $res['publicKey']['publicKeyPem'],
|
|
|
|
'key_id' => $res['publicKey']['id'],
|
|
|
|
'webfinger' => Purify::clean($webfinger),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if( $profile->last_fetched_at == null ||
|
|
|
|
$profile->last_fetched_at->lt(now()->subHours(24))
|
2021-01-07 07:25:45 +00:00
|
|
|
) {
|
2022-05-21 05:09:25 +00:00
|
|
|
RemoteAvatarFetch::dispatch($profile);
|
2021-01-07 07:25:45 +00:00
|
|
|
}
|
2022-05-21 05:09:25 +00:00
|
|
|
$profile->last_fetched_at = now();
|
|
|
|
$profile->save();
|
|
|
|
return $profile;
|
|
|
|
});
|
|
|
|
|
2021-01-07 07:25:45 +00:00
|
|
|
return $profile;
|
2022-05-21 05:09:25 +00:00
|
|
|
} catch (LockTimeoutException $e) {
|
|
|
|
} finally {
|
|
|
|
optional($lock)->release();
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:29:44 +00:00
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:44:01 +00:00
|
|
|
public static function profileFetch($url)
|
|
|
|
{
|
|
|
|
return self::profileFirstOrNew($url);
|
|
|
|
}
|
|
|
|
|
2020-02-01 18:42:24 +00:00
|
|
|
public static function sendSignedObject($profile, $url, $body)
|
2019-03-08 06:46:38 +00:00
|
|
|
{
|
2020-02-01 18:42:24 +00:00
|
|
|
ActivityPubDeliveryService::queue()
|
|
|
|
->from($profile)
|
|
|
|
->to($url)
|
|
|
|
->payload($body)
|
|
|
|
->send();
|
2019-03-08 06:46:38 +00:00
|
|
|
}
|
2019-08-22 04:14:39 +00:00
|
|
|
}
|