ApiV1Controller.php 54 KB

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