1
0

ApiV1Controller.php 67 KB

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