PublicApiController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. $pid = Auth::user()->profile->id;
  107. $filtered = UserFilter::whereUserId($pid)
  108. ->whereFilterableType('App\Profile')
  109. ->whereIn('filter_type', ['mute', 'block'])
  110. ->pluck('filterable_id')->toArray();
  111. } else {
  112. $filtered = [];
  113. }
  114. if($request->filled('min_id') || $request->filled('max_id')) {
  115. if($request->filled('min_id')) {
  116. $replies = $status->comments()
  117. ->whereNull('reblog_of_id')
  118. ->whereNotIn('profile_id', $filtered)
  119. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  120. ->where('id', '>=', $request->min_id)
  121. ->orderBy('id', 'desc')
  122. ->paginate($limit);
  123. }
  124. if($request->filled('max_id')) {
  125. $replies = $status->comments()
  126. ->whereNull('reblog_of_id')
  127. ->whereNotIn('profile_id', $filtered)
  128. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  129. ->where('id', '<=', $request->max_id)
  130. ->orderBy('id', 'desc')
  131. ->paginate($limit);
  132. }
  133. } else {
  134. $replies = $status->comments()
  135. ->whereNull('reblog_of_id')
  136. ->whereNotIn('profile_id', $filtered)
  137. ->select('id', 'caption', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
  138. ->orderBy('id', 'desc')
  139. ->paginate($limit);
  140. }
  141. $resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
  142. $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
  143. $res = $this->fractal->createData($resource)->toArray();
  144. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  145. }
  146. public function statusLikes(Request $request, $username, $id)
  147. {
  148. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  149. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  150. $this->scopeCheck($profile, $status);
  151. $likes = $this->getLikes($status);
  152. return response()->json([
  153. 'data' => $likes
  154. ]);
  155. }
  156. public function statusShares(Request $request, $username, $id)
  157. {
  158. $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
  159. $status = Status::whereProfileId($profile->id)->findOrFail($id);
  160. $this->scopeCheck($profile, $status);
  161. $shares = $this->getShares($status);
  162. return response()->json([
  163. 'data' => $shares
  164. ]);
  165. }
  166. protected function scopeCheck(Profile $profile, Status $status)
  167. {
  168. if($profile->is_private == true && Auth::check() == false) {
  169. abort(404);
  170. }
  171. switch ($status->scope) {
  172. case 'public':
  173. case 'unlisted':
  174. break;
  175. case 'private':
  176. $user = Auth::check() ? Auth::user() : false;
  177. if(!$user) {
  178. abort(403);
  179. } else {
  180. $follows = $profile->followedBy($user->profile);
  181. if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
  182. abort(404);
  183. }
  184. }
  185. break;
  186. case 'direct':
  187. abort(404);
  188. break;
  189. case 'draft':
  190. abort(404);
  191. break;
  192. default:
  193. abort(404);
  194. break;
  195. }
  196. }
  197. public function publicTimelineApi(Request $request)
  198. {
  199. $this->validate($request,[
  200. 'page' => 'nullable|integer|max:40',
  201. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  202. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  203. 'limit' => 'nullable|integer|max:30'
  204. ]);
  205. if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
  206. abort(403, 'Authentication required.');
  207. }
  208. $page = $request->input('page');
  209. $min = $request->input('min_id');
  210. $max = $request->input('max_id');
  211. $limit = $request->input('limit') ?? 3;
  212. $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  213. return Profile::whereIsPrivate(true)
  214. ->orWhere('unlisted', true)
  215. ->orWhere('status', '!=', null)
  216. ->pluck('id')
  217. ->toArray();
  218. });
  219. // if(Auth::check()) {
  220. // // $pid = Auth::user()->profile->id;
  221. // // $filters = UserFilter::whereUserId($pid)
  222. // // ->whereFilterableType('App\Profile')
  223. // // ->whereIn('filter_type', ['mute', 'block'])
  224. // // ->pluck('filterable_id')->toArray();
  225. // // $filtered = array_merge($private->toArray(), $filters);
  226. // $filtered = UserFilterService::filters(Auth::user()->profile_id);
  227. // } else {
  228. // // $filtered = $private->toArray();
  229. // $filtered = [];
  230. // }
  231. $filtered = Auth::check() ? array_merge($private, UserFilterService::filters(Auth::user()->profile_id)) : [];
  232. if($max == 0) {
  233. $res = PublicTimelineService::count();
  234. if($res == 0) {
  235. PublicTimelineService::warmCache();
  236. $res = PublicTimelineService::get(0,4);
  237. } else {
  238. $res = PublicTimelineService::get(0,4);
  239. }
  240. return $res;
  241. }
  242. if($min || $max) {
  243. $dir = $min ? '>' : '<';
  244. $id = $min ?? $max;
  245. $timeline = Status::select(
  246. 'id',
  247. 'uri',
  248. 'caption',
  249. 'rendered',
  250. 'profile_id',
  251. 'type',
  252. 'in_reply_to_id',
  253. 'reblog_of_id',
  254. 'is_nsfw',
  255. 'scope',
  256. 'local',
  257. 'reply_count',
  258. 'comments_disabled',
  259. 'place_id',
  260. 'likes_count',
  261. 'reblogs_count',
  262. 'created_at',
  263. 'updated_at'
  264. )->where('id', $dir, $id)
  265. ->with('profile', 'hashtags', 'mentions')
  266. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  267. ->whereLocal(true)
  268. ->whereNotIn('profile_id', $filtered)
  269. ->whereVisibility('public')
  270. ->orderBy('created_at', 'desc')
  271. ->limit($limit)
  272. ->get();
  273. //->toSql();
  274. } else {
  275. $timeline = Status::select(
  276. 'id',
  277. 'uri',
  278. 'caption',
  279. 'rendered',
  280. 'profile_id',
  281. 'type',
  282. 'in_reply_to_id',
  283. 'reblog_of_id',
  284. 'is_nsfw',
  285. 'scope',
  286. 'local',
  287. 'reply_count',
  288. 'comments_disabled',
  289. 'created_at',
  290. 'place_id',
  291. 'likes_count',
  292. 'reblogs_count',
  293. 'updated_at'
  294. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  295. ->with('profile', 'hashtags', 'mentions')
  296. ->whereLocal(true)
  297. ->whereNotIn('profile_id', $filtered)
  298. ->whereVisibility('public')
  299. ->orderBy('created_at', 'desc')
  300. ->simplePaginate($limit);
  301. //->toSql();
  302. }
  303. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  304. $res = $this->fractal->createData($fractal)->toArray();
  305. return response()->json($res);
  306. }
  307. public function homeTimelineApi(Request $request)
  308. {
  309. if(!Auth::check()) {
  310. return abort(403);
  311. }
  312. $this->validate($request,[
  313. 'page' => 'nullable|integer|max:40',
  314. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  315. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  316. 'limit' => 'nullable|integer|max:40'
  317. ]);
  318. $page = $request->input('page');
  319. $min = $request->input('min_id');
  320. $max = $request->input('max_id');
  321. $limit = $request->input('limit') ?? 3;
  322. // TODO: Use redis for timelines
  323. // $timeline = Timeline::build()->local();
  324. $pid = Auth::user()->profile->id;
  325. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  326. $following = Follower::whereProfileId($pid)->pluck('following_id');
  327. return $following->push($pid)->toArray();
  328. });
  329. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  330. // return Profile::whereIsPrivate(true)
  331. // ->orWhere('unlisted', true)
  332. // ->orWhere('status', '!=', null)
  333. // ->pluck('id');
  334. // });
  335. // $private = $private->diff($following)->flatten();
  336. // $filters = UserFilter::whereUserId($pid)
  337. // ->whereFilterableType('App\Profile')
  338. // ->whereIn('filter_type', ['mute', 'block'])
  339. // ->pluck('filterable_id')->toArray();
  340. // $filtered = array_merge($private->toArray(), $filters);
  341. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  342. if($min || $max) {
  343. $dir = $min ? '>' : '<';
  344. $id = $min ?? $max;
  345. $timeline = Status::select(
  346. 'id',
  347. 'uri',
  348. 'caption',
  349. 'rendered',
  350. 'profile_id',
  351. 'type',
  352. 'in_reply_to_id',
  353. 'reblog_of_id',
  354. 'is_nsfw',
  355. 'scope',
  356. 'local',
  357. 'reply_count',
  358. 'comments_disabled',
  359. 'place_id',
  360. 'likes_count',
  361. 'reblogs_count',
  362. 'created_at',
  363. 'updated_at'
  364. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  365. ->with('profile', 'hashtags', 'mentions')
  366. ->where('id', $dir, $id)
  367. ->whereIn('profile_id', $following)
  368. ->whereNotIn('profile_id', $filtered)
  369. ->whereIn('visibility',['public', 'unlisted', 'private'])
  370. ->orderBy('created_at', 'desc')
  371. ->limit($limit)
  372. ->get();
  373. } else {
  374. $timeline = Status::select(
  375. 'id',
  376. 'uri',
  377. 'caption',
  378. 'rendered',
  379. 'profile_id',
  380. 'type',
  381. 'in_reply_to_id',
  382. 'reblog_of_id',
  383. 'is_nsfw',
  384. 'scope',
  385. 'local',
  386. 'reply_count',
  387. 'comments_disabled',
  388. 'place_id',
  389. 'likes_count',
  390. 'reblogs_count',
  391. 'created_at',
  392. 'updated_at'
  393. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  394. ->with('profile', 'hashtags', 'mentions')
  395. ->whereIn('profile_id', $following)
  396. ->whereNotIn('profile_id', $filtered)
  397. ->whereIn('visibility',['public', 'unlisted', 'private'])
  398. ->orderBy('created_at', 'desc')
  399. ->simplePaginate($limit);
  400. }
  401. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  402. $res = $this->fractal->createData($fractal)->toArray();
  403. return response()->json($res);
  404. }
  405. public function networkTimelineApi(Request $request)
  406. {
  407. return response()->json([]);
  408. }
  409. public function relationships(Request $request)
  410. {
  411. if(!Auth::check()) {
  412. return response()->json([]);
  413. }
  414. $this->validate($request, [
  415. 'id' => 'required|array|min:1|max:20',
  416. 'id.*' => 'required|integer'
  417. ]);
  418. $ids = collect($request->input('id'));
  419. $filtered = $ids->filter(function($v) {
  420. return $v != Auth::user()->profile->id;
  421. });
  422. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  423. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  424. $res = $this->fractal->createData($fractal)->toArray();
  425. return response()->json($res);
  426. }
  427. public function account(Request $request, $id)
  428. {
  429. $res = AccountService::get($id);
  430. return response()->json($res);
  431. }
  432. public function accountFollowers(Request $request, $id)
  433. {
  434. abort_unless(Auth::check(), 403);
  435. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  436. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  437. return response()->json([]);
  438. }
  439. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  440. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  441. $res = $this->fractal->createData($resource)->toArray();
  442. return response()->json($res);
  443. }
  444. public function accountFollowing(Request $request, $id)
  445. {
  446. abort_unless(Auth::check(), 403);
  447. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  448. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_following) {
  449. return response()->json([]);
  450. }
  451. $following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
  452. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  453. $res = $this->fractal->createData($resource)->toArray();
  454. return response()->json($res);
  455. }
  456. public function accountStatuses(Request $request, $id)
  457. {
  458. $this->validate($request, [
  459. 'only_media' => 'nullable',
  460. 'pinned' => 'nullable',
  461. 'exclude_replies' => 'nullable',
  462. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  463. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  464. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  465. 'limit' => 'nullable|integer|min:1|max:24'
  466. ]);
  467. $profile = Profile::whereNull('status')->findOrFail($id);
  468. $limit = $request->limit ?? 9;
  469. $max_id = $request->max_id;
  470. $min_id = $request->min_id;
  471. $scope = $request->only_media == true ?
  472. ['photo', 'photo:album', 'video', 'video:album'] :
  473. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  474. if($profile->is_private) {
  475. if(!Auth::check()) {
  476. return response()->json([]);
  477. }
  478. $pid = Auth::user()->profile->id;
  479. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  480. $following = Follower::whereProfileId($pid)->pluck('following_id');
  481. return $following->push($pid)->toArray();
  482. });
  483. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  484. } else {
  485. if(Auth::check()) {
  486. $pid = Auth::user()->profile->id;
  487. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  488. $following = Follower::whereProfileId($pid)->pluck('following_id');
  489. return $following->push($pid)->toArray();
  490. });
  491. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  492. } else {
  493. $visibility = ['public', 'unlisted'];
  494. }
  495. }
  496. $dir = $min_id ? '>' : '<';
  497. $id = $min_id ?? $max_id;
  498. $timeline = Status::select(
  499. 'id',
  500. 'uri',
  501. 'caption',
  502. 'rendered',
  503. 'profile_id',
  504. 'type',
  505. 'in_reply_to_id',
  506. 'reblog_of_id',
  507. 'is_nsfw',
  508. 'likes_count',
  509. 'reblogs_count',
  510. 'scope',
  511. 'local',
  512. 'created_at',
  513. 'updated_at'
  514. )->whereProfileId($profile->id)
  515. ->whereIn('type', $scope)
  516. ->whereLocal(true)
  517. ->whereNull('uri')
  518. ->where('id', $dir, $id)
  519. ->whereIn('visibility', $visibility)
  520. ->latest()
  521. ->limit($limit)
  522. ->get();
  523. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  524. $res = $this->fractal->createData($resource)->toArray();
  525. return response()->json($res);
  526. }
  527. }