Inbox.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. <?php
  2. namespace App\Util\ActivityPub;
  3. use Cache, DB, Log, Purify, Redis, 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. UserFilter
  16. };
  17. use Carbon\Carbon;
  18. use App\Util\ActivityPub\Helpers;
  19. use Illuminate\Support\Str;
  20. use App\Jobs\LikePipeline\LikePipeline;
  21. use App\Jobs\FollowPipeline\FollowPipeline;
  22. use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
  23. use App\Util\ActivityPub\Validator\Accept as AcceptValidator;
  24. use App\Util\ActivityPub\Validator\Add as AddValidator;
  25. use App\Util\ActivityPub\Validator\Announce as AnnounceValidator;
  26. use App\Util\ActivityPub\Validator\Follow as FollowValidator;
  27. use App\Util\ActivityPub\Validator\Like as LikeValidator;
  28. use App\Util\ActivityPub\Validator\UndoFollow as UndoFollowValidator;
  29. class Inbox
  30. {
  31. protected $headers;
  32. protected $profile;
  33. protected $payload;
  34. protected $logger;
  35. public function __construct($headers, $profile, $payload)
  36. {
  37. $this->headers = $headers;
  38. $this->profile = $profile;
  39. $this->payload = $payload;
  40. }
  41. public function handle()
  42. {
  43. $this->handleVerb();
  44. // if(!Activity::where('data->id', $this->payload['id'])->exists()) {
  45. // (new Activity())->create([
  46. // 'to_id' => $this->profile->id,
  47. // 'data' => json_encode($this->payload)
  48. // ]);
  49. // }
  50. return;
  51. }
  52. public function handleVerb()
  53. {
  54. $verb = (string) $this->payload['type'];
  55. switch ($verb) {
  56. case 'Add':
  57. if(AddValidator::validate($this->payload) == false) { return; }
  58. $this->handleAddActivity();
  59. break;
  60. case 'Create':
  61. $this->handleCreateActivity();
  62. break;
  63. case 'Follow':
  64. if(FollowValidator::validate($this->payload) == false) { return; }
  65. $this->handleFollowActivity();
  66. break;
  67. case 'Announce':
  68. if(AnnounceValidator::validate($this->payload) == false) { return; }
  69. $this->handleAnnounceActivity();
  70. break;
  71. case 'Accept':
  72. if(AcceptValidator::validate($this->payload) == false) { return; }
  73. $this->handleAcceptActivity();
  74. break;
  75. case 'Delete':
  76. $this->handleDeleteActivity();
  77. break;
  78. case 'Like':
  79. if(LikeValidator::validate($this->payload) == false) { return; }
  80. $this->handleLikeActivity();
  81. break;
  82. case 'Reject':
  83. $this->handleRejectActivity();
  84. break;
  85. case 'Undo':
  86. $this->handleUndoActivity();
  87. break;
  88. default:
  89. // TODO: decide how to handle invalid verbs.
  90. break;
  91. }
  92. }
  93. public function verifyNoteAttachment()
  94. {
  95. $activity = $this->payload['object'];
  96. if(isset($activity['inReplyTo']) &&
  97. !empty($activity['inReplyTo']) &&
  98. Helpers::validateUrl($activity['inReplyTo'])
  99. ) {
  100. // reply detected, skip attachment check
  101. return true;
  102. }
  103. $valid = Helpers::verifyAttachments($activity);
  104. return $valid;
  105. }
  106. public function actorFirstOrCreate($actorUrl)
  107. {
  108. return Helpers::profileFetch($actorUrl);
  109. }
  110. public function handleAddActivity()
  111. {
  112. // stories ;)
  113. }
  114. public function handleCreateActivity()
  115. {
  116. $activity = $this->payload['object'];
  117. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  118. if(!$actor || $actor->domain == null) {
  119. return;
  120. }
  121. $to = $activity['to'];
  122. $cc = isset($activity['cc']) ? $activity['cc'] : [];
  123. if(count($to) == 1 &&
  124. count($cc) == 0 &&
  125. parse_url($to[0], PHP_URL_HOST) == config('pixelfed.domain.app')
  126. ) {
  127. $this->handleDirectMessage();
  128. return;
  129. }
  130. if(!$this->verifyNoteAttachment()) {
  131. return;
  132. }
  133. if($activity['type'] == 'Note' && !empty($activity['inReplyTo'])) {
  134. $this->handleNoteReply();
  135. } elseif($activity['type'] == 'Note' && !empty($activity['attachment'])) {
  136. $this->handleNoteCreate();
  137. }
  138. }
  139. public function handleNoteReply()
  140. {
  141. $activity = $this->payload['object'];
  142. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  143. if(!$actor || $actor->domain == null) {
  144. return;
  145. }
  146. $inReplyTo = $activity['inReplyTo'];
  147. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  148. Helpers::statusFirstOrFetch($url, true);
  149. return;
  150. }
  151. public function handleNoteCreate()
  152. {
  153. $activity = $this->payload['object'];
  154. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  155. if(!$actor || $actor->domain == null) {
  156. return;
  157. }
  158. if($actor->followers()->count() == 0) {
  159. return;
  160. }
  161. $url = isset($activity['url']) ? $activity['url'] : $activity['id'];
  162. if(Status::whereUrl($url)->exists()) {
  163. return;
  164. }
  165. Helpers::statusFetch($url);
  166. return;
  167. }
  168. public function handleDirectMessage()
  169. {
  170. $activity = $this->payload['object'];
  171. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  172. $profile = Profile::whereNull('domain')
  173. ->whereUsername(array_last(explode('/', $activity['to'][0])))
  174. ->firstOrFail();
  175. if(in_array($actor->id, $profile->blockedIds()->toArray())) {
  176. return;
  177. }
  178. $msg = $activity['content'];
  179. $msgText = strip_tags($activity['content']);
  180. if(Str::startsWith($msgText, '@' . $profile->username)) {
  181. $len = strlen('@' . $profile->username);
  182. $msgText = substr($msgText, $len + 1);
  183. }
  184. if($profile->user->settings->public_dm == false || $profile->is_private) {
  185. if($profile->follows($actor) == true) {
  186. $hidden = false;
  187. } else {
  188. $hidden = true;
  189. }
  190. } else {
  191. $hidden = false;
  192. }
  193. $status = new Status;
  194. $status->profile_id = $actor->id;
  195. $status->caption = $msgText;
  196. $status->rendered = $msg;
  197. $status->visibility = 'direct';
  198. $status->scope = 'direct';
  199. $status->url = $activity['id'];
  200. $status->in_reply_to_profile_id = $profile->id;
  201. $status->save();
  202. $dm = new DirectMessage;
  203. $dm->to_id = $profile->id;
  204. $dm->from_id = $actor->id;
  205. $dm->status_id = $status->id;
  206. $dm->is_hidden = $hidden;
  207. $dm->type = 'text';
  208. $dm->save();
  209. if(count($activity['attachment'])) {
  210. $photos = 0;
  211. $videos = 0;
  212. $allowed = explode(',', config('pixelfed.media_types'));
  213. $activity['attachment'] = array_slice($activity['attachment'], 0, config('pixelfed.max_album_length'));
  214. foreach($activity['attachment'] as $a) {
  215. $type = $a['mediaType'];
  216. $url = $a['url'];
  217. $valid = Helpers::validateUrl($url);
  218. if(in_array($type, $allowed) == false || $valid == false) {
  219. continue;
  220. }
  221. $media = new Media();
  222. $media->remote_media = true;
  223. $media->status_id = $status->id;
  224. $media->profile_id = $status->profile_id;
  225. $media->user_id = null;
  226. $media->media_path = $url;
  227. $media->remote_url = $url;
  228. $media->mime = $type;
  229. $media->save();
  230. if(explode('/', $type)[0] == 'image') {
  231. $photos = $photos + 1;
  232. }
  233. if(explode('/', $type)[0] == 'video') {
  234. $videos = $videos + 1;
  235. }
  236. }
  237. if($photos && $videos == 0) {
  238. $dm->type = $photos == 1 ? 'photo' : 'photos';
  239. $dm->save();
  240. }
  241. if($videos && $photos == 0) {
  242. $dm->type = $videos == 1 ? 'video' : 'videos';
  243. $dm->save();
  244. }
  245. }
  246. if(filter_var($msgText, FILTER_VALIDATE_URL)) {
  247. if(Helpers::validateUrl($msgText)) {
  248. $dm->type = 'link';
  249. $dm->meta = [
  250. 'domain' => parse_url($msgText, PHP_URL_HOST),
  251. 'local' => parse_url($msgText, PHP_URL_HOST) ==
  252. parse_url(config('app.url'), PHP_URL_HOST)
  253. ];
  254. $dm->save();
  255. }
  256. }
  257. $nf = UserFilter::whereUserId($profile->id)
  258. ->whereFilterableId($actor->id)
  259. ->whereFilterableType('App\Profile')
  260. ->whereFilterType('dm.mute')
  261. ->exists();
  262. if($profile->domain == null && $hidden == false && !$nf) {
  263. $notification = new Notification();
  264. $notification->profile_id = $profile->id;
  265. $notification->actor_id = $actor->id;
  266. $notification->action = 'dm';
  267. $notification->message = $dm->toText();
  268. $notification->rendered = $dm->toHtml();
  269. $notification->item_id = $dm->id;
  270. $notification->item_type = "App\DirectMessage";
  271. $notification->save();
  272. }
  273. return;
  274. }
  275. public function handleFollowActivity()
  276. {
  277. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  278. $target = $this->actorFirstOrCreate($this->payload['object']);
  279. if(!$actor || $actor->domain == null || $target->domain !== null) {
  280. return;
  281. }
  282. if(
  283. Follower::whereProfileId($actor->id)
  284. ->whereFollowingId($target->id)
  285. ->exists() ||
  286. FollowRequest::whereFollowerId($actor->id)
  287. ->whereFollowingId($target->id)
  288. ->exists()
  289. ) {
  290. return;
  291. }
  292. if($target->is_private == true) {
  293. FollowRequest::firstOrCreate([
  294. 'follower_id' => $actor->id,
  295. 'following_id' => $target->id
  296. ]);
  297. Cache::forget('profile:follower_count:'.$target->id);
  298. Cache::forget('profile:follower_count:'.$actor->id);
  299. Cache::forget('profile:following_count:'.$target->id);
  300. Cache::forget('profile:following_count:'.$actor->id);
  301. } else {
  302. $follower = new Follower;
  303. $follower->profile_id = $actor->id;
  304. $follower->following_id = $target->id;
  305. $follower->local_profile = empty($actor->domain);
  306. $follower->save();
  307. FollowPipeline::dispatch($follower);
  308. // send Accept to remote profile
  309. $accept = [
  310. '@context' => 'https://www.w3.org/ns/activitystreams',
  311. 'id' => $target->permalink().'#accepts/follows/' . $follower->id,
  312. 'type' => 'Accept',
  313. 'actor' => $target->permalink(),
  314. 'object' => [
  315. 'id' => $this->payload['id'],
  316. 'actor' => $actor->permalink(),
  317. 'type' => 'Follow',
  318. 'object' => $target->permalink()
  319. ]
  320. ];
  321. Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
  322. Cache::forget('profile:follower_count:'.$target->id);
  323. Cache::forget('profile:follower_count:'.$actor->id);
  324. Cache::forget('profile:following_count:'.$target->id);
  325. Cache::forget('profile:following_count:'.$actor->id);
  326. }
  327. }
  328. public function handleAnnounceActivity()
  329. {
  330. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  331. $activity = $this->payload['object'];
  332. if(!$actor || $actor->domain == null) {
  333. return;
  334. }
  335. if(Helpers::validateLocalUrl($activity) == false) {
  336. return;
  337. }
  338. $parent = Helpers::statusFetch($activity);
  339. if(empty($parent)) {
  340. return;
  341. }
  342. $status = Status::firstOrCreate([
  343. 'profile_id' => $actor->id,
  344. 'reblog_of_id' => $parent->id,
  345. 'type' => 'share'
  346. ]);
  347. Notification::firstOrCreate([
  348. 'profile_id' => $parent->profile->id,
  349. 'actor_id' => $actor->id,
  350. 'action' => 'share',
  351. 'message' => $status->replyToText(),
  352. 'rendered' => $status->replyToHtml(),
  353. 'item_id' => $parent->id,
  354. 'item_type' => 'App\Status'
  355. ]);
  356. $parent->reblogs_count = $parent->shares()->count();
  357. $parent->save();
  358. }
  359. public function handleAcceptActivity()
  360. {
  361. $actor = $this->payload['object']['actor'];
  362. $obj = $this->payload['object']['object'];
  363. $type = $this->payload['object']['type'];
  364. if($type !== 'Follow') {
  365. return;
  366. }
  367. $actor = Helpers::validateLocalUrl($actor);
  368. $target = Helpers::validateUrl($obj);
  369. if(!$actor || !$target) {
  370. return;
  371. }
  372. $actor = Helpers::profileFetch($actor);
  373. $target = Helpers::profileFetch($target);
  374. $request = FollowRequest::whereFollowerId($actor->id)
  375. ->whereFollowingId($target->id)
  376. ->whereIsRejected(false)
  377. ->first();
  378. if(!$request) {
  379. return;
  380. }
  381. $follower = Follower::firstOrCreate([
  382. 'profile_id' => $actor->id,
  383. 'following_id' => $target->id,
  384. ]);
  385. FollowPipeline::dispatch($follower);
  386. $request->delete();
  387. }
  388. public function handleDeleteActivity()
  389. {
  390. if(!isset(
  391. $this->payload['actor'],
  392. $this->payload['object']
  393. )) {
  394. return;
  395. }
  396. $actor = $this->payload['actor'];
  397. $obj = $this->payload['object'];
  398. if(is_string($obj) == true && $actor == $obj && Helpers::validateUrl($obj)) {
  399. $profile = Profile::whereRemoteUrl($obj)->first();
  400. if(!$profile || $profile->private_key != null) {
  401. return;
  402. }
  403. DeleteRemoteProfilePipeline::dispatchNow($profile);
  404. return;
  405. } else {
  406. $type = $this->payload['object']['type'];
  407. $typeCheck = in_array($type, ['Person', 'Tombstone']);
  408. if(!Helpers::validateUrl($actor) || !Helpers::validateUrl($obj['id']) || !$typeCheck) {
  409. return;
  410. }
  411. if(parse_url($obj['id'], PHP_URL_HOST) !== parse_url($actor, PHP_URL_HOST)) {
  412. return;
  413. }
  414. $id = $this->payload['object']['id'];
  415. switch ($type) {
  416. case 'Person':
  417. $profile = Profile::whereRemoteUrl($actor)->first();
  418. if(!$profile || $profile->private_key != null) {
  419. return;
  420. }
  421. DeleteRemoteProfilePipeline::dispatchNow($profile);
  422. return;
  423. break;
  424. case 'Tombstone':
  425. $profile = Helpers::profileFetch($actor);
  426. $status = Status::whereProfileId($profile->id)
  427. ->whereUri($id)
  428. ->orWhere('url', $id)
  429. ->orWhere('object_url', $id)
  430. ->first();
  431. if(!$status) {
  432. return;
  433. }
  434. $status->directMessage()->delete();
  435. $status->media()->delete();
  436. $status->likes()->delete();
  437. $status->shares()->delete();
  438. $status->delete();
  439. return;
  440. break;
  441. default:
  442. return;
  443. break;
  444. }
  445. }
  446. }
  447. public function handleLikeActivity()
  448. {
  449. $actor = $this->payload['actor'];
  450. if(!Helpers::validateUrl($actor)) {
  451. return;
  452. }
  453. $profile = self::actorFirstOrCreate($actor);
  454. $obj = $this->payload['object'];
  455. if(!Helpers::validateUrl($obj)) {
  456. return;
  457. }
  458. $status = Helpers::statusFirstOrFetch($obj);
  459. if(!$status || !$profile) {
  460. return;
  461. }
  462. $like = Like::firstOrCreate([
  463. 'profile_id' => $profile->id,
  464. 'status_id' => $status->id
  465. ]);
  466. if($like->wasRecentlyCreated == true) {
  467. $status->likes_count = $status->likes()->count();
  468. $status->save();
  469. LikePipeline::dispatch($like);
  470. }
  471. return;
  472. }
  473. public function handleRejectActivity()
  474. {
  475. }
  476. public function handleUndoActivity()
  477. {
  478. $actor = $this->payload['actor'];
  479. $profile = self::actorFirstOrCreate($actor);
  480. $obj = $this->payload['object'];
  481. switch ($obj['type']) {
  482. case 'Accept':
  483. break;
  484. case 'Announce':
  485. $obj = $obj['object'];
  486. if(!Helpers::validateLocalUrl($obj)) {
  487. return;
  488. }
  489. $status = Helpers::statusFetch($obj);
  490. if(!$status) {
  491. return;
  492. }
  493. Status::whereProfileId($profile->id)
  494. ->whereReblogOfId($status->id)
  495. ->forceDelete();
  496. Notification::whereProfileId($status->profile->id)
  497. ->whereActorId($profile->id)
  498. ->whereAction('share')
  499. ->whereItemId($status->reblog_of_id)
  500. ->whereItemType('App\Status')
  501. ->forceDelete();
  502. break;
  503. case 'Block':
  504. break;
  505. case 'Follow':
  506. $following = self::actorFirstOrCreate($obj['object']);
  507. if(!$following) {
  508. return;
  509. }
  510. Follower::whereProfileId($profile->id)
  511. ->whereFollowingId($following->id)
  512. ->delete();
  513. Notification::whereProfileId($following->id)
  514. ->whereActorId($profile->id)
  515. ->whereAction('follow')
  516. ->whereItemId($following->id)
  517. ->whereItemType('App\Profile')
  518. ->forceDelete();
  519. break;
  520. case 'Like':
  521. $status = Helpers::statusFirstOrFetch($obj['object']);
  522. if(!$status) {
  523. return;
  524. }
  525. Like::whereProfileId($profile->id)
  526. ->whereStatusId($status->id)
  527. ->forceDelete();
  528. Notification::whereProfileId($status->profile->id)
  529. ->whereActorId($profile->id)
  530. ->whereAction('like')
  531. ->whereItemId($status->id)
  532. ->whereItemType('App\Status')
  533. ->forceDelete();
  534. break;
  535. }
  536. return;
  537. }
  538. }