Inbox.php 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. <?php
  2. namespace App\Util\ActivityPub;
  3. use Cache, DB, Log, Purify, Redis, Storage, Validator;
  4. use App\{
  5. Activity,
  6. DirectMessage,
  7. Follower,
  8. FollowRequest,
  9. Like,
  10. Notification,
  11. Media,
  12. Profile,
  13. Status,
  14. StatusHashtag,
  15. Story,
  16. StoryView,
  17. UserFilter
  18. };
  19. use Carbon\Carbon;
  20. use App\Util\ActivityPub\Helpers;
  21. use Illuminate\Support\Str;
  22. use App\Jobs\LikePipeline\LikePipeline;
  23. use App\Jobs\FollowPipeline\FollowPipeline;
  24. use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
  25. use App\Jobs\DeletePipeline\DeleteRemoteStatusPipeline;
  26. use App\Jobs\StoryPipeline\StoryExpire;
  27. use App\Jobs\StoryPipeline\StoryFetch;
  28. use App\Util\ActivityPub\Validator\Accept as AcceptValidator;
  29. use App\Util\ActivityPub\Validator\Add as AddValidator;
  30. use App\Util\ActivityPub\Validator\Announce as AnnounceValidator;
  31. use App\Util\ActivityPub\Validator\Follow as FollowValidator;
  32. use App\Util\ActivityPub\Validator\Like as LikeValidator;
  33. use App\Util\ActivityPub\Validator\UndoFollow as UndoFollowValidator;
  34. use App\Services\PollService;
  35. use App\Services\FollowerService;
  36. use App\Services\StatusService;
  37. use App\Services\UserFilterService;
  38. use App\Services\NetworkTimelineService;
  39. use App\Models\Conversation;
  40. use App\Jobs\ProfilePipeline\IncrementPostCount;
  41. use App\Jobs\ProfilePipeline\DecrementPostCount;
  42. class Inbox
  43. {
  44. protected $headers;
  45. protected $profile;
  46. protected $payload;
  47. protected $logger;
  48. public function __construct($headers, $profile, $payload)
  49. {
  50. $this->headers = $headers;
  51. $this->profile = $profile;
  52. $this->payload = $payload;
  53. }
  54. public function handle()
  55. {
  56. $this->handleVerb();
  57. return;
  58. }
  59. public function handleVerb()
  60. {
  61. $verb = (string) $this->payload['type'];
  62. switch ($verb) {
  63. case 'Add':
  64. $this->handleAddActivity();
  65. break;
  66. case 'Create':
  67. $this->handleCreateActivity();
  68. break;
  69. case 'Follow':
  70. if(FollowValidator::validate($this->payload) == false) { return; }
  71. $this->handleFollowActivity();
  72. break;
  73. case 'Announce':
  74. if(AnnounceValidator::validate($this->payload) == false) { return; }
  75. $this->handleAnnounceActivity();
  76. break;
  77. case 'Accept':
  78. if(AcceptValidator::validate($this->payload) == false) { return; }
  79. $this->handleAcceptActivity();
  80. break;
  81. case 'Delete':
  82. $this->handleDeleteActivity();
  83. break;
  84. case 'Like':
  85. if(LikeValidator::validate($this->payload) == false) { return; }
  86. $this->handleLikeActivity();
  87. break;
  88. case 'Reject':
  89. $this->handleRejectActivity();
  90. break;
  91. case 'Undo':
  92. $this->handleUndoActivity();
  93. break;
  94. case 'View':
  95. $this->handleViewActivity();
  96. break;
  97. case 'Story:Reaction':
  98. $this->handleStoryReactionActivity();
  99. break;
  100. case 'Story:Reply':
  101. $this->handleStoryReplyActivity();
  102. break;
  103. // case 'Update':
  104. // (new UpdateActivity($this->payload, $this->profile))->handle();
  105. // break;
  106. default:
  107. // TODO: decide how to handle invalid verbs.
  108. break;
  109. }
  110. }
  111. public function verifyNoteAttachment()
  112. {
  113. $activity = $this->payload['object'];
  114. if(isset($activity['inReplyTo']) &&
  115. !empty($activity['inReplyTo']) &&
  116. Helpers::validateUrl($activity['inReplyTo'])
  117. ) {
  118. // reply detected, skip attachment check
  119. return true;
  120. }
  121. $valid = Helpers::verifyAttachments($activity);
  122. return $valid;
  123. }
  124. public function actorFirstOrCreate($actorUrl)
  125. {
  126. return Helpers::profileFetch($actorUrl);
  127. }
  128. public function handleAddActivity()
  129. {
  130. // stories ;)
  131. if(!isset(
  132. $this->payload['actor'],
  133. $this->payload['object']
  134. )) {
  135. return;
  136. }
  137. $actor = $this->payload['actor'];
  138. $obj = $this->payload['object'];
  139. if(!Helpers::validateUrl($actor)) {
  140. return;
  141. }
  142. if(!isset($obj['type'])) {
  143. return;
  144. }
  145. switch($obj['type']) {
  146. case 'Story':
  147. StoryFetch::dispatchNow($this->payload);
  148. break;
  149. }
  150. return;
  151. }
  152. public function handleCreateActivity()
  153. {
  154. $activity = $this->payload['object'];
  155. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  156. if(!$actor || $actor->domain == null) {
  157. return;
  158. }
  159. if(!isset($activity['to'])) {
  160. return;
  161. }
  162. $to = isset($activity['to']) ? $activity['to'] : [];
  163. $cc = isset($activity['cc']) ? $activity['cc'] : [];
  164. if($activity['type'] == 'Question') {
  165. $this->handlePollCreate();
  166. return;
  167. }
  168. if( is_array($to) &&
  169. is_array($cc) &&
  170. count($to) == 1 &&
  171. count($cc) == 0 &&
  172. parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
  173. ) {
  174. $this->handleDirectMessage();
  175. return;
  176. }
  177. if($activity['type'] == 'Note' && !empty($activity['inReplyTo'])) {
  178. $this->handleNoteReply();
  179. } elseif ($activity['type'] == 'Note' && !empty($activity['attachment'])) {
  180. if(!$this->verifyNoteAttachment()) {
  181. return;
  182. }
  183. $this->handleNoteCreate();
  184. }
  185. return;
  186. }
  187. public function handleNoteReply()
  188. {
  189. $activity = $this->payload['object'];
  190. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  191. if(!$actor || $actor->domain == null) {
  192. return;
  193. }
  194. $inReplyTo = $activity['inReplyTo'];
  195. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  196. Helpers::statusFirstOrFetch($url, true);
  197. return;
  198. }
  199. public function handlePollCreate()
  200. {
  201. $activity = $this->payload['object'];
  202. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  203. if(!$actor || $actor->domain == null) {
  204. return;
  205. }
  206. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  207. Helpers::statusFirstOrFetch($url);
  208. return;
  209. }
  210. public function handleNoteCreate()
  211. {
  212. $activity = $this->payload['object'];
  213. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  214. if(!$actor || $actor->domain == null) {
  215. return;
  216. }
  217. if( isset($activity['inReplyTo']) &&
  218. isset($activity['name']) &&
  219. !isset($activity['content']) &&
  220. !isset($activity['attachment']) &&
  221. Helpers::validateLocalUrl($activity['inReplyTo'])
  222. ) {
  223. $this->handlePollVote();
  224. return;
  225. }
  226. if($actor->followers_count == 0) {
  227. if(FollowerService::followerCount($actor->id, true) == 0) {
  228. return;
  229. }
  230. }
  231. $hasUrl = isset($activity['url']);
  232. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  233. if($hasUrl) {
  234. if(Status::whereUri($url)->exists()) {
  235. return;
  236. }
  237. } else {
  238. if(Status::whereObjectUrl($url)->exists()) {
  239. return;
  240. }
  241. }
  242. Helpers::storeStatus(
  243. $url,
  244. $actor,
  245. $activity
  246. );
  247. return;
  248. }
  249. public function handlePollVote()
  250. {
  251. $activity = $this->payload['object'];
  252. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  253. $status = Helpers::statusFetch($activity['inReplyTo']);
  254. $poll = $status->poll;
  255. if(!$status || !$poll) {
  256. return;
  257. }
  258. if(now()->gt($poll->expires_at)) {
  259. return;
  260. }
  261. $choices = $poll->poll_options;
  262. $choice = array_search($activity['name'], $choices);
  263. if($choice === false) {
  264. return;
  265. }
  266. if(PollVote::whereStatusId($status->id)->whereProfileId($actor->id)->exists()) {
  267. return;
  268. }
  269. $vote = new PollVote;
  270. $vote->status_id = $status->id;
  271. $vote->profile_id = $actor->id;
  272. $vote->poll_id = $poll->id;
  273. $vote->choice = $choice;
  274. $vote->uri = isset($activity['id']) ? $activity['id'] : null;
  275. $vote->save();
  276. $tallies = $poll->cached_tallies;
  277. $tallies[$choice] = $tallies[$choice] + 1;
  278. $poll->cached_tallies = $tallies;
  279. $poll->votes_count = array_sum($tallies);
  280. $poll->save();
  281. PollService::del($status->id);
  282. return;
  283. }
  284. public function handleDirectMessage()
  285. {
  286. $activity = $this->payload['object'];
  287. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  288. $profile = Profile::whereNull('domain')
  289. ->whereUsername(array_last(explode('/', $activity['to'][0])))
  290. ->firstOrFail();
  291. if(in_array($actor->id, $profile->blockedIds()->toArray())) {
  292. return;
  293. }
  294. $msg = $activity['content'];
  295. $msgText = strip_tags($activity['content']);
  296. if(Str::startsWith($msgText, '@' . $profile->username)) {
  297. $len = strlen('@' . $profile->username);
  298. $msgText = substr($msgText, $len + 1);
  299. }
  300. if($profile->user->settings->public_dm == false || $profile->is_private) {
  301. if($profile->follows($actor) == true) {
  302. $hidden = false;
  303. } else {
  304. $hidden = true;
  305. }
  306. } else {
  307. $hidden = false;
  308. }
  309. $status = new Status;
  310. $status->profile_id = $actor->id;
  311. $status->caption = $msgText;
  312. $status->rendered = $msg;
  313. $status->visibility = 'direct';
  314. $status->scope = 'direct';
  315. $status->url = $activity['id'];
  316. $status->in_reply_to_profile_id = $profile->id;
  317. $status->save();
  318. $dm = new DirectMessage;
  319. $dm->to_id = $profile->id;
  320. $dm->from_id = $actor->id;
  321. $dm->status_id = $status->id;
  322. $dm->is_hidden = $hidden;
  323. $dm->type = 'text';
  324. $dm->save();
  325. Conversation::updateOrInsert(
  326. [
  327. 'to_id' => $profile->id,
  328. 'from_id' => $actor->id
  329. ],
  330. [
  331. 'type' => 'text',
  332. 'status_id' => $status->id,
  333. 'dm_id' => $dm->id,
  334. 'is_hidden' => $hidden
  335. ]
  336. );
  337. if(count($activity['attachment'])) {
  338. $photos = 0;
  339. $videos = 0;
  340. $allowed = explode(',', config_cache('pixelfed.media_types'));
  341. $activity['attachment'] = array_slice($activity['attachment'], 0, config_cache('pixelfed.max_album_length'));
  342. foreach($activity['attachment'] as $a) {
  343. $type = $a['mediaType'];
  344. $url = $a['url'];
  345. $valid = Helpers::validateUrl($url);
  346. if(in_array($type, $allowed) == false || $valid == false) {
  347. continue;
  348. }
  349. $media = new Media();
  350. $media->remote_media = true;
  351. $media->status_id = $status->id;
  352. $media->profile_id = $status->profile_id;
  353. $media->user_id = null;
  354. $media->media_path = $url;
  355. $media->remote_url = $url;
  356. $media->mime = $type;
  357. $media->save();
  358. if(explode('/', $type)[0] == 'image') {
  359. $photos = $photos + 1;
  360. }
  361. if(explode('/', $type)[0] == 'video') {
  362. $videos = $videos + 1;
  363. }
  364. }
  365. if($photos && $videos == 0) {
  366. $dm->type = $photos == 1 ? 'photo' : 'photos';
  367. $dm->save();
  368. }
  369. if($videos && $photos == 0) {
  370. $dm->type = $videos == 1 ? 'video' : 'videos';
  371. $dm->save();
  372. }
  373. }
  374. if(filter_var($msgText, FILTER_VALIDATE_URL)) {
  375. if(Helpers::validateUrl($msgText)) {
  376. $dm->type = 'link';
  377. $dm->meta = [
  378. 'domain' => parse_url($msgText, PHP_URL_HOST),
  379. 'local' => parse_url($msgText, PHP_URL_HOST) ==
  380. parse_url(config('app.url'), PHP_URL_HOST)
  381. ];
  382. $dm->save();
  383. }
  384. }
  385. $nf = UserFilter::whereUserId($profile->id)
  386. ->whereFilterableId($actor->id)
  387. ->whereFilterableType('App\Profile')
  388. ->whereFilterType('dm.mute')
  389. ->exists();
  390. if($profile->domain == null && $hidden == false && !$nf) {
  391. $notification = new Notification();
  392. $notification->profile_id = $profile->id;
  393. $notification->actor_id = $actor->id;
  394. $notification->action = 'dm';
  395. $notification->message = $dm->toText();
  396. $notification->rendered = $dm->toHtml();
  397. $notification->item_id = $dm->id;
  398. $notification->item_type = "App\DirectMessage";
  399. $notification->save();
  400. }
  401. return;
  402. }
  403. public function handleFollowActivity()
  404. {
  405. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  406. $target = $this->actorFirstOrCreate($this->payload['object']);
  407. if(!$actor || $actor->domain == null || $target->domain !== null) {
  408. return;
  409. }
  410. if(
  411. Follower::whereProfileId($actor->id)
  412. ->whereFollowingId($target->id)
  413. ->exists() ||
  414. FollowRequest::whereFollowerId($actor->id)
  415. ->whereFollowingId($target->id)
  416. ->exists()
  417. ) {
  418. return;
  419. }
  420. $blocks = UserFilterService::blocks($target->id);
  421. if($blocks && in_array($actor->id, $blocks)) {
  422. return;
  423. }
  424. if($target->is_private == true) {
  425. FollowRequest::updateOrCreate([
  426. 'follower_id' => $actor->id,
  427. 'following_id' => $target->id,
  428. ],[
  429. 'activity' => collect($this->payload)->only(['id','actor','object','type'])->toArray()
  430. ]);
  431. } else {
  432. $follower = new Follower;
  433. $follower->profile_id = $actor->id;
  434. $follower->following_id = $target->id;
  435. $follower->local_profile = empty($actor->domain);
  436. $follower->save();
  437. FollowPipeline::dispatch($follower);
  438. FollowerService::add($actor->id, $target->id);
  439. // send Accept to remote profile
  440. $accept = [
  441. '@context' => 'https://www.w3.org/ns/activitystreams',
  442. 'id' => $target->permalink().'#accepts/follows/' . $follower->id,
  443. 'type' => 'Accept',
  444. 'actor' => $target->permalink(),
  445. 'object' => [
  446. 'id' => $this->payload['id'],
  447. 'actor' => $actor->permalink(),
  448. 'type' => 'Follow',
  449. 'object' => $target->permalink()
  450. ]
  451. ];
  452. Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
  453. Cache::forget('profile:follower_count:'.$target->id);
  454. Cache::forget('profile:follower_count:'.$actor->id);
  455. Cache::forget('profile:following_count:'.$target->id);
  456. Cache::forget('profile:following_count:'.$actor->id);
  457. }
  458. return;
  459. }
  460. public function handleAnnounceActivity()
  461. {
  462. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  463. $activity = $this->payload['object'];
  464. if(!$actor || $actor->domain == null) {
  465. return;
  466. }
  467. if(Helpers::validateLocalUrl($activity) == false) {
  468. return;
  469. }
  470. $parent = Helpers::statusFetch($activity);
  471. if(empty($parent)) {
  472. return;
  473. }
  474. $blocks = UserFilterService::blocks($parent->profile_id);
  475. if($blocks && in_array($actor->id, $blocks)) {
  476. return;
  477. }
  478. $status = Status::firstOrCreate([
  479. 'profile_id' => $actor->id,
  480. 'reblog_of_id' => $parent->id,
  481. 'type' => 'share'
  482. ]);
  483. Notification::firstOrCreate([
  484. 'profile_id' => $parent->profile->id,
  485. 'actor_id' => $actor->id,
  486. 'action' => 'share',
  487. 'message' => $status->replyToText(),
  488. 'rendered' => $status->replyToHtml(),
  489. 'item_id' => $parent->id,
  490. 'item_type' => 'App\Status'
  491. ]);
  492. $parent->reblogs_count = $parent->reblogs_count + 1;
  493. $parent->save();
  494. return;
  495. }
  496. public function handleAcceptActivity()
  497. {
  498. $actor = $this->payload['object']['actor'];
  499. $obj = $this->payload['object']['object'];
  500. $type = $this->payload['object']['type'];
  501. if($type !== 'Follow') {
  502. return;
  503. }
  504. $actor = Helpers::validateLocalUrl($actor);
  505. $target = Helpers::validateUrl($obj);
  506. if(!$actor || !$target) {
  507. return;
  508. }
  509. $actor = Helpers::profileFetch($actor);
  510. $target = Helpers::profileFetch($target);
  511. if(!$actor || !$target) {
  512. return;
  513. }
  514. $request = FollowRequest::whereFollowerId($actor->id)
  515. ->whereFollowingId($target->id)
  516. ->whereIsRejected(false)
  517. ->first();
  518. if(!$request) {
  519. return;
  520. }
  521. $follower = Follower::firstOrCreate([
  522. 'profile_id' => $actor->id,
  523. 'following_id' => $target->id,
  524. ]);
  525. FollowPipeline::dispatch($follower);
  526. $request->delete();
  527. return;
  528. }
  529. public function handleDeleteActivity()
  530. {
  531. if(!isset(
  532. $this->payload['actor'],
  533. $this->payload['object']
  534. )) {
  535. return;
  536. }
  537. $actor = $this->payload['actor'];
  538. $obj = $this->payload['object'];
  539. if(is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
  540. $profile = Profile::whereRemoteUrl($obj)->first();
  541. if(!$profile || $profile->private_key != null) {
  542. return;
  543. }
  544. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('delete');
  545. return;
  546. } else {
  547. if(!isset($obj['id'], $this->payload['object'], $this->payload['object']['id'])) {
  548. return;
  549. }
  550. $type = $this->payload['object']['type'];
  551. $typeCheck = in_array($type, ['Person', 'Tombstone', 'Story']);
  552. if(!Helpers::validateUrl($actor) || !Helpers::validateUrl($obj['id']) || !$typeCheck) {
  553. return;
  554. }
  555. if(parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  556. return;
  557. }
  558. $id = $this->payload['object']['id'];
  559. switch ($type) {
  560. case 'Person':
  561. $profile = Profile::whereRemoteUrl($actor)->first();
  562. if(!$profile || $profile->private_key != null) {
  563. return;
  564. }
  565. DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('delete');
  566. return;
  567. break;
  568. case 'Tombstone':
  569. $profile = Profile::whereRemoteUrl($actor)->first();
  570. if(!$profile || $profile->private_key != null) {
  571. return;
  572. }
  573. $status = Status::whereProfileId($profile->id)
  574. ->whereUri($id)
  575. ->orWhere('url', $id)
  576. ->orWhere('object_url', $id)
  577. ->first();
  578. if(!$status) {
  579. return;
  580. }
  581. DeleteRemoteStatusPipeline::dispatch($status)->onQueue('high');
  582. return;
  583. break;
  584. case 'Story':
  585. $story = Story::whereObjectId($id)
  586. ->first();
  587. if($story) {
  588. StoryExpire::dispatch($story)->onQueue('story');
  589. }
  590. return;
  591. break;
  592. default:
  593. return;
  594. break;
  595. }
  596. }
  597. return;
  598. }
  599. public function handleLikeActivity()
  600. {
  601. $actor = $this->payload['actor'];
  602. if(!Helpers::validateUrl($actor)) {
  603. return;
  604. }
  605. $profile = self::actorFirstOrCreate($actor);
  606. $obj = $this->payload['object'];
  607. if(!Helpers::validateUrl($obj)) {
  608. return;
  609. }
  610. $status = Helpers::statusFirstOrFetch($obj);
  611. if(!$status || !$profile) {
  612. return;
  613. }
  614. $blocks = UserFilterService::blocks($status->profile_id);
  615. if($blocks && in_array($profile->id, $blocks)) {
  616. return;
  617. }
  618. $like = Like::firstOrCreate([
  619. 'profile_id' => $profile->id,
  620. 'status_id' => $status->id
  621. ]);
  622. if($like->wasRecentlyCreated == true) {
  623. $status->likes_count = $status->likes_count + 1;
  624. $status->save();
  625. LikePipeline::dispatch($like);
  626. }
  627. return;
  628. }
  629. public function handleRejectActivity()
  630. {
  631. }
  632. public function handleUndoActivity()
  633. {
  634. $actor = $this->payload['actor'];
  635. $profile = self::actorFirstOrCreate($actor);
  636. $obj = $this->payload['object'];
  637. if(!$profile) {
  638. return;
  639. }
  640. // TODO: Some implementations do not inline the object, skip for now
  641. if(!$obj || !is_array($obj) || !isset($obj['type'])) {
  642. return;
  643. }
  644. switch ($obj['type']) {
  645. case 'Accept':
  646. break;
  647. case 'Announce':
  648. if(is_array($obj) && isset($obj['object'])) {
  649. $obj = $obj['object'];
  650. }
  651. if(!is_string($obj) || !Helpers::validateLocalUrl($obj)) {
  652. return;
  653. }
  654. $status = Status::whereUri($obj)->exists();
  655. if(!$status) {
  656. return;
  657. }
  658. Status::whereProfileId($profile->id)
  659. ->whereReblogOfId($status->id)
  660. ->forceDelete();
  661. Notification::whereProfileId($status->profile->id)
  662. ->whereActorId($profile->id)
  663. ->whereAction('share')
  664. ->whereItemId($status->reblog_of_id)
  665. ->whereItemType('App\Status')
  666. ->forceDelete();
  667. break;
  668. case 'Block':
  669. break;
  670. case 'Follow':
  671. $following = self::actorFirstOrCreate($obj['object']);
  672. if(!$following) {
  673. return;
  674. }
  675. Follower::whereProfileId($profile->id)
  676. ->whereFollowingId($following->id)
  677. ->delete();
  678. Notification::whereProfileId($following->id)
  679. ->whereActorId($profile->id)
  680. ->whereAction('follow')
  681. ->whereItemId($following->id)
  682. ->whereItemType('App\Profile')
  683. ->forceDelete();
  684. FollowerService::remove($profile->id, $following->id);
  685. break;
  686. case 'Like':
  687. $status = Helpers::statusFirstOrFetch($obj['object']);
  688. if(!$status) {
  689. return;
  690. }
  691. Like::whereProfileId($profile->id)
  692. ->whereStatusId($status->id)
  693. ->forceDelete();
  694. Notification::whereProfileId($status->profile_id)
  695. ->whereActorId($profile->id)
  696. ->whereAction('like')
  697. ->whereItemId($status->id)
  698. ->whereItemType('App\Status')
  699. ->forceDelete();
  700. break;
  701. }
  702. return;
  703. }
  704. public function handleViewActivity()
  705. {
  706. if(!isset(
  707. $this->payload['actor'],
  708. $this->payload['object']
  709. )) {
  710. return;
  711. }
  712. $actor = $this->payload['actor'];
  713. $obj = $this->payload['object'];
  714. if(!Helpers::validateUrl($actor)) {
  715. return;
  716. }
  717. if(!$obj || !is_array($obj)) {
  718. return;
  719. }
  720. if(!isset($obj['type']) || !isset($obj['object']) || $obj['type'] != 'Story') {
  721. return;
  722. }
  723. if(!Helpers::validateLocalUrl($obj['object'])) {
  724. return;
  725. }
  726. $profile = Helpers::profileFetch($actor);
  727. $storyId = Str::of($obj['object'])->explode('/')->last();
  728. $story = Story::whereActive(true)
  729. ->whereLocal(true)
  730. ->find($storyId);
  731. if(!$story) {
  732. return;
  733. }
  734. if(!FollowerService::follows($profile->id, $story->profile_id)) {
  735. return;
  736. }
  737. $view = StoryView::firstOrCreate([
  738. 'story_id' => $story->id,
  739. 'profile_id' => $profile->id
  740. ]);
  741. if($view->wasRecentlyCreated == true) {
  742. $story->view_count++;
  743. $story->save();
  744. }
  745. return;
  746. }
  747. public function handleStoryReactionActivity()
  748. {
  749. if(!isset(
  750. $this->payload['actor'],
  751. $this->payload['id'],
  752. $this->payload['inReplyTo'],
  753. $this->payload['content']
  754. )) {
  755. return;
  756. }
  757. $id = $this->payload['id'];
  758. $actor = $this->payload['actor'];
  759. $storyUrl = $this->payload['inReplyTo'];
  760. $to = $this->payload['to'];
  761. $text = Purify::clean($this->payload['content']);
  762. if(parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  763. return;
  764. }
  765. if(!Helpers::validateUrl($id) || !Helpers::validateUrl($actor)) {
  766. return;
  767. }
  768. if(!Helpers::validateLocalUrl($storyUrl)) {
  769. return;
  770. }
  771. if(!Helpers::validateLocalUrl($to)) {
  772. return;
  773. }
  774. if(Status::whereObjectUrl($id)->exists()) {
  775. return;
  776. }
  777. $storyId = Str::of($storyUrl)->explode('/')->last();
  778. $targetProfile = Helpers::profileFetch($to);
  779. $story = Story::whereProfileId($targetProfile->id)
  780. ->find($storyId);
  781. if(!$story) {
  782. return;
  783. }
  784. if($story->can_react == false) {
  785. return;
  786. }
  787. $actorProfile = Helpers::profileFetch($actor);
  788. if(!FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  789. return;
  790. }
  791. $status = new Status;
  792. $status->profile_id = $actorProfile->id;
  793. $status->type = 'story:reaction';
  794. $status->caption = $text;
  795. $status->rendered = $text;
  796. $status->scope = 'direct';
  797. $status->visibility = 'direct';
  798. $status->in_reply_to_profile_id = $story->profile_id;
  799. $status->entities = json_encode([
  800. 'story_id' => $story->id,
  801. 'reaction' => $text
  802. ]);
  803. $status->save();
  804. $dm = new DirectMessage;
  805. $dm->to_id = $story->profile_id;
  806. $dm->from_id = $actorProfile->id;
  807. $dm->type = 'story:react';
  808. $dm->status_id = $status->id;
  809. $dm->meta = json_encode([
  810. 'story_username' => $targetProfile->username,
  811. 'story_actor_username' => $actorProfile->username,
  812. 'story_id' => $story->id,
  813. 'story_media_url' => url(Storage::url($story->path)),
  814. 'reaction' => $text
  815. ]);
  816. $dm->save();
  817. Conversation::updateOrInsert(
  818. [
  819. 'to_id' => $story->profile_id,
  820. 'from_id' => $actorProfile->id
  821. ],
  822. [
  823. 'type' => 'story:react',
  824. 'status_id' => $status->id,
  825. 'dm_id' => $dm->id,
  826. 'is_hidden' => false
  827. ]
  828. );
  829. $n = new Notification;
  830. $n->profile_id = $dm->to_id;
  831. $n->actor_id = $dm->from_id;
  832. $n->item_id = $dm->id;
  833. $n->item_type = 'App\DirectMessage';
  834. $n->action = 'story:react';
  835. $n->message = "{$actorProfile->username} reacted to your story";
  836. $n->rendered = "{$actorProfile->username} reacted to your story";
  837. $n->save();
  838. return;
  839. }
  840. public function handleStoryReplyActivity()
  841. {
  842. if(!isset(
  843. $this->payload['actor'],
  844. $this->payload['id'],
  845. $this->payload['inReplyTo'],
  846. $this->payload['content']
  847. )) {
  848. return;
  849. }
  850. $id = $this->payload['id'];
  851. $actor = $this->payload['actor'];
  852. $storyUrl = $this->payload['inReplyTo'];
  853. $to = $this->payload['to'];
  854. $text = Purify::clean($this->payload['content']);
  855. if(parse_url($id, PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  856. return;
  857. }
  858. if(!Helpers::validateUrl($id) || !Helpers::validateUrl($actor)) {
  859. return;
  860. }
  861. if(!Helpers::validateLocalUrl($storyUrl)) {
  862. return;
  863. }
  864. if(!Helpers::validateLocalUrl($to)) {
  865. return;
  866. }
  867. if(Status::whereObjectUrl($id)->exists()) {
  868. return;
  869. }
  870. $storyId = Str::of($storyUrl)->explode('/')->last();
  871. $targetProfile = Helpers::profileFetch($to);
  872. $story = Story::whereProfileId($targetProfile->id)
  873. ->find($storyId);
  874. if(!$story) {
  875. return;
  876. }
  877. if($story->can_react == false) {
  878. return;
  879. }
  880. $actorProfile = Helpers::profileFetch($actor);
  881. if(!FollowerService::follows($actorProfile->id, $targetProfile->id)) {
  882. return;
  883. }
  884. $status = new Status;
  885. $status->profile_id = $actorProfile->id;
  886. $status->type = 'story:reply';
  887. $status->caption = $text;
  888. $status->rendered = $text;
  889. $status->scope = 'direct';
  890. $status->visibility = 'direct';
  891. $status->in_reply_to_profile_id = $story->profile_id;
  892. $status->entities = json_encode([
  893. 'story_id' => $story->id,
  894. 'caption' => $text
  895. ]);
  896. $status->save();
  897. $dm = new DirectMessage;
  898. $dm->to_id = $story->profile_id;
  899. $dm->from_id = $actorProfile->id;
  900. $dm->type = 'story:comment';
  901. $dm->status_id = $status->id;
  902. $dm->meta = json_encode([
  903. 'story_username' => $targetProfile->username,
  904. 'story_actor_username' => $actorProfile->username,
  905. 'story_id' => $story->id,
  906. 'story_media_url' => url(Storage::url($story->path)),
  907. 'caption' => $text
  908. ]);
  909. $dm->save();
  910. Conversation::updateOrInsert(
  911. [
  912. 'to_id' => $story->profile_id,
  913. 'from_id' => $actorProfile->id
  914. ],
  915. [
  916. 'type' => 'story:comment',
  917. 'status_id' => $status->id,
  918. 'dm_id' => $dm->id,
  919. 'is_hidden' => false
  920. ]
  921. );
  922. $n = new Notification;
  923. $n->profile_id = $dm->to_id;
  924. $n->actor_id = $dm->from_id;
  925. $n->item_id = $dm->id;
  926. $n->item_type = 'App\DirectMessage';
  927. $n->action = 'story:comment';
  928. $n->message = "{$actorProfile->username} commented on story";
  929. $n->rendered = "{$actorProfile->username} commented on story";
  930. $n->save();
  931. return;
  932. }
  933. }