ApiV1Controller.php 67 KB

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