Inbox.php 43 KB

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