Helpers.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. <?php
  2. namespace App\Util\ActivityPub;
  3. use DB, Cache, Purify, Storage, Request, Validator;
  4. use App\{
  5. Activity,
  6. Follower,
  7. Instance,
  8. Like,
  9. Media,
  10. Notification,
  11. Profile,
  12. Status
  13. };
  14. use Zttp\Zttp;
  15. use Carbon\Carbon;
  16. use GuzzleHttp\Client;
  17. use Illuminate\Http\File;
  18. use Illuminate\Validation\Rule;
  19. use App\Jobs\AvatarPipeline\CreateAvatar;
  20. use App\Jobs\RemoteFollowPipeline\RemoteFollowImportRecent;
  21. use App\Jobs\ImageOptimizePipeline\{ImageOptimize,ImageThumbnail};
  22. use App\Jobs\StatusPipeline\NewStatusPipeline;
  23. use App\Jobs\StatusPipeline\StatusReplyPipeline;
  24. use App\Jobs\StatusPipeline\StatusTagsPipeline;
  25. use App\Util\ActivityPub\HttpSignature;
  26. use Illuminate\Support\Str;
  27. use App\Services\ActivityPubFetchService;
  28. use App\Services\ActivityPubDeliveryService;
  29. use App\Services\CustomEmojiService;
  30. use App\Services\InstanceService;
  31. use App\Services\MediaPathService;
  32. use App\Services\MediaStorageService;
  33. use App\Services\NetworkTimelineService;
  34. use App\Jobs\MediaPipeline\MediaStoragePipeline;
  35. use App\Jobs\AvatarPipeline\RemoteAvatarFetch;
  36. use App\Jobs\HomeFeedPipeline\FeedInsertRemotePipeline;
  37. use App\Util\Media\License;
  38. use App\Models\Poll;
  39. use Illuminate\Contracts\Cache\LockTimeoutException;
  40. use App\Jobs\ProfilePipeline\IncrementPostCount;
  41. use App\Jobs\ProfilePipeline\DecrementPostCount;
  42. use App\Services\DomainService;
  43. use App\Services\UserFilterService;
  44. class Helpers {
  45. public static function validateObject($data)
  46. {
  47. $verbs = ['Create', 'Announce', 'Like', 'Follow', 'Delete', 'Accept', 'Reject', 'Undo', 'Tombstone'];
  48. $valid = Validator::make($data, [
  49. 'type' => [
  50. 'required',
  51. 'string',
  52. Rule::in($verbs)
  53. ],
  54. 'id' => 'required|string',
  55. 'actor' => 'required|string|url',
  56. 'object' => 'required',
  57. 'object.type' => 'required_if:type,Create',
  58. 'object.attributedTo' => 'required_if:type,Create|url',
  59. 'published' => 'required_if:type,Create|date'
  60. ])->passes();
  61. return $valid;
  62. }
  63. public static function verifyAttachments($data)
  64. {
  65. if(!isset($data['object']) || empty($data['object'])) {
  66. $data = ['object'=>$data];
  67. }
  68. $activity = $data['object'];
  69. $mimeTypes = explode(',', config_cache('pixelfed.media_types'));
  70. $mediaTypes = in_array('video/mp4', $mimeTypes) ? ['Document', 'Image', 'Video'] : ['Document', 'Image'];
  71. // Peertube
  72. // $mediaTypes = in_array('video/mp4', $mimeTypes) ? ['Document', 'Image', 'Video', 'Link'] : ['Document', 'Image'];
  73. if(!isset($activity['attachment']) || empty($activity['attachment'])) {
  74. return false;
  75. }
  76. // peertube
  77. // $attachment = is_array($activity['url']) ?
  78. // collect($activity['url'])
  79. // ->filter(function($media) {
  80. // return $media['type'] == 'Link' && $media['mediaType'] == 'video/mp4';
  81. // })
  82. // ->take(1)
  83. // ->values()
  84. // ->toArray()[0] : $activity['attachment'];
  85. $attachment = $activity['attachment'];
  86. $valid = Validator::make($attachment, [
  87. '*.type' => [
  88. 'required',
  89. 'string',
  90. Rule::in($mediaTypes)
  91. ],
  92. '*.url' => 'required|url',
  93. '*.mediaType' => [
  94. 'required',
  95. 'string',
  96. Rule::in($mimeTypes)
  97. ],
  98. '*.name' => 'sometimes|nullable|string',
  99. '*.blurhash' => 'sometimes|nullable|string|min:6|max:164',
  100. '*.width' => 'sometimes|nullable|integer|min:1|max:5000',
  101. '*.height' => 'sometimes|nullable|integer|min:1|max:5000',
  102. ])->passes();
  103. return $valid;
  104. }
  105. public static function normalizeAudience($data, $localOnly = true)
  106. {
  107. if(!isset($data['to'])) {
  108. return;
  109. }
  110. $audience = [];
  111. $audience['to'] = [];
  112. $audience['cc'] = [];
  113. $scope = 'private';
  114. if(is_array($data['to']) && !empty($data['to'])) {
  115. foreach ($data['to'] as $to) {
  116. if($to == 'https://www.w3.org/ns/activitystreams#Public') {
  117. $scope = 'public';
  118. continue;
  119. }
  120. $url = $localOnly ? self::validateLocalUrl($to) : self::validateUrl($to);
  121. if($url != false) {
  122. array_push($audience['to'], $url);
  123. }
  124. }
  125. }
  126. if(is_array($data['cc']) && !empty($data['cc'])) {
  127. foreach ($data['cc'] as $cc) {
  128. if($cc == 'https://www.w3.org/ns/activitystreams#Public') {
  129. $scope = 'unlisted';
  130. continue;
  131. }
  132. $url = $localOnly ? self::validateLocalUrl($cc) : self::validateUrl($cc);
  133. if($url != false) {
  134. array_push($audience['cc'], $url);
  135. }
  136. }
  137. }
  138. $audience['scope'] = $scope;
  139. return $audience;
  140. }
  141. public static function userInAudience($profile, $data)
  142. {
  143. $audience = self::normalizeAudience($data);
  144. $url = $profile->permalink();
  145. return in_array($url, $audience['to']) || in_array($url, $audience['cc']);
  146. }
  147. public static function validateUrl($url)
  148. {
  149. if(is_array($url)) {
  150. $url = $url[0];
  151. }
  152. $hash = hash('sha256', $url);
  153. $key = "helpers:url:valid:sha256-{$hash}";
  154. $valid = Cache::remember($key, 900, function() use($url) {
  155. $localhosts = [
  156. '127.0.0.1', 'localhost', '::1'
  157. ];
  158. if(strtolower(mb_substr($url, 0, 8)) !== 'https://') {
  159. return false;
  160. }
  161. if(substr_count($url, '://') !== 1) {
  162. return false;
  163. }
  164. if(mb_substr($url, 0, 8) !== 'https://') {
  165. $url = 'https://' . substr($url, 8);
  166. }
  167. $valid = filter_var($url, FILTER_VALIDATE_URL);
  168. if(!$valid) {
  169. return false;
  170. }
  171. $host = parse_url($valid, PHP_URL_HOST);
  172. if(in_array($host, $localhosts)) {
  173. return false;
  174. }
  175. if(config('security.url.verify_dns')) {
  176. if(DomainService::hasValidDns($host) === false) {
  177. return false;
  178. }
  179. }
  180. if(app()->environment() === 'production') {
  181. $bannedInstances = InstanceService::getBannedDomains();
  182. if(in_array($host, $bannedInstances)) {
  183. return false;
  184. }
  185. }
  186. return $url;
  187. });
  188. return $valid;
  189. }
  190. public static function validateLocalUrl($url)
  191. {
  192. $url = self::validateUrl($url);
  193. if($url == true) {
  194. $domain = config('pixelfed.domain.app');
  195. $host = parse_url($url, PHP_URL_HOST);
  196. $url = strtolower($domain) === strtolower($host) ? $url : false;
  197. return $url;
  198. }
  199. return false;
  200. }
  201. public static function zttpUserAgent()
  202. {
  203. $version = config('pixelfed.version');
  204. $url = config('app.url');
  205. return [
  206. 'Accept' => 'application/activity+json',
  207. 'User-Agent' => "(Pixelfed/{$version}; +{$url})",
  208. ];
  209. }
  210. public static function fetchFromUrl($url = false)
  211. {
  212. if(self::validateUrl($url) == false) {
  213. return;
  214. }
  215. $hash = hash('sha256', $url);
  216. $key = "helpers:url:fetcher:sha256-{$hash}";
  217. $ttl = now()->addMinutes(15);
  218. return Cache::remember($key, $ttl, function() use($url) {
  219. $res = ActivityPubFetchService::get($url);
  220. if(!$res || empty($res)) {
  221. return false;
  222. }
  223. $res = json_decode($res, true, 8);
  224. if(json_last_error() == JSON_ERROR_NONE) {
  225. return $res;
  226. } else {
  227. return false;
  228. }
  229. });
  230. }
  231. public static function fetchProfileFromUrl($url)
  232. {
  233. return self::fetchFromUrl($url);
  234. }
  235. public static function pluckval($val)
  236. {
  237. if(is_string($val)) {
  238. return $val;
  239. }
  240. if(is_array($val)) {
  241. return !empty($val) ? head($val) : null;
  242. }
  243. return null;
  244. }
  245. public static function statusFirstOrFetch($url, $replyTo = false)
  246. {
  247. $url = self::validateUrl($url);
  248. if($url == false) {
  249. return;
  250. }
  251. $host = parse_url($url, PHP_URL_HOST);
  252. $local = config('pixelfed.domain.app') == $host ? true : false;
  253. if($local) {
  254. $id = (int) last(explode('/', $url));
  255. return Status::whereNotIn('scope', ['draft','archived'])->findOrFail($id);
  256. }
  257. $cached = Status::whereNotIn('scope', ['draft','archived'])
  258. ->whereUri($url)
  259. ->orWhere('object_url', $url)
  260. ->first();
  261. if($cached) {
  262. return $cached;
  263. }
  264. $res = self::fetchFromUrl($url);
  265. if(!$res || empty($res) || isset($res['error']) || !isset($res['@context']) || !isset($res['published']) ) {
  266. return;
  267. }
  268. if(isset($res['object'])) {
  269. $activity = $res;
  270. } else {
  271. $activity = ['object' => $res];
  272. }
  273. $scope = 'private';
  274. $cw = isset($res['sensitive']) ? (bool) $res['sensitive'] : false;
  275. if(isset($res['to']) == true) {
  276. if(is_array($res['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['to'])) {
  277. $scope = 'public';
  278. }
  279. if(is_string($res['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['to']) {
  280. $scope = 'public';
  281. }
  282. }
  283. if(isset($res['cc']) == true) {
  284. if(is_array($res['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $res['cc'])) {
  285. $scope = 'unlisted';
  286. }
  287. if(is_string($res['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $res['cc']) {
  288. $scope = 'unlisted';
  289. }
  290. }
  291. if(config('costar.enabled') == true) {
  292. $blockedKeywords = config('costar.keyword.block');
  293. if($blockedKeywords !== null) {
  294. $keywords = config('costar.keyword.block');
  295. foreach($keywords as $kw) {
  296. if(Str::contains($res['content'], $kw) == true) {
  297. return;
  298. }
  299. }
  300. }
  301. $unlisted = config('costar.domain.unlisted');
  302. if(in_array(parse_url($url, PHP_URL_HOST), $unlisted) == true) {
  303. $unlisted = true;
  304. $scope = 'unlisted';
  305. } else {
  306. $unlisted = false;
  307. }
  308. $cwDomains = config('costar.domain.cw');
  309. if(in_array(parse_url($url, PHP_URL_HOST), $cwDomains) == true) {
  310. $cw = true;
  311. }
  312. }
  313. $id = isset($res['id']) ? self::pluckval($res['id']) : self::pluckval($url);
  314. $idDomain = parse_url($id, PHP_URL_HOST);
  315. $urlDomain = parse_url($url, PHP_URL_HOST);
  316. if(!self::validateUrl($id)) {
  317. return;
  318. }
  319. if(!isset($activity['object']['attributedTo'])) {
  320. return;
  321. }
  322. $attributedTo = is_string($activity['object']['attributedTo']) ?
  323. $activity['object']['attributedTo'] :
  324. (is_array($activity['object']['attributedTo']) ?
  325. collect($activity['object']['attributedTo'])
  326. ->filter(function($o) {
  327. return $o && isset($o['type']) && $o['type'] == 'Person';
  328. })
  329. ->pluck('id')
  330. ->first() : null
  331. );
  332. if($attributedTo) {
  333. $actorDomain = parse_url($attributedTo, PHP_URL_HOST);
  334. if(!self::validateUrl($attributedTo) ||
  335. $idDomain !== $actorDomain ||
  336. $actorDomain !== $urlDomain
  337. )
  338. {
  339. return;
  340. }
  341. }
  342. if($idDomain !== $urlDomain) {
  343. return;
  344. }
  345. $profile = self::profileFirstOrNew($attributedTo);
  346. if(!$profile) {
  347. return;
  348. }
  349. if(isset($activity['object']['inReplyTo']) && !empty($activity['object']['inReplyTo']) || $replyTo == true) {
  350. $reply_to = self::statusFirstOrFetch(self::pluckval($activity['object']['inReplyTo']), false);
  351. if($reply_to) {
  352. $blocks = UserFilterService::blocks($reply_to->profile_id);
  353. if(in_array($profile->id, $blocks)) {
  354. return;
  355. }
  356. }
  357. $reply_to = optional($reply_to)->id;
  358. } else {
  359. $reply_to = null;
  360. }
  361. $ts = self::pluckval($res['published']);
  362. if($scope == 'public' && in_array($urlDomain, InstanceService::getUnlistedDomains())) {
  363. $scope = 'unlisted';
  364. }
  365. if(in_array($urlDomain, InstanceService::getNsfwDomains())) {
  366. $cw = true;
  367. }
  368. if($res['type'] === 'Question') {
  369. $status = self::storePoll(
  370. $profile,
  371. $res,
  372. $url,
  373. $ts,
  374. $reply_to,
  375. $cw,
  376. $scope,
  377. $id
  378. );
  379. return $status;
  380. } else {
  381. $status = self::storeStatus($url, $profile, $res);
  382. }
  383. return $status;
  384. }
  385. public static function storeStatus($url, $profile, $activity)
  386. {
  387. $id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($activity['url']);
  388. $url = isset($activity['url']) && is_string($activity['url']) ? self::pluckval($activity['url']) : self::pluckval($id);
  389. $idDomain = parse_url($id, PHP_URL_HOST);
  390. $urlDomain = parse_url($url, PHP_URL_HOST);
  391. if(!self::validateUrl($id) || !self::validateUrl($url)) {
  392. return;
  393. }
  394. $reply_to = self::getReplyTo($activity);
  395. $ts = self::pluckval($activity['published']);
  396. $scope = self::getScope($activity, $url);
  397. $cw = self::getSensitive($activity, $url);
  398. $pid = is_object($profile) ? $profile->id : (is_array($profile) ? $profile['id'] : null);
  399. $isUnlisted = is_object($profile) ? $profile->unlisted : (is_array($profile) ? $profile['unlisted'] : false);
  400. $commentsDisabled = isset($activity['commentsEnabled']) ? !boolval($activity['commentsEnabled']) : false;
  401. if(!$pid) {
  402. return;
  403. }
  404. if($scope == 'public') {
  405. if($isUnlisted == true) {
  406. $scope = 'unlisted';
  407. }
  408. }
  409. $status = Status::updateOrCreate(
  410. [
  411. 'uri' => $url
  412. ], [
  413. 'profile_id' => $pid,
  414. 'url' => $url,
  415. 'object_url' => $id,
  416. 'caption' => isset($activity['content']) ? Purify::clean(strip_tags($activity['content'])) : null,
  417. 'rendered' => isset($activity['content']) ? Purify::clean($activity['content']) : null,
  418. 'created_at' => Carbon::parse($ts)->tz('UTC'),
  419. 'in_reply_to_id' => $reply_to,
  420. 'local' => false,
  421. 'is_nsfw' => $cw,
  422. 'scope' => $scope,
  423. 'visibility' => $scope,
  424. 'cw_summary' => ($cw == true && isset($activity['summary']) ?
  425. Purify::clean(strip_tags($activity['summary'])) : null),
  426. 'comments_disabled' => $commentsDisabled
  427. ]
  428. );
  429. if($reply_to == null) {
  430. self::importNoteAttachment($activity, $status);
  431. } else {
  432. if(isset($activity['attachment']) && !empty($activity['attachment'])) {
  433. self::importNoteAttachment($activity, $status);
  434. }
  435. StatusReplyPipeline::dispatch($status);
  436. }
  437. if(isset($activity['tag']) && is_array($activity['tag']) && !empty($activity['tag'])) {
  438. StatusTagsPipeline::dispatch($activity, $status);
  439. }
  440. if( config('instance.timeline.network.cached') &&
  441. $status->in_reply_to_id === null &&
  442. $status->reblog_of_id === null &&
  443. in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) &&
  444. $status->created_at->gt(now()->subHours(config('instance.timeline.network.max_hours_old'))) &&
  445. (config('instance.hide_nsfw_on_public_feeds') == true ? $status->is_nsfw == false : true)
  446. ) {
  447. $filteredDomains = collect(InstanceService::getBannedDomains())
  448. ->merge(InstanceService::getUnlistedDomains())
  449. ->unique()
  450. ->values()
  451. ->toArray();
  452. if(!in_array($urlDomain, $filteredDomains)) {
  453. if(!$isUnlisted) {
  454. NetworkTimelineService::add($status->id);
  455. }
  456. }
  457. }
  458. IncrementPostCount::dispatch($pid)->onQueue('low');
  459. if( $status->in_reply_to_id === null &&
  460. in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  461. ) {
  462. FeedInsertRemotePipeline::dispatch($status->id, $pid)->onQueue('feed');
  463. }
  464. return $status;
  465. }
  466. public static function getSensitive($activity, $url)
  467. {
  468. $id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($url);
  469. $url = isset($activity['url']) ? self::pluckval($activity['url']) : $id;
  470. $urlDomain = parse_url($url, PHP_URL_HOST);
  471. $cw = isset($activity['sensitive']) ? (bool) $activity['sensitive'] : false;
  472. if(in_array($urlDomain, InstanceService::getNsfwDomains())) {
  473. $cw = true;
  474. }
  475. return $cw;
  476. }
  477. public static function getReplyTo($activity)
  478. {
  479. $reply_to = null;
  480. $inReplyTo = isset($activity['inReplyTo']) && !empty($activity['inReplyTo']) ?
  481. self::pluckval($activity['inReplyTo']) :
  482. false;
  483. if($inReplyTo) {
  484. $reply_to = self::statusFirstOrFetch($inReplyTo);
  485. if($reply_to) {
  486. $reply_to = optional($reply_to)->id;
  487. }
  488. } else {
  489. $reply_to = null;
  490. }
  491. return $reply_to;
  492. }
  493. public static function getScope($activity, $url)
  494. {
  495. $id = isset($activity['id']) ? self::pluckval($activity['id']) : self::pluckval($url);
  496. $url = isset($activity['url']) ? self::pluckval($activity['url']) : self::pluckval($id);
  497. $urlDomain = parse_url(self::pluckval($url), PHP_URL_HOST);
  498. $scope = 'private';
  499. if(isset($activity['to']) == true) {
  500. if(is_array($activity['to']) && in_array('https://www.w3.org/ns/activitystreams#Public', $activity['to'])) {
  501. $scope = 'public';
  502. }
  503. if(is_string($activity['to']) && 'https://www.w3.org/ns/activitystreams#Public' == $activity['to']) {
  504. $scope = 'public';
  505. }
  506. }
  507. if(isset($activity['cc']) == true) {
  508. if(is_array($activity['cc']) && in_array('https://www.w3.org/ns/activitystreams#Public', $activity['cc'])) {
  509. $scope = 'unlisted';
  510. }
  511. if(is_string($activity['cc']) && 'https://www.w3.org/ns/activitystreams#Public' == $activity['cc']) {
  512. $scope = 'unlisted';
  513. }
  514. }
  515. if($scope == 'public' && in_array($urlDomain, InstanceService::getUnlistedDomains())) {
  516. $scope = 'unlisted';
  517. }
  518. return $scope;
  519. }
  520. private static function storePoll($profile, $res, $url, $ts, $reply_to, $cw, $scope, $id)
  521. {
  522. if(!isset($res['endTime']) || !isset($res['oneOf']) || !is_array($res['oneOf']) || count($res['oneOf']) > 4) {
  523. return;
  524. }
  525. $options = collect($res['oneOf'])->map(function($option) {
  526. return $option['name'];
  527. })->toArray();
  528. $cachedTallies = collect($res['oneOf'])->map(function($option) {
  529. return $option['replies']['totalItems'] ?? 0;
  530. })->toArray();
  531. $status = new Status;
  532. $status->profile_id = $profile->id;
  533. $status->url = isset($res['url']) ? $res['url'] : $url;
  534. $status->uri = isset($res['url']) ? $res['url'] : $url;
  535. $status->object_url = $id;
  536. $status->caption = strip_tags($res['content']);
  537. $status->rendered = Purify::clean($res['content']);
  538. $status->created_at = Carbon::parse($ts)->tz('UTC');
  539. $status->in_reply_to_id = null;
  540. $status->local = false;
  541. $status->is_nsfw = $cw;
  542. $status->scope = 'draft';
  543. $status->visibility = 'draft';
  544. $status->cw_summary = $cw == true && isset($res['summary']) ?
  545. Purify::clean(strip_tags($res['summary'])) : null;
  546. $status->save();
  547. $poll = new Poll;
  548. $poll->status_id = $status->id;
  549. $poll->profile_id = $status->profile_id;
  550. $poll->poll_options = $options;
  551. $poll->cached_tallies = $cachedTallies;
  552. $poll->votes_count = array_sum($cachedTallies);
  553. $poll->expires_at = now()->parse($res['endTime']);
  554. $poll->last_fetched_at = now();
  555. $poll->save();
  556. $status->type = 'poll';
  557. $status->scope = $scope;
  558. $status->visibility = $scope;
  559. $status->save();
  560. return $status;
  561. }
  562. public static function statusFetch($url)
  563. {
  564. return self::statusFirstOrFetch($url);
  565. }
  566. public static function importNoteAttachment($data, Status $status)
  567. {
  568. if(self::verifyAttachments($data) == false) {
  569. // \Log::info('importNoteAttachment::failedVerification.', [$data['id']]);
  570. $status->viewType();
  571. return;
  572. }
  573. $attachments = isset($data['object']) ? $data['object']['attachment'] : $data['attachment'];
  574. // peertube
  575. // if(!$attachments) {
  576. // $obj = isset($data['object']) ? $data['object'] : $data;
  577. // $attachments = is_array($obj['url']) ? $obj['url'] : null;
  578. // }
  579. $user = $status->profile;
  580. $storagePath = MediaPathService::get($user, 2);
  581. $allowed = explode(',', config_cache('pixelfed.media_types'));
  582. foreach($attachments as $key => $media) {
  583. $type = $media['mediaType'];
  584. $url = $media['url'];
  585. $valid = self::validateUrl($url);
  586. if(in_array($type, $allowed) == false || $valid == false) {
  587. continue;
  588. }
  589. $blurhash = isset($media['blurhash']) ? $media['blurhash'] : null;
  590. $license = isset($media['license']) ? License::nameToId($media['license']) : null;
  591. $caption = isset($media['name']) ? Purify::clean($media['name']) : null;
  592. $width = isset($media['width']) ? $media['width'] : false;
  593. $height = isset($media['height']) ? $media['height'] : false;
  594. $media = new Media();
  595. $media->blurhash = $blurhash;
  596. $media->remote_media = true;
  597. $media->status_id = $status->id;
  598. $media->profile_id = $status->profile_id;
  599. $media->user_id = null;
  600. $media->media_path = $url;
  601. $media->remote_url = $url;
  602. $media->caption = $caption;
  603. $media->order = $key + 1;
  604. if($width) {
  605. $media->width = $width;
  606. }
  607. if($height) {
  608. $media->height = $height;
  609. }
  610. if($license) {
  611. $media->license = $license;
  612. }
  613. $media->mime = $type;
  614. $media->version = 3;
  615. $media->save();
  616. if(config_cache('pixelfed.cloud_storage') == true) {
  617. MediaStoragePipeline::dispatch($media);
  618. }
  619. }
  620. $status->viewType();
  621. return;
  622. }
  623. public static function profileFirstOrNew($url)
  624. {
  625. $url = self::validateUrl($url);
  626. if($url == false) {
  627. return;
  628. }
  629. $host = parse_url($url, PHP_URL_HOST);
  630. $local = config('pixelfed.domain.app') == $host ? true : false;
  631. if($local == true) {
  632. $id = last(explode('/', $url));
  633. return Profile::whereNull('status')
  634. ->whereNull('domain')
  635. ->whereUsername($id)
  636. ->firstOrFail();
  637. }
  638. if($profile = Profile::whereRemoteUrl($url)->first()) {
  639. if($profile->last_fetched_at && $profile->last_fetched_at->lt(now()->subHours(24))) {
  640. return self::profileUpdateOrCreate($url);
  641. }
  642. return $profile;
  643. }
  644. return self::profileUpdateOrCreate($url);
  645. }
  646. public static function profileUpdateOrCreate($url)
  647. {
  648. $res = self::fetchProfileFromUrl($url);
  649. if(!$res || isset($res['id']) == false) {
  650. return;
  651. }
  652. $domain = parse_url($res['id'], PHP_URL_HOST);
  653. if(!isset($res['preferredUsername']) && !isset($res['nickname'])) {
  654. return;
  655. }
  656. // skip invalid usernames
  657. if(!ctype_alnum($res['preferredUsername'])) {
  658. $tmpUsername = str_replace(['_', '.', '-'], '', $res['preferredUsername']);
  659. if(!ctype_alnum($tmpUsername)) {
  660. return;
  661. }
  662. }
  663. $username = (string) Purify::clean($res['preferredUsername'] ?? $res['nickname']);
  664. if(empty($username)) {
  665. return;
  666. }
  667. $remoteUsername = $username;
  668. $webfinger = "@{$username}@{$domain}";
  669. if(!self::validateUrl($res['inbox'])) {
  670. return;
  671. }
  672. if(!self::validateUrl($res['id'])) {
  673. return;
  674. }
  675. $instance = Instance::updateOrCreate([
  676. 'domain' => $domain
  677. ]);
  678. if($instance->wasRecentlyCreated == true) {
  679. \App\Jobs\InstancePipeline\FetchNodeinfoPipeline::dispatch($instance)->onQueue('low');
  680. }
  681. $profile = Profile::updateOrCreate(
  682. [
  683. 'domain' => strtolower($domain),
  684. 'username' => Purify::clean($webfinger),
  685. ],
  686. [
  687. 'webfinger' => Purify::clean($webfinger),
  688. 'key_id' => $res['publicKey']['id'],
  689. 'remote_url' => $res['id'],
  690. 'name' => isset($res['name']) ? Purify::clean($res['name']) : 'user',
  691. 'bio' => isset($res['summary']) ? Purify::clean($res['summary']) : null,
  692. 'sharedInbox' => isset($res['endpoints']) && isset($res['endpoints']['sharedInbox']) ? $res['endpoints']['sharedInbox'] : null,
  693. 'inbox_url' => $res['inbox'],
  694. 'outbox_url' => isset($res['outbox']) ? $res['outbox'] : null,
  695. 'public_key' => $res['publicKey']['publicKeyPem'],
  696. 'indexable' => isset($res['indexable']) && is_bool($res['indexable']) ? $res['indexable'] : false,
  697. ]
  698. );
  699. if( $profile->last_fetched_at == null ||
  700. $profile->last_fetched_at->lt(now()->subMonths(3))
  701. ) {
  702. RemoteAvatarFetch::dispatch($profile);
  703. }
  704. $profile->last_fetched_at = now();
  705. $profile->save();
  706. return $profile;
  707. }
  708. public static function profileFetch($url)
  709. {
  710. return self::profileFirstOrNew($url);
  711. }
  712. public static function sendSignedObject($profile, $url, $body)
  713. {
  714. ActivityPubDeliveryService::queue()
  715. ->from($profile)
  716. ->to($url)
  717. ->payload($body)
  718. ->send();
  719. }
  720. }