ApiV1Controller.php 55 KB

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