Inbox.php 23 KB

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