Inbox.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. <?php
  2. namespace App\Util\ActivityPub;
  3. use Cache, DB, Log, Purify, Redis, Storage, Validator;
  4. use Illuminate\Support\Facades\Bus;
  5. use App\{
  6. Activity,
  7. DirectMessage,
  8. Follower,
  9. FollowRequest,
  10. Instance,
  11. Like,
  12. Notification,
  13. Media,
  14. Profile,
  15. Status,
  16. StatusHashtag,
  17. Story,
  18. StoryView,
  19. UserFilter
  20. };
  21. use Carbon\Carbon;
  22. use App\Util\ActivityPub\Helpers;
  23. use Illuminate\Support\Str;
  24. use App\Jobs\LikePipeline\LikePipeline;
  25. use App\Jobs\FollowPipeline\FollowPipeline;
  26. use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
  27. use App\Jobs\StatusPipeline\RemoteStatusDelete;
  28. use App\Jobs\StoryPipeline\StoryExpire;
  29. use App\Jobs\StoryPipeline\StoryFetch;
  30. use App\Jobs\StatusPipeline\StatusRemoteUpdatePipeline;
  31. use App\Jobs\ProfilePipeline\HandleUpdateActivity;
  32. use App\Jobs\MovePipeline\ProcessMovePipeline;
  33. use App\Jobs\MovePipeline\MoveMigrateFollowersPipeline;
  34. use App\Jobs\MovePipeline\UnfollowLegacyAccountMovePipeline;
  35. use App\Jobs\MovePipeline\CleanupLegacyAccountMovePipeline;
  36. use App\Util\ActivityPub\Validator\Accept as AcceptValidator;
  37. use App\Util\ActivityPub\Validator\Add as AddValidator;
  38. use App\Util\ActivityPub\Validator\Announce as AnnounceValidator;
  39. use App\Util\ActivityPub\Validator\Follow as FollowValidator;
  40. use App\Util\ActivityPub\Validator\Like as LikeValidator;
  41. use App\Util\ActivityPub\Validator\UndoFollow as UndoFollowValidator;
  42. use App\Util\ActivityPub\Validator\UpdatePersonValidator;
  43. use App\Util\ActivityPub\Validator\MoveValidator;
  44. use App\Services\AccountService;
  45. use App\Services\PollService;
  46. use App\Services\FollowerService;
  47. use App\Services\ReblogService;
  48. use App\Services\StatusService;
  49. use App\Services\UserFilterService;
  50. use App\Services\NetworkTimelineService;
  51. use App\Models\Conversation;
  52. use App\Models\RemoteReport;
  53. use App\Jobs\HomeFeedPipeline\FeedRemoveRemotePipeline;
  54. class Inbox
  55. {
  56. protected $headers;
  57. protected $profile;
  58. protected $payload;
  59. protected $logger;
  60. public function __construct($headers, $profile, $payload)
  61. {
  62. $this->headers = $headers;
  63. $this->profile = $profile;
  64. $this->payload = $payload;
  65. }
  66. public function handle()
  67. {
  68. $this->handleVerb();
  69. return;
  70. }
  71. public function handleVerb()
  72. {
  73. $verb = (string) $this->payload['type'];
  74. switch ($verb) {
  75. case 'Add':
  76. $this->handleAddActivity();
  77. break;
  78. case 'Create':
  79. $this->handleCreateActivity();
  80. break;
  81. case 'Follow':
  82. if(FollowValidator::validate($this->payload) == false) { return; }
  83. $this->handleFollowActivity();
  84. break;
  85. case 'Announce':
  86. if(AnnounceValidator::validate($this->payload) == false) { return; }
  87. $this->handleAnnounceActivity();
  88. break;
  89. case 'Accept':
  90. if(AcceptValidator::validate($this->payload) == false) { return; }
  91. $this->handleAcceptActivity();
  92. break;
  93. case 'Delete':
  94. $this->handleDeleteActivity();
  95. break;
  96. case 'Like':
  97. if(LikeValidator::validate($this->payload) == false) { return; }
  98. $this->handleLikeActivity();
  99. break;
  100. case 'Reject':
  101. $this->handleRejectActivity();
  102. break;
  103. case 'Undo':
  104. $this->handleUndoActivity();
  105. break;
  106. case 'View':
  107. $this->handleViewActivity();
  108. break;
  109. case 'Story:Reaction':
  110. $this->handleStoryReactionActivity();
  111. break;
  112. case 'Story:Reply':
  113. $this->handleStoryReplyActivity();
  114. break;
  115. case 'Flag':
  116. $this->handleFlagActivity();
  117. break;
  118. case 'Update':
  119. $this->handleUpdateActivity();
  120. break;
  121. case 'Move':
  122. if(MoveValidator::validate($this->payload) == false) {
  123. \Log::info('[AP][INBOX][MOVE] VALIDATE_FAILURE ' . json_encode($this->payload));
  124. return;
  125. }
  126. $this->handleMoveActivity();
  127. break;
  128. default:
  129. // TODO: decide how to handle invalid verbs.
  130. break;
  131. }
  132. }
  133. public function verifyNoteAttachment()
  134. {
  135. $activity = $this->payload['object'];
  136. if(isset($activity['inReplyTo']) &&
  137. !empty($activity['inReplyTo']) &&
  138. Helpers::validateUrl($activity['inReplyTo'])
  139. ) {
  140. // reply detected, skip attachment check
  141. return true;
  142. }
  143. $valid = Helpers::verifyAttachments($activity);
  144. return $valid;
  145. }
  146. public function actorFirstOrCreate($actorUrl)
  147. {
  148. return Helpers::profileFetch($actorUrl);
  149. }
  150. public function handleAddActivity()
  151. {
  152. // stories ;)
  153. if(!isset(
  154. $this->payload['actor'],
  155. $this->payload['object']
  156. )) {
  157. return;
  158. }
  159. $actor = $this->payload['actor'];
  160. $obj = $this->payload['object'];
  161. if(!Helpers::validateUrl($actor)) {
  162. return;
  163. }
  164. if(!isset($obj['type'])) {
  165. return;
  166. }
  167. switch($obj['type']) {
  168. case 'Story':
  169. StoryFetch::dispatch($this->payload);
  170. break;
  171. }
  172. return;
  173. }
  174. public function handleCreateActivity()
  175. {
  176. $activity = $this->payload['object'];
  177. if(config('autospam.live_filters.enabled')) {
  178. $filters = config('autospam.live_filters.filters');
  179. if(!empty($filters) && isset($activity['content']) && !empty($activity['content']) && strlen($filters) > 3) {
  180. $filters = array_map('trim', explode(',', $filters));
  181. $content = $activity['content'];
  182. foreach($filters as $filter) {
  183. $filter = trim(strtolower($filter));
  184. if(!$filter || !strlen($filter)) {
  185. continue;
  186. }
  187. if(str_contains(strtolower($content), $filter)) {
  188. return;
  189. }
  190. }
  191. }
  192. }
  193. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  194. if(!$actor || $actor->domain == null) {
  195. return;
  196. }
  197. if(!isset($activity['to'])) {
  198. return;
  199. }
  200. $to = isset($activity['to']) ? $activity['to'] : [];
  201. $cc = isset($activity['cc']) ? $activity['cc'] : [];
  202. if($activity['type'] == 'Question') {
  203. $this->handlePollCreate();
  204. return;
  205. }
  206. if( is_array($to) &&
  207. is_array($cc) &&
  208. count($to) == 1 &&
  209. count($cc) == 0 &&
  210. parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
  211. ) {
  212. $this->handleDirectMessage();
  213. return;
  214. }
  215. if($activity['type'] == 'Note' && !empty($activity['inReplyTo'])) {
  216. $this->handleNoteReply();
  217. } elseif ($activity['type'] == 'Note' && !empty($activity['attachment'])) {
  218. if(!$this->verifyNoteAttachment()) {
  219. return;
  220. }
  221. $this->handleNoteCreate();
  222. }
  223. return;
  224. }
  225. public function handleNoteReply()
  226. {
  227. $activity = $this->payload['object'];
  228. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  229. if(!$actor || $actor->domain == null) {
  230. return;
  231. }
  232. $inReplyTo = $activity['inReplyTo'];
  233. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  234. Helpers::statusFirstOrFetch($url, true);
  235. return;
  236. }
  237. public function handlePollCreate()
  238. {
  239. $activity = $this->payload['object'];
  240. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  241. if(!$actor || $actor->domain == null) {
  242. return;
  243. }
  244. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  245. Helpers::statusFirstOrFetch($url);
  246. return;
  247. }
  248. public function handleNoteCreate()
  249. {
  250. $activity = $this->payload['object'];
  251. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  252. if(!$actor || $actor->domain == null) {
  253. return;
  254. }
  255. if( isset($activity['inReplyTo']) &&
  256. isset($activity['name']) &&
  257. !isset($activity['content']) &&
  258. !isset($activity['attachment']) &&
  259. Helpers::validateLocalUrl($activity['inReplyTo'])
  260. ) {
  261. $this->handlePollVote();
  262. return;
  263. }
  264. if($actor->followers_count == 0) {
  265. if(config('federation.activitypub.ingest.store_notes_without_followers')) {
  266. } else if(FollowerService::followerCount($actor->id, true) == 0) {
  267. return;
  268. }
  269. }
  270. $hasUrl = isset($activity['url']);
  271. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  272. if($hasUrl) {
  273. if(Status::whereUri($url)->exists()) {
  274. return;
  275. }
  276. } else {
  277. if(Status::whereObjectUrl($url)->exists()) {
  278. return;
  279. }
  280. }
  281. Helpers::storeStatus(
  282. $url,
  283. $actor,
  284. $activity
  285. );
  286. return;
  287. }
  288. public function handlePollVote()
  289. {
  290. $activity = $this->payload['object'];
  291. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  292. if(!$actor) {
  293. return;
  294. }
  295. $status = Helpers::statusFetch($activity['inReplyTo']);
  296. if(!$status) {
  297. return;
  298. }
  299. $poll = $status->poll;
  300. if(!$poll) {
  301. return;
  302. }
  303. if(now()->gt($poll->expires_at)) {
  304. return;
  305. }
  306. $choices = $poll->poll_options;
  307. $choice = array_search($activity['name'], $choices);
  308. if($choice === false) {
  309. return;
  310. }
  311. if(PollVote::whereStatusId($status->id)->whereProfileId($actor->id)->exists()) {
  312. return;
  313. }
  314. $vote = new PollVote;
  315. $vote->status_id = $status->id;
  316. $vote->profile_id = $actor->id;
  317. $vote->poll_id = $poll->id;
  318. $vote->choice = $choice;
  319. $vote->uri = isset($activity['id']) ? $activity['id'] : null;
  320. $vote->save();
  321. $tallies = $poll->cached_tallies;
  322. $tallies[$choice] = $tallies[$choice] + 1;
  323. $poll->cached_tallies = $tallies;
  324. $poll->votes_count = array_sum($tallies);
  325. $poll->save();
  326. PollService::del($status->id);
  327. return;
  328. }
  329. public function handleDirectMessage()
  330. {
  331. $activity = $this->payload['object'];
  332. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  333. $profile = Profile::whereNull('domain')
  334. ->whereUsername(array_last(explode('/', $activity['to'][0])))
  335. ->firstOrFail();
  336. if(!$actor || in_array($actor->id, $profile->blockedIds()->toArray())) {
  337. return;
  338. }
  339. if(AccountService::blocksDomain($profile->id, $actor->domain) == true) {
  340. return;
  341. }
  342. $msg = $activity['content'];
  343. $msgText = strip_tags($activity['content']);
  344. if(Str::startsWith($msgText, '@' . $profile->username)) {
  345. $len = strlen('@' . $profile->username);
  346. $msgText = substr($msgText, $len + 1);
  347. }
  348. if($profile->user->settings->public_dm == false || $profile->is_private) {
  349. if($profile->follows($actor) == true) {
  350. $hidden = false;
  351. } else {
  352. $hidden = true;
  353. }
  354. } else {
  355. $hidden = false;
  356. }
  357. $status = new Status;
  358. $status->profile_id = $actor->id;
  359. $status->caption = $msgText;
  360. $status->rendered = $msg;
  361. $status->visibility = 'direct';
  362. $status->scope = 'direct';
  363. $status->url = $activity['id'];
  364. $status->uri = $activity['id'];
  365. $status->object_url = $activity['id'];
  366. $status->in_reply_to_profile_id = $profile->id;
  367. $status->save();
  368. $dm = new DirectMessage;
  369. $dm->to_id = $profile->id;
  370. $dm->from_id = $actor->id;
  371. $dm->status_id = $status->id;
  372. $dm->is_hidden = $hidden;
  373. $dm->type = 'text';
  374. $dm->save();
  375. Conversation::updateOrInsert(
  376. [
  377. 'to_id' => $profile->id,
  378. 'from_id' => $actor->id
  379. ],
  380. [
  381. 'type' => 'text',
  382. 'status_id' => $status->id,
  383. 'dm_id' => $dm->id,
  384. 'is_hidden' => $hidden
  385. ]
  386. );
  387. if(count($activity['attachment'])) {
  388. $photos = 0;
  389. $videos = 0;
  390. $allowed = explode(',', config_cache('pixelfed.media_types'));
  391. $activity['attachment'] = array_slice($activity['attachment'], 0, config_cache('pixelfed.max_album_length'));
  392. foreach($activity['attachment'] as $a) {
  393. $type = $a['mediaType'];
  394. $url = $a['url'];
  395. $valid = Helpers::validateUrl($url);
  396. if(in_array($type, $allowed) == false || $valid == false) {
  397. continue;
  398. }
  399. $media = new Media();
  400. $media->remote_media = true;
  401. $media->status_id = $status->id;
  402. $media->profile_id = $status->profile_id;
  403. $media->user_id = null;
  404. $media->media_path = $url;
  405. $media->remote_url = $url;
  406. $media->mime = $type;
  407. $media->save();
  408. if(explode('/', $type)[0] == 'image') {
  409. $photos = $photos + 1;
  410. }
  411. if(explode('/', $type)[0] == 'video') {
  412. $videos = $videos + 1;
  413. }
  414. }
  415. if($photos && $videos == 0) {
  416. $dm->type = $photos == 1 ? 'photo' : 'photos';
  417. $dm->save();
  418. }
  419. if($videos && $photos == 0) {
  420. $dm->type = $videos == 1 ? 'video' : 'videos';
  421. $dm->save();
  422. }
  423. }
  424. if(filter_var($msgText, FILTER_VALIDATE_URL)) {
  425. if(Helpers::validateUrl($msgText)) {
  426. $dm->type = 'link';
  427. $dm->meta = [
  428. 'domain' => parse_url($msgText, PHP_URL_HOST),
  429. 'local' => parse_url($msgText, PHP_URL_HOST) ==
  430. parse_url(config('app.url'), PHP_URL_HOST)
  431. ];
  432. $dm->save();
  433. }
  434. }
  435. $nf = UserFilter::whereUserId($profile->id)
  436. ->whereFilterableId($actor->id)
  437. ->whereFilterableType('App\Profile')
  438. ->whereFilterType('dm.mute')
  439. ->exists();
  440. if($profile->domain == null && $hidden == false && !$nf) {
  441. $notification = new Notification();
  442. $notification->profile_id = $profile->id;
  443. $notification->actor_id = $actor->id;
  444. $notification->action = 'dm';
  445. $notification->item_id = $dm->id;
  446. $notification->item_type = "App\DirectMessage";
  447. $notification->save();
  448. }
  449. return;
  450. }
  451. public function handleFollowActivity()
  452. {
  453. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  454. $target = $this->actorFirstOrCreate($this->payload['object']);
  455. if(!$actor || !$target) {
  456. return;
  457. }
  458. if($actor->domain == null || $target->domain !== null) {
  459. return;
  460. }
  461. if(AccountService::blocksDomain($target->id, $actor->domain) == true) {
  462. return;
  463. }
  464. if(
  465. Follower::whereProfileId($actor->id)
  466. ->whereFollowingId($target->id)
  467. ->exists() ||
  468. FollowRequest::whereFollowerId($actor->id)
  469. ->whereFollowingId($target->id)
  470. ->exists()
  471. ) {
  472. return;
  473. }
  474. $blocks = UserFilterService::blocks($target->id);
  475. if($blocks && in_array($actor->id, $blocks)) {
  476. return;
  477. }
  478. if($target->is_private == true) {
  479. FollowRequest::updateOrCreate([
  480. 'follower_id' => $actor->id,
  481. 'following_id' => $target->id,
  482. ],[
  483. 'activity' => collect($this->payload)->only(['id','actor','object','type'])->toArray()
  484. ]);
  485. } else {
  486. $follower = new Follower;
  487. $follower->profile_id = $actor->id;
  488. $follower->following_id = $target->id;
  489. $follower->local_profile = empty($actor->domain);
  490. $follower->save();
  491. FollowPipeline::dispatch($follower);
  492. FollowerService::add($actor->id, $target->id);
  493. // send Accept to remote profile
  494. $accept = [
  495. '@context' => 'https://www.w3.org/ns/activitystreams',
  496. 'id' => $target->permalink().'#accepts/follows/' . $follower->id,
  497. 'type' => 'Accept',
  498. 'actor' => $target->permalink(),
  499. 'object' => [
  500. 'id' => $this->payload['id'],
  501. 'actor' => $actor->permalink(),
  502. 'type' => 'Follow',
  503. 'object' => $target->permalink()
  504. ]
  505. ];
  506. Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
  507. Cache::forget('profile:follower_count:'.$target->id);
  508. Cache::forget('profile:follower_count:'.$actor->id);
  509. Cache::forget('profile:following_count:'.$target->id);
  510. Cache::forget('profile:following_count:'.$actor->id);
  511. }
  512. return;
  513. }
  514. public function handleAnnounceActivity()
  515. {
  516. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  517. $activity = $this->payload['object'];
  518. if(!$actor || $actor->domain == null) {
  519. return;
  520. }
  521. $parent = Helpers::statusFetch($activity);
  522. if(!$parent || empty($parent)) {
  523. return;
  524. }
  525. if(AccountService::blocksDomain($parent->profile_id, $actor->domain) == true) {
  526. return;
  527. }
  528. $blocks = UserFilterService::blocks($parent->profile_id);
  529. if($blocks && in_array($actor->id, $blocks)) {
  530. return;
  531. }
  532. $status = Status::firstOrCreate([
  533. 'profile_id' => $actor->id,
  534. 'reblog_of_id' => $parent->id,
  535. 'type' => 'share'
  536. ]);
  537. Notification::firstOrCreate(
  538. [
  539. 'profile_id' => $parent->profile_id,
  540. 'actor_id' => $actor->id,
  541. 'action' => 'share',
  542. 'item_id' => $parent->id,
  543. 'item_type' => 'App\Status',
  544. ]
  545. );
  546. $parent->reblogs_count = $parent->reblogs_count + 1;
  547. $parent->save();
  548. ReblogService::addPostReblog($parent->profile_id, $status->id);
  549. return;
  550. }
  551. public function handleAcceptActivity()
  552. {
  553. $actor = $this->payload['object']['actor'];
  554. $obj = $this->payload['object']['object'];
  555. $type = $this->payload['object']['type'];
  556. if($type !== 'Follow') {
  557. return;
  558. }
  559. $actor = Helpers::validateLocalUrl($actor);
  560. $target = Helpers::validateUrl($obj);
  561. if(!$actor || !$target) {
  562. return;
  563. }
  564. $actor = Helpers::profileFetch($actor);
  565. $target = Helpers::profileFetch($target);
  566. if(!$actor || !$target) {
  567. return;
  568. }
  569. if(AccountService::blocksDomain($target->id, $actor->domain) == true) {
  570. return;
  571. }
  572. $request = FollowRequest::whereFollowerId($actor->id)
  573. ->whereFollowingId($target->id)
  574. ->whereIsRejected(false)
  575. ->first();
  576. if(!$request) {
  577. return;
  578. }
  579. $follower = Follower::firstOrCreate([
  580. 'profile_id' => $actor->id,
  581. 'following_id' => $target->id,
  582. ]);
  583. FollowPipeline::dispatch($follower);
  584. $request->delete();
  585. return;
  586. }
  587. public function handleDeleteActivity()
  588. {
  589. if(!isset(
  590. $this->payload['actor'],
  591. $this->payload['object']
  592. )) {
  593. return;
  594. }
  595. $actor = $this->payload['actor'];
  596. $obj = $this->payload['object'];
  597. if(is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
  598. $profile = Profile::whereRemoteUrl($obj)->first();
  599. if(!$profile || $profile->private_key != null) {
  600. return;
  601. }
  602. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
  603. return;
  604. } else {
  605. if(!isset(
  606. $obj['id'],
  607. $this->payload['object'],
  608. $this->payload['object']['id'],
  609. $this->payload['object']['type']
  610. )) {
  611. return;
  612. }
  613. $type = $this->payload['object']['type'];
  614. $typeCheck = in_array($type, ['Person', 'Tombstone', 'Story']);
  615. if(!Helpers::validateUrl($actor) || !Helpers::validateUrl($obj['id']) || !$typeCheck) {
  616. return;
  617. }
  618. if(parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  619. return;
  620. }
  621. $id = $this->payload['object']['id'];
  622. switch ($type) {
  623. case 'Person':
  624. $profile = Profile::whereRemoteUrl($actor)->first();
  625. if(!$profile || $profile->private_key != null) {
  626. return;
  627. }
  628. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
  629. return;
  630. break;
  631. case 'Tombstone':
  632. $profile = Profile::whereRemoteUrl($actor)->first();
  633. if(!$profile || $profile->private_key != null) {
  634. return;
  635. }
  636. $status = Status::where('object_url', $id)->first();
  637. if(!$status) {
  638. $status = Status::where('url', $id)->first();
  639. if(!$status) {
  640. return;
  641. }
  642. }
  643. if($status->profile_id != $profile->id) {
  644. return;
  645. }
  646. if($status->scope && in_array($status->scope, ['public', 'unlisted', 'private'])) {
  647. if($status->type && !in_array($status->type, ['story:reaction', 'story:reply', 'reply'])) {
  648. FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  649. }
  650. }
  651. RemoteStatusDelete::dispatch($status)->onQueue('high');
  652. return;
  653. break;
  654. case 'Story':
  655. $story = Story::whereObjectId($id)
  656. ->first();
  657. if($story) {
  658. StoryExpire::dispatch($story)->onQueue('story');
  659. }
  660. return;
  661. break;
  662. default:
  663. return;
  664. break;
  665. }
  666. }
  667. return;
  668. }
  669. public function handleLikeActivity()
  670. {
  671. $actor = $this->payload['actor'];
  672. if(!Helpers::validateUrl($actor)) {
  673. return;
  674. }
  675. $profile = self::actorFirstOrCreate($actor);
  676. $obj = $this->payload['object'];
  677. if(!Helpers::validateUrl($obj)) {
  678. return;
  679. }
  680. $status = Helpers::statusFirstOrFetch($obj);
  681. if(!$status || !$profile) {
  682. return;
  683. }
  684. if(AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  685. return;
  686. }
  687. $blocks = UserFilterService::blocks($status->profile_id);
  688. if($blocks && in_array($profile->id, $blocks)) {
  689. return;
  690. }
  691. $like = Like::firstOrCreate([
  692. 'profile_id' => $profile->id,
  693. 'status_id' => $status->id
  694. ]);
  695. if($like->wasRecentlyCreated == true) {
  696. $status->likes_count = $status->likes_count + 1;
  697. $status->save();
  698. LikePipeline::dispatch($like);
  699. }
  700. return;
  701. }
  702. public function handleRejectActivity()
  703. {
  704. }
  705. public function handleUndoActivity()
  706. {
  707. $actor = $this->payload['actor'];
  708. $profile = self::actorFirstOrCreate($actor);
  709. $obj = $this->payload['object'];
  710. if(!$profile) {
  711. return;
  712. }
  713. // TODO: Some implementations do not inline the object, skip for now
  714. if(!$obj || !is_array($obj) || !isset($obj['type'])) {
  715. return;
  716. }
  717. switch ($obj['type']) {
  718. case 'Accept':
  719. break;
  720. case 'Announce':
  721. if(is_array($obj) && isset($obj['object'])) {
  722. $obj = $obj['object'];
  723. }
  724. if(!is_string($obj)) {
  725. return;
  726. }
  727. if(Helpers::validateLocalUrl($obj)) {
  728. $parsedId = last(explode('/', $obj));
  729. $status = Status::find($parsedId);
  730. } else {
  731. $status = Status::whereUri($obj)->first();
  732. }
  733. if(!$status) {
  734. return;
  735. }
  736. if(AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  737. return;
  738. }
  739. FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  740. Status::whereProfileId($profile->id)
  741. ->whereReblogOfId($status->id)
  742. ->delete();
  743. ReblogService::removePostReblog($profile->id, $status->id);
  744. Notification::whereProfileId($status->profile_id)
  745. ->whereActorId($profile->id)
  746. ->whereAction('share')
  747. ->whereItemId($status->reblog_of_id)
  748. ->whereItemType('App\Status')
  749. ->forceDelete();
  750. break;
  751. case 'Block':
  752. break;
  753. case 'Follow':
  754. $following = self::actorFirstOrCreate($obj['object']);
  755. if(!$following) {
  756. return;
  757. }
  758. if(AccountService::blocksDomain($following->id, $profile->domain) == true) {
  759. return;
  760. }
  761. Follower::whereProfileId($profile->id)
  762. ->whereFollowingId($following->id)
  763. ->delete();
  764. Notification::whereProfileId($following->id)
  765. ->whereActorId($profile->id)
  766. ->whereAction('follow')
  767. ->whereItemId($following->id)
  768. ->whereItemType('App\Profile')
  769. ->forceDelete();
  770. FollowerService::remove($profile->id, $following->id);
  771. break;
  772. case 'Like':
  773. $objectUri = $obj['object'];
  774. if(!is_string($objectUri)) {
  775. if(is_array($objectUri) && isset($objectUri['id']) && is_string($objectUri['id'])) {
  776. $objectUri = $objectUri['id'];
  777. } else {
  778. return;
  779. }
  780. }
  781. $status = Helpers::statusFirstOrFetch($objectUri);
  782. if(!$status) {
  783. return;
  784. }
  785. if(AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  786. return;
  787. }
  788. Like::whereProfileId($profile->id)
  789. ->whereStatusId($status->id)
  790. ->forceDelete();
  791. Notification::whereProfileId($status->profile_id)
  792. ->whereActorId($profile->id)
  793. ->whereAction('like')
  794. ->whereItemId($status->id)
  795. ->whereItemType('App\Status')
  796. ->forceDelete();
  797. break;
  798. }
  799. return;
  800. }
  801. public function handleViewActivity()
  802. {
  803. if(!isset(
  804. $this->payload['actor'],
  805. $this->payload['object']
  806. )) {
  807. return;
  808. }
  809. $actor = $this->payload['actor'];
  810. $obj = $this->payload['object'];
  811. if(!Helpers::validateUrl($actor)) {
  812. return;
  813. }
  814. if(!$obj || !is_array($obj)) {
  815. return;
  816. }
  817. if(!isset($obj['type']) || !isset($obj['object']) || $obj['type'] != 'Story') {
  818. return;
  819. }
  820. if(!Helpers::validateLocalUrl($obj['object'])) {
  821. return;
  822. }
  823. $profile = Helpers::profileFetch($actor);
  824. $storyId = Str::of($obj['object'])->explode('/')->last();
  825. $story = Story::whereActive(true)
  826. ->whereLocal(true)
  827. ->find($storyId);
  828. if(!$story) {
  829. return;
  830. }
  831. if(AccountService::blocksDomain($story->profile_id, $profile->domain) == true) {
  832. return;
  833. }
  834. if(!FollowerService::follows($profile->id, $story->profile_id)) {
  835. return;
  836. }
  837. $view = StoryView::firstOrCreate([
  838. 'story_id' => $story->id,
  839. 'profile_id' => $profile->id
  840. ]);
  841. if($view->wasRecentlyCreated == true) {
  842. $story->view_count++;
  843. $story->save();
  844. }
  845. return;
  846. }
  847. public function handleStoryReactionActivity()
  848. {
  849. if(!isset(
  850. $this->payload['actor'],
  851. $this->payload['id'],
  852. $this->payload['inReplyTo'],
  853. $this->payload['content']
  854. )) {
  855. return;
  856. }
  857. $id = $this->payload['id'];
  858. $actor = $this->payload['actor'];
  859. $storyUrl = $this->payload['inReplyTo'];
  860. $to = $this->payload['to'];
  861. $text = Purify::clean($this->payload['content']);
  862. if(parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  863. return;
  864. }
  865. if(!Helpers::validateUrl($id) || !Helpers::validateUrl($actor)) {
  866. return;
  867. }
  868. if(!Helpers::validateLocalUrl($storyUrl)) {
  869. return;
  870. }
  871. if(!Helpers::validateLocalUrl($to)) {
  872. return;
  873. }
  874. if(Status::whereObjectUrl($id)->exists()) {
  875. return;
  876. }
  877. $storyId = Str::of($storyUrl)->explode('/')->last();
  878. $targetProfile = Helpers::profileFetch($to);
  879. $story = Story::whereProfileId($targetProfile->id)
  880. ->find($storyId);
  881. if(!$story) {
  882. return;
  883. }
  884. if($story->can_react == false) {
  885. return;
  886. }
  887. $actorProfile = Helpers::profileFetch($actor);
  888. if(AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
  889. return;
  890. }
  891. if(!FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  892. return;
  893. }
  894. $url = $id;
  895. if(str_ends_with($url, '/activity')) {
  896. $url = substr($url, 0, -9);
  897. }
  898. $status = new Status;
  899. $status->profile_id = $actorProfile->id;
  900. $status->type = 'story:reaction';
  901. $status->url = $url;
  902. $status->uri = $url;
  903. $status->object_url = $url;
  904. $status->caption = $text;
  905. $status->rendered = $text;
  906. $status->scope = 'direct';
  907. $status->visibility = 'direct';
  908. $status->in_reply_to_profile_id = $story->profile_id;
  909. $status->entities = json_encode([
  910. 'story_id' => $story->id,
  911. 'reaction' => $text
  912. ]);
  913. $status->save();
  914. $dm = new DirectMessage;
  915. $dm->to_id = $story->profile_id;
  916. $dm->from_id = $actorProfile->id;
  917. $dm->type = 'story:react';
  918. $dm->status_id = $status->id;
  919. $dm->meta = json_encode([
  920. 'story_username' => $targetProfile->username,
  921. 'story_actor_username' => $actorProfile->username,
  922. 'story_id' => $story->id,
  923. 'story_media_url' => url(Storage::url($story->path)),
  924. 'reaction' => $text
  925. ]);
  926. $dm->save();
  927. Conversation::updateOrInsert(
  928. [
  929. 'to_id' => $story->profile_id,
  930. 'from_id' => $actorProfile->id
  931. ],
  932. [
  933. 'type' => 'story:react',
  934. 'status_id' => $status->id,
  935. 'dm_id' => $dm->id,
  936. 'is_hidden' => false
  937. ]
  938. );
  939. $n = new Notification;
  940. $n->profile_id = $dm->to_id;
  941. $n->actor_id = $dm->from_id;
  942. $n->item_id = $dm->id;
  943. $n->item_type = 'App\DirectMessage';
  944. $n->action = 'story:react';
  945. $n->save();
  946. return;
  947. }
  948. public function handleStoryReplyActivity()
  949. {
  950. if(!isset(
  951. $this->payload['actor'],
  952. $this->payload['id'],
  953. $this->payload['inReplyTo'],
  954. $this->payload['content']
  955. )) {
  956. return;
  957. }
  958. $id = $this->payload['id'];
  959. $actor = $this->payload['actor'];
  960. $storyUrl = $this->payload['inReplyTo'];
  961. $to = $this->payload['to'];
  962. $text = Purify::clean($this->payload['content']);
  963. if(parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  964. return;
  965. }
  966. if(!Helpers::validateUrl($id) || !Helpers::validateUrl($actor)) {
  967. return;
  968. }
  969. if(!Helpers::validateLocalUrl($storyUrl)) {
  970. return;
  971. }
  972. if(!Helpers::validateLocalUrl($to)) {
  973. return;
  974. }
  975. if(Status::whereObjectUrl($id)->exists()) {
  976. return;
  977. }
  978. $storyId = Str::of($storyUrl)->explode('/')->last();
  979. $targetProfile = Helpers::profileFetch($to);
  980. $story = Story::whereProfileId($targetProfile->id)
  981. ->find($storyId);
  982. if(!$story) {
  983. return;
  984. }
  985. if($story->can_react == false) {
  986. return;
  987. }
  988. $actorProfile = Helpers::profileFetch($actor);
  989. if(AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
  990. return;
  991. }
  992. if(!FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  993. return;
  994. }
  995. $url = $id;
  996. if(str_ends_with($url, '/activity')) {
  997. $url = substr($url, 0, -9);
  998. }
  999. $status = new Status;
  1000. $status->profile_id = $actorProfile->id;
  1001. $status->type = 'story:reply';
  1002. $status->caption = $text;
  1003. $status->rendered = $text;
  1004. $status->url = $url;
  1005. $status->uri = $url;
  1006. $status->object_url = $url;
  1007. $status->scope = 'direct';
  1008. $status->visibility = 'direct';
  1009. $status->in_reply_to_profile_id = $story->profile_id;
  1010. $status->entities = json_encode([
  1011. 'story_id' => $story->id,
  1012. 'caption' => $text
  1013. ]);
  1014. $status->save();
  1015. $dm = new DirectMessage;
  1016. $dm->to_id = $story->profile_id;
  1017. $dm->from_id = $actorProfile->id;
  1018. $dm->type = 'story:comment';
  1019. $dm->status_id = $status->id;
  1020. $dm->meta = json_encode([
  1021. 'story_username' => $targetProfile->username,
  1022. 'story_actor_username' => $actorProfile->username,
  1023. 'story_id' => $story->id,
  1024. 'story_media_url' => url(Storage::url($story->path)),
  1025. 'caption' => $text
  1026. ]);
  1027. $dm->save();
  1028. Conversation::updateOrInsert(
  1029. [
  1030. 'to_id' => $story->profile_id,
  1031. 'from_id' => $actorProfile->id
  1032. ],
  1033. [
  1034. 'type' => 'story:comment',
  1035. 'status_id' => $status->id,
  1036. 'dm_id' => $dm->id,
  1037. 'is_hidden' => false
  1038. ]
  1039. );
  1040. $n = new Notification;
  1041. $n->profile_id = $dm->to_id;
  1042. $n->actor_id = $dm->from_id;
  1043. $n->item_id = $dm->id;
  1044. $n->item_type = 'App\DirectMessage';
  1045. $n->action = 'story:comment';
  1046. $n->save();
  1047. return;
  1048. }
  1049. public function handleFlagActivity()
  1050. {
  1051. if(!isset(
  1052. $this->payload['id'],
  1053. $this->payload['type'],
  1054. $this->payload['actor'],
  1055. $this->payload['object']
  1056. )) {
  1057. return;
  1058. }
  1059. $id = $this->payload['id'];
  1060. $actor = $this->payload['actor'];
  1061. if(Helpers::validateLocalUrl($id) || parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  1062. return;
  1063. }
  1064. $content = null;
  1065. if(isset($this->payload['content'])) {
  1066. if(strlen($this->payload['content']) > 5000) {
  1067. $content = Purify::clean(substr($this->payload['content'], 0, 5000) . ' ... (truncated message due to exceeding max length)');
  1068. } else {
  1069. $content = Purify::clean($this->payload['content']);
  1070. }
  1071. }
  1072. $object = $this->payload['object'];
  1073. if(empty($object) || (!is_array($object) && !is_string($object))) {
  1074. return;
  1075. }
  1076. if(is_array($object) && count($object) > 100) {
  1077. return;
  1078. }
  1079. $objects = collect([]);
  1080. $accountId = null;
  1081. foreach($object as $objectUrl) {
  1082. if(!Helpers::validateLocalUrl($objectUrl)) {
  1083. return;
  1084. }
  1085. if(str_contains($objectUrl, '/users/')) {
  1086. $username = last(explode('/', $objectUrl));
  1087. $profileId = Profile::whereUsername($username)->first();
  1088. if($profileId) {
  1089. $accountId = $profileId->id;
  1090. }
  1091. } else if(str_contains($objectUrl, '/p/')) {
  1092. $postId = last(explode('/', $objectUrl));
  1093. $objects->push($postId);
  1094. } else {
  1095. continue;
  1096. }
  1097. }
  1098. if(!$accountId && !$objects->count()) {
  1099. return;
  1100. }
  1101. if($objects->count()) {
  1102. $obc = $objects->count();
  1103. if($obc > 25) {
  1104. if($obc > 30) {
  1105. return;
  1106. } else {
  1107. $objLimit = $objects->take(20);
  1108. $objects = collect($objLimit->all());
  1109. $obc = $objects->count();
  1110. }
  1111. }
  1112. $count = Status::whereProfileId($accountId)->find($objects)->count();
  1113. if($obc !== $count) {
  1114. return;
  1115. }
  1116. }
  1117. $instanceHost = parse_url($id, PHP_URL_HOST);
  1118. $instance = Instance::updateOrCreate([
  1119. 'domain' => $instanceHost
  1120. ]);
  1121. $report = new RemoteReport;
  1122. $report->status_ids = $objects->toArray();
  1123. $report->comment = $content;
  1124. $report->account_id = $accountId;
  1125. $report->uri = $id;
  1126. $report->instance_id = $instance->id;
  1127. $report->report_meta = [
  1128. 'actor' => $actor,
  1129. 'object' => $object
  1130. ];
  1131. $report->save();
  1132. return;
  1133. }
  1134. public function handleUpdateActivity()
  1135. {
  1136. $activity = $this->payload['object'];
  1137. if(!isset($activity['type'], $activity['id'])) {
  1138. return;
  1139. }
  1140. if(!Helpers::validateUrl($activity['id'])) {
  1141. return;
  1142. }
  1143. if($activity['type'] === 'Note') {
  1144. if(Status::whereObjectUrl($activity['id'])->exists()) {
  1145. StatusRemoteUpdatePipeline::dispatch($activity);
  1146. }
  1147. } else if ($activity['type'] === 'Person') {
  1148. if(UpdatePersonValidator::validate($this->payload)) {
  1149. HandleUpdateActivity::dispatch($this->payload)->onQueue('low');
  1150. }
  1151. }
  1152. }
  1153. public function handleMoveActivity()
  1154. {
  1155. $actor = $this->payload['actor'];
  1156. $activity = $this->payload['object'];
  1157. $target = $this->payload['target'];
  1158. if(
  1159. !Helpers::validateUrl($actor) ||
  1160. !Helpers::validateUrl($activity) ||
  1161. !Helpers::validateUrl($target)
  1162. ) {
  1163. return;
  1164. }
  1165. Bus::chain([
  1166. new ProcessMoveActivity,
  1167. new MoveMigrateFollowersPipeline,
  1168. new UnfollowLegacyAccountMovePipeline,
  1169. new CleanupLegacyAccountMovePipeline
  1170. ])
  1171. ->onQueue('move')
  1172. ->dispatch($target, $activity);
  1173. }
  1174. }