PublicApiController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\{
  5. Hashtag,
  6. Follower,
  7. Like,
  8. Media,
  9. Notification,
  10. Profile,
  11. StatusHashtag,
  12. Status,
  13. UserFilter
  14. };
  15. use Auth,Cache;
  16. use Carbon\Carbon;
  17. use League\Fractal;
  18. use App\Transformer\Api\{
  19. AccountTransformer,
  20. RelationshipTransformer,
  21. StatusTransformer,
  22. };
  23. use App\Services\{
  24. AccountService,
  25. PublicTimelineService,
  26. UserFilterService
  27. };
  28. use App\Jobs\StatusPipeline\NewStatusPipeline;
  29. use League\Fractal\Serializer\ArraySerializer;
  30. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  31. class PublicApiController extends Controller
  32. {
  33. protected $fractal;
  34. public function __construct()
  35. {
  36. $this->fractal = new Fractal\Manager();
  37. $this->fractal->setSerializer(new ArraySerializer());
  38. }
  39. protected function getUserData($user)
  40. {
  41. if(!$user) {
  42. return [];
  43. } else {
  44. return AccountService::get($user->profile_id);
  45. }
  46. }
  47. protected function getLikes($status)
  48. {
  49. if(false == Auth::check()) {
  50. return [];
  51. } else {
  52. $profile = Auth::user()->profile;
  53. if($profile->status) {
  54. return [];
  55. }
  56. $likes = $status->likedBy()->orderBy('created_at','desc')->paginate(10);
  57. $collection = new Fractal\Resource\Collection($likes, new AccountTransformer());
  58. return $this->fractal->createData($collection)->toArray();
  59. }
  60. }
  61. protected function getShares($status)
  62. {
  63. if(false == Auth::check()) {
  64. return [];
  65. } else {
  66. $profile = Auth::user()->profile;
  67. if($profile->status) {
  68. return [];
  69. }
  70. $shares = $status->sharedBy()->orderBy('created_at','desc')->paginate(10);
  71. $collection = new Fractal\Resource\Collection($shares, new AccountTransformer());
  72. return $this->fractal->createData($collection)->toArray();
  73. }
  74. }
  75. public function status(Request $request, $username, int $postid)
  76. {
  77. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  78. $status = Status::whereProfileId($profile->id)->findOrFail($postid);
  79. $this->scopeCheck($profile, $status);
  80. $item = new Fractal\Resource\Item($status, new StatusTransformer());
  81. $res = [
  82. 'status' => $this->fractal->createData($item)->toArray(),
  83. 'user' => $this->getUserData($request->user()),
  84. 'likes' => $this->getLikes($status),
  85. 'shares' => $this->getShares($status),
  86. 'reactions' => [
  87. 'liked' => $status->liked(),
  88. 'shared' => $status->shared(),
  89. 'bookmarked' => $status->bookmarked(),
  90. ],
  91. ];
  92. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  93. }
  94. public function statusComments(Request $request, $username, int $postId)
  95. {
  96. $this->validate($request, [
  97. 'min_id' => 'nullable|integer|min:1',
  98. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  99. 'limit' => 'nullable|integer|min:5|max:50'
  100. ]);
  101. $limit = $request->limit ?? 10;
  102. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  103. $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
  104. $this->scopeCheck($profile, $status);
  105. if(Auth::check()) {
  106. $p = Auth::user()->profile;
  107. $filtered = UserFilter::whereUserId($p->id)
  108. ->whereFilterableType('App\Profile')
  109. ->whereIn('filter_type', ['mute', 'block'])
  110. ->pluck('filterable_id')->toArray();
  111. $scope = $p->id == $status->profile_id ? ['public', 'private'] : ['public'];
  112. } else {
  113. $filtered = [];
  114. $scope = ['public'];
  115. }
  116. if($request->filled('min_id') || $request->filled('max_id')) {
  117. if($request->filled('min_id')) {
  118. $replies = $status->comments()
  119. ->whereNull('reblog_of_id')
  120. ->whereIn('scope', $scope)
  121. ->whereNotIn('profile_id', $filtered)
  122. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  123. ->where('id', '>=', $request->min_id)
  124. ->orderBy('id', 'desc')
  125. ->paginate($limit);
  126. }
  127. if($request->filled('max_id')) {
  128. $replies = $status->comments()
  129. ->whereNull('reblog_of_id')
  130. ->whereIn('scope', $scope)
  131. ->whereNotIn('profile_id', $filtered)
  132. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  133. ->where('id', '<=', $request->max_id)
  134. ->orderBy('id', 'desc')
  135. ->paginate($limit);
  136. }
  137. } else {
  138. $replies = $status->comments()
  139. ->whereNull('reblog_of_id')
  140. ->whereIn('scope', $scope)
  141. ->whereNotIn('profile_id', $filtered)
  142. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  143. ->orderBy('id', 'desc')
  144. ->paginate($limit);
  145. }
  146. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  147. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  148. $res = $this->fractal->createData($resource)->toArray();
  149. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  150. }
  151. public function statusLikes(Request $request, $username, $id)
  152. {
  153. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  154. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  155. $this->scopeCheck($profile, $status);
  156. $likes = $this->getLikes($status);
  157. return response()->json([
  158. 'data' => $likes
  159. ]);
  160. }
  161. public function statusShares(Request $request, $username, $id)
  162. {
  163. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  164. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  165. $this->scopeCheck($profile, $status);
  166. $shares = $this->getShares($status);
  167. return response()->json([
  168. 'data' => $shares
  169. ]);
  170. }
  171. protected function scopeCheck(Profile $profile, Status $status)
  172. {
  173. if($profile->is_private == true && Auth::check() == false) {
  174. abort(404);
  175. }
  176. switch ($status->scope) {
  177. case 'public':
  178. case 'unlisted':
  179. break;
  180. case 'private':
  181. $user = Auth::check() ? Auth::user() : false;
  182. if(!$user) {
  183. abort(403);
  184. } else {
  185. $follows = $profile->followedBy($user->profile);
  186. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  187. abort(404);
  188. }
  189. }
  190. break;
  191. case 'direct':
  192. abort(404);
  193. break;
  194. case 'draft':
  195. abort(404);
  196. break;
  197. default:
  198. abort(404);
  199. break;
  200. }
  201. }
  202. public function publicTimelineApi(Request $request)
  203. {
  204. $this->validate($request,[
  205. 'page' => 'nullable|integer|max:40',
  206. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  207. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  208. 'limit' => 'nullable|integer|max:30'
  209. ]);
  210. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  211. abort(403, 'Authentication required.');
  212. }
  213. $page = $request->input('page');
  214. $min = $request->input('min_id');
  215. $max = $request->input('max_id');
  216. $limit = $request->input('limit') ?? 3;
  217. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  218. return Profile::whereIsPrivate(true)
  219. ->orWhere('unlisted', true)
  220. ->orWhere('status', '!=', null)
  221. ->pluck('id')
  222. ->toArray();
  223. });
  224. // if(Auth::check()) {
  225. // // $pid = Auth::user()->profile->id;
  226. // // $filters = UserFilter::whereUserId($pid)
  227. // // ->whereFilterableType('App\Profile')
  228. // // ->whereIn('filter_type', ['mute', 'block'])
  229. // // ->pluck('filterable_id')->toArray();
  230. // // $filtered = array_merge($private->toArray(), $filters);
  231. // $filtered = UserFilterService::filters(Auth::user()->profile_id);
  232. // } else {
  233. // // $filtered = $private->toArray();
  234. // $filtered = [];
  235. // }
  236. $filtered = Auth::check() ? array_merge($private, UserFilterService::filters(Auth::user()->profile_id)) : [];
  237. // if($max == 0) {
  238. // $res = PublicTimelineService::count();
  239. // if($res == 0) {
  240. // PublicTimelineService::warmCache();
  241. // $res = PublicTimelineService::get(0,4);
  242. // } else {
  243. // $res = PublicTimelineService::get(0,4);
  244. // }
  245. // return response()->json($res);
  246. // }
  247. if($min || $max) {
  248. $dir = $min ? '>' : '<';
  249. $id = $min ?? $max;
  250. $timeline = Status::select(
  251. 'id',
  252. 'uri',
  253. 'caption',
  254. 'rendered',
  255. 'profile_id',
  256. 'type',
  257. 'in_reply_to_id',
  258. 'reblog_of_id',
  259. 'is_nsfw',
  260. 'scope',
  261. 'local',
  262. 'reply_count',
  263. 'comments_disabled',
  264. 'place_id',
  265. 'likes_count',
  266. 'reblogs_count',
  267. 'created_at',
  268. 'updated_at'
  269. )->where('id', $dir, $id)
  270. ->with('profile', 'hashtags', 'mentions')
  271. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  272. ->whereLocal(true)
  273. ->whereNotIn('profile_id', $filtered)
  274. ->whereVisibility('public')
  275. ->orderBy('created_at', 'desc')
  276. ->limit($limit)
  277. ->get();
  278. //->toSql();
  279. } else {
  280. $timeline = Status::select(
  281. 'id',
  282. 'uri',
  283. 'caption',
  284. 'rendered',
  285. 'profile_id',
  286. 'type',
  287. 'in_reply_to_id',
  288. 'reblog_of_id',
  289. 'is_nsfw',
  290. 'scope',
  291. 'local',
  292. 'reply_count',
  293. 'comments_disabled',
  294. 'created_at',
  295. 'place_id',
  296. 'likes_count',
  297. 'reblogs_count',
  298. 'updated_at'
  299. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  300. ->with('profile', 'hashtags', 'mentions')
  301. ->whereLocal(true)
  302. ->whereNotIn('profile_id', $filtered)
  303. ->whereVisibility('public')
  304. ->orderBy('created_at', 'desc')
  305. ->simplePaginate($limit);
  306. //->toSql();
  307. }
  308. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  309. $res = $this->fractal->createData($fractal)->toArray();
  310. return response()->json($res);
  311. }
  312. public function homeTimelineApi(Request $request)
  313. {
  314. if(!Auth::check()) {
  315. return abort(403);
  316. }
  317. $this->validate($request,[
  318. 'page' => 'nullable|integer|max:40',
  319. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  320. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  321. 'limit' => 'nullable|integer|max:40'
  322. ]);
  323. $page = $request->input('page');
  324. $min = $request->input('min_id');
  325. $max = $request->input('max_id');
  326. $limit = $request->input('limit') ?? 3;
  327. // TODO: Use redis for timelines
  328. // $timeline = Timeline::build()->local();
  329. $pid = Auth::user()->profile->id;
  330. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  331. $following = Follower::whereProfileId($pid)->pluck('following_id');
  332. return $following->push($pid)->toArray();
  333. });
  334. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  335. // return Profile::whereIsPrivate(true)
  336. // ->orWhere('unlisted', true)
  337. // ->orWhere('status', '!=', null)
  338. // ->pluck('id');
  339. // });
  340. // $private = $private->diff($following)->flatten();
  341. // $filters = UserFilter::whereUserId($pid)
  342. // ->whereFilterableType('App\Profile')
  343. // ->whereIn('filter_type', ['mute', 'block'])
  344. // ->pluck('filterable_id')->toArray();
  345. // $filtered = array_merge($private->toArray(), $filters);
  346. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  347. if($min || $max) {
  348. $dir = $min ? '>' : '<';
  349. $id = $min ?? $max;
  350. $timeline = Status::select(
  351. 'id',
  352. 'uri',
  353. 'caption',
  354. 'rendered',
  355. 'profile_id',
  356. 'type',
  357. 'in_reply_to_id',
  358. 'reblog_of_id',
  359. 'is_nsfw',
  360. 'scope',
  361. 'local',
  362. 'reply_count',
  363. 'comments_disabled',
  364. 'place_id',
  365. 'likes_count',
  366. 'reblogs_count',
  367. 'created_at',
  368. 'updated_at'
  369. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  370. ->with('profile', 'hashtags', 'mentions')
  371. ->where('id', $dir, $id)
  372. ->whereIn('profile_id', $following)
  373. ->whereNotIn('profile_id', $filtered)
  374. ->whereIn('visibility',['public', 'unlisted', 'private'])
  375. ->orderBy('created_at', 'desc')
  376. ->limit($limit)
  377. ->get();
  378. } else {
  379. $timeline = Status::select(
  380. 'id',
  381. 'uri',
  382. 'caption',
  383. 'rendered',
  384. 'profile_id',
  385. 'type',
  386. 'in_reply_to_id',
  387. 'reblog_of_id',
  388. 'is_nsfw',
  389. 'scope',
  390. 'local',
  391. 'reply_count',
  392. 'comments_disabled',
  393. 'place_id',
  394. 'likes_count',
  395. 'reblogs_count',
  396. 'created_at',
  397. 'updated_at'
  398. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  399. ->with('profile', 'hashtags', 'mentions')
  400. ->whereIn('profile_id', $following)
  401. ->whereNotIn('profile_id', $filtered)
  402. ->whereIn('visibility',['public', 'unlisted', 'private'])
  403. ->orderBy('created_at', 'desc')
  404. ->simplePaginate($limit);
  405. }
  406. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  407. $res = $this->fractal->createData($fractal)->toArray();
  408. return response()->json($res);
  409. }
  410. public function networkTimelineApi(Request $request)
  411. {
  412. return response()->json([]);
  413. }
  414. public function relationships(Request $request)
  415. {
  416. if(!Auth::check()) {
  417. return response()->json([]);
  418. }
  419. $this->validate($request, [
  420. 'id' => 'required|array|min:1|max:20',
  421. 'id.*' => 'required|integer'
  422. ]);
  423. $ids = collect($request->input('id'));
  424. $filtered = $ids->filter(function($v) {
  425. return $v != Auth::user()->profile->id;
  426. });
  427. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  428. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  429. $res = $this->fractal->createData($fractal)->toArray();
  430. return response()->json($res);
  431. }
  432. public function account(Request $request, $id)
  433. {
  434. $res = AccountService::get($id);
  435. return response()->json($res);
  436. }
  437. public function accountFollowers(Request $request, $id)
  438. {
  439. abort_unless(Auth::check(), 403);
  440. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  441. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  442. return response()->json([]);
  443. }
  444. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  445. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  446. $res = $this->fractal->createData($resource)->toArray();
  447. return response()->json($res);
  448. }
  449. public function accountFollowing(Request $request, $id)
  450. {
  451. abort_unless(Auth::check(), 403);
  452. $profile = Profile::with('user')
  453. ->whereNull('status')
  454. ->whereNull('domain')
  455. ->findOrFail($id);
  456. // filter by username
  457. $search = $request->input('fbu');
  458. $owner = Auth::id() == $profile->user_id;
  459. $filter = ($owner == true) && ($search != null);
  460. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  461. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  462. if($search) {
  463. abort_if(!$owner, 404);
  464. $following = $profile->following()
  465. ->where('profiles.username', 'like', '%'.$search.'%')
  466. ->orderByDesc('followers.created_at')
  467. ->paginate(10);
  468. } else {
  469. $following = $profile->following()
  470. ->orderByDesc('followers.created_at')
  471. ->paginate(10);
  472. }
  473. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  474. $res = $this->fractal->createData($resource)->toArray();
  475. return response()->json($res);
  476. }
  477. public function accountStatuses(Request $request, $id)
  478. {
  479. $this->validate($request, [
  480. 'only_media' => 'nullable',
  481. 'pinned' => 'nullable',
  482. 'exclude_replies' => 'nullable',
  483. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  484. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  485. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  486. 'limit' => 'nullable|integer|min:1|max:24'
  487. ]);
  488. $profile = Profile::whereNull('status')->findOrFail($id);
  489. $limit = $request->limit ?? 9;
  490. $max_id = $request->max_id;
  491. $min_id = $request->min_id;
  492. $scope = $request->only_media == true ?
  493. ['photo', 'photo:album', 'video', 'video:album'] :
  494. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  495. if($profile->is_private) {
  496. if(!Auth::check()) {
  497. return response()->json([]);
  498. }
  499. $pid = Auth::user()->profile->id;
  500. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  501. $following = Follower::whereProfileId($pid)->pluck('following_id');
  502. return $following->push($pid)->toArray();
  503. });
  504. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  505. } else {
  506. if(Auth::check()) {
  507. $pid = Auth::user()->profile->id;
  508. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  509. $following = Follower::whereProfileId($pid)->pluck('following_id');
  510. return $following->push($pid)->toArray();
  511. });
  512. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  513. } else {
  514. $visibility = ['public', 'unlisted'];
  515. }
  516. }
  517. $dir = $min_id ? '>' : '<';
  518. $id = $min_id ?? $max_id;
  519. $timeline = Status::select(
  520. 'id',
  521. 'uri',
  522. 'caption',
  523. 'rendered',
  524. 'profile_id',
  525. 'type',
  526. 'in_reply_to_id',
  527. 'reblog_of_id',
  528. 'is_nsfw',
  529. 'likes_count',
  530. 'reblogs_count',
  531. 'scope',
  532. 'local',
  533. 'created_at',
  534. 'updated_at'
  535. )->whereProfileId($profile->id)
  536. ->whereIn('type', $scope)
  537. ->whereLocal(true)
  538. ->whereNull('uri')
  539. ->where('id', $dir, $id)
  540. ->whereIn('visibility', $visibility)
  541. ->latest()
  542. ->limit($limit)
  543. ->get();
  544. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  545. $res = $this->fractal->createData($resource)->toArray();
  546. return response()->json($res);
  547. }
  548. }