1
0

Inbox.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. RelationshipService::refresh($actor->id, $target->id);
  522. AccountService::del($actor->id);
  523. AccountService::del($target->id);
  524. }
  525. public function handleAnnounceActivity()
  526. {
  527. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  528. $activity = $this->payload['object'];
  529. if (! $actor || $actor->domain == null) {
  530. return;
  531. }
  532. $parent = Helpers::statusFetch($activity);
  533. if (! $parent || empty($parent)) {
  534. return;
  535. }
  536. if (AccountService::blocksDomain($parent->profile_id, $actor->domain) == true) {
  537. return;
  538. }
  539. $blocks = UserFilterService::blocks($parent->profile_id);
  540. if ($blocks && in_array($actor->id, $blocks)) {
  541. return;
  542. }
  543. $status = Status::firstOrCreate([
  544. 'profile_id' => $actor->id,
  545. 'reblog_of_id' => $parent->id,
  546. 'type' => 'share',
  547. 'local' => false,
  548. ]);
  549. Notification::firstOrCreate(
  550. [
  551. 'profile_id' => $parent->profile_id,
  552. 'actor_id' => $actor->id,
  553. 'action' => 'share',
  554. 'item_id' => $parent->id,
  555. 'item_type' => 'App\Status',
  556. ]
  557. );
  558. $parent->reblogs_count = $parent->reblogs_count + 1;
  559. $parent->save();
  560. ReblogService::addPostReblog($parent->profile_id, $status->id);
  561. }
  562. public function handleAcceptActivity()
  563. {
  564. $actor = $this->payload['object']['actor'];
  565. $obj = $this->payload['object']['object'];
  566. $type = $this->payload['object']['type'];
  567. if ($type !== 'Follow') {
  568. return;
  569. }
  570. $actor = Helpers::validateLocalUrl($actor);
  571. $target = Helpers::validateUrl($obj);
  572. if (! $actor || ! $target) {
  573. return;
  574. }
  575. $actor = Helpers::profileFetch($actor);
  576. $target = Helpers::profileFetch($target);
  577. if (! $actor || ! $target) {
  578. return;
  579. }
  580. if (AccountService::blocksDomain($target->id, $actor->domain) == true) {
  581. return;
  582. }
  583. $request = FollowRequest::whereFollowerId($actor->id)
  584. ->whereFollowingId($target->id)
  585. ->whereIsRejected(false)
  586. ->first();
  587. if (! $request) {
  588. return;
  589. }
  590. $follower = Follower::firstOrCreate([
  591. 'profile_id' => $actor->id,
  592. 'following_id' => $target->id,
  593. ]);
  594. FollowPipeline::dispatch($follower)->onQueue('high');
  595. RelationshipService::refresh($actor->id, $target->id);
  596. Cache::forget('profile:following:'.$target->id);
  597. Cache::forget('profile:followers:'.$target->id);
  598. Cache::forget('profile:following:'.$actor->id);
  599. Cache::forget('profile:followers:'.$actor->id);
  600. Cache::forget('profile:follower_count:'.$target->id);
  601. Cache::forget('profile:follower_count:'.$actor->id);
  602. Cache::forget('profile:following_count:'.$target->id);
  603. Cache::forget('profile:following_count:'.$actor->id);
  604. AccountService::del($actor->id);
  605. AccountService::del($target->id);
  606. RelationshipService::get($actor->id, $target->id);
  607. $request->delete();
  608. }
  609. public function handleDeleteActivity()
  610. {
  611. if (! isset(
  612. $this->payload['actor'],
  613. $this->payload['object']
  614. )) {
  615. return;
  616. }
  617. $actor = $this->payload['actor'];
  618. $obj = $this->payload['object'];
  619. if (is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
  620. $profile = Profile::whereRemoteUrl($obj)->first();
  621. if (! $profile || $profile->private_key != null) {
  622. return;
  623. }
  624. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
  625. return;
  626. } else {
  627. if (! isset(
  628. $obj['id'],
  629. $this->payload['object'],
  630. $this->payload['object']['id'],
  631. $this->payload['object']['type']
  632. )) {
  633. return;
  634. }
  635. $type = $this->payload['object']['type'];
  636. $typeCheck = in_array($type, ['Person', 'Tombstone', 'Story']);
  637. if (! Helpers::validateUrl($actor) || ! Helpers::validateUrl($obj['id']) || ! $typeCheck) {
  638. return;
  639. }
  640. if (parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  641. return;
  642. }
  643. $id = $this->payload['object']['id'];
  644. switch ($type) {
  645. case 'Person':
  646. $profile = Profile::whereRemoteUrl($actor)->first();
  647. if (! $profile || $profile->private_key != null) {
  648. return;
  649. }
  650. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('inbox');
  651. return;
  652. break;
  653. case 'Tombstone':
  654. $profile = Profile::whereRemoteUrl($actor)->first();
  655. if (! $profile || $profile->private_key != null) {
  656. return;
  657. }
  658. $status = Status::where('object_url', $id)->first();
  659. if (! $status) {
  660. $status = Status::where('url', $id)->first();
  661. if (! $status) {
  662. return;
  663. }
  664. }
  665. if ($status->profile_id != $profile->id) {
  666. return;
  667. }
  668. if ($status->scope && in_array($status->scope, ['public', 'unlisted', 'private'])) {
  669. if ($status->type && ! in_array($status->type, ['story:reaction', 'story:reply', 'reply'])) {
  670. FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  671. }
  672. }
  673. RemoteStatusDelete::dispatch($status)->onQueue('high');
  674. return;
  675. break;
  676. case 'Story':
  677. $story = Story::whereObjectId($id)
  678. ->first();
  679. if ($story) {
  680. StoryExpire::dispatch($story)->onQueue('story');
  681. }
  682. return;
  683. break;
  684. default:
  685. return;
  686. break;
  687. }
  688. }
  689. }
  690. public function handleLikeActivity()
  691. {
  692. $actor = $this->payload['actor'];
  693. if (! Helpers::validateUrl($actor)) {
  694. return;
  695. }
  696. $profile = self::actorFirstOrCreate($actor);
  697. $obj = $this->payload['object'];
  698. if (! Helpers::validateUrl($obj)) {
  699. return;
  700. }
  701. $status = Helpers::statusFirstOrFetch($obj);
  702. if (! $status || ! $profile) {
  703. return;
  704. }
  705. if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  706. return;
  707. }
  708. $blocks = UserFilterService::blocks($status->profile_id);
  709. if ($blocks && in_array($profile->id, $blocks)) {
  710. return;
  711. }
  712. $like = Like::firstOrCreate([
  713. 'profile_id' => $profile->id,
  714. 'status_id' => $status->id,
  715. ]);
  716. if ($like->wasRecentlyCreated == true) {
  717. $status->likes_count = $status->likes_count + 1;
  718. $status->save();
  719. LikePipeline::dispatch($like);
  720. }
  721. }
  722. public function handleRejectActivity()
  723. {
  724. $actorUrl = $this->payload['actor'];
  725. $obj = $this->payload['object'];
  726. $profileUrl = $obj['actor'];
  727. if (! Helpers::validateUrl($actorUrl) || ! Helpers::validateLocalUrl($profileUrl)) {
  728. return;
  729. }
  730. $actor = Helpers::profileFetch($actorUrl);
  731. $profile = Helpers::profileFetch($profileUrl);
  732. FollowRequest::whereFollowerId($profile->id)->whereFollowingId($actor->id)->forceDelete();
  733. RelationshipService::refresh($actor->id, $profile->id);
  734. }
  735. public function handleUndoActivity()
  736. {
  737. $actor = $this->payload['actor'];
  738. $profile = self::actorFirstOrCreate($actor);
  739. $obj = $this->payload['object'];
  740. if (! $profile) {
  741. return;
  742. }
  743. // TODO: Some implementations do not inline the object, skip for now
  744. if (! $obj || ! is_array($obj) || ! isset($obj['type'])) {
  745. return;
  746. }
  747. switch ($obj['type']) {
  748. case 'Accept':
  749. break;
  750. case 'Announce':
  751. if (is_array($obj) && isset($obj['object'])) {
  752. $obj = $obj['object'];
  753. }
  754. if (! is_string($obj)) {
  755. return;
  756. }
  757. if (Helpers::validateLocalUrl($obj)) {
  758. $parsedId = last(explode('/', $obj));
  759. $status = Status::find($parsedId);
  760. } else {
  761. $status = Status::whereUri($obj)->first();
  762. }
  763. if (! $status) {
  764. return;
  765. }
  766. if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  767. return;
  768. }
  769. FeedRemoveRemotePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  770. Status::whereProfileId($profile->id)
  771. ->whereReblogOfId($status->id)
  772. ->delete();
  773. ReblogService::removePostReblog($profile->id, $status->id);
  774. Notification::whereProfileId($status->profile_id)
  775. ->whereActorId($profile->id)
  776. ->whereAction('share')
  777. ->whereItemId($status->reblog_of_id)
  778. ->whereItemType('App\Status')
  779. ->forceDelete();
  780. break;
  781. case 'Block':
  782. break;
  783. case 'Follow':
  784. $following = self::actorFirstOrCreate($obj['object']);
  785. if (! $following) {
  786. return;
  787. }
  788. if (AccountService::blocksDomain($following->id, $profile->domain) == true) {
  789. return;
  790. }
  791. Follower::whereProfileId($profile->id)
  792. ->whereFollowingId($following->id)
  793. ->delete();
  794. FollowRequest::whereFollowingId($following->id)
  795. ->whereFollowerId($profile->id)
  796. ->forceDelete();
  797. Notification::whereProfileId($following->id)
  798. ->whereActorId($profile->id)
  799. ->whereAction('follow')
  800. ->whereItemId($following->id)
  801. ->whereItemType('App\Profile')
  802. ->forceDelete();
  803. FollowerService::remove($profile->id, $following->id);
  804. RelationshipService::refresh($following->id, $profile->id);
  805. AccountService::del($profile->id);
  806. AccountService::del($following->id);
  807. break;
  808. case 'Like':
  809. $objectUri = $obj['object'];
  810. if (! is_string($objectUri)) {
  811. if (is_array($objectUri) && isset($objectUri['id']) && is_string($objectUri['id'])) {
  812. $objectUri = $objectUri['id'];
  813. } else {
  814. return;
  815. }
  816. }
  817. $status = Helpers::statusFirstOrFetch($objectUri);
  818. if (! $status) {
  819. return;
  820. }
  821. if (AccountService::blocksDomain($status->profile_id, $profile->domain) == true) {
  822. return;
  823. }
  824. Like::whereProfileId($profile->id)
  825. ->whereStatusId($status->id)
  826. ->forceDelete();
  827. Notification::whereProfileId($status->profile_id)
  828. ->whereActorId($profile->id)
  829. ->whereAction('like')
  830. ->whereItemId($status->id)
  831. ->whereItemType('App\Status')
  832. ->forceDelete();
  833. break;
  834. }
  835. }
  836. public function handleViewActivity()
  837. {
  838. if (! isset(
  839. $this->payload['actor'],
  840. $this->payload['object']
  841. )) {
  842. return;
  843. }
  844. $actor = $this->payload['actor'];
  845. $obj = $this->payload['object'];
  846. if (! Helpers::validateUrl($actor)) {
  847. return;
  848. }
  849. if (! $obj || ! is_array($obj)) {
  850. return;
  851. }
  852. if (! isset($obj['type']) || ! isset($obj['object']) || $obj['type'] != 'Story') {
  853. return;
  854. }
  855. if (! Helpers::validateLocalUrl($obj['object'])) {
  856. return;
  857. }
  858. $profile = Helpers::profileFetch($actor);
  859. $storyId = Str::of($obj['object'])->explode('/')->last();
  860. $story = Story::whereActive(true)
  861. ->whereLocal(true)
  862. ->find($storyId);
  863. if (! $story) {
  864. return;
  865. }
  866. if (AccountService::blocksDomain($story->profile_id, $profile->domain) == true) {
  867. return;
  868. }
  869. if (! FollowerService::follows($profile->id, $story->profile_id)) {
  870. return;
  871. }
  872. $view = StoryView::firstOrCreate([
  873. 'story_id' => $story->id,
  874. 'profile_id' => $profile->id,
  875. ]);
  876. if ($view->wasRecentlyCreated == true) {
  877. $story->view_count++;
  878. $story->save();
  879. }
  880. }
  881. public function handleStoryReactionActivity()
  882. {
  883. if (! isset(
  884. $this->payload['actor'],
  885. $this->payload['id'],
  886. $this->payload['inReplyTo'],
  887. $this->payload['content']
  888. )) {
  889. return;
  890. }
  891. $id = $this->payload['id'];
  892. $actor = $this->payload['actor'];
  893. $storyUrl = $this->payload['inReplyTo'];
  894. $to = $this->payload['to'];
  895. $text = Purify::clean($this->payload['content']);
  896. if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  897. return;
  898. }
  899. if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
  900. return;
  901. }
  902. if (! Helpers::validateLocalUrl($storyUrl)) {
  903. return;
  904. }
  905. if (! Helpers::validateLocalUrl($to)) {
  906. return;
  907. }
  908. if (Status::whereObjectUrl($id)->exists()) {
  909. return;
  910. }
  911. $storyId = Str::of($storyUrl)->explode('/')->last();
  912. $targetProfile = Helpers::profileFetch($to);
  913. $story = Story::whereProfileId($targetProfile->id)
  914. ->find($storyId);
  915. if (! $story) {
  916. return;
  917. }
  918. if ($story->can_react == false) {
  919. return;
  920. }
  921. $actorProfile = Helpers::profileFetch($actor);
  922. if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
  923. return;
  924. }
  925. if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  926. return;
  927. }
  928. $url = $id;
  929. if (str_ends_with($url, '/activity')) {
  930. $url = substr($url, 0, -9);
  931. }
  932. $status = new Status;
  933. $status->profile_id = $actorProfile->id;
  934. $status->type = 'story:reaction';
  935. $status->url = $url;
  936. $status->uri = $url;
  937. $status->object_url = $url;
  938. $status->caption = $text;
  939. $status->scope = 'direct';
  940. $status->visibility = 'direct';
  941. $status->in_reply_to_profile_id = $story->profile_id;
  942. $status->entities = json_encode([
  943. 'story_id' => $story->id,
  944. 'reaction' => $text,
  945. ]);
  946. $status->save();
  947. $dm = new DirectMessage;
  948. $dm->to_id = $story->profile_id;
  949. $dm->from_id = $actorProfile->id;
  950. $dm->type = 'story:react';
  951. $dm->status_id = $status->id;
  952. $dm->meta = json_encode([
  953. 'story_username' => $targetProfile->username,
  954. 'story_actor_username' => $actorProfile->username,
  955. 'story_id' => $story->id,
  956. 'story_media_url' => url(Storage::url($story->path)),
  957. 'reaction' => $text,
  958. ]);
  959. $dm->save();
  960. Conversation::updateOrInsert(
  961. [
  962. 'to_id' => $story->profile_id,
  963. 'from_id' => $actorProfile->id,
  964. ],
  965. [
  966. 'type' => 'story:react',
  967. 'status_id' => $status->id,
  968. 'dm_id' => $dm->id,
  969. 'is_hidden' => false,
  970. ]
  971. );
  972. $n = new Notification;
  973. $n->profile_id = $dm->to_id;
  974. $n->actor_id = $dm->from_id;
  975. $n->item_id = $dm->id;
  976. $n->item_type = 'App\DirectMessage';
  977. $n->action = 'story:react';
  978. $n->save();
  979. }
  980. public function handleStoryReplyActivity()
  981. {
  982. if (! isset(
  983. $this->payload['actor'],
  984. $this->payload['id'],
  985. $this->payload['inReplyTo'],
  986. $this->payload['content']
  987. )) {
  988. return;
  989. }
  990. $id = $this->payload['id'];
  991. $actor = $this->payload['actor'];
  992. $storyUrl = $this->payload['inReplyTo'];
  993. $to = $this->payload['to'];
  994. $text = Purify::clean($this->payload['content']);
  995. if (parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  996. return;
  997. }
  998. if (! Helpers::validateUrl($id) || ! Helpers::validateUrl($actor)) {
  999. return;
  1000. }
  1001. if (! Helpers::validateLocalUrl($storyUrl)) {
  1002. return;
  1003. }
  1004. if (! Helpers::validateLocalUrl($to)) {
  1005. return;
  1006. }
  1007. if (Status::whereObjectUrl($id)->exists()) {
  1008. return;
  1009. }
  1010. $storyId = Str::of($storyUrl)->explode('/')->last();
  1011. $targetProfile = Helpers::profileFetch($to);
  1012. $story = Story::whereProfileId($targetProfile->id)
  1013. ->find($storyId);
  1014. if (! $story) {
  1015. return;
  1016. }
  1017. if ($story->can_react == false) {
  1018. return;
  1019. }
  1020. $actorProfile = Helpers::profileFetch($actor);
  1021. if (AccountService::blocksDomain($targetProfile->id, $actorProfile->domain) == true) {
  1022. return;
  1023. }
  1024. if (! FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  1025. return;
  1026. }
  1027. $url = $id;
  1028. if (str_ends_with($url, '/activity')) {
  1029. $url = substr($url, 0, -9);
  1030. }
  1031. $status = new Status;
  1032. $status->profile_id = $actorProfile->id;
  1033. $status->type = 'story:reply';
  1034. $status->caption = $text;
  1035. $status->url = $url;
  1036. $status->uri = $url;
  1037. $status->object_url = $url;
  1038. $status->scope = 'direct';
  1039. $status->visibility = 'direct';
  1040. $status->in_reply_to_profile_id = $story->profile_id;
  1041. $status->entities = json_encode([
  1042. 'story_id' => $story->id,
  1043. 'caption' => $text,
  1044. ]);
  1045. $status->save();
  1046. $dm = new DirectMessage;
  1047. $dm->to_id = $story->profile_id;
  1048. $dm->from_id = $actorProfile->id;
  1049. $dm->type = 'story:comment';
  1050. $dm->status_id = $status->id;
  1051. $dm->meta = json_encode([
  1052. 'story_username' => $targetProfile->username,
  1053. 'story_actor_username' => $actorProfile->username,
  1054. 'story_id' => $story->id,
  1055. 'story_media_url' => url(Storage::url($story->path)),
  1056. 'caption' => $text,
  1057. ]);
  1058. $dm->save();
  1059. Conversation::updateOrInsert(
  1060. [
  1061. 'to_id' => $story->profile_id,
  1062. 'from_id' => $actorProfile->id,
  1063. ],
  1064. [
  1065. 'type' => 'story:comment',
  1066. 'status_id' => $status->id,
  1067. 'dm_id' => $dm->id,
  1068. 'is_hidden' => false,
  1069. ]
  1070. );
  1071. $n = new Notification;
  1072. $n->profile_id = $dm->to_id;
  1073. $n->actor_id = $dm->from_id;
  1074. $n->item_id = $dm->id;
  1075. $n->item_type = 'App\DirectMessage';
  1076. $n->action = 'story:comment';
  1077. $n->save();
  1078. }
  1079. public function handleFlagActivity()
  1080. {
  1081. if (! isset(
  1082. $this->payload['id'],
  1083. $this->payload['type'],
  1084. $this->payload['actor'],
  1085. $this->payload['object']
  1086. )) {
  1087. return;
  1088. }
  1089. $id = $this->payload['id'];
  1090. $actor = $this->payload['actor'];
  1091. if (Helpers::validateLocalUrl($id) || parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  1092. return;
  1093. }
  1094. $content = null;
  1095. if (isset($this->payload['content'])) {
  1096. if (strlen($this->payload['content']) > 5000) {
  1097. $content = Purify::clean(substr($this->payload['content'], 0, 5000).' ... (truncated message due to exceeding max length)');
  1098. } else {
  1099. $content = Purify::clean($this->payload['content']);
  1100. }
  1101. }
  1102. $object = $this->payload['object'];
  1103. if (empty($object) || (! is_array($object) && ! is_string($object))) {
  1104. return;
  1105. }
  1106. if (is_array($object) && count($object) > 100) {
  1107. return;
  1108. }
  1109. $objects = collect([]);
  1110. $accountId = null;
  1111. foreach ($object as $objectUrl) {
  1112. if (! Helpers::validateLocalUrl($objectUrl)) {
  1113. return;
  1114. }
  1115. if (str_contains($objectUrl, '/users/')) {
  1116. $username = last(explode('/', $objectUrl));
  1117. $profileId = Profile::whereUsername($username)->first();
  1118. if ($profileId) {
  1119. $accountId = $profileId->id;
  1120. }
  1121. } elseif (str_contains($objectUrl, '/p/')) {
  1122. $postId = last(explode('/', $objectUrl));
  1123. $objects->push($postId);
  1124. } else {
  1125. continue;
  1126. }
  1127. }
  1128. if (! $accountId && ! $objects->count()) {
  1129. return;
  1130. }
  1131. if ($objects->count()) {
  1132. $obc = $objects->count();
  1133. if ($obc > 25) {
  1134. if ($obc > 30) {
  1135. return;
  1136. } else {
  1137. $objLimit = $objects->take(20);
  1138. $objects = collect($objLimit->all());
  1139. $obc = $objects->count();
  1140. }
  1141. }
  1142. $count = Status::whereProfileId($accountId)->find($objects)->count();
  1143. if ($obc !== $count) {
  1144. return;
  1145. }
  1146. }
  1147. $instanceHost = parse_url($id, PHP_URL_HOST);
  1148. $instance = Instance::updateOrCreate([
  1149. 'domain' => $instanceHost,
  1150. ]);
  1151. $report = new RemoteReport;
  1152. $report->status_ids = $objects->toArray();
  1153. $report->comment = $content;
  1154. $report->account_id = $accountId;
  1155. $report->uri = $id;
  1156. $report->instance_id = $instance->id;
  1157. $report->report_meta = [
  1158. 'actor' => $actor,
  1159. 'object' => $object,
  1160. ];
  1161. $report->save();
  1162. }
  1163. public function handleUpdateActivity()
  1164. {
  1165. $activity = $this->payload['object'];
  1166. if (! isset($activity['type'], $activity['id'])) {
  1167. return;
  1168. }
  1169. if (! Helpers::validateUrl($activity['id'])) {
  1170. return;
  1171. }
  1172. if ($activity['type'] === 'Note') {
  1173. if (Status::whereObjectUrl($activity['id'])->exists()) {
  1174. StatusRemoteUpdatePipeline::dispatch($activity);
  1175. }
  1176. } elseif ($activity['type'] === 'Person') {
  1177. if (UpdatePersonValidator::validate($this->payload)) {
  1178. HandleUpdateActivity::dispatch($this->payload)->onQueue('low');
  1179. }
  1180. }
  1181. }
  1182. public function handleMoveActivity()
  1183. {
  1184. $actor = $this->payload['actor'];
  1185. $activity = $this->payload['object'];
  1186. $target = $this->payload['target'];
  1187. if (
  1188. ! Helpers::validateUrl($actor) ||
  1189. ! Helpers::validateUrl($activity) ||
  1190. ! Helpers::validateUrl($target)
  1191. ) {
  1192. return;
  1193. }
  1194. Bus::chain([
  1195. new ProcessMovePipeline($target, $activity),
  1196. new MoveMigrateFollowersPipeline($target, $activity),
  1197. new UnfollowLegacyAccountMovePipeline($target, $activity),
  1198. new CleanupLegacyAccountMovePipeline($target, $activity),
  1199. ])
  1200. ->catch(function (Throwable $e) {
  1201. Log::error($e);
  1202. })
  1203. ->onQueue('move')
  1204. ->delay(now()->addMinutes(random_int(1, 3)))
  1205. ->dispatch();
  1206. }
  1207. }