PublicApiController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. $filtered = UserFilter::whereUserId(Auth::user()->profile_id)
  218. ->whereFilterableType('App\Profile')
  219. ->whereIn('filter_type', ['mute', 'block'])
  220. ->pluck('filterable_id')->toArray();
  221. if($min || $max) {
  222. $dir = $min ? '>' : '<';
  223. $id = $min ?? $max;
  224. $timeline = Status::select(
  225. 'id',
  226. 'uri',
  227. 'caption',
  228. 'rendered',
  229. 'profile_id',
  230. 'type',
  231. 'in_reply_to_id',
  232. 'reblog_of_id',
  233. 'is_nsfw',
  234. 'scope',
  235. 'local',
  236. 'reply_count',
  237. 'comments_disabled',
  238. 'place_id',
  239. 'likes_count',
  240. 'reblogs_count',
  241. 'created_at',
  242. 'updated_at'
  243. )->where('id', $dir, $id)
  244. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  245. ->whereNotIn('profile_id', $filtered)
  246. ->whereLocal(true)
  247. ->whereVisibility('public')
  248. ->orderBy('created_at', 'desc')
  249. ->limit($limit)
  250. ->get();
  251. } else {
  252. $timeline = Status::select(
  253. 'id',
  254. 'uri',
  255. 'caption',
  256. 'rendered',
  257. 'profile_id',
  258. 'type',
  259. 'in_reply_to_id',
  260. 'reblog_of_id',
  261. 'is_nsfw',
  262. 'scope',
  263. 'local',
  264. 'reply_count',
  265. 'comments_disabled',
  266. 'created_at',
  267. 'place_id',
  268. 'likes_count',
  269. 'reblogs_count',
  270. 'updated_at'
  271. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  272. ->whereNotIn('profile_id', $filtered)
  273. ->with('profile', 'hashtags', 'mentions')
  274. ->whereLocal(true)
  275. ->whereVisibility('public')
  276. ->orderBy('created_at', 'desc')
  277. ->simplePaginate($limit);
  278. }
  279. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  280. $res = $this->fractal->createData($fractal)->toArray();
  281. return response()->json($res);
  282. }
  283. public function homeTimelineApi(Request $request)
  284. {
  285. if(!Auth::check()) {
  286. return abort(403);
  287. }
  288. $this->validate($request,[
  289. 'page' => 'nullable|integer|max:40',
  290. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  291. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  292. 'limit' => 'nullable|integer|max:40'
  293. ]);
  294. $page = $request->input('page');
  295. $min = $request->input('min_id');
  296. $max = $request->input('max_id');
  297. $limit = $request->input('limit') ?? 3;
  298. // TODO: Use redis for timelines
  299. // $timeline = Timeline::build()->local();
  300. $pid = Auth::user()->profile->id;
  301. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  302. $following = Follower::whereProfileId($pid)->pluck('following_id');
  303. return $following->push($pid)->toArray();
  304. });
  305. // $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
  306. // return Profile::whereIsPrivate(true)
  307. // ->orWhere('unlisted', true)
  308. // ->orWhere('status', '!=', null)
  309. // ->pluck('id');
  310. // });
  311. // $private = $private->diff($following)->flatten();
  312. // $filters = UserFilter::whereUserId($pid)
  313. // ->whereFilterableType('App\Profile')
  314. // ->whereIn('filter_type', ['mute', 'block'])
  315. // ->pluck('filterable_id')->toArray();
  316. // $filtered = array_merge($private->toArray(), $filters);
  317. $filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
  318. if($min || $max) {
  319. $dir = $min ? '>' : '<';
  320. $id = $min ?? $max;
  321. $timeline = Status::select(
  322. 'id',
  323. 'uri',
  324. 'caption',
  325. 'rendered',
  326. 'profile_id',
  327. 'type',
  328. 'in_reply_to_id',
  329. 'reblog_of_id',
  330. 'is_nsfw',
  331. 'scope',
  332. 'local',
  333. 'reply_count',
  334. 'comments_disabled',
  335. 'place_id',
  336. 'likes_count',
  337. 'reblogs_count',
  338. 'created_at',
  339. 'updated_at'
  340. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  341. ->with('profile', 'hashtags', 'mentions')
  342. ->where('id', $dir, $id)
  343. ->whereIn('profile_id', $following)
  344. ->whereNotIn('profile_id', $filtered)
  345. ->whereIn('visibility',['public', 'unlisted', 'private'])
  346. ->orderBy('created_at', 'desc')
  347. ->limit($limit)
  348. ->get();
  349. } else {
  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. ->whereIn('profile_id', $following)
  372. ->whereNotIn('profile_id', $filtered)
  373. ->whereIn('visibility',['public', 'unlisted', 'private'])
  374. ->orderBy('created_at', 'desc')
  375. ->simplePaginate($limit);
  376. }
  377. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  378. $res = $this->fractal->createData($fractal)->toArray();
  379. return response()->json($res);
  380. }
  381. public function networkTimelineApi(Request $request)
  382. {
  383. return response()->json([]);
  384. }
  385. public function relationships(Request $request)
  386. {
  387. if(!Auth::check()) {
  388. return response()->json([]);
  389. }
  390. $this->validate($request, [
  391. 'id' => 'required|array|min:1|max:20',
  392. 'id.*' => 'required|integer'
  393. ]);
  394. $ids = collect($request->input('id'));
  395. $filtered = $ids->filter(function($v) {
  396. return $v != Auth::user()->profile->id;
  397. });
  398. $relations = Profile::whereNull('status')->findOrFail($filtered->all());
  399. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  400. $res = $this->fractal->createData($fractal)->toArray();
  401. return response()->json($res);
  402. }
  403. public function account(Request $request, $id)
  404. {
  405. $res = AccountService::get($id);
  406. return response()->json($res);
  407. }
  408. public function accountFollowers(Request $request, $id)
  409. {
  410. abort_unless(Auth::check(), 403);
  411. $profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
  412. if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
  413. return response()->json([]);
  414. }
  415. $followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
  416. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  417. $res = $this->fractal->createData($resource)->toArray();
  418. return response()->json($res);
  419. }
  420. public function accountFollowing(Request $request, $id)
  421. {
  422. abort_unless(Auth::check(), 403);
  423. $profile = Profile::with('user')
  424. ->whereNull('status')
  425. ->whereNull('domain')
  426. ->findOrFail($id);
  427. // filter by username
  428. $search = $request->input('fbu');
  429. $owner = Auth::id() == $profile->user_id;
  430. $filter = ($owner == true) && ($search != null);
  431. abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
  432. abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
  433. if($search) {
  434. abort_if(!$owner, 404);
  435. $following = $profile->following()
  436. ->where('profiles.username', 'like', '%'.$search.'%')
  437. ->orderByDesc('followers.created_at')
  438. ->paginate(10);
  439. } else {
  440. $following = $profile->following()
  441. ->orderByDesc('followers.created_at')
  442. ->paginate(10);
  443. }
  444. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  445. $res = $this->fractal->createData($resource)->toArray();
  446. return response()->json($res);
  447. }
  448. public function accountStatuses(Request $request, $id)
  449. {
  450. $this->validate($request, [
  451. 'only_media' => 'nullable',
  452. 'pinned' => 'nullable',
  453. 'exclude_replies' => 'nullable',
  454. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  455. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  456. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  457. 'limit' => 'nullable|integer|min:1|max:24'
  458. ]);
  459. $profile = Profile::whereNull('status')->findOrFail($id);
  460. $limit = $request->limit ?? 9;
  461. $max_id = $request->max_id;
  462. $min_id = $request->min_id;
  463. $scope = $request->only_media == true ?
  464. ['photo', 'photo:album', 'video', 'video:album'] :
  465. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  466. if($profile->is_private) {
  467. if(!Auth::check()) {
  468. return response()->json([]);
  469. }
  470. $pid = Auth::user()->profile->id;
  471. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  472. $following = Follower::whereProfileId($pid)->pluck('following_id');
  473. return $following->push($pid)->toArray();
  474. });
  475. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  476. } else {
  477. if(Auth::check()) {
  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'] : ['public', 'unlisted'];
  484. } else {
  485. $visibility = ['public', 'unlisted'];
  486. }
  487. }
  488. $dir = $min_id ? '>' : '<';
  489. $id = $min_id ?? $max_id;
  490. $timeline = Status::select(
  491. 'id',
  492. 'uri',
  493. 'caption',
  494. 'rendered',
  495. 'profile_id',
  496. 'type',
  497. 'in_reply_to_id',
  498. 'reblog_of_id',
  499. 'is_nsfw',
  500. 'likes_count',
  501. 'reblogs_count',
  502. 'scope',
  503. 'local',
  504. 'cw_summary',
  505. 'created_at',
  506. 'updated_at'
  507. )->whereProfileId($profile->id)
  508. ->whereIn('type', $scope)
  509. ->where('id', $dir, $id)
  510. ->whereIn('visibility', $visibility)
  511. ->latest()
  512. ->limit($limit)
  513. ->get();
  514. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  515. $res = $this->fractal->createData($resource)->toArray();
  516. return response()->json($res);
  517. }
  518. }