1
0

ApiV1Controller.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Str;
  6. use App\Util\ActivityPub\Helpers;
  7. use App\Util\Media\Filter;
  8. use Laravel\Passport\Passport;
  9. use Auth, Cache, DB, URL;
  10. use App\{
  11. Bookmark,
  12. Follower,
  13. FollowRequest,
  14. Hashtag,
  15. Like,
  16. Media,
  17. Notification,
  18. Profile,
  19. Status,
  20. StatusHashtag,
  21. User,
  22. UserFilter,
  23. };
  24. use League\Fractal;
  25. use App\Transformer\Api\Mastodon\v1\{
  26. AccountTransformer,
  27. MediaTransformer,
  28. NotificationTransformer,
  29. StatusTransformer,
  30. };
  31. use App\Transformer\Api\{
  32. RelationshipTransformer,
  33. };
  34. use App\Http\Controllers\FollowerController;
  35. use League\Fractal\Serializer\ArraySerializer;
  36. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  37. use App\Http\Controllers\StatusController;
  38. use App\Jobs\LikePipeline\LikePipeline;
  39. use App\Jobs\SharePipeline\SharePipeline;
  40. use App\Jobs\StatusPipeline\NewStatusPipeline;
  41. use App\Jobs\StatusPipeline\StatusDelete;
  42. use App\Jobs\FollowPipeline\FollowPipeline;
  43. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  44. use App\Jobs\VideoPipeline\{
  45. VideoOptimize,
  46. VideoPostProcess,
  47. VideoThumbnail
  48. };
  49. use App\Services\{
  50. NotificationService,
  51. MediaPathService,
  52. SearchApiV2Service,
  53. StatusService,
  54. MediaBlocklistService
  55. };
  56. use App\Util\Lexer\Autolink;
  57. class ApiV1Controller extends Controller
  58. {
  59. protected $fractal;
  60. public function __construct()
  61. {
  62. $this->fractal = new Fractal\Manager();
  63. $this->fractal->setSerializer(new ArraySerializer());
  64. }
  65. public function apps(Request $request)
  66. {
  67. abort_if(!config_cache('pixelfed.oauth_enabled'), 404);
  68. $this->validate($request, [
  69. 'client_name' => 'required',
  70. 'redirect_uris' => 'required',
  71. 'scopes' => 'nullable',
  72. 'website' => 'nullable'
  73. ]);
  74. $uris = implode(',', explode('\n', $request->redirect_uris));
  75. $client = Passport::client()->forceFill([
  76. 'user_id' => null,
  77. 'name' => e($request->client_name),
  78. 'secret' => Str::random(40),
  79. 'redirect' => $uris,
  80. 'personal_access_client' => false,
  81. 'password_client' => false,
  82. 'revoked' => false,
  83. ]);
  84. $client->save();
  85. $res = [
  86. 'id' => $client->id,
  87. 'name' => $client->name,
  88. 'website' => null,
  89. 'redirect_uri' => $client->redirect,
  90. 'client_id' => $client->id,
  91. 'client_secret' => $client->secret,
  92. 'vapid_key' => null
  93. ];
  94. return response()->json($res, 200, [
  95. 'Access-Control-Allow-Origin' => '*'
  96. ]);
  97. }
  98. /**
  99. * GET /api/v1/accounts/verify_credentials
  100. *
  101. *
  102. * @return \App\Transformer\Api\AccountTransformer
  103. */
  104. public function verifyCredentials(Request $request)
  105. {
  106. abort_if(!$request->user(), 403);
  107. $id = $request->user()->id;
  108. if($request->user()->last_active_at) {
  109. $key = 'user:last_active_at:id:'.$id;
  110. $ttl = now()->addMinutes(5);
  111. Cache::remember($key, $ttl, function() use($id) {
  112. $user = User::findOrFail($id);
  113. $user->last_active_at = now();
  114. $user->save();
  115. return;
  116. });
  117. }
  118. $profile = Profile::whereNull('status')->whereUserId($id)->firstOrFail();
  119. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  120. $res = $this->fractal->createData($resource)->toArray();
  121. $res['source'] = [
  122. 'privacy' => $profile->is_private ? 'private' : 'public',
  123. 'sensitive' => $profile->cw ? true : false,
  124. 'language' => null,
  125. 'note' => '',
  126. 'fields' => []
  127. ];
  128. return response()->json($res);
  129. }
  130. /**
  131. * GET /api/v1/accounts/{id}
  132. *
  133. * @param integer $id
  134. *
  135. * @return \App\Transformer\Api\AccountTransformer
  136. */
  137. public function accountById(Request $request, $id)
  138. {
  139. $profile = Profile::whereNull('status')->findOrFail($id);
  140. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  141. $res = $this->fractal->createData($resource)->toArray();
  142. return response()->json($res);
  143. }
  144. /**
  145. * PATCH /api/v1/accounts/update_credentials
  146. *
  147. * @return \App\Transformer\Api\AccountTransformer
  148. */
  149. public function accountUpdateCredentials(Request $request)
  150. {
  151. abort_if(!$request->user(), 403);
  152. $this->validate($request, [
  153. 'display_name' => 'nullable|string',
  154. 'note' => 'nullable|string',
  155. 'locked' => 'nullable',
  156. // 'source.privacy' => 'nullable|in:unlisted,public,private',
  157. // 'source.sensitive' => 'nullable|boolean'
  158. ]);
  159. $user = $request->user();
  160. $profile = $user->profile;
  161. $displayName = $request->input('display_name');
  162. $note = $request->input('note');
  163. $locked = $request->input('locked');
  164. // $privacy = $request->input('source.privacy');
  165. // $sensitive = $request->input('source.sensitive');
  166. $changes = false;
  167. if($displayName !== $user->name) {
  168. $user->name = $displayName;
  169. $profile->name = $displayName;
  170. $changes = true;
  171. }
  172. if($note !== strip_tags($profile->bio)) {
  173. $profile->bio = Autolink::create()->autolink(strip_tags($note));
  174. $changes = true;
  175. }
  176. if(!is_null($locked)) {
  177. $profile->is_private = $locked;
  178. $changes = true;
  179. }
  180. if($changes) {
  181. $user->save();
  182. $profile->save();
  183. }
  184. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  185. $res = $this->fractal->createData($resource)->toArray();
  186. return response()->json($res);
  187. }
  188. /**
  189. * GET /api/v1/accounts/{id}/followers
  190. *
  191. * @param integer $id
  192. *
  193. * @return \App\Transformer\Api\AccountTransformer
  194. */
  195. public function accountFollowersById(Request $request, $id)
  196. {
  197. abort_if(!$request->user(), 403);
  198. $user = $request->user();
  199. $profile = Profile::whereNull('status')->findOrFail($id);
  200. $limit = $request->input('limit') ?? 40;
  201. if($profile->domain) {
  202. $res = [];
  203. } else {
  204. if($profile->id == $user->profile_id) {
  205. $followers = $profile->followers()->paginate($limit);
  206. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  207. $res = $this->fractal->createData($resource)->toArray();
  208. } else {
  209. if($profile->is_private) {
  210. abort_if(!$profile->followedBy($user->profile), 403);
  211. }
  212. $settings = $profile->user->settings;
  213. if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
  214. $settings->show_profile_followers == false
  215. ) {
  216. $res = [];
  217. } else {
  218. $followers = $profile->followers()->paginate($limit);
  219. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  220. $res = $this->fractal->createData($resource)->toArray();
  221. }
  222. }
  223. }
  224. return response()->json($res);
  225. }
  226. /**
  227. * GET /api/v1/accounts/{id}/following
  228. *
  229. * @param integer $id
  230. *
  231. * @return \App\Transformer\Api\AccountTransformer
  232. */
  233. public function accountFollowingById(Request $request, $id)
  234. {
  235. abort_if(!$request->user(), 403);
  236. $user = $request->user();
  237. $profile = Profile::whereNull('status')->findOrFail($id);
  238. $limit = $request->input('limit') ?? 40;
  239. if($profile->domain) {
  240. $res = [];
  241. } else {
  242. if($profile->id == $user->profile_id) {
  243. $following = $profile->following()->paginate($limit);
  244. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  245. $res = $this->fractal->createData($resource)->toArray();
  246. } else {
  247. if($profile->is_private) {
  248. abort_if(!$profile->followedBy($user->profile), 403);
  249. }
  250. $settings = $profile->user->settings;
  251. if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
  252. $settings->show_profile_following == false
  253. ) {
  254. $res = [];
  255. } else {
  256. $following = $profile->following()->paginate($limit);
  257. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  258. $res = $this->fractal->createData($resource)->toArray();
  259. }
  260. }
  261. }
  262. return response()->json($res);
  263. }
  264. /**
  265. * GET /api/v1/accounts/{id}/statuses
  266. *
  267. * @param integer $id
  268. *
  269. * @return \App\Transformer\Api\StatusTransformer
  270. */
  271. public function accountStatusesById(Request $request, $id)
  272. {
  273. abort_if(!$request->user(), 403);
  274. $this->validate($request, [
  275. 'only_media' => 'nullable',
  276. 'pinned' => 'nullable',
  277. 'exclude_replies' => 'nullable',
  278. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  279. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  280. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  281. 'limit' => 'nullable|integer|min:1|max:80'
  282. ]);
  283. $profile = Profile::whereNull('status')->findOrFail($id);
  284. $limit = $request->limit ?? 20;
  285. $max_id = $request->max_id;
  286. $min_id = $request->min_id;
  287. $pid = $request->user()->profile_id;
  288. $scope = $request->only_media == true ?
  289. ['photo', 'photo:album', 'video', 'video:album'] :
  290. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  291. if($pid == $profile->id) {
  292. $visibility = ['public', 'unlisted', 'private'];
  293. } else if($profile->is_private) {
  294. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  295. $following = Follower::whereProfileId($pid)->pluck('following_id');
  296. return $following->push($pid)->toArray();
  297. });
  298. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  299. } else {
  300. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  301. $following = Follower::whereProfileId($pid)->pluck('following_id');
  302. return $following->push($pid)->toArray();
  303. });
  304. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  305. }
  306. if($min_id || $max_id) {
  307. $dir = $min_id ? '>' : '<';
  308. $id = $min_id ?? $max_id;
  309. $timeline = Status::select(
  310. 'id',
  311. 'uri',
  312. 'caption',
  313. 'rendered',
  314. 'profile_id',
  315. 'type',
  316. 'in_reply_to_id',
  317. 'reblog_of_id',
  318. 'is_nsfw',
  319. 'scope',
  320. 'local',
  321. 'place_id',
  322. 'likes_count',
  323. 'reblogs_count',
  324. 'created_at',
  325. 'updated_at'
  326. )->whereProfileId($profile->id)
  327. ->whereIn('type', $scope)
  328. ->where('id', $dir, $id)
  329. ->whereIn('visibility', $visibility)
  330. ->latest()
  331. ->limit($limit)
  332. ->get();
  333. } else {
  334. $timeline = Status::select(
  335. 'id',
  336. 'uri',
  337. 'caption',
  338. 'rendered',
  339. 'profile_id',
  340. 'type',
  341. 'in_reply_to_id',
  342. 'reblog_of_id',
  343. 'is_nsfw',
  344. 'scope',
  345. 'local',
  346. 'place_id',
  347. 'likes_count',
  348. 'reblogs_count',
  349. 'created_at',
  350. 'updated_at'
  351. )->whereProfileId($profile->id)
  352. ->whereIn('type', $scope)
  353. ->whereIn('visibility', $visibility)
  354. ->latest()
  355. ->limit($limit)
  356. ->get();
  357. }
  358. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  359. $res = $this->fractal->createData($resource)->toArray();
  360. return response()->json($res);
  361. }
  362. /**
  363. * POST /api/v1/accounts/{id}/follow
  364. *
  365. * @param integer $id
  366. *
  367. * @return \App\Transformer\Api\RelationshipTransformer
  368. */
  369. public function accountFollowById(Request $request, $id)
  370. {
  371. abort_if(!$request->user(), 403);
  372. $user = $request->user();
  373. $target = Profile::where('id', '!=', $user->profile_id)
  374. ->whereNull('status')
  375. ->findOrFail($id);
  376. $private = (bool) $target->is_private;
  377. $remote = (bool) $target->domain;
  378. $blocked = UserFilter::whereUserId($target->id)
  379. ->whereFilterType('block')
  380. ->whereFilterableId($user->profile_id)
  381. ->whereFilterableType('App\Profile')
  382. ->exists();
  383. if($blocked == true) {
  384. abort(400, 'You cannot follow this user.');
  385. }
  386. $isFollowing = Follower::whereProfileId($user->profile_id)
  387. ->whereFollowingId($target->id)
  388. ->exists();
  389. // Following already, return empty relationship
  390. if($isFollowing == true) {
  391. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  392. $res = $this->fractal->createData($resource)->toArray();
  393. return response()->json($res);
  394. }
  395. // Rate limits, max 7500 followers per account
  396. if($user->profile->following()->count() >= Follower::MAX_FOLLOWING) {
  397. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  398. }
  399. // Rate limits, follow 30 accounts per hour max
  400. if($user->profile->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  401. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  402. }
  403. if($private == true) {
  404. $follow = FollowRequest::firstOrCreate([
  405. 'follower_id' => $user->profile_id,
  406. 'following_id' => $target->id
  407. ]);
  408. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  409. (new FollowerController())->sendFollow($user->profile, $target);
  410. }
  411. } else {
  412. $follower = new Follower();
  413. $follower->profile_id = $user->profile_id;
  414. $follower->following_id = $target->id;
  415. $follower->save();
  416. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  417. (new FollowerController())->sendFollow($user->profile, $target);
  418. }
  419. FollowPipeline::dispatch($follower);
  420. }
  421. Cache::forget('profile:following:'.$target->id);
  422. Cache::forget('profile:followers:'.$target->id);
  423. Cache::forget('profile:following:'.$user->profile_id);
  424. Cache::forget('profile:followers:'.$user->profile_id);
  425. Cache::forget('api:local:exp:rec:'.$user->profile_id);
  426. Cache::forget('user:account:id:'.$target->user_id);
  427. Cache::forget('user:account:id:'.$user->id);
  428. Cache::forget('profile:follower_count:'.$target->id);
  429. Cache::forget('profile:follower_count:'.$user->profile_id);
  430. Cache::forget('profile:following_count:'.$target->id);
  431. Cache::forget('profile:following_count:'.$user->profile_id);
  432. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  433. $res = $this->fractal->createData($resource)->toArray();
  434. return response()->json($res);
  435. }
  436. /**
  437. * POST /api/v1/accounts/{id}/unfollow
  438. *
  439. * @param integer $id
  440. *
  441. * @return \App\Transformer\Api\RelationshipTransformer
  442. */
  443. public function accountUnfollowById(Request $request, $id)
  444. {
  445. abort_if(!$request->user(), 403);
  446. $user = $request->user();
  447. $target = Profile::where('id', '!=', $user->profile_id)
  448. ->whereNull('status')
  449. ->findOrFail($id);
  450. $private = (bool) $target->is_private;
  451. $remote = (bool) $target->domain;
  452. $isFollowing = Follower::whereProfileId($user->profile_id)
  453. ->whereFollowingId($target->id)
  454. ->exists();
  455. if($isFollowing == false) {
  456. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  457. $res = $this->fractal->createData($resource)->toArray();
  458. return response()->json($res);
  459. }
  460. // Rate limits, follow 30 accounts per hour max
  461. if($user->profile->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  462. abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  463. }
  464. FollowRequest::whereFollowerId($user->profile_id)
  465. ->whereFollowingId($target->id)
  466. ->delete();
  467. Follower::whereProfileId($user->profile_id)
  468. ->whereFollowingId($target->id)
  469. ->delete();
  470. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  471. (new FollowerController())->sendUndoFollow($user->profile, $target);
  472. }
  473. Cache::forget('profile:following:'.$target->id);
  474. Cache::forget('profile:followers:'.$target->id);
  475. Cache::forget('profile:following:'.$user->profile_id);
  476. Cache::forget('profile:followers:'.$user->profile_id);
  477. Cache::forget('api:local:exp:rec:'.$user->profile_id);
  478. Cache::forget('user:account:id:'.$target->user_id);
  479. Cache::forget('user:account:id:'.$user->id);
  480. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  481. $res = $this->fractal->createData($resource)->toArray();
  482. return response()->json($res);
  483. }
  484. /**
  485. * GET /api/v1/accounts/relationships
  486. *
  487. * @param array|integer $id
  488. *
  489. * @return \App\Transformer\Api\RelationshipTransformer
  490. */
  491. public function accountRelationshipsById(Request $request)
  492. {
  493. abort_if(!$request->user(), 403);
  494. $this->validate($request, [
  495. 'id' => 'required|array|min:1|max:20',
  496. 'id.*' => 'required|integer|min:1|max:' . PHP_INT_MAX
  497. ]);
  498. $pid = $request->user()->profile_id ?? $request->user()->profile->id;
  499. $ids = collect($request->input('id'));
  500. $filtered = $ids->filter(function($v) use($pid) {
  501. return $v != $pid;
  502. });
  503. $relations = Profile::whereNull('status')->findOrFail($filtered->values());
  504. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  505. $res = $this->fractal->createData($fractal)->toArray();
  506. return response()->json($res);
  507. }
  508. /**
  509. * GET /api/v1/accounts/search
  510. *
  511. *
  512. *
  513. * @return \App\Transformer\Api\AccountTransformer
  514. */
  515. public function accountSearch(Request $request)
  516. {
  517. abort_if(!$request->user(), 403);
  518. $this->validate($request, [
  519. 'q' => 'required|string|min:1|max:255',
  520. 'limit' => 'nullable|integer|min:1|max:40',
  521. 'resolve' => 'nullable'
  522. ]);
  523. $user = $request->user();
  524. $query = $request->input('q');
  525. $limit = $request->input('limit') ?? 20;
  526. $resolve = (bool) $request->input('resolve', false);
  527. $q = '%' . $query . '%';
  528. $profiles = Profile::whereNull('status')
  529. ->where('username', 'like', $q)
  530. ->orWhere('name', 'like', $q)
  531. ->limit($limit)
  532. ->get();
  533. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  534. $res = $this->fractal->createData($resource)->toArray();
  535. return response()->json($res);
  536. }
  537. /**
  538. * GET /api/v1/blocks
  539. *
  540. *
  541. *
  542. * @return \App\Transformer\Api\AccountTransformer
  543. */
  544. public function accountBlocks(Request $request)
  545. {
  546. abort_if(!$request->user(), 403);
  547. $this->validate($request, [
  548. 'limit' => 'nullable|integer|min:1|max:40',
  549. 'page' => 'nullable|integer|min:1|max:10'
  550. ]);
  551. $user = $request->user();
  552. $limit = $request->input('limit') ?? 40;
  553. $blocked = UserFilter::select('filterable_id','filterable_type','filter_type','user_id')
  554. ->whereUserId($user->profile_id)
  555. ->whereFilterableType('App\Profile')
  556. ->whereFilterType('block')
  557. ->simplePaginate($limit)
  558. ->pluck('filterable_id');
  559. $profiles = Profile::findOrFail($blocked);
  560. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  561. $res = $this->fractal->createData($resource)->toArray();
  562. return response()->json($res);
  563. }
  564. /**
  565. * POST /api/v1/accounts/{id}/block
  566. *
  567. * @param integer $id
  568. *
  569. * @return \App\Transformer\Api\RelationshipTransformer
  570. */
  571. public function accountBlockById(Request $request, $id)
  572. {
  573. abort_if(!$request->user(), 403);
  574. $user = $request->user();
  575. $pid = $user->profile_id ?? $user->profile->id;
  576. if($id == $pid) {
  577. abort(400, 'You cannot block yourself');
  578. }
  579. $profile = Profile::findOrFail($id);
  580. if($profile->user->is_admin == true) {
  581. abort(400, 'You cannot block an admin');
  582. }
  583. Follower::whereProfileId($profile->id)->whereFollowingId($pid)->delete();
  584. Follower::whereProfileId($pid)->whereFollowingId($profile->id)->delete();
  585. Notification::whereProfileId($pid)->whereActorId($profile->id)->delete();
  586. $filter = UserFilter::firstOrCreate([
  587. 'user_id' => $pid,
  588. 'filterable_id' => $profile->id,
  589. 'filterable_type' => 'App\Profile',
  590. 'filter_type' => 'block',
  591. ]);
  592. Cache::forget("user:filter:list:$pid");
  593. Cache::forget("api:local:exp:rec:$pid");
  594. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  595. $res = $this->fractal->createData($resource)->toArray();
  596. return response()->json($res);
  597. }
  598. /**
  599. * POST /api/v1/accounts/{id}/unblock
  600. *
  601. * @param integer $id
  602. *
  603. * @return \App\Transformer\Api\RelationshipTransformer
  604. */
  605. public function accountUnblockById(Request $request, $id)
  606. {
  607. abort_if(!$request->user(), 403);
  608. $user = $request->user();
  609. $pid = $user->profile_id ?? $user->profile->id;
  610. if($id == $pid) {
  611. abort(400, 'You cannot unblock yourself');
  612. }
  613. $profile = Profile::findOrFail($id);
  614. UserFilter::whereUserId($pid)
  615. ->whereFilterableId($profile->id)
  616. ->whereFilterableType('App\Profile')
  617. ->whereFilterType('block')
  618. ->delete();
  619. Cache::forget("user:filter:list:$pid");
  620. Cache::forget("api:local:exp:rec:$pid");
  621. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  622. $res = $this->fractal->createData($resource)->toArray();
  623. return response()->json($res);
  624. }
  625. /**
  626. * GET /api/v1/custom_emojis
  627. *
  628. * Return empty array, we don't support custom emoji
  629. *
  630. * @return array
  631. */
  632. public function customEmojis()
  633. {
  634. return response()->json([]);
  635. }
  636. /**
  637. * GET /api/v1/domain_blocks
  638. *
  639. * Return empty array
  640. *
  641. * @return array
  642. */
  643. public function accountDomainBlocks(Request $request)
  644. {
  645. abort_if(!$request->user(), 403);
  646. return response()->json([]);
  647. }
  648. /**
  649. * GET /api/v1/endorsements
  650. *
  651. * Return empty array
  652. *
  653. * @return array
  654. */
  655. public function accountEndorsements(Request $request)
  656. {
  657. abort_if(!$request->user(), 403);
  658. return response()->json([]);
  659. }
  660. /**
  661. * GET /api/v1/favourites
  662. *
  663. * Returns collection of liked statuses
  664. *
  665. * @return \App\Transformer\Api\StatusTransformer
  666. */
  667. public function accountFavourites(Request $request)
  668. {
  669. abort_if(!$request->user(), 403);
  670. $user = $request->user();
  671. $limit = $request->input('limit') ?? 20;
  672. $favourites = Like::whereProfileId($user->profile_id)
  673. ->latest()
  674. ->simplePaginate($limit)
  675. ->pluck('status_id');
  676. $statuses = Status::findOrFail($favourites);
  677. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  678. $res = $this->fractal->createData($resource)->toArray();
  679. return response()->json($res);
  680. }
  681. /**
  682. * POST /api/v1/statuses/{id}/favourite
  683. *
  684. * @param integer $id
  685. *
  686. * @return \App\Transformer\Api\StatusTransformer
  687. */
  688. public function statusFavouriteById(Request $request, $id)
  689. {
  690. abort_if(!$request->user(), 403);
  691. $user = $request->user();
  692. $status = Status::findOrFail($id);
  693. if($status->profile_id !== $user->profile_id) {
  694. if($status->scope == 'private') {
  695. abort_if(!$status->profile->followedBy($user->profile), 403);
  696. } else {
  697. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  698. }
  699. }
  700. $like = Like::firstOrCreate([
  701. 'profile_id' => $user->profile_id,
  702. 'status_id' => $status->id
  703. ]);
  704. if($like->wasRecentlyCreated == true) {
  705. $like->status_profile_id = $status->profile_id;
  706. $like->is_comment = !empty($status->in_reply_to_id);
  707. $like->save();
  708. $status->likes_count = $status->likes()->count();
  709. $status->save();
  710. LikePipeline::dispatch($like);
  711. }
  712. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  713. $res = $this->fractal->createData($resource)->toArray();
  714. return response()->json($res);
  715. }
  716. /**
  717. * POST /api/v1/statuses/{id}/unfavourite
  718. *
  719. * @param integer $id
  720. *
  721. * @return \App\Transformer\Api\StatusTransformer
  722. */
  723. public function statusUnfavouriteById(Request $request, $id)
  724. {
  725. abort_if(!$request->user(), 403);
  726. $user = $request->user();
  727. $status = Status::findOrFail($id);
  728. if($status->profile_id !== $user->profile_id) {
  729. if($status->scope == 'private') {
  730. abort_if(!$status->profile->followedBy($user->profile), 403);
  731. } else {
  732. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  733. }
  734. }
  735. $like = Like::whereProfileId($user->profile_id)
  736. ->whereStatusId($status->id)
  737. ->first();
  738. if($like) {
  739. $like->forceDelete();
  740. $status->likes_count = $status->likes()->count();
  741. $status->save();
  742. }
  743. StatusService::del($status->id);
  744. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  745. $res = $this->fractal->createData($resource)->toArray();
  746. return response()->json($res);
  747. }
  748. /**
  749. * GET /api/v1/filters
  750. *
  751. * Return empty response since we filter server side
  752. *
  753. * @return array
  754. */
  755. public function accountFilters(Request $request)
  756. {
  757. abort_if(!$request->user(), 403);
  758. return response()->json([]);
  759. }
  760. /**
  761. * GET /api/v1/follow_requests
  762. *
  763. * Return array of Accounts that have sent follow requests
  764. *
  765. * @return \App\Transformer\Api\AccountTransformer
  766. */
  767. public function accountFollowRequests(Request $request)
  768. {
  769. abort_if(!$request->user(), 403);
  770. $user = $request->user();
  771. $followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
  772. $profiles = Profile::find($followRequests);
  773. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  774. $res = $this->fractal->createData($resource)->toArray();
  775. return response()->json($res);
  776. }
  777. /**
  778. * POST /api/v1/follow_requests/{id}/authorize
  779. *
  780. * @param integer $id
  781. *
  782. * @return null
  783. */
  784. public function accountFollowRequestAccept(Request $request, $id)
  785. {
  786. abort_if(!$request->user(), 403);
  787. // todo
  788. return response()->json([]);
  789. }
  790. /**
  791. * POST /api/v1/follow_requests/{id}/reject
  792. *
  793. * @param integer $id
  794. *
  795. * @return null
  796. */
  797. public function accountFollowRequestReject(Request $request, $id)
  798. {
  799. abort_if(!$request->user(), 403);
  800. // todo
  801. return response()->json([]);
  802. }
  803. /**
  804. * GET /api/v1/suggestions
  805. *
  806. * Return empty array as we don't support suggestions
  807. *
  808. * @return null
  809. */
  810. public function accountSuggestions(Request $request)
  811. {
  812. abort_if(!$request->user(), 403);
  813. // todo
  814. return response()->json([]);
  815. }
  816. /**
  817. * GET /api/v1/instance
  818. *
  819. * Information about the server.
  820. *
  821. * @return Instance
  822. */
  823. public function instance(Request $request)
  824. {
  825. $res = Cache::remember('api:v1:instance-data', now()->addMinutes(15), function () {
  826. $rules = config_cache('app.rules') ? collect(json_decode(config_cache('app.rules'), true))
  827. ->map(function($rule, $key) {
  828. $id = $key + 1;
  829. return [
  830. 'id' => "{$id}",
  831. 'text' => $rule
  832. ];
  833. })
  834. ->toArray() : [];
  835. $res = [
  836. 'approval_required' => false,
  837. 'contact_account' => null,
  838. 'description' => config_cache('app.description'),
  839. 'email' => config('instance.email'),
  840. 'invites_enabled' => false,
  841. 'rules' => $rules,
  842. 'short_description' => 'Pixelfed - Photo sharing for everyone',
  843. 'languages' => ['en'],
  844. 'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
  845. 'registrations' => (bool) config_cache('pixelfed.open_registration'),
  846. 'stats' => [
  847. 'user_count' => 0,
  848. 'status_count' => 0,
  849. 'domain_count' => 0
  850. ],
  851. 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
  852. 'title' => config_cache('app.name'),
  853. 'uri' => config('pixelfed.domain.app'),
  854. 'urls' => [],
  855. 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
  856. 'environment' => [
  857. 'max_photo_size' => (int) config_cache('pixelfed.max_photo_size'),
  858. 'max_avatar_size' => (int) config('pixelfed.max_avatar_size'),
  859. 'max_caption_length' => (int) config('pixelfed.max_caption_length'),
  860. 'max_bio_length' => (int) config('pixelfed.max_bio_length'),
  861. 'max_album_length' => (int) config_cache('pixelfed.max_album_length'),
  862. 'mobile_apis' => (bool) config_cache('pixelfed.oauth_enabled')
  863. ]
  864. ];
  865. return $res;
  866. });
  867. return response()->json($res);
  868. }
  869. /**
  870. * GET /api/v1/lists
  871. *
  872. * Return empty array as we don't support lists
  873. *
  874. * @return null
  875. */
  876. public function accountLists(Request $request)
  877. {
  878. abort_if(!$request->user(), 403);
  879. return response()->json([]);
  880. }
  881. /**
  882. * GET /api/v1/accounts/{id}/lists
  883. *
  884. * @param integer $id
  885. *
  886. * @return null
  887. */
  888. public function accountListsById(Request $request, $id)
  889. {
  890. abort_if(!$request->user(), 403);
  891. return response()->json([]);
  892. }
  893. /**
  894. * POST /api/v1/media
  895. *
  896. *
  897. * @return MediaTransformer
  898. */
  899. public function mediaUpload(Request $request)
  900. {
  901. abort_if(!$request->user(), 403);
  902. $this->validate($request, [
  903. 'file.*' => function() {
  904. return [
  905. 'required',
  906. 'mimes:' . config_cache('pixelfed.media_types'),
  907. 'max:' . config_cache('pixelfed.max_photo_size'),
  908. ];
  909. },
  910. 'filter_name' => 'nullable|string|max:24',
  911. 'filter_class' => 'nullable|alpha_dash|max:24',
  912. 'description' => 'nullable|string|max:420'
  913. ]);
  914. $user = $request->user();
  915. if($user->last_active_at == null) {
  916. return [];
  917. }
  918. $limitKey = 'compose:rate-limit:media-upload:' . $user->id;
  919. $limitTtl = now()->addMinutes(15);
  920. $limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) {
  921. $dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
  922. return $dailyLimit >= 250;
  923. });
  924. abort_if($limitReached == true, 429);
  925. $profile = $user->profile;
  926. if(config_cache('pixelfed.enforce_account_limit') == true) {
  927. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  928. return Media::whereUserId($user->id)->sum('size') / 1000;
  929. });
  930. $limit = (int) config_cache('pixelfed.max_account_size');
  931. if ($size >= $limit) {
  932. abort(403, 'Account size limit reached.');
  933. }
  934. }
  935. $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
  936. $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
  937. $photo = $request->file('file');
  938. $mimes = explode(',', config_cache('pixelfed.media_types'));
  939. if(in_array($photo->getMimeType(), $mimes) == false) {
  940. abort(403, 'Invalid or unsupported mime type.');
  941. }
  942. $storagePath = MediaPathService::get($user, 2);
  943. $path = $photo->store($storagePath);
  944. $hash = \hash_file('sha256', $photo);
  945. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  946. $media = new Media();
  947. $media->status_id = null;
  948. $media->profile_id = $profile->id;
  949. $media->user_id = $user->id;
  950. $media->media_path = $path;
  951. $media->original_sha256 = $hash;
  952. $media->size = $photo->getSize();
  953. $media->mime = $photo->getMimeType();
  954. $media->caption = $request->input('description');
  955. $media->filter_class = $filterClass;
  956. $media->filter_name = $filterName;
  957. $media->save();
  958. switch ($media->mime) {
  959. case 'image/jpeg':
  960. case 'image/png':
  961. ImageOptimize::dispatch($media);
  962. break;
  963. case 'video/mp4':
  964. VideoThumbnail::dispatch($media);
  965. $preview_url = '/storage/no-preview.png';
  966. $url = '/storage/no-preview.png';
  967. break;
  968. }
  969. Cache::forget($limitKey);
  970. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  971. $res = $this->fractal->createData($resource)->toArray();
  972. $res['preview_url'] = $media->url(). '?cb=1&_v=' . time();
  973. $res['url'] = $media->url(). '?cb=1&_v=' . time();
  974. return response()->json($res);
  975. }
  976. /**
  977. * PUT /api/v1/media/{id}
  978. *
  979. * @param integer $id
  980. *
  981. * @return MediaTransformer
  982. */
  983. public function mediaUpdate(Request $request, $id)
  984. {
  985. abort_if(!$request->user(), 403);
  986. $this->validate($request, [
  987. 'description' => 'nullable|string|max:420'
  988. ]);
  989. $user = $request->user();
  990. $media = Media::whereUserId($user->id)
  991. ->whereNull('status_id')
  992. ->findOrFail($id);
  993. $media->caption = $request->input('description');
  994. $media->save();
  995. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  996. $res = $this->fractal->createData($resource)->toArray();
  997. $res['preview_url'] = url('/storage/no-preview.png');
  998. $res['url'] = url('/storage/no-preview.png');
  999. return response()->json($res);
  1000. }
  1001. /**
  1002. * GET /api/v1/mutes
  1003. *
  1004. *
  1005. * @return AccountTransformer
  1006. */
  1007. public function accountMutes(Request $request)
  1008. {
  1009. abort_if(!$request->user(), 403);
  1010. $this->validate($request, [
  1011. 'limit' => 'nullable|integer|min:1|max:40'
  1012. ]);
  1013. $user = $request->user();
  1014. $limit = $request->input('limit') ?? 40;
  1015. $mutes = UserFilter::whereUserId($user->profile_id)
  1016. ->whereFilterableType('App\Profile')
  1017. ->whereFilterType('mute')
  1018. ->simplePaginate($limit)
  1019. ->pluck('filterable_id');
  1020. $accounts = Profile::find($mutes);
  1021. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  1022. $res = $this->fractal->createData($resource)->toArray();
  1023. return response()->json($res);
  1024. }
  1025. /**
  1026. * POST /api/v1/accounts/{id}/mute
  1027. *
  1028. * @param integer $id
  1029. *
  1030. * @return RelationshipTransformer
  1031. */
  1032. public function accountMuteById(Request $request, $id)
  1033. {
  1034. abort_if(!$request->user(), 403);
  1035. $user = $request->user();
  1036. $pid = $user->profile_id;
  1037. $account = Profile::findOrFail($id);
  1038. $filter = UserFilter::firstOrCreate([
  1039. 'user_id' => $pid,
  1040. 'filterable_id' => $account->id,
  1041. 'filterable_type' => 'App\Profile',
  1042. 'filter_type' => 'mute',
  1043. ]);
  1044. Cache::forget("user:filter:list:$pid");
  1045. Cache::forget("feature:discover:posts:$pid");
  1046. Cache::forget("api:local:exp:rec:$pid");
  1047. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1048. $res = $this->fractal->createData($resource)->toArray();
  1049. return response()->json($res);
  1050. }
  1051. /**
  1052. * POST /api/v1/accounts/{id}/unmute
  1053. *
  1054. * @param integer $id
  1055. *
  1056. * @return RelationshipTransformer
  1057. */
  1058. public function accountUnmuteById(Request $request, $id)
  1059. {
  1060. abort_if(!$request->user(), 403);
  1061. $user = $request->user();
  1062. $pid = $user->profile_id;
  1063. $account = Profile::findOrFail($id);
  1064. $filter = UserFilter::whereUserId($pid)
  1065. ->whereFilterableId($account->id)
  1066. ->whereFilterableType('App\Profile')
  1067. ->whereFilterType('mute')
  1068. ->first();
  1069. if($filter) {
  1070. $filter->delete();
  1071. Cache::forget("user:filter:list:$pid");
  1072. Cache::forget("feature:discover:posts:$pid");
  1073. Cache::forget("api:local:exp:rec:$pid");
  1074. }
  1075. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1076. $res = $this->fractal->createData($resource)->toArray();
  1077. return response()->json($res);
  1078. }
  1079. /**
  1080. * GET /api/v1/notifications
  1081. *
  1082. *
  1083. * @return NotificationTransformer
  1084. */
  1085. public function accountNotifications(Request $request)
  1086. {
  1087. abort_if(!$request->user(), 403);
  1088. $this->validate($request, [
  1089. 'limit' => 'nullable|integer|min:1|max:80',
  1090. 'min_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1091. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1092. 'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1093. ]);
  1094. $pid = $request->user()->profile_id;
  1095. $limit = $request->input('limit', 20);
  1096. $since = $request->input('since_id');
  1097. $min = $request->input('min_id');
  1098. $max = $request->input('max_id');
  1099. if(!$since && !$min && !$max) {
  1100. $min = 1;
  1101. }
  1102. $maxId = null;
  1103. $minId = null;
  1104. if($max) {
  1105. $res = NotificationService::getMax($pid, $max, $limit);
  1106. $ids = NotificationService::getRankedMaxId($pid, $max, $limit);
  1107. $maxId = max($ids);
  1108. $minId = min($ids);
  1109. } else {
  1110. $res = NotificationService::getMin($pid, $min ?? $since, $limit);
  1111. $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
  1112. $maxId = max($ids);
  1113. $minId = min($ids);
  1114. }
  1115. $baseUrl = config('app.url') . '/api/v1/notifications?';
  1116. if($minId == $maxId) {
  1117. $minId = null;
  1118. }
  1119. if($maxId) {
  1120. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next"';
  1121. }
  1122. if($minId) {
  1123. $link = '<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1124. }
  1125. if($maxId && $minId) {
  1126. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next",<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1127. }
  1128. $res = response()->json($res);
  1129. if(isset($link)) {
  1130. $res->withHeaders([
  1131. 'Link' => $link,
  1132. ]);
  1133. }
  1134. return $res;
  1135. }
  1136. /**
  1137. * GET /api/v1/timelines/home
  1138. *
  1139. *
  1140. * @return StatusTransformer
  1141. */
  1142. public function timelineHome(Request $request)
  1143. {
  1144. abort_if(!$request->user(), 403);
  1145. $this->validate($request,[
  1146. 'page' => 'nullable|integer|max:40',
  1147. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1148. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1149. 'limit' => 'nullable|integer|max:80'
  1150. ]);
  1151. $page = $request->input('page');
  1152. $min = $request->input('min_id');
  1153. $max = $request->input('max_id');
  1154. $limit = $request->input('limit') ?? 3;
  1155. $user = $request->user();
  1156. if($user->last_active_at) {
  1157. $key = 'user:last_active_at:id:'.$user->id;
  1158. $ttl = now()->addMinutes(5);
  1159. Cache::remember($key, $ttl, function() use($user) {
  1160. $user->last_active_at = now();
  1161. $user->save();
  1162. return;
  1163. });
  1164. }
  1165. $pid = $request->user()->profile_id;
  1166. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1167. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1168. return $following->push($pid)->toArray();
  1169. });
  1170. if($min || $max) {
  1171. $dir = $min ? '>' : '<';
  1172. $id = $min ?? $max;
  1173. $timeline = Status::select(
  1174. 'id',
  1175. 'uri',
  1176. 'caption',
  1177. 'rendered',
  1178. 'profile_id',
  1179. 'type',
  1180. 'in_reply_to_id',
  1181. 'reblog_of_id',
  1182. 'is_nsfw',
  1183. 'scope',
  1184. 'local',
  1185. 'reply_count',
  1186. 'likes_count',
  1187. 'reblogs_count',
  1188. 'comments_disabled',
  1189. 'place_id',
  1190. 'created_at',
  1191. 'updated_at'
  1192. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1193. ->with('profile', 'hashtags', 'mentions')
  1194. ->where('id', $dir, $id)
  1195. ->whereIn('profile_id', $following)
  1196. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1197. ->latest()
  1198. ->limit($limit)
  1199. ->get();
  1200. } else {
  1201. $timeline = Status::select(
  1202. 'id',
  1203. 'uri',
  1204. 'caption',
  1205. 'rendered',
  1206. 'profile_id',
  1207. 'type',
  1208. 'in_reply_to_id',
  1209. 'reblog_of_id',
  1210. 'is_nsfw',
  1211. 'scope',
  1212. 'local',
  1213. 'reply_count',
  1214. 'comments_disabled',
  1215. 'likes_count',
  1216. 'reblogs_count',
  1217. 'place_id',
  1218. 'created_at',
  1219. 'updated_at'
  1220. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1221. ->with('profile', 'hashtags', 'mentions')
  1222. ->whereIn('profile_id', $following)
  1223. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1224. ->latest()
  1225. ->simplePaginate($limit);
  1226. }
  1227. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1228. $res = $this->fractal->createData($fractal)->toArray();
  1229. return response()->json($res);
  1230. }
  1231. /**
  1232. * GET /api/v1/conversations
  1233. *
  1234. * Not implemented
  1235. *
  1236. * @return array
  1237. */
  1238. public function conversations(Request $request)
  1239. {
  1240. abort_if(!$request->user(), 403);
  1241. return response()->json([]);
  1242. }
  1243. /**
  1244. * GET /api/v1/timelines/public
  1245. *
  1246. *
  1247. * @return StatusTransformer
  1248. */
  1249. public function timelinePublic(Request $request)
  1250. {
  1251. $this->validate($request,[
  1252. 'page' => 'nullable|integer|max:40',
  1253. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1254. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1255. 'limit' => 'nullable|integer|max:80'
  1256. ]);
  1257. $page = $request->input('page');
  1258. $min = $request->input('min_id');
  1259. $max = $request->input('max_id');
  1260. $limit = $request->input('limit') ?? 3;
  1261. $user = $request->user();
  1262. if($user) {
  1263. $key = 'user:last_active_at:id:'.$user->id;
  1264. $ttl = now()->addMinutes(5);
  1265. Cache::remember($key, $ttl, function() use($user) {
  1266. $user->last_active_at = now();
  1267. $user->save();
  1268. return;
  1269. });
  1270. }
  1271. if($min || $max) {
  1272. $dir = $min ? '>' : '<';
  1273. $id = $min ?? $max;
  1274. $timeline = Status::select(
  1275. 'id',
  1276. 'uri',
  1277. 'caption',
  1278. 'rendered',
  1279. 'profile_id',
  1280. 'type',
  1281. 'in_reply_to_id',
  1282. 'reblog_of_id',
  1283. 'is_nsfw',
  1284. 'scope',
  1285. 'local',
  1286. 'reply_count',
  1287. 'comments_disabled',
  1288. 'place_id',
  1289. 'likes_count',
  1290. 'reblogs_count',
  1291. 'created_at',
  1292. 'updated_at'
  1293. )->whereNull('uri')
  1294. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1295. ->with('profile', 'hashtags', 'mentions')
  1296. ->where('id', $dir, $id)
  1297. ->whereScope('public')
  1298. ->where('created_at', '>', now()->subDays(14))
  1299. ->latest()
  1300. ->limit($limit)
  1301. ->get();
  1302. } else {
  1303. $timeline = Status::select(
  1304. 'id',
  1305. 'uri',
  1306. 'caption',
  1307. 'rendered',
  1308. 'profile_id',
  1309. 'type',
  1310. 'in_reply_to_id',
  1311. 'reblog_of_id',
  1312. 'is_nsfw',
  1313. 'scope',
  1314. 'local',
  1315. 'reply_count',
  1316. 'comments_disabled',
  1317. 'place_id',
  1318. 'likes_count',
  1319. 'reblogs_count',
  1320. 'created_at',
  1321. 'updated_at'
  1322. )->whereNull('uri')
  1323. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1324. ->with('profile', 'hashtags', 'mentions')
  1325. ->whereScope('public')
  1326. ->where('created_at', '>', now()->subDays(14))
  1327. ->latest()
  1328. ->simplePaginate($limit);
  1329. }
  1330. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1331. $res = $this->fractal->createData($fractal)->toArray();
  1332. return response()->json($res);
  1333. }
  1334. /**
  1335. * GET /api/v1/statuses/{id}
  1336. *
  1337. * @param integer $id
  1338. *
  1339. * @return StatusTransformer
  1340. */
  1341. public function statusById(Request $request, $id)
  1342. {
  1343. abort_if(!$request->user(), 403);
  1344. $user = $request->user();
  1345. $status = Status::findOrFail($id);
  1346. if($status->profile_id !== $user->profile_id) {
  1347. if($status->scope == 'private') {
  1348. abort_if(!$status->profile->followedBy($user->profile), 403);
  1349. } else {
  1350. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1351. }
  1352. }
  1353. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1354. $res = $this->fractal->createData($resource)->toArray();
  1355. return response()->json($res);
  1356. }
  1357. /**
  1358. * GET /api/v1/statuses/{id}/context
  1359. *
  1360. * @param integer $id
  1361. *
  1362. * @return StatusTransformer
  1363. */
  1364. public function statusContext(Request $request, $id)
  1365. {
  1366. abort_if(!$request->user(), 403);
  1367. $user = $request->user();
  1368. $status = Status::findOrFail($id);
  1369. if($status->profile_id !== $user->profile_id) {
  1370. if($status->scope == 'private') {
  1371. abort_if(!$status->profile->followedBy($user->profile), 403);
  1372. } else {
  1373. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1374. }
  1375. }
  1376. if($status->comments_disabled) {
  1377. $res = [
  1378. 'ancestors' => [],
  1379. 'descendants' => []
  1380. ];
  1381. } else {
  1382. $ancestors = $status->parent();
  1383. if($ancestors) {
  1384. $ares = new Fractal\Resource\Item($ancestors, new StatusTransformer());
  1385. $ancestors = [
  1386. $this->fractal->createData($ares)->toArray()
  1387. ];
  1388. } else {
  1389. $ancestors = [];
  1390. }
  1391. $descendants = Status::whereInReplyToId($id)->latest()->limit(20)->get();
  1392. $dres = new Fractal\Resource\Collection($descendants, new StatusTransformer());
  1393. $descendants = $this->fractal->createData($dres)->toArray();
  1394. $res = [
  1395. 'ancestors' => $ancestors,
  1396. 'descendants' => $descendants
  1397. ];
  1398. }
  1399. return response()->json($res);
  1400. }
  1401. /**
  1402. * GET /api/v1/statuses/{id}/card
  1403. *
  1404. * @param integer $id
  1405. *
  1406. * @return StatusTransformer
  1407. */
  1408. public function statusCard(Request $request, $id)
  1409. {
  1410. abort_if(!$request->user(), 403);
  1411. $user = $request->user();
  1412. $status = Status::findOrFail($id);
  1413. if($status->profile_id !== $user->profile_id) {
  1414. if($status->scope == 'private') {
  1415. abort_if(!$status->profile->followedBy($user->profile), 403);
  1416. } else {
  1417. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1418. }
  1419. }
  1420. // Return empty response since we don't handle support cards
  1421. $res = [];
  1422. return response()->json($res);
  1423. }
  1424. /**
  1425. * GET /api/v1/statuses/{id}/reblogged_by
  1426. *
  1427. * @param integer $id
  1428. *
  1429. * @return AccountTransformer
  1430. */
  1431. public function statusRebloggedBy(Request $request, $id)
  1432. {
  1433. abort_if(!$request->user(), 403);
  1434. $this->validate($request, [
  1435. 'page' => 'nullable|integer|min:1|max:40',
  1436. 'limit' => 'nullable|integer|min:1|max:80'
  1437. ]);
  1438. $limit = $request->input('limit') ?? 40;
  1439. $user = $request->user();
  1440. $status = Status::findOrFail($id);
  1441. if($status->profile_id !== $user->profile_id) {
  1442. if($status->scope == 'private') {
  1443. abort_if(!$status->profile->followedBy($user->profile), 403);
  1444. } else {
  1445. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1446. }
  1447. }
  1448. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1449. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1450. $res = $this->fractal->createData($resource)->toArray();
  1451. $url = $request->url();
  1452. $page = $request->input('page', 1);
  1453. $next = $page < 40 ? $page + 1 : 40;
  1454. $prev = $page > 1 ? $page - 1 : 1;
  1455. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1456. return response()->json($res, 200, ['Link' => $links]);
  1457. }
  1458. /**
  1459. * GET /api/v1/statuses/{id}/favourited_by
  1460. *
  1461. * @param integer $id
  1462. *
  1463. * @return AccountTransformer
  1464. */
  1465. public function statusFavouritedBy(Request $request, $id)
  1466. {
  1467. abort_if(!$request->user(), 403);
  1468. $this->validate($request, [
  1469. 'page' => 'nullable|integer|min:1|max:40',
  1470. 'limit' => 'nullable|integer|min:1|max:80'
  1471. ]);
  1472. $limit = $request->input('limit') ?? 40;
  1473. $user = $request->user();
  1474. $status = Status::findOrFail($id);
  1475. if($status->profile_id !== $user->profile_id) {
  1476. if($status->scope == 'private') {
  1477. abort_if(!$status->profile->followedBy($user->profile), 403);
  1478. } else {
  1479. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1480. }
  1481. }
  1482. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1483. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1484. $res = $this->fractal->createData($resource)->toArray();
  1485. $url = $request->url();
  1486. $page = $request->input('page', 1);
  1487. $next = $page < 40 ? $page + 1 : 40;
  1488. $prev = $page > 1 ? $page - 1 : 1;
  1489. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1490. return response()->json($res, 200, ['Link' => $links]);
  1491. }
  1492. /**
  1493. * POST /api/v1/statuses
  1494. *
  1495. *
  1496. * @return StatusTransformer
  1497. */
  1498. public function statusCreate(Request $request)
  1499. {
  1500. abort_if(!$request->user(), 403);
  1501. $this->validate($request, [
  1502. 'status' => 'nullable|string',
  1503. 'in_reply_to_id' => 'nullable|integer',
  1504. 'media_ids' => 'array|max:' . config_cache('pixelfed.max_album_length'),
  1505. 'media_ids.*' => 'integer|min:1',
  1506. 'sensitive' => 'nullable|boolean',
  1507. 'visibility' => 'string|in:private,unlisted,public',
  1508. ]);
  1509. if(config('costar.enabled') == true) {
  1510. $blockedKeywords = config('costar.keyword.block');
  1511. if($blockedKeywords !== null && $request->status) {
  1512. $keywords = config('costar.keyword.block');
  1513. foreach($keywords as $kw) {
  1514. if(Str::contains($request->status, $kw) == true) {
  1515. abort(400, 'Invalid object. Contains banned keyword.');
  1516. }
  1517. }
  1518. }
  1519. }
  1520. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1521. abort(403, 'Empty statuses are not allowed');
  1522. }
  1523. $ids = $request->input('media_ids');
  1524. $in_reply_to_id = $request->input('in_reply_to_id');
  1525. $user = $request->user();
  1526. $profile = $user->profile;
  1527. $limitKey = 'compose:rate-limit:store:' . $user->id;
  1528. $limitTtl = now()->addMinutes(15);
  1529. $limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) {
  1530. $dailyLimit = Status::whereProfileId($user->profile_id)
  1531. ->whereNull('in_reply_to_id')
  1532. ->whereNull('reblog_of_id')
  1533. ->where('created_at', '>', now()->subDays(1))
  1534. ->count();
  1535. return $dailyLimit >= 100;
  1536. });
  1537. abort_if($limitReached == true, 429);
  1538. $visibility = $profile->is_private ? 'private' : (
  1539. $profile->unlisted == true &&
  1540. $request->input('visibility', 'public') == 'public' ?
  1541. 'unlisted' :
  1542. $request->input('visibility', 'public'));
  1543. if($user->last_active_at == null) {
  1544. return [];
  1545. }
  1546. if($in_reply_to_id) {
  1547. $parent = Status::findOrFail($in_reply_to_id);
  1548. $status = new Status;
  1549. $status->caption = strip_tags($request->input('status'));
  1550. $status->scope = $visibility;
  1551. $status->visibility = $visibility;
  1552. $status->profile_id = $user->profile_id;
  1553. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1554. $status->in_reply_to_id = $parent->id;
  1555. $status->in_reply_to_profile_id = $parent->profile_id;
  1556. $status->save();
  1557. StatusService::del($parent->id);
  1558. } else if($ids) {
  1559. if(Media::whereUserId($user->id)
  1560. ->whereNull('status_id')
  1561. ->find($ids)
  1562. ->count() == 0
  1563. ) {
  1564. abort(400, 'Invalid media_ids');
  1565. }
  1566. $status = new Status;
  1567. $status->caption = strip_tags($request->input('status'));
  1568. $status->profile_id = $user->profile_id;
  1569. $status->scope = 'draft';
  1570. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1571. $status->save();
  1572. $mimes = [];
  1573. foreach($ids as $k => $v) {
  1574. if($k + 1 > config_cache('pixelfed.max_album_length')) {
  1575. continue;
  1576. }
  1577. $m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v);
  1578. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1579. abort(403, 'Invalid media id');
  1580. }
  1581. $m->status_id = $status->id;
  1582. $m->save();
  1583. array_push($mimes, $m->mime);
  1584. }
  1585. if(empty($mimes)) {
  1586. $status->delete();
  1587. abort(400, 'Invalid media ids');
  1588. }
  1589. $status->scope = $visibility;
  1590. $status->visibility = $visibility;
  1591. $status->type = StatusController::mimeTypeCheck($mimes);
  1592. $status->save();
  1593. }
  1594. if(!$status) {
  1595. abort(500, 'An error occured.');
  1596. }
  1597. NewStatusPipeline::dispatch($status);
  1598. Cache::forget('user:account:id:'.$user->id);
  1599. Cache::forget('_api:statuses:recent_9:'.$user->profile_id);
  1600. Cache::forget('profile:status_count:'.$user->profile_id);
  1601. Cache::forget($user->storageUsedKey());
  1602. Cache::forget('profile:embed:' . $status->profile_id);
  1603. Cache::forget($limitKey);
  1604. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1605. $res = $this->fractal->createData($resource)->toArray();
  1606. return response()->json($res);
  1607. }
  1608. /**
  1609. * DELETE /api/v1/statuses
  1610. *
  1611. * @param integer $id
  1612. *
  1613. * @return null
  1614. */
  1615. public function statusDelete(Request $request, $id)
  1616. {
  1617. abort_if(!$request->user(), 403);
  1618. $status = Status::whereProfileId($request->user()->profile->id)
  1619. ->findOrFail($id);
  1620. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1621. Cache::forget('profile:status_count:'.$status->profile_id);
  1622. StatusDelete::dispatch($status);
  1623. $res = $this->fractal->createData($resource)->toArray();
  1624. $res['text'] = $res['content'];
  1625. unset($res['content']);
  1626. return response()->json($res);
  1627. }
  1628. /**
  1629. * POST /api/v1/statuses/{id}/reblog
  1630. *
  1631. * @param integer $id
  1632. *
  1633. * @return StatusTransformer
  1634. */
  1635. public function statusShare(Request $request, $id)
  1636. {
  1637. abort_if(!$request->user(), 403);
  1638. $user = $request->user();
  1639. $status = Status::findOrFail($id);
  1640. if($status->profile_id !== $user->profile_id) {
  1641. if($status->scope == 'private') {
  1642. abort_if(!$status->profile->followedBy($user->profile), 403);
  1643. } else {
  1644. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1645. }
  1646. }
  1647. $share = Status::firstOrCreate([
  1648. 'profile_id' => $user->profile_id,
  1649. 'reblog_of_id' => $status->id,
  1650. 'in_reply_to_profile_id' => $status->profile_id,
  1651. 'scope' => 'public',
  1652. 'visibility' => 'public'
  1653. ]);
  1654. if($share->wasRecentlyCreated == true) {
  1655. SharePipeline::dispatch($share);
  1656. }
  1657. StatusService::del($status->id);
  1658. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1659. $res = $this->fractal->createData($resource)->toArray();
  1660. return response()->json($res);
  1661. }
  1662. /**
  1663. * POST /api/v1/statuses/{id}/unreblog
  1664. *
  1665. * @param integer $id
  1666. *
  1667. * @return StatusTransformer
  1668. */
  1669. public function statusUnshare(Request $request, $id)
  1670. {
  1671. abort_if(!$request->user(), 403);
  1672. $user = $request->user();
  1673. $status = Status::findOrFail($id);
  1674. if($status->profile_id !== $user->profile_id) {
  1675. if($status->scope == 'private') {
  1676. abort_if(!$status->profile->followedBy($user->profile), 403);
  1677. } else {
  1678. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1679. }
  1680. }
  1681. $reblog = Status::whereProfileId($user->profile_id)
  1682. ->whereReblogOfId($status->id)
  1683. ->first();
  1684. if(!$reblog) {
  1685. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1686. $res = $this->fractal->createData($resource)->toArray();
  1687. return response()->json($res);
  1688. }
  1689. UndoSharePipeline::dispatch($reblog);
  1690. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1691. $res = $this->fractal->createData($resource)->toArray();
  1692. return response()->json($res);
  1693. }
  1694. /**
  1695. * GET /api/v1/timelines/tag/{hashtag}
  1696. *
  1697. * @param string $hashtag
  1698. *
  1699. * @return StatusTransformer
  1700. */
  1701. public function timelineHashtag(Request $request, $hashtag)
  1702. {
  1703. $this->validate($request,[
  1704. 'page' => 'nullable|integer|max:40',
  1705. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1706. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1707. 'limit' => 'nullable|integer|max:40'
  1708. ]);
  1709. $tag = Hashtag::whereName($hashtag)
  1710. ->orWhere('slug', $hashtag)
  1711. ->first();
  1712. if(!$tag) {
  1713. return response()->json([]);
  1714. }
  1715. $min = $request->input('min_id');
  1716. $max = $request->input('max_id');
  1717. $limit = $request->input('limit', 20);
  1718. if(!$min && !$max) {
  1719. $id = 1;
  1720. $dir = '>';
  1721. } else {
  1722. $dir = $min ? '>' : '<';
  1723. $id = $min ?? $max;
  1724. }
  1725. $res = StatusHashtag::whereHashtagId($tag->id)
  1726. ->whereStatusVisibility('public')
  1727. ->whereHas('media')
  1728. ->where('status_id', $dir, $id)
  1729. ->latest()
  1730. ->limit($limit)
  1731. ->pluck('status_id')
  1732. ->filter(function($i) {
  1733. return StatusService::get($i);
  1734. })
  1735. ->map(function ($i) {
  1736. return StatusService::get($i);
  1737. })
  1738. ->filter()
  1739. ->values()
  1740. ->toArray();
  1741. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  1742. }
  1743. /**
  1744. * GET /api/v1/bookmarks
  1745. *
  1746. *
  1747. *
  1748. * @return StatusTransformer
  1749. */
  1750. public function bookmarks(Request $request)
  1751. {
  1752. abort_if(!$request->user(), 403);
  1753. $this->validate($request, [
  1754. 'limit' => 'nullable|integer|min:1|max:40',
  1755. 'max_id' => 'nullable|integer|min:0',
  1756. 'since_id' => 'nullable|integer|min:0',
  1757. 'min_id' => 'nullable|integer|min:0'
  1758. ]);
  1759. $pid = $request->user()->profile_id;
  1760. $limit = $request->input('limit') ?? 20;
  1761. $max_id = $request->input('max_id');
  1762. $since_id = $request->input('since_id');
  1763. $min_id = $request->input('min_id');
  1764. $dir = $min_id ? '>' : '<';
  1765. $id = $min_id ?? $max_id;
  1766. if($id) {
  1767. $bookmarks = Bookmark::whereProfileId($pid)
  1768. ->where('status_id', $dir, $id)
  1769. ->limit($limit)
  1770. ->pluck('status_id');
  1771. } else {
  1772. $bookmarks = Bookmark::whereProfileId($pid)
  1773. ->latest()
  1774. ->limit($limit)
  1775. ->pluck('status_id');
  1776. }
  1777. $res = [];
  1778. foreach($bookmarks as $id) {
  1779. $res[] = \App\Services\StatusService::get($id);
  1780. }
  1781. return $res;
  1782. }
  1783. /**
  1784. * POST /api/v1/statuses/{id}/bookmark
  1785. *
  1786. *
  1787. *
  1788. * @return StatusTransformer
  1789. */
  1790. public function bookmarkStatus(Request $request, $id)
  1791. {
  1792. abort_if(!$request->user(), 403);
  1793. $status = Status::whereNull('uri')
  1794. ->whereScope('public')
  1795. ->findOrFail($id);
  1796. Bookmark::firstOrCreate([
  1797. 'status_id' => $status->id,
  1798. 'profile_id' => $request->user()->profile_id
  1799. ]);
  1800. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1801. $res = $this->fractal->createData($resource)->toArray();
  1802. return response()->json($res);
  1803. }
  1804. /**
  1805. * POST /api/v1/statuses/{id}/unbookmark
  1806. *
  1807. *
  1808. *
  1809. * @return StatusTransformer
  1810. */
  1811. public function unbookmarkStatus(Request $request, $id)
  1812. {
  1813. abort_if(!$request->user(), 403);
  1814. $status = Status::whereNull('uri')
  1815. ->whereScope('public')
  1816. ->findOrFail($id);
  1817. Bookmark::firstOrCreate([
  1818. 'status_id' => $status->id,
  1819. 'profile_id' => $request->user()->profile_id
  1820. ]);
  1821. $bookmark = Bookmark::whereStatusId($status->id)
  1822. ->whereProfileId($request->user()->profile_id)
  1823. ->firstOrFail();
  1824. $bookmark->delete();
  1825. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1826. $res = $this->fractal->createData($resource)->toArray();
  1827. return response()->json($res);
  1828. }
  1829. /**
  1830. * GET /api/v2/search
  1831. *
  1832. *
  1833. * @return array
  1834. */
  1835. public function searchV2(Request $request)
  1836. {
  1837. abort_if(!$request->user(), 403);
  1838. $this->validate($request, [
  1839. 'q' => 'required|string|min:1|max:80',
  1840. 'account_id' => 'nullable|string',
  1841. 'max_id' => 'nullable|string',
  1842. 'min_id' => 'nullable|string',
  1843. 'type' => 'nullable|in:accounts,hashtags,statuses',
  1844. 'exclude_unreviewed' => 'nullable',
  1845. 'resolve' => 'nullable',
  1846. 'limit' => 'nullable|integer|max:40',
  1847. 'offset' => 'nullable|integer',
  1848. 'following' => 'nullable'
  1849. ]);
  1850. return SearchApiV2Service::query($request);
  1851. }
  1852. }