ApiV1Controller.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  937. $media = new Media();
  938. $media->status_id = null;
  939. $media->profile_id = $profile->id;
  940. $media->user_id = $user->id;
  941. $media->media_path = $path;
  942. $media->original_sha256 = $hash;
  943. $media->size = $photo->getSize();
  944. $media->mime = $photo->getMimeType();
  945. $media->caption = $request->input('description');
  946. $media->filter_class = $filterClass;
  947. $media->filter_name = $filterName;
  948. $media->save();
  949. switch ($media->mime) {
  950. case 'image/jpeg':
  951. case 'image/png':
  952. ImageOptimize::dispatch($media);
  953. break;
  954. case 'video/mp4':
  955. VideoThumbnail::dispatch($media);
  956. $preview_url = '/storage/no-preview.png';
  957. $url = '/storage/no-preview.png';
  958. break;
  959. }
  960. Cache::forget($limitKey);
  961. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  962. $res = $this->fractal->createData($resource)->toArray();
  963. $res['preview_url'] = $media->url(). '?cb=1&_v=' . time();
  964. $res['url'] = $media->url(). '?cb=1&_v=' . time();
  965. return response()->json($res);
  966. }
  967. /**
  968. * PUT /api/v1/media/{id}
  969. *
  970. * @param integer $id
  971. *
  972. * @return MediaTransformer
  973. */
  974. public function mediaUpdate(Request $request, $id)
  975. {
  976. abort_if(!$request->user(), 403);
  977. $this->validate($request, [
  978. 'description' => 'nullable|string|max:' . config_cache('pixelfed.max_altext_length')
  979. ]);
  980. $user = $request->user();
  981. $media = Media::whereUserId($user->id)
  982. ->whereNull('status_id')
  983. ->findOrFail($id);
  984. $media->caption = $request->input('description');
  985. $media->save();
  986. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  987. $res = $this->fractal->createData($resource)->toArray();
  988. $res['preview_url'] = url('/storage/no-preview.png');
  989. $res['url'] = url('/storage/no-preview.png');
  990. return response()->json($res);
  991. }
  992. /**
  993. * GET /api/v1/mutes
  994. *
  995. *
  996. * @return AccountTransformer
  997. */
  998. public function accountMutes(Request $request)
  999. {
  1000. abort_if(!$request->user(), 403);
  1001. $this->validate($request, [
  1002. 'limit' => 'nullable|integer|min:1|max:40'
  1003. ]);
  1004. $user = $request->user();
  1005. $limit = $request->input('limit') ?? 40;
  1006. $mutes = UserFilter::whereUserId($user->profile_id)
  1007. ->whereFilterableType('App\Profile')
  1008. ->whereFilterType('mute')
  1009. ->simplePaginate($limit)
  1010. ->pluck('filterable_id');
  1011. $accounts = Profile::find($mutes);
  1012. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  1013. $res = $this->fractal->createData($resource)->toArray();
  1014. return response()->json($res);
  1015. }
  1016. /**
  1017. * POST /api/v1/accounts/{id}/mute
  1018. *
  1019. * @param integer $id
  1020. *
  1021. * @return RelationshipTransformer
  1022. */
  1023. public function accountMuteById(Request $request, $id)
  1024. {
  1025. abort_if(!$request->user(), 403);
  1026. $user = $request->user();
  1027. $pid = $user->profile_id;
  1028. $account = Profile::findOrFail($id);
  1029. $filter = UserFilter::firstOrCreate([
  1030. 'user_id' => $pid,
  1031. 'filterable_id' => $account->id,
  1032. 'filterable_type' => 'App\Profile',
  1033. 'filter_type' => 'mute',
  1034. ]);
  1035. Cache::forget("user:filter:list:$pid");
  1036. Cache::forget("feature:discover:posts:$pid");
  1037. Cache::forget("api:local:exp:rec:$pid");
  1038. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1039. $res = $this->fractal->createData($resource)->toArray();
  1040. return response()->json($res);
  1041. }
  1042. /**
  1043. * POST /api/v1/accounts/{id}/unmute
  1044. *
  1045. * @param integer $id
  1046. *
  1047. * @return RelationshipTransformer
  1048. */
  1049. public function accountUnmuteById(Request $request, $id)
  1050. {
  1051. abort_if(!$request->user(), 403);
  1052. $user = $request->user();
  1053. $pid = $user->profile_id;
  1054. $account = Profile::findOrFail($id);
  1055. $filter = UserFilter::whereUserId($pid)
  1056. ->whereFilterableId($account->id)
  1057. ->whereFilterableType('App\Profile')
  1058. ->whereFilterType('mute')
  1059. ->first();
  1060. if($filter) {
  1061. $filter->delete();
  1062. Cache::forget("user:filter:list:$pid");
  1063. Cache::forget("feature:discover:posts:$pid");
  1064. Cache::forget("api:local:exp:rec:$pid");
  1065. }
  1066. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1067. $res = $this->fractal->createData($resource)->toArray();
  1068. return response()->json($res);
  1069. }
  1070. /**
  1071. * GET /api/v1/notifications
  1072. *
  1073. *
  1074. * @return NotificationTransformer
  1075. */
  1076. public function accountNotifications(Request $request)
  1077. {
  1078. abort_if(!$request->user(), 403);
  1079. $this->validate($request, [
  1080. 'limit' => 'nullable|integer|min:1|max:80',
  1081. 'min_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1082. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1083. 'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1084. ]);
  1085. $pid = $request->user()->profile_id;
  1086. $limit = $request->input('limit', 20);
  1087. $since = $request->input('since_id');
  1088. $min = $request->input('min_id');
  1089. $max = $request->input('max_id');
  1090. if(!$since && !$min && !$max) {
  1091. $min = 1;
  1092. }
  1093. $maxId = null;
  1094. $minId = null;
  1095. if($max) {
  1096. $res = NotificationService::getMax($pid, $max, $limit);
  1097. $ids = NotificationService::getRankedMaxId($pid, $max, $limit);
  1098. if(!empty($ids)) {
  1099. $maxId = max($ids);
  1100. $minId = min($ids);
  1101. }
  1102. } else {
  1103. $res = NotificationService::getMin($pid, $min ?? $since, $limit);
  1104. $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
  1105. if(!empty($ids)) {
  1106. $maxId = max($ids);
  1107. $minId = min($ids);
  1108. }
  1109. }
  1110. $baseUrl = config('app.url') . '/api/v1/notifications?';
  1111. if($minId == $maxId) {
  1112. $minId = null;
  1113. }
  1114. if($maxId) {
  1115. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next"';
  1116. }
  1117. if($minId) {
  1118. $link = '<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1119. }
  1120. if($maxId && $minId) {
  1121. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next",<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1122. }
  1123. $res = response()->json($res);
  1124. if(isset($link)) {
  1125. $res->withHeaders([
  1126. 'Link' => $link,
  1127. ]);
  1128. }
  1129. return $res;
  1130. }
  1131. /**
  1132. * GET /api/v1/timelines/home
  1133. *
  1134. *
  1135. * @return StatusTransformer
  1136. */
  1137. public function timelineHome(Request $request)
  1138. {
  1139. abort_if(!$request->user(), 403);
  1140. $this->validate($request,[
  1141. 'page' => 'nullable|integer|max:40',
  1142. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1143. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1144. 'limit' => 'nullable|integer|max:80'
  1145. ]);
  1146. $page = $request->input('page');
  1147. $min = $request->input('min_id');
  1148. $max = $request->input('max_id');
  1149. $limit = $request->input('limit') ?? 3;
  1150. $user = $request->user();
  1151. if($user->last_active_at) {
  1152. $key = 'user:last_active_at:id:'.$user->id;
  1153. $ttl = now()->addMinutes(5);
  1154. Cache::remember($key, $ttl, function() use($user) {
  1155. $user->last_active_at = now();
  1156. $user->save();
  1157. return;
  1158. });
  1159. }
  1160. $pid = $request->user()->profile_id;
  1161. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1162. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1163. return $following->push($pid)->toArray();
  1164. });
  1165. if($min || $max) {
  1166. $dir = $min ? '>' : '<';
  1167. $id = $min ?? $max;
  1168. $timeline = Status::select(
  1169. 'id',
  1170. 'uri',
  1171. 'caption',
  1172. 'rendered',
  1173. 'profile_id',
  1174. 'type',
  1175. 'in_reply_to_id',
  1176. 'reblog_of_id',
  1177. 'is_nsfw',
  1178. 'scope',
  1179. 'local',
  1180. 'reply_count',
  1181. 'likes_count',
  1182. 'reblogs_count',
  1183. 'comments_disabled',
  1184. 'place_id',
  1185. 'created_at',
  1186. 'updated_at'
  1187. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1188. ->with('profile', 'hashtags', 'mentions')
  1189. ->where('id', $dir, $id)
  1190. ->whereIn('profile_id', $following)
  1191. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1192. ->latest()
  1193. ->limit($limit)
  1194. ->get();
  1195. } else {
  1196. $timeline = Status::select(
  1197. 'id',
  1198. 'uri',
  1199. 'caption',
  1200. 'rendered',
  1201. 'profile_id',
  1202. 'type',
  1203. 'in_reply_to_id',
  1204. 'reblog_of_id',
  1205. 'is_nsfw',
  1206. 'scope',
  1207. 'local',
  1208. 'reply_count',
  1209. 'comments_disabled',
  1210. 'likes_count',
  1211. 'reblogs_count',
  1212. 'place_id',
  1213. 'created_at',
  1214. 'updated_at'
  1215. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1216. ->with('profile', 'hashtags', 'mentions')
  1217. ->whereIn('profile_id', $following)
  1218. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1219. ->latest()
  1220. ->simplePaginate($limit);
  1221. }
  1222. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1223. $res = $this->fractal->createData($fractal)->toArray();
  1224. return response()->json($res);
  1225. }
  1226. /**
  1227. * GET /api/v1/conversations
  1228. *
  1229. * Not implemented
  1230. *
  1231. * @return array
  1232. */
  1233. public function conversations(Request $request)
  1234. {
  1235. abort_if(!$request->user(), 403);
  1236. return response()->json([]);
  1237. }
  1238. /**
  1239. * GET /api/v1/timelines/public
  1240. *
  1241. *
  1242. * @return StatusTransformer
  1243. */
  1244. public function timelinePublic(Request $request)
  1245. {
  1246. $this->validate($request,[
  1247. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1248. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1249. 'limit' => 'nullable|integer|max:80'
  1250. ]);
  1251. $min = $request->input('min_id');
  1252. $max = $request->input('max_id');
  1253. $limit = $request->input('limit') ?? 3;
  1254. $user = $request->user();
  1255. if(PublicTimelineService::count() == 0) {
  1256. PublicTimelineService::warmCache(true, 400);
  1257. }
  1258. if ($max) {
  1259. $feed = PublicTimelineService::getRankedMaxId($max, $limit);
  1260. } else if ($min) {
  1261. $feed = PublicTimelineService::getRankedMinId($min, $limit);
  1262. } else {
  1263. $feed = PublicTimelineService::get(0, $limit);
  1264. }
  1265. $res = collect($feed)
  1266. ->map(function($k) use($user) {
  1267. $status = StatusService::get($k);
  1268. if($user) {
  1269. $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
  1270. }
  1271. return $status;
  1272. })
  1273. ->toArray();
  1274. return response()->json($res);
  1275. }
  1276. /**
  1277. * GET /api/v1/statuses/{id}
  1278. *
  1279. * @param integer $id
  1280. *
  1281. * @return StatusTransformer
  1282. */
  1283. public function statusById(Request $request, $id)
  1284. {
  1285. abort_if(!$request->user(), 403);
  1286. $user = $request->user();
  1287. $status = Status::findOrFail($id);
  1288. if($status->profile_id !== $user->profile_id) {
  1289. if($status->scope == 'private') {
  1290. abort_if(!$status->profile->followedBy($user->profile), 403);
  1291. } else {
  1292. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1293. }
  1294. }
  1295. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1296. $res = $this->fractal->createData($resource)->toArray();
  1297. return response()->json($res);
  1298. }
  1299. /**
  1300. * GET /api/v1/statuses/{id}/context
  1301. *
  1302. * @param integer $id
  1303. *
  1304. * @return StatusTransformer
  1305. */
  1306. public function statusContext(Request $request, $id)
  1307. {
  1308. abort_if(!$request->user(), 403);
  1309. $user = $request->user();
  1310. $status = Status::findOrFail($id);
  1311. if($status->profile_id !== $user->profile_id) {
  1312. if($status->scope == 'private') {
  1313. abort_if(!$status->profile->followedBy($user->profile), 403);
  1314. } else {
  1315. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1316. }
  1317. }
  1318. if($status->comments_disabled) {
  1319. $res = [
  1320. 'ancestors' => [],
  1321. 'descendants' => []
  1322. ];
  1323. } else {
  1324. $ancestors = $status->parent();
  1325. if($ancestors) {
  1326. $ares = new Fractal\Resource\Item($ancestors, new StatusTransformer());
  1327. $ancestors = [
  1328. $this->fractal->createData($ares)->toArray()
  1329. ];
  1330. } else {
  1331. $ancestors = [];
  1332. }
  1333. $descendants = Status::whereInReplyToId($id)->latest()->limit(20)->get();
  1334. $dres = new Fractal\Resource\Collection($descendants, new StatusTransformer());
  1335. $descendants = $this->fractal->createData($dres)->toArray();
  1336. $res = [
  1337. 'ancestors' => $ancestors,
  1338. 'descendants' => $descendants
  1339. ];
  1340. }
  1341. return response()->json($res);
  1342. }
  1343. /**
  1344. * GET /api/v1/statuses/{id}/card
  1345. *
  1346. * @param integer $id
  1347. *
  1348. * @return StatusTransformer
  1349. */
  1350. public function statusCard(Request $request, $id)
  1351. {
  1352. abort_if(!$request->user(), 403);
  1353. $user = $request->user();
  1354. $status = Status::findOrFail($id);
  1355. if($status->profile_id !== $user->profile_id) {
  1356. if($status->scope == 'private') {
  1357. abort_if(!$status->profile->followedBy($user->profile), 403);
  1358. } else {
  1359. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1360. }
  1361. }
  1362. // Return empty response since we don't handle support cards
  1363. $res = [];
  1364. return response()->json($res);
  1365. }
  1366. /**
  1367. * GET /api/v1/statuses/{id}/reblogged_by
  1368. *
  1369. * @param integer $id
  1370. *
  1371. * @return AccountTransformer
  1372. */
  1373. public function statusRebloggedBy(Request $request, $id)
  1374. {
  1375. abort_if(!$request->user(), 403);
  1376. $this->validate($request, [
  1377. 'page' => 'nullable|integer|min:1|max:40',
  1378. 'limit' => 'nullable|integer|min:1|max:80'
  1379. ]);
  1380. $limit = $request->input('limit') ?? 40;
  1381. $user = $request->user();
  1382. $status = Status::findOrFail($id);
  1383. if($status->profile_id !== $user->profile_id) {
  1384. if($status->scope == 'private') {
  1385. abort_if(!$status->profile->followedBy($user->profile), 403);
  1386. } else {
  1387. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1388. }
  1389. }
  1390. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1391. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1392. $res = $this->fractal->createData($resource)->toArray();
  1393. $url = $request->url();
  1394. $page = $request->input('page', 1);
  1395. $next = $page < 40 ? $page + 1 : 40;
  1396. $prev = $page > 1 ? $page - 1 : 1;
  1397. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1398. return response()->json($res, 200, ['Link' => $links]);
  1399. }
  1400. /**
  1401. * GET /api/v1/statuses/{id}/favourited_by
  1402. *
  1403. * @param integer $id
  1404. *
  1405. * @return AccountTransformer
  1406. */
  1407. public function statusFavouritedBy(Request $request, $id)
  1408. {
  1409. abort_if(!$request->user(), 403);
  1410. $this->validate($request, [
  1411. 'page' => 'nullable|integer|min:1|max:40',
  1412. 'limit' => 'nullable|integer|min:1|max:80'
  1413. ]);
  1414. $limit = $request->input('limit') ?? 40;
  1415. $user = $request->user();
  1416. $status = Status::findOrFail($id);
  1417. if($status->profile_id !== $user->profile_id) {
  1418. if($status->scope == 'private') {
  1419. abort_if(!$status->profile->followedBy($user->profile), 403);
  1420. } else {
  1421. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1422. }
  1423. }
  1424. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1425. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1426. $res = $this->fractal->createData($resource)->toArray();
  1427. $url = $request->url();
  1428. $page = $request->input('page', 1);
  1429. $next = $page < 40 ? $page + 1 : 40;
  1430. $prev = $page > 1 ? $page - 1 : 1;
  1431. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1432. return response()->json($res, 200, ['Link' => $links]);
  1433. }
  1434. /**
  1435. * POST /api/v1/statuses
  1436. *
  1437. *
  1438. * @return StatusTransformer
  1439. */
  1440. public function statusCreate(Request $request)
  1441. {
  1442. abort_if(!$request->user(), 403);
  1443. $this->validate($request, [
  1444. 'status' => 'nullable|string',
  1445. 'in_reply_to_id' => 'nullable|integer',
  1446. 'media_ids' => 'array|max:' . config_cache('pixelfed.max_album_length'),
  1447. 'media_ids.*' => 'integer|min:1',
  1448. 'sensitive' => 'nullable|boolean',
  1449. 'visibility' => 'string|in:private,unlisted,public',
  1450. ]);
  1451. if(config('costar.enabled') == true) {
  1452. $blockedKeywords = config('costar.keyword.block');
  1453. if($blockedKeywords !== null && $request->status) {
  1454. $keywords = config('costar.keyword.block');
  1455. foreach($keywords as $kw) {
  1456. if(Str::contains($request->status, $kw) == true) {
  1457. abort(400, 'Invalid object. Contains banned keyword.');
  1458. }
  1459. }
  1460. }
  1461. }
  1462. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1463. abort(403, 'Empty statuses are not allowed');
  1464. }
  1465. $ids = $request->input('media_ids');
  1466. $in_reply_to_id = $request->input('in_reply_to_id');
  1467. $user = $request->user();
  1468. $profile = $user->profile;
  1469. $limitKey = 'compose:rate-limit:store:' . $user->id;
  1470. $limitTtl = now()->addMinutes(15);
  1471. $limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) {
  1472. $dailyLimit = Status::whereProfileId($user->profile_id)
  1473. ->whereNull('in_reply_to_id')
  1474. ->whereNull('reblog_of_id')
  1475. ->where('created_at', '>', now()->subDays(1))
  1476. ->count();
  1477. return $dailyLimit >= 100;
  1478. });
  1479. abort_if($limitReached == true, 429);
  1480. $visibility = $profile->is_private ? 'private' : (
  1481. $profile->unlisted == true &&
  1482. $request->input('visibility', 'public') == 'public' ?
  1483. 'unlisted' :
  1484. $request->input('visibility', 'public'));
  1485. if($user->last_active_at == null) {
  1486. return [];
  1487. }
  1488. if($in_reply_to_id) {
  1489. $parent = Status::findOrFail($in_reply_to_id);
  1490. $status = new Status;
  1491. $status->caption = strip_tags($request->input('status'));
  1492. $status->scope = $visibility;
  1493. $status->visibility = $visibility;
  1494. $status->profile_id = $user->profile_id;
  1495. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1496. $status->in_reply_to_id = $parent->id;
  1497. $status->in_reply_to_profile_id = $parent->profile_id;
  1498. $status->save();
  1499. StatusService::del($parent->id);
  1500. } else if($ids) {
  1501. if(Media::whereUserId($user->id)
  1502. ->whereNull('status_id')
  1503. ->find($ids)
  1504. ->count() == 0
  1505. ) {
  1506. abort(400, 'Invalid media_ids');
  1507. }
  1508. $status = new Status;
  1509. $status->caption = strip_tags($request->input('status'));
  1510. $status->profile_id = $user->profile_id;
  1511. $status->scope = 'draft';
  1512. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1513. $status->save();
  1514. $mimes = [];
  1515. foreach($ids as $k => $v) {
  1516. if($k + 1 > config_cache('pixelfed.max_album_length')) {
  1517. continue;
  1518. }
  1519. $m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v);
  1520. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1521. abort(403, 'Invalid media id');
  1522. }
  1523. $m->status_id = $status->id;
  1524. $m->save();
  1525. array_push($mimes, $m->mime);
  1526. }
  1527. if(empty($mimes)) {
  1528. $status->delete();
  1529. abort(400, 'Invalid media ids');
  1530. }
  1531. $status->scope = $visibility;
  1532. $status->visibility = $visibility;
  1533. $status->type = StatusController::mimeTypeCheck($mimes);
  1534. $status->save();
  1535. }
  1536. if(!$status) {
  1537. abort(500, 'An error occured.');
  1538. }
  1539. NewStatusPipeline::dispatch($status);
  1540. Cache::forget('user:account:id:'.$user->id);
  1541. Cache::forget('_api:statuses:recent_9:'.$user->profile_id);
  1542. Cache::forget('profile:status_count:'.$user->profile_id);
  1543. Cache::forget($user->storageUsedKey());
  1544. Cache::forget('profile:embed:' . $status->profile_id);
  1545. Cache::forget($limitKey);
  1546. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1547. $res = $this->fractal->createData($resource)->toArray();
  1548. return response()->json($res);
  1549. }
  1550. /**
  1551. * DELETE /api/v1/statuses
  1552. *
  1553. * @param integer $id
  1554. *
  1555. * @return null
  1556. */
  1557. public function statusDelete(Request $request, $id)
  1558. {
  1559. abort_if(!$request->user(), 403);
  1560. $status = Status::whereProfileId($request->user()->profile->id)
  1561. ->findOrFail($id);
  1562. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1563. Cache::forget('profile:status_count:'.$status->profile_id);
  1564. StatusDelete::dispatch($status);
  1565. $res = $this->fractal->createData($resource)->toArray();
  1566. $res['text'] = $res['content'];
  1567. unset($res['content']);
  1568. return response()->json($res);
  1569. }
  1570. /**
  1571. * POST /api/v1/statuses/{id}/reblog
  1572. *
  1573. * @param integer $id
  1574. *
  1575. * @return StatusTransformer
  1576. */
  1577. public function statusShare(Request $request, $id)
  1578. {
  1579. abort_if(!$request->user(), 403);
  1580. $user = $request->user();
  1581. $status = Status::findOrFail($id);
  1582. if($status->profile_id !== $user->profile_id) {
  1583. if($status->scope == 'private') {
  1584. abort_if(!$status->profile->followedBy($user->profile), 403);
  1585. } else {
  1586. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1587. }
  1588. }
  1589. $share = Status::firstOrCreate([
  1590. 'profile_id' => $user->profile_id,
  1591. 'reblog_of_id' => $status->id,
  1592. 'in_reply_to_profile_id' => $status->profile_id,
  1593. 'scope' => 'public',
  1594. 'visibility' => 'public'
  1595. ]);
  1596. if($share->wasRecentlyCreated == true) {
  1597. SharePipeline::dispatch($share);
  1598. }
  1599. StatusService::del($status->id);
  1600. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1601. $res = $this->fractal->createData($resource)->toArray();
  1602. return response()->json($res);
  1603. }
  1604. /**
  1605. * POST /api/v1/statuses/{id}/unreblog
  1606. *
  1607. * @param integer $id
  1608. *
  1609. * @return StatusTransformer
  1610. */
  1611. public function statusUnshare(Request $request, $id)
  1612. {
  1613. abort_if(!$request->user(), 403);
  1614. $user = $request->user();
  1615. $status = Status::findOrFail($id);
  1616. if($status->profile_id !== $user->profile_id) {
  1617. if($status->scope == 'private') {
  1618. abort_if(!$status->profile->followedBy($user->profile), 403);
  1619. } else {
  1620. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1621. }
  1622. }
  1623. $reblog = Status::whereProfileId($user->profile_id)
  1624. ->whereReblogOfId($status->id)
  1625. ->first();
  1626. if(!$reblog) {
  1627. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1628. $res = $this->fractal->createData($resource)->toArray();
  1629. return response()->json($res);
  1630. }
  1631. UndoSharePipeline::dispatch($reblog);
  1632. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1633. $res = $this->fractal->createData($resource)->toArray();
  1634. return response()->json($res);
  1635. }
  1636. /**
  1637. * GET /api/v1/timelines/tag/{hashtag}
  1638. *
  1639. * @param string $hashtag
  1640. *
  1641. * @return StatusTransformer
  1642. */
  1643. public function timelineHashtag(Request $request, $hashtag)
  1644. {
  1645. $this->validate($request,[
  1646. 'page' => 'nullable|integer|max:40',
  1647. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1648. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1649. 'limit' => 'nullable|integer|max:40'
  1650. ]);
  1651. $tag = Hashtag::whereName($hashtag)
  1652. ->orWhere('slug', $hashtag)
  1653. ->first();
  1654. if(!$tag) {
  1655. return response()->json([]);
  1656. }
  1657. $min = $request->input('min_id');
  1658. $max = $request->input('max_id');
  1659. $limit = $request->input('limit', 20);
  1660. if(!$min && !$max) {
  1661. $id = 1;
  1662. $dir = '>';
  1663. } else {
  1664. $dir = $min ? '>' : '<';
  1665. $id = $min ?? $max;
  1666. }
  1667. $res = StatusHashtag::whereHashtagId($tag->id)
  1668. ->whereStatusVisibility('public')
  1669. ->whereHas('media')
  1670. ->where('status_id', $dir, $id)
  1671. ->latest()
  1672. ->limit($limit)
  1673. ->pluck('status_id')
  1674. ->filter(function($i) {
  1675. return StatusService::get($i);
  1676. })
  1677. ->map(function ($i) {
  1678. return StatusService::get($i);
  1679. })
  1680. ->filter()
  1681. ->values()
  1682. ->toArray();
  1683. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  1684. }
  1685. /**
  1686. * GET /api/v1/bookmarks
  1687. *
  1688. *
  1689. *
  1690. * @return StatusTransformer
  1691. */
  1692. public function bookmarks(Request $request)
  1693. {
  1694. abort_if(!$request->user(), 403);
  1695. $this->validate($request, [
  1696. 'limit' => 'nullable|integer|min:1|max:40',
  1697. 'max_id' => 'nullable|integer|min:0',
  1698. 'since_id' => 'nullable|integer|min:0',
  1699. 'min_id' => 'nullable|integer|min:0'
  1700. ]);
  1701. $pid = $request->user()->profile_id;
  1702. $limit = $request->input('limit') ?? 20;
  1703. $max_id = $request->input('max_id');
  1704. $since_id = $request->input('since_id');
  1705. $min_id = $request->input('min_id');
  1706. $dir = $min_id ? '>' : '<';
  1707. $id = $min_id ?? $max_id;
  1708. if($id) {
  1709. $bookmarks = Bookmark::whereProfileId($pid)
  1710. ->where('status_id', $dir, $id)
  1711. ->limit($limit)
  1712. ->pluck('status_id');
  1713. } else {
  1714. $bookmarks = Bookmark::whereProfileId($pid)
  1715. ->latest()
  1716. ->limit($limit)
  1717. ->pluck('status_id');
  1718. }
  1719. $res = [];
  1720. foreach($bookmarks as $id) {
  1721. $res[] = \App\Services\StatusService::get($id);
  1722. }
  1723. return $res;
  1724. }
  1725. /**
  1726. * POST /api/v1/statuses/{id}/bookmark
  1727. *
  1728. *
  1729. *
  1730. * @return StatusTransformer
  1731. */
  1732. public function bookmarkStatus(Request $request, $id)
  1733. {
  1734. abort_if(!$request->user(), 403);
  1735. $status = Status::whereNull('uri')
  1736. ->whereScope('public')
  1737. ->findOrFail($id);
  1738. Bookmark::firstOrCreate([
  1739. 'status_id' => $status->id,
  1740. 'profile_id' => $request->user()->profile_id
  1741. ]);
  1742. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1743. $res = $this->fractal->createData($resource)->toArray();
  1744. return response()->json($res);
  1745. }
  1746. /**
  1747. * POST /api/v1/statuses/{id}/unbookmark
  1748. *
  1749. *
  1750. *
  1751. * @return StatusTransformer
  1752. */
  1753. public function unbookmarkStatus(Request $request, $id)
  1754. {
  1755. abort_if(!$request->user(), 403);
  1756. $status = Status::whereNull('uri')
  1757. ->whereScope('public')
  1758. ->findOrFail($id);
  1759. Bookmark::firstOrCreate([
  1760. 'status_id' => $status->id,
  1761. 'profile_id' => $request->user()->profile_id
  1762. ]);
  1763. $bookmark = Bookmark::whereStatusId($status->id)
  1764. ->whereProfileId($request->user()->profile_id)
  1765. ->firstOrFail();
  1766. $bookmark->delete();
  1767. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1768. $res = $this->fractal->createData($resource)->toArray();
  1769. return response()->json($res);
  1770. }
  1771. /**
  1772. * GET /api/v2/search
  1773. *
  1774. *
  1775. * @return array
  1776. */
  1777. public function searchV2(Request $request)
  1778. {
  1779. abort_if(!$request->user(), 403);
  1780. $this->validate($request, [
  1781. 'q' => 'required|string|min:1|max:80',
  1782. 'account_id' => 'nullable|string',
  1783. 'max_id' => 'nullable|string',
  1784. 'min_id' => 'nullable|string',
  1785. 'type' => 'nullable|in:accounts,hashtags,statuses',
  1786. 'exclude_unreviewed' => 'nullable',
  1787. 'resolve' => 'nullable',
  1788. 'limit' => 'nullable|integer|max:40',
  1789. 'offset' => 'nullable|integer',
  1790. 'following' => 'nullable'
  1791. ]);
  1792. return SearchApiV2Service::query($request);
  1793. }
  1794. }