Inbox.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace App\Util\ActivityPub;
  3. use Cache, DB, Log, Purify, Redis, Validator;
  4. use App\{
  5. Activity,
  6. Follower,
  7. FollowRequest,
  8. Like,
  9. Notification,
  10. Profile,
  11. Status
  12. };
  13. use Carbon\Carbon;
  14. use App\Util\ActivityPub\Helpers;
  15. use App\Jobs\LikePipeline\LikePipeline;
  16. use App\Util\ActivityPub\Validator\{
  17. Follow
  18. };
  19. class Inbox
  20. {
  21. protected $headers;
  22. protected $profile;
  23. protected $payload;
  24. protected $logger;
  25. public function __construct($headers, $profile, $payload)
  26. {
  27. $this->headers = $headers;
  28. $this->profile = $profile;
  29. $this->payload = $payload;
  30. }
  31. public function handle()
  32. {
  33. $this->handleVerb();
  34. }
  35. public function handleVerb()
  36. {
  37. $verb = $this->payload['type'];
  38. switch ($verb) {
  39. case 'Create':
  40. $this->handleCreateActivity();
  41. break;
  42. case 'Follow':
  43. $this->handleFollowActivity();
  44. break;
  45. case 'Announce':
  46. $this->handleAnnounceActivity();
  47. break;
  48. case 'Accept':
  49. if(Accept::validate($this->payload) == false) { return; }
  50. $this->handleAcceptActivity();
  51. break;
  52. case 'Delete':
  53. $this->handleDeleteActivity();
  54. break;
  55. case 'Like':
  56. $this->handleLikeActivity();
  57. break;
  58. case 'Reject':
  59. $this->handleRejectActivity();
  60. break;
  61. case 'Undo':
  62. $this->handleUndoActivity();
  63. break;
  64. default:
  65. // TODO: decide how to handle invalid verbs.
  66. break;
  67. }
  68. }
  69. public function verifyNoteAttachment()
  70. {
  71. $activity = $this->payload['object'];
  72. if(isset($activity['inReplyTo']) &&
  73. !empty($activity['inReplyTo']) &&
  74. Helpers::validateUrl($activity['inReplyTo'])
  75. ) {
  76. // reply detected, skip attachment check
  77. return true;
  78. }
  79. $valid = Helpers::verifyAttachments($activity);
  80. return $valid;
  81. }
  82. public function actorFirstOrCreate($actorUrl)
  83. {
  84. return Helpers::profileFirstOrNew($actorUrl);
  85. }
  86. public function handleCreateActivity()
  87. {
  88. $activity = $this->payload['object'];
  89. if(!$this->verifyNoteAttachment()) {
  90. return;
  91. }
  92. if($activity['type'] == 'Note' && !empty($activity['inReplyTo'])) {
  93. $this->handleNoteReply();
  94. } elseif($activity['type'] == 'Note' && !empty($activity['attachment'])) {
  95. $this->handleNoteCreate();
  96. }
  97. }
  98. public function handleNoteReply()
  99. {
  100. $activity = $this->payload['object'];
  101. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  102. $inReplyTo = $activity['inReplyTo'];
  103. $url = $activity['id'];
  104. if(!Helpers::statusFirstOrFetch($url, true)) {
  105. return;
  106. }
  107. }
  108. public function handleNoteCreate()
  109. {
  110. $activity = $this->payload['object'];
  111. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  112. if(!$actor || $actor->domain == null) {
  113. return;
  114. }
  115. if(Helpers::userInAudience($this->profile, $this->payload) == false) {
  116. //Log::error('AP:inbox:userInAudience:false - Activity#'.$this->logger->id);
  117. return;
  118. }
  119. $url = $activity['id'];
  120. if(Status::whereUrl($url)->exists()) {
  121. return;
  122. }
  123. $status = DB::transaction(function() use($activity, $actor, $url) {
  124. $caption = str_limit(strip_tags($activity['content']), config('pixelfed.max_caption_length'));
  125. $status = new Status;
  126. $status->profile_id = $actor->id;
  127. $status->caption = strip_tags($caption);
  128. $status->rendered = Purify::clean($caption);
  129. $status->visibility = $status->scope = 'public';
  130. $status->uri = $url;
  131. $status->url = $url;
  132. $status->save();
  133. return $status;
  134. });
  135. Helpers::importNoteAttachment($activity, $status);
  136. }
  137. public function handleFollowActivity()
  138. {
  139. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  140. if(!$actor || $actor->domain == null) {
  141. return;
  142. }
  143. $target = $this->profile;
  144. if($target->is_private == true) {
  145. // make follow request
  146. FollowRequest::firstOrCreate([
  147. 'follower_id' => $actor->id,
  148. 'following_id' => $target->id
  149. ]);
  150. // todo: send notification
  151. } else {
  152. // store new follower
  153. $follower = Follower::firstOrCreate([
  154. 'profile_id' => $actor->id,
  155. 'following_id' => $target->id,
  156. 'local_profile' => empty($actor->domain)
  157. ]);
  158. if($follower->wasRecentlyCreated == true) {
  159. // send notification
  160. Notification::firstOrCreate([
  161. 'profile_id' => $target->id,
  162. 'actor_id' => $actor->id,
  163. 'action' => 'follow',
  164. 'message' => $follower->toText(),
  165. 'rendered' => $follower->toHtml(),
  166. 'item_id' => $target->id,
  167. 'item_type' => 'App\Profile'
  168. ]);
  169. }
  170. // send Accept to remote profile
  171. $accept = [
  172. '@context' => 'https://www.w3.org/ns/activitystreams',
  173. 'id' => $target->permalink().'#accepts/follows/' . $follower->id,
  174. 'type' => 'Accept',
  175. 'actor' => $target->permalink(),
  176. 'object' => [
  177. 'id' => $actor->permalink('#follows/'.$target->id),
  178. 'type' => 'Follow',
  179. 'actor' => $actor->permalink(),
  180. 'object' => $target->permalink()
  181. ]
  182. ];
  183. Helpers::sendSignedObject($target, $actor->inbox_url, $accept);
  184. }
  185. }
  186. public function handleAnnounceActivity()
  187. {
  188. $actor = $this->actorFirstOrCreate($this->payload['actor']);
  189. $activity = $this->payload['object'];
  190. if(!$actor || $actor->domain == null) {
  191. return;
  192. }
  193. if(Helpers::validateLocalUrl($activity) == false) {
  194. return;
  195. }
  196. $parent = Helpers::statusFirstOrFetch($activity, true);
  197. if(!$parent) {
  198. return;
  199. }
  200. $status = Status::firstOrCreate([
  201. 'profile_id' => $actor->id,
  202. 'reblog_of_id' => $parent->id,
  203. 'type' => 'reply'
  204. ]);
  205. Notification::firstOrCreate([
  206. 'profile_id' => $parent->profile->id,
  207. 'actor_id' => $actor->id,
  208. 'action' => 'share',
  209. 'message' => $status->replyToText(),
  210. 'rendered' => $status->replyToHtml(),
  211. 'item_id' => $parent->id,
  212. 'item_type' => 'App\Status'
  213. ]);
  214. }
  215. public function handleAcceptActivity()
  216. {
  217. $actor = $this->payload['actor'];
  218. $obj = $this->payload['object'];
  219. switch ($obj['type']) {
  220. case 'Follow':
  221. $accept = [
  222. '@context' => 'https://www.w3.org/ns/activitystreams',
  223. 'id' => $target->permalink().'#accepts/follows/' . $follower->id,
  224. 'type' => 'Accept',
  225. 'actor' => $target->permalink(),
  226. 'object' => [
  227. 'id' => $actor->permalink('#follows/'.$target->id),
  228. 'type' => 'Follow',
  229. 'actor' => $actor->permalink(),
  230. 'object' => $target->permalink()
  231. ]
  232. ];
  233. break;
  234. default:
  235. # code...
  236. break;
  237. }
  238. }
  239. public function handleDeleteActivity()
  240. {
  241. $actor = $this->payload['actor'];
  242. $obj = $this->payload['object'];
  243. if(is_string($obj) && Helpers::validateUrl($obj)) {
  244. // actor object detected
  245. // todo delete actor
  246. } else if (is_array($obj) && isset($obj['type']) && $obj['type'] == 'Tombstone') {
  247. // tombstone detected
  248. $status = Status::whereUri($obj['id'])->firstOrFail();
  249. $status->forceDelete();
  250. }
  251. }
  252. public function handleLikeActivity()
  253. {
  254. $actor = $this->payload['actor'];
  255. $profile = self::actorFirstOrCreate($actor);
  256. $obj = $this->payload['object'];
  257. if(Helpers::validateLocalUrl($obj) == false) {
  258. return;
  259. }
  260. $status = Helpers::statusFirstOrFetch($obj);
  261. $like = Like::firstOrCreate([
  262. 'profile_id' => $profile->id,
  263. 'status_id' => $status->id
  264. ]);
  265. if($like->wasRecentlyCreated == false) {
  266. return;
  267. }
  268. LikePipeline::dispatch($like);
  269. }
  270. public function handleRejectActivity()
  271. {
  272. }
  273. public function handleUndoActivity()
  274. {
  275. $actor = $this->payload['actor'];
  276. $profile = self::actorFirstOrCreate($actor);
  277. $obj = $this->payload['object'];
  278. switch ($obj['type']) {
  279. case 'Accept':
  280. break;
  281. case 'Announce':
  282. break;
  283. case 'Block':
  284. break;
  285. case 'Follow':
  286. $following = self::actorFirstOrCreate($obj['object']);
  287. Follower::whereProfileId($profile->id)
  288. ->whereFollowingId($following->id)
  289. ->delete();
  290. break;
  291. case 'Like':
  292. $status = Helpers::statusFirstOrFetch($obj['object']);
  293. Like::whereProfileId($profile->id)
  294. ->whereStatusId($status->id)
  295. ->forceDelete();
  296. Notification::whereProfileId($status->profile->id)
  297. ->whereActorId($profile->id)
  298. ->whereAction('like')
  299. ->whereItemId($status->id)
  300. ->whereItemType('App\Status')
  301. ->forceDelete();
  302. break;
  303. }
  304. }
  305. }