ApiV1Controller.php 55 KB

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