ApiV1Controller.php 54 KB

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