ApiV1Controller.php 54 KB

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