PublicApiController.php 21 KB

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