ApiV1Controller.php 54 KB

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