ApiV1Controller.php 65 KB

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