ApiV1Controller.php 54 KB

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