ApiV1Controller.php 54 KB

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