ApiV1Controller.php 54 KB

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