Inbox.php 43 KB

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