PublicApiController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 response($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. // $res = $timeline;
  306. return response()->json($res);
  307. }
  308. public function homeTimelineApi(Request $request)
  309. {
  310. if(!Auth::check()) {
  311. return abort(403);
  312. }
  313. $this->validate($request,[
  314. 'page' => 'nullable|integer|max:40',
  315. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  316. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  317. 'limit' => 'nullable|integer|max:40'
  318. ]);
  319. $page = $request->input('page');
  320. $min = $request->input('min_id');
  321. $max = $request->input('max_id');
  322. $limit = $request->input('limit') ?? 3;
  323. // TODO: Use redis for timelines
  324. // $timeline = Timeline::build()->local();
  325. $pid = Auth::user()->profile->id;
  326. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  327. $following = Follower::whereProfileId($pid)->pluck('following_id');
  328. return $following->push($pid)->toArray();
  329. });
  330. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  331. // return Profile::whereIsPrivate(true)
  332. // ->orWhere('unlisted', true)
  333. // ->orWhere('status', '!=', null)
  334. // ->pluck('id');
  335. // });
  336. // $private = $private->diff($following)->flatten();
  337. // $filters = UserFilter::whereUserId($pid)
  338. // ->whereFilterableType('App\Profile')
  339. // ->whereIn('filter_type', ['mute', 'block'])
  340. // ->pluck('filterable_id')->toArray();
  341. // $filtered = array_merge($private->toArray(), $filters);
  342. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  343. if($min || $max) {
  344. $dir = $min ? '>' : '<';
  345. $id = $min ?? $max;
  346. $timeline = Status::select(
  347. 'id',
  348. 'uri',
  349. 'caption',
  350. 'rendered',
  351. 'profile_id',
  352. 'type',
  353. 'in_reply_to_id',
  354. 'reblog_of_id',
  355. 'is_nsfw',
  356. 'scope',
  357. 'local',
  358. 'reply_count',
  359. 'comments_disabled',
  360. 'place_id',
  361. 'likes_count',
  362. 'reblogs_count',
  363. 'created_at',
  364. 'updated_at'
  365. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  366. ->with('profile', 'hashtags', 'mentions')
  367. ->where('id', $dir, $id)
  368. ->whereIn('profile_id', $following)
  369. ->whereNotIn('profile_id', $filtered)
  370. ->whereIn('visibility',['public', 'unlisted', 'private'])
  371. ->orderBy('created_at', 'desc')
  372. ->limit($limit)
  373. ->get();
  374. } else {
  375. $timeline = Status::select(
  376. 'id',
  377. 'uri',
  378. 'caption',
  379. 'rendered',
  380. 'profile_id',
  381. 'type',
  382. 'in_reply_to_id',
  383. 'reblog_of_id',
  384. 'is_nsfw',
  385. 'scope',
  386. 'local',
  387. 'reply_count',
  388. 'comments_disabled',
  389. 'place_id',
  390. 'likes_count',
  391. 'reblogs_count',
  392. 'created_at',
  393. 'updated_at'
  394. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  395. ->with('profile', 'hashtags', 'mentions')
  396. ->whereIn('profile_id', $following)
  397. ->whereNotIn('profile_id', $filtered)
  398. ->whereIn('visibility',['public', 'unlisted', 'private'])
  399. ->orderBy('created_at', 'desc')
  400. ->simplePaginate($limit);
  401. }
  402. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  403. $res = $this->fractal->createData($fractal)->toArray();
  404. return response()->json($res);
  405. }
  406. public function networkTimelineApi(Request $request)
  407. {
  408. return response()->json([]);
  409. }
  410. public function relationships(Request $request)
  411. {
  412. if(!Auth::check()) {
  413. return response()->json([]);
  414. }
  415. $this->validate($request, [
  416. 'id' => 'required|array|min:1|max:20',
  417. 'id.*' => 'required|integer'
  418. ]);
  419. $ids = collect($request->input('id'));
  420. $filtered = $ids->filter(function($v) {
  421. return $v != Auth::user()->profile->id;
  422. });
  423. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  424. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  425. $res = $this->fractal->createData($fractal)->toArray();
  426. return response()->json($res);
  427. }
  428. public function account(Request $request, $id)
  429. {
  430. $res = AccountService::get($id);
  431. return response()->json($res);
  432. }
  433. public function accountFollowers(Request $request, $id)
  434. {
  435. abort_unless(Auth::check(), 403);
  436. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  437. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  438. return response()->json([]);
  439. }
  440. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  441. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  442. $res = $this->fractal->createData($resource)->toArray();
  443. return response()->json($res);
  444. }
  445. public function accountFollowing(Request $request, $id)
  446. {
  447. abort_unless(Auth::check(), 403);
  448. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  449. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_following) {
  450. return response()->json([]);
  451. }
  452. $following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
  453. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  454. $res = $this->fractal->createData($resource)->toArray();
  455. return response()->json($res);
  456. }
  457. public function accountStatuses(Request $request, $id)
  458. {
  459. $this->validate($request, [
  460. 'only_media' => 'nullable',
  461. 'pinned' => 'nullable',
  462. 'exclude_replies' => 'nullable',
  463. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  464. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  465. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  466. 'limit' => 'nullable|integer|min:1|max:24'
  467. ]);
  468. $profile = Profile::whereNull('status')->findOrFail($id);
  469. $limit = $request->limit ?? 9;
  470. $max_id = $request->max_id;
  471. $min_id = $request->min_id;
  472. $scope = $request->only_media == true ?
  473. ['photo', 'photo:album', 'video', 'video:album'] :
  474. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  475. if($profile->is_private) {
  476. if(!Auth::check()) {
  477. return response()->json([]);
  478. }
  479. $pid = Auth::user()->profile->id;
  480. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  481. $following = Follower::whereProfileId($pid)->pluck('following_id');
  482. return $following->push($pid)->toArray();
  483. });
  484. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  485. } else {
  486. if(Auth::check()) {
  487. $pid = Auth::user()->profile->id;
  488. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  489. $following = Follower::whereProfileId($pid)->pluck('following_id');
  490. return $following->push($pid)->toArray();
  491. });
  492. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  493. } else {
  494. $visibility = ['public', 'unlisted'];
  495. }
  496. }
  497. $dir = $min_id ? '>' : '<';
  498. $id = $min_id ?? $max_id;
  499. $timeline = Status::select(
  500. 'id',
  501. 'uri',
  502. 'caption',
  503. 'rendered',
  504. 'profile_id',
  505. 'type',
  506. 'in_reply_to_id',
  507. 'reblog_of_id',
  508. 'is_nsfw',
  509. 'likes_count',
  510. 'reblogs_count',
  511. 'scope',
  512. 'local',
  513. 'created_at',
  514. 'updated_at'
  515. )->whereProfileId($profile->id)
  516. ->whereIn('type', $scope)
  517. ->whereLocal(true)
  518. ->whereNull('uri')
  519. ->where('id', $dir, $id)
  520. ->whereIn('visibility', $visibility)
  521. ->latest()
  522. ->limit($limit)
  523. ->get();
  524. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  525. $res = $this->fractal->createData($resource)->toArray();
  526. return response()->json($res);
  527. }
  528. }