ApiV1Controller.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  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. $like->status_profile_id = $status->profile_id;
  706. $like->is_comment = !empty($status->in_reply_to_id);
  707. $like->save();
  708. $status->likes_count = $status->likes()->count();
  709. $status->save();
  710. LikePipeline::dispatch($like);
  711. }
  712. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  713. $res = $this->fractal->createData($resource)->toArray();
  714. return response()->json($res);
  715. }
  716. /**
  717. * POST /api/v1/statuses/{id}/unfavourite
  718. *
  719. * @param integer $id
  720. *
  721. * @return \App\Transformer\Api\StatusTransformer
  722. */
  723. public function statusUnfavouriteById(Request $request, $id)
  724. {
  725. abort_if(!$request->user(), 403);
  726. $user = $request->user();
  727. $status = Status::findOrFail($id);
  728. if($status->profile_id !== $user->profile_id) {
  729. if($status->scope == 'private') {
  730. abort_if(!$status->profile->followedBy($user->profile), 403);
  731. } else {
  732. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  733. }
  734. }
  735. $like = Like::whereProfileId($user->profile_id)
  736. ->whereStatusId($status->id)
  737. ->first();
  738. if($like) {
  739. $like->forceDelete();
  740. $status->likes_count = $status->likes()->count();
  741. $status->save();
  742. }
  743. StatusService::del($status->id);
  744. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  745. $res = $this->fractal->createData($resource)->toArray();
  746. return response()->json($res);
  747. }
  748. /**
  749. * GET /api/v1/filters
  750. *
  751. * Return empty response since we filter server side
  752. *
  753. * @return array
  754. */
  755. public function accountFilters(Request $request)
  756. {
  757. abort_if(!$request->user(), 403);
  758. return response()->json([]);
  759. }
  760. /**
  761. * GET /api/v1/follow_requests
  762. *
  763. * Return array of Accounts that have sent follow requests
  764. *
  765. * @return \App\Transformer\Api\AccountTransformer
  766. */
  767. public function accountFollowRequests(Request $request)
  768. {
  769. abort_if(!$request->user(), 403);
  770. $user = $request->user();
  771. $followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
  772. $profiles = Profile::find($followRequests);
  773. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  774. $res = $this->fractal->createData($resource)->toArray();
  775. return response()->json($res);
  776. }
  777. /**
  778. * POST /api/v1/follow_requests/{id}/authorize
  779. *
  780. * @param integer $id
  781. *
  782. * @return null
  783. */
  784. public function accountFollowRequestAccept(Request $request, $id)
  785. {
  786. abort_if(!$request->user(), 403);
  787. // todo
  788. return response()->json([]);
  789. }
  790. /**
  791. * POST /api/v1/follow_requests/{id}/reject
  792. *
  793. * @param integer $id
  794. *
  795. * @return null
  796. */
  797. public function accountFollowRequestReject(Request $request, $id)
  798. {
  799. abort_if(!$request->user(), 403);
  800. // todo
  801. return response()->json([]);
  802. }
  803. /**
  804. * GET /api/v1/suggestions
  805. *
  806. * Return empty array as we don't support suggestions
  807. *
  808. * @return null
  809. */
  810. public function accountSuggestions(Request $request)
  811. {
  812. abort_if(!$request->user(), 403);
  813. // todo
  814. return response()->json([]);
  815. }
  816. /**
  817. * GET /api/v1/instance
  818. *
  819. * Information about the server.
  820. *
  821. * @return Instance
  822. */
  823. public function instance(Request $request)
  824. {
  825. $res = Cache::remember('api:v1:instance-data', now()->addMinutes(15), function () {
  826. $rules = config_cache('app.rules') ? collect(json_decode(config_cache('app.rules'), true))
  827. ->map(function($rule, $key) {
  828. $id = $key + 1;
  829. return [
  830. 'id' => "{$id}",
  831. 'text' => $rule
  832. ];
  833. })
  834. ->toArray() : [];
  835. $res = [
  836. 'approval_required' => false,
  837. 'contact_account' => null,
  838. 'description' => config_cache('app.description'),
  839. 'email' => config('instance.email'),
  840. 'invites_enabled' => false,
  841. 'rules' => $rules,
  842. 'short_description' => 'Pixelfed - Photo sharing for everyone',
  843. 'languages' => ['en'],
  844. 'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
  845. 'registrations' => (bool) config_cache('pixelfed.open_registration'),
  846. 'stats' => [
  847. 'user_count' => 0,
  848. 'status_count' => 0,
  849. 'domain_count' => 0
  850. ],
  851. 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
  852. 'title' => config_cache('app.name'),
  853. 'uri' => config('pixelfed.domain.app'),
  854. 'urls' => [],
  855. 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
  856. 'environment' => [
  857. 'max_photo_size' => (int) config_cache('pixelfed.max_photo_size'),
  858. 'max_avatar_size' => (int) config('pixelfed.max_avatar_size'),
  859. 'max_caption_length' => (int) config('pixelfed.max_caption_length'),
  860. 'max_bio_length' => (int) config('pixelfed.max_bio_length'),
  861. 'max_album_length' => (int) config_cache('pixelfed.max_album_length'),
  862. 'mobile_apis' => (bool) config_cache('pixelfed.oauth_enabled')
  863. ]
  864. ];
  865. return $res;
  866. });
  867. return response()->json($res);
  868. }
  869. /**
  870. * GET /api/v1/lists
  871. *
  872. * Return empty array as we don't support lists
  873. *
  874. * @return null
  875. */
  876. public function accountLists(Request $request)
  877. {
  878. abort_if(!$request->user(), 403);
  879. return response()->json([]);
  880. }
  881. /**
  882. * GET /api/v1/accounts/{id}/lists
  883. *
  884. * @param integer $id
  885. *
  886. * @return null
  887. */
  888. public function accountListsById(Request $request, $id)
  889. {
  890. abort_if(!$request->user(), 403);
  891. return response()->json([]);
  892. }
  893. /**
  894. * POST /api/v1/media
  895. *
  896. *
  897. * @return MediaTransformer
  898. */
  899. public function mediaUpload(Request $request)
  900. {
  901. abort_if(!$request->user(), 403);
  902. $this->validate($request, [
  903. 'file.*' => function() {
  904. return [
  905. 'required',
  906. 'mimes:' . config_cache('pixelfed.media_types'),
  907. 'max:' . config_cache('pixelfed.max_photo_size'),
  908. ];
  909. },
  910. 'filter_name' => 'nullable|string|max:24',
  911. 'filter_class' => 'nullable|alpha_dash|max:24',
  912. 'description' => 'nullable|string|max:420'
  913. ]);
  914. $user = $request->user();
  915. if($user->last_active_at == null) {
  916. return [];
  917. }
  918. $limitKey = 'compose:rate-limit:media-upload:' . $user->id;
  919. $limitTtl = now()->addMinutes(15);
  920. $limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) {
  921. $dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
  922. return $dailyLimit >= 250;
  923. });
  924. abort_if($limitReached == true, 429);
  925. $profile = $user->profile;
  926. if(config_cache('pixelfed.enforce_account_limit') == true) {
  927. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  928. return Media::whereUserId($user->id)->sum('size') / 1000;
  929. });
  930. $limit = (int) config_cache('pixelfed.max_account_size');
  931. if ($size >= $limit) {
  932. abort(403, 'Account size limit reached.');
  933. }
  934. }
  935. $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
  936. $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
  937. $photo = $request->file('file');
  938. $mimes = explode(',', config_cache('pixelfed.media_types'));
  939. if(in_array($photo->getMimeType(), $mimes) == false) {
  940. abort(403, 'Invalid or unsupported mime type.');
  941. }
  942. $storagePath = MediaPathService::get($user, 2);
  943. $path = $photo->store($storagePath);
  944. $hash = \hash_file('sha256', $photo);
  945. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  946. $media = new Media();
  947. $media->status_id = null;
  948. $media->profile_id = $profile->id;
  949. $media->user_id = $user->id;
  950. $media->media_path = $path;
  951. $media->original_sha256 = $hash;
  952. $media->size = $photo->getSize();
  953. $media->mime = $photo->getMimeType();
  954. $media->caption = $request->input('description');
  955. $media->filter_class = $filterClass;
  956. $media->filter_name = $filterName;
  957. $media->save();
  958. switch ($media->mime) {
  959. case 'image/jpeg':
  960. case 'image/png':
  961. ImageOptimize::dispatch($media);
  962. break;
  963. case 'video/mp4':
  964. VideoThumbnail::dispatch($media);
  965. $preview_url = '/storage/no-preview.png';
  966. $url = '/storage/no-preview.png';
  967. break;
  968. }
  969. Cache::forget($limitKey);
  970. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  971. $res = $this->fractal->createData($resource)->toArray();
  972. $res['preview_url'] = $media->url(). '?cb=1&_v=' . time();
  973. $res['url'] = $media->url(). '?cb=1&_v=' . time();
  974. return response()->json($res);
  975. }
  976. /**
  977. * PUT /api/v1/media/{id}
  978. *
  979. * @param integer $id
  980. *
  981. * @return MediaTransformer
  982. */
  983. public function mediaUpdate(Request $request, $id)
  984. {
  985. abort_if(!$request->user(), 403);
  986. $this->validate($request, [
  987. 'description' => 'nullable|string|max:420'
  988. ]);
  989. $user = $request->user();
  990. $media = Media::whereUserId($user->id)
  991. ->whereNull('status_id')
  992. ->findOrFail($id);
  993. $media->caption = $request->input('description');
  994. $media->save();
  995. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  996. $res = $this->fractal->createData($resource)->toArray();
  997. $res['preview_url'] = url('/storage/no-preview.png');
  998. $res['url'] = url('/storage/no-preview.png');
  999. return response()->json($res);
  1000. }
  1001. /**
  1002. * GET /api/v1/mutes
  1003. *
  1004. *
  1005. * @return AccountTransformer
  1006. */
  1007. public function accountMutes(Request $request)
  1008. {
  1009. abort_if(!$request->user(), 403);
  1010. $this->validate($request, [
  1011. 'limit' => 'nullable|integer|min:1|max:40'
  1012. ]);
  1013. $user = $request->user();
  1014. $limit = $request->input('limit') ?? 40;
  1015. $mutes = UserFilter::whereUserId($user->profile_id)
  1016. ->whereFilterableType('App\Profile')
  1017. ->whereFilterType('mute')
  1018. ->simplePaginate($limit)
  1019. ->pluck('filterable_id');
  1020. $accounts = Profile::find($mutes);
  1021. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  1022. $res = $this->fractal->createData($resource)->toArray();
  1023. return response()->json($res);
  1024. }
  1025. /**
  1026. * POST /api/v1/accounts/{id}/mute
  1027. *
  1028. * @param integer $id
  1029. *
  1030. * @return RelationshipTransformer
  1031. */
  1032. public function accountMuteById(Request $request, $id)
  1033. {
  1034. abort_if(!$request->user(), 403);
  1035. $user = $request->user();
  1036. $pid = $user->profile_id;
  1037. $account = Profile::findOrFail($id);
  1038. $filter = UserFilter::firstOrCreate([
  1039. 'user_id' => $pid,
  1040. 'filterable_id' => $account->id,
  1041. 'filterable_type' => 'App\Profile',
  1042. 'filter_type' => 'mute',
  1043. ]);
  1044. Cache::forget("user:filter:list:$pid");
  1045. Cache::forget("feature:discover:posts:$pid");
  1046. Cache::forget("api:local:exp:rec:$pid");
  1047. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1048. $res = $this->fractal->createData($resource)->toArray();
  1049. return response()->json($res);
  1050. }
  1051. /**
  1052. * POST /api/v1/accounts/{id}/unmute
  1053. *
  1054. * @param integer $id
  1055. *
  1056. * @return RelationshipTransformer
  1057. */
  1058. public function accountUnmuteById(Request $request, $id)
  1059. {
  1060. abort_if(!$request->user(), 403);
  1061. $user = $request->user();
  1062. $pid = $user->profile_id;
  1063. $account = Profile::findOrFail($id);
  1064. $filter = UserFilter::whereUserId($pid)
  1065. ->whereFilterableId($account->id)
  1066. ->whereFilterableType('App\Profile')
  1067. ->whereFilterType('mute')
  1068. ->first();
  1069. if($filter) {
  1070. $filter->delete();
  1071. Cache::forget("user:filter:list:$pid");
  1072. Cache::forget("feature:discover:posts:$pid");
  1073. Cache::forget("api:local:exp:rec:$pid");
  1074. }
  1075. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  1076. $res = $this->fractal->createData($resource)->toArray();
  1077. return response()->json($res);
  1078. }
  1079. /**
  1080. * GET /api/v1/notifications
  1081. *
  1082. *
  1083. * @return NotificationTransformer
  1084. */
  1085. public function accountNotifications(Request $request)
  1086. {
  1087. abort_if(!$request->user(), 403);
  1088. $this->validate($request, [
  1089. 'limit' => 'nullable|integer|min:1|max:80',
  1090. 'min_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1091. 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1092. 'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
  1093. ]);
  1094. $pid = $request->user()->profile_id;
  1095. $limit = $request->input('limit', 20);
  1096. $timeago = now()->subMonths(6);
  1097. $since = $request->input('since_id');
  1098. $min = $request->input('min_id');
  1099. $max = $request->input('max_id');
  1100. if(!$since && !$min && !$max) {
  1101. $min = 1;
  1102. }
  1103. $dir = $since ? '>' : ($min ? '>=' : '<');
  1104. $id = $since ?? $min ?? $max;
  1105. $notifications = Notification::whereProfileId($pid)
  1106. ->where('id', $dir, $id)
  1107. ->whereDate('created_at', '>', $timeago)
  1108. ->orderByDesc('id')
  1109. ->limit($limit)
  1110. ->get();
  1111. $minId = $notifications->min('id');
  1112. $maxId = $notifications->max('id');
  1113. $resource = new Fractal\Resource\Collection(
  1114. $notifications,
  1115. new NotificationTransformer()
  1116. );
  1117. $res = $this->fractal
  1118. ->createData($resource)
  1119. ->toArray();
  1120. $baseUrl = config('app.url') . '/api/v1/notifications?';
  1121. if($minId == $maxId) {
  1122. $minId = null;
  1123. }
  1124. if($maxId) {
  1125. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next"';
  1126. }
  1127. if($minId) {
  1128. $link = '<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1129. }
  1130. if($maxId && $minId) {
  1131. $link = '<'.$baseUrl.'max_id='.$maxId.'>; rel="next",<'.$baseUrl.'min_id='.$minId.'>; rel="prev"';
  1132. }
  1133. $res = response()->json($res);
  1134. if(isset($link)) {
  1135. $res->withHeaders([
  1136. 'Link' => $link,
  1137. ]);
  1138. }
  1139. return $res;
  1140. }
  1141. /**
  1142. * GET /api/v1/timelines/home
  1143. *
  1144. *
  1145. * @return StatusTransformer
  1146. */
  1147. public function timelineHome(Request $request)
  1148. {
  1149. abort_if(!$request->user(), 403);
  1150. $this->validate($request,[
  1151. 'page' => 'nullable|integer|max:40',
  1152. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1153. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1154. 'limit' => 'nullable|integer|max:80'
  1155. ]);
  1156. $page = $request->input('page');
  1157. $min = $request->input('min_id');
  1158. $max = $request->input('max_id');
  1159. $limit = $request->input('limit') ?? 3;
  1160. $user = $request->user();
  1161. if($user->last_active_at) {
  1162. $key = 'user:last_active_at:id:'.$user->id;
  1163. $ttl = now()->addMinutes(5);
  1164. Cache::remember($key, $ttl, function() use($user) {
  1165. $user->last_active_at = now();
  1166. $user->save();
  1167. return;
  1168. });
  1169. }
  1170. $pid = $request->user()->profile_id;
  1171. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1172. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1173. return $following->push($pid)->toArray();
  1174. });
  1175. if($min || $max) {
  1176. $dir = $min ? '>' : '<';
  1177. $id = $min ?? $max;
  1178. $timeline = Status::select(
  1179. 'id',
  1180. 'uri',
  1181. 'caption',
  1182. 'rendered',
  1183. 'profile_id',
  1184. 'type',
  1185. 'in_reply_to_id',
  1186. 'reblog_of_id',
  1187. 'is_nsfw',
  1188. 'scope',
  1189. 'local',
  1190. 'reply_count',
  1191. 'likes_count',
  1192. 'reblogs_count',
  1193. 'comments_disabled',
  1194. 'place_id',
  1195. 'created_at',
  1196. 'updated_at'
  1197. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1198. ->with('profile', 'hashtags', 'mentions')
  1199. ->where('id', $dir, $id)
  1200. ->whereIn('profile_id', $following)
  1201. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1202. ->latest()
  1203. ->limit($limit)
  1204. ->get();
  1205. } else {
  1206. $timeline = Status::select(
  1207. 'id',
  1208. 'uri',
  1209. 'caption',
  1210. 'rendered',
  1211. 'profile_id',
  1212. 'type',
  1213. 'in_reply_to_id',
  1214. 'reblog_of_id',
  1215. 'is_nsfw',
  1216. 'scope',
  1217. 'local',
  1218. 'reply_count',
  1219. 'comments_disabled',
  1220. 'likes_count',
  1221. 'reblogs_count',
  1222. 'place_id',
  1223. 'created_at',
  1224. 'updated_at'
  1225. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1226. ->with('profile', 'hashtags', 'mentions')
  1227. ->whereIn('profile_id', $following)
  1228. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1229. ->latest()
  1230. ->simplePaginate($limit);
  1231. }
  1232. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1233. $res = $this->fractal->createData($fractal)->toArray();
  1234. return response()->json($res);
  1235. }
  1236. /**
  1237. * GET /api/v1/conversations
  1238. *
  1239. * Not implemented
  1240. *
  1241. * @return array
  1242. */
  1243. public function conversations(Request $request)
  1244. {
  1245. abort_if(!$request->user(), 403);
  1246. return response()->json([]);
  1247. }
  1248. /**
  1249. * GET /api/v1/timelines/public
  1250. *
  1251. *
  1252. * @return StatusTransformer
  1253. */
  1254. public function timelinePublic(Request $request)
  1255. {
  1256. $this->validate($request,[
  1257. 'page' => 'nullable|integer|max:40',
  1258. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1259. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1260. 'limit' => 'nullable|integer|max:80'
  1261. ]);
  1262. $page = $request->input('page');
  1263. $min = $request->input('min_id');
  1264. $max = $request->input('max_id');
  1265. $limit = $request->input('limit') ?? 3;
  1266. $user = $request->user();
  1267. if($user) {
  1268. $key = 'user:last_active_at:id:'.$user->id;
  1269. $ttl = now()->addMinutes(5);
  1270. Cache::remember($key, $ttl, function() use($user) {
  1271. $user->last_active_at = now();
  1272. $user->save();
  1273. return;
  1274. });
  1275. }
  1276. if($min || $max) {
  1277. $dir = $min ? '>' : '<';
  1278. $id = $min ?? $max;
  1279. $timeline = Status::select(
  1280. 'id',
  1281. 'uri',
  1282. 'caption',
  1283. 'rendered',
  1284. 'profile_id',
  1285. 'type',
  1286. 'in_reply_to_id',
  1287. 'reblog_of_id',
  1288. 'is_nsfw',
  1289. 'scope',
  1290. 'local',
  1291. 'reply_count',
  1292. 'comments_disabled',
  1293. 'place_id',
  1294. 'likes_count',
  1295. 'reblogs_count',
  1296. 'created_at',
  1297. 'updated_at'
  1298. )->whereNull('uri')
  1299. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1300. ->with('profile', 'hashtags', 'mentions')
  1301. ->where('id', $dir, $id)
  1302. ->whereScope('public')
  1303. ->where('created_at', '>', now()->subDays(14))
  1304. ->latest()
  1305. ->limit($limit)
  1306. ->get();
  1307. } else {
  1308. $timeline = Status::select(
  1309. 'id',
  1310. 'uri',
  1311. 'caption',
  1312. 'rendered',
  1313. 'profile_id',
  1314. 'type',
  1315. 'in_reply_to_id',
  1316. 'reblog_of_id',
  1317. 'is_nsfw',
  1318. 'scope',
  1319. 'local',
  1320. 'reply_count',
  1321. 'comments_disabled',
  1322. 'place_id',
  1323. 'likes_count',
  1324. 'reblogs_count',
  1325. 'created_at',
  1326. 'updated_at'
  1327. )->whereNull('uri')
  1328. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1329. ->with('profile', 'hashtags', 'mentions')
  1330. ->whereScope('public')
  1331. ->where('created_at', '>', now()->subDays(14))
  1332. ->latest()
  1333. ->simplePaginate($limit);
  1334. }
  1335. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1336. $res = $this->fractal->createData($fractal)->toArray();
  1337. return response()->json($res);
  1338. }
  1339. /**
  1340. * GET /api/v1/statuses/{id}
  1341. *
  1342. * @param integer $id
  1343. *
  1344. * @return StatusTransformer
  1345. */
  1346. public function statusById(Request $request, $id)
  1347. {
  1348. abort_if(!$request->user(), 403);
  1349. $user = $request->user();
  1350. $status = Status::findOrFail($id);
  1351. if($status->profile_id !== $user->profile_id) {
  1352. if($status->scope == 'private') {
  1353. abort_if(!$status->profile->followedBy($user->profile), 403);
  1354. } else {
  1355. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1356. }
  1357. }
  1358. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1359. $res = $this->fractal->createData($resource)->toArray();
  1360. return response()->json($res);
  1361. }
  1362. /**
  1363. * GET /api/v1/statuses/{id}/context
  1364. *
  1365. * @param integer $id
  1366. *
  1367. * @return StatusTransformer
  1368. */
  1369. public function statusContext(Request $request, $id)
  1370. {
  1371. abort_if(!$request->user(), 403);
  1372. $user = $request->user();
  1373. $status = Status::findOrFail($id);
  1374. if($status->profile_id !== $user->profile_id) {
  1375. if($status->scope == 'private') {
  1376. abort_if(!$status->profile->followedBy($user->profile), 403);
  1377. } else {
  1378. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1379. }
  1380. }
  1381. if($status->comments_disabled) {
  1382. $res = [
  1383. 'ancestors' => [],
  1384. 'descendants' => []
  1385. ];
  1386. } else {
  1387. $ancestors = $status->parent();
  1388. if($ancestors) {
  1389. $ares = new Fractal\Resource\Item($ancestors, new StatusTransformer());
  1390. $ancestors = [
  1391. $this->fractal->createData($ares)->toArray()
  1392. ];
  1393. } else {
  1394. $ancestors = [];
  1395. }
  1396. $descendants = Status::whereInReplyToId($id)->latest()->limit(20)->get();
  1397. $dres = new Fractal\Resource\Collection($descendants, new StatusTransformer());
  1398. $descendants = $this->fractal->createData($dres)->toArray();
  1399. $res = [
  1400. 'ancestors' => $ancestors,
  1401. 'descendants' => $descendants
  1402. ];
  1403. }
  1404. return response()->json($res);
  1405. }
  1406. /**
  1407. * GET /api/v1/statuses/{id}/card
  1408. *
  1409. * @param integer $id
  1410. *
  1411. * @return StatusTransformer
  1412. */
  1413. public function statusCard(Request $request, $id)
  1414. {
  1415. abort_if(!$request->user(), 403);
  1416. $user = $request->user();
  1417. $status = Status::findOrFail($id);
  1418. if($status->profile_id !== $user->profile_id) {
  1419. if($status->scope == 'private') {
  1420. abort_if(!$status->profile->followedBy($user->profile), 403);
  1421. } else {
  1422. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1423. }
  1424. }
  1425. // Return empty response since we don't handle support cards
  1426. $res = [];
  1427. return response()->json($res);
  1428. }
  1429. /**
  1430. * GET /api/v1/statuses/{id}/reblogged_by
  1431. *
  1432. * @param integer $id
  1433. *
  1434. * @return AccountTransformer
  1435. */
  1436. public function statusRebloggedBy(Request $request, $id)
  1437. {
  1438. abort_if(!$request->user(), 403);
  1439. $this->validate($request, [
  1440. 'page' => 'nullable|integer|min:1|max:40',
  1441. 'limit' => 'nullable|integer|min:1|max:80'
  1442. ]);
  1443. $limit = $request->input('limit') ?? 40;
  1444. $user = $request->user();
  1445. $status = Status::findOrFail($id);
  1446. if($status->profile_id !== $user->profile_id) {
  1447. if($status->scope == 'private') {
  1448. abort_if(!$status->profile->followedBy($user->profile), 403);
  1449. } else {
  1450. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1451. }
  1452. }
  1453. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1454. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1455. $res = $this->fractal->createData($resource)->toArray();
  1456. $url = $request->url();
  1457. $page = $request->input('page', 1);
  1458. $next = $page < 40 ? $page + 1 : 40;
  1459. $prev = $page > 1 ? $page - 1 : 1;
  1460. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1461. return response()->json($res, 200, ['Link' => $links]);
  1462. }
  1463. /**
  1464. * GET /api/v1/statuses/{id}/favourited_by
  1465. *
  1466. * @param integer $id
  1467. *
  1468. * @return AccountTransformer
  1469. */
  1470. public function statusFavouritedBy(Request $request, $id)
  1471. {
  1472. abort_if(!$request->user(), 403);
  1473. $this->validate($request, [
  1474. 'page' => 'nullable|integer|min:1|max:40',
  1475. 'limit' => 'nullable|integer|min:1|max:80'
  1476. ]);
  1477. $limit = $request->input('limit') ?? 40;
  1478. $user = $request->user();
  1479. $status = Status::findOrFail($id);
  1480. if($status->profile_id !== $user->profile_id) {
  1481. if($status->scope == 'private') {
  1482. abort_if(!$status->profile->followedBy($user->profile), 403);
  1483. } else {
  1484. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1485. }
  1486. }
  1487. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1488. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1489. $res = $this->fractal->createData($resource)->toArray();
  1490. $url = $request->url();
  1491. $page = $request->input('page', 1);
  1492. $next = $page < 40 ? $page + 1 : 40;
  1493. $prev = $page > 1 ? $page - 1 : 1;
  1494. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1495. return response()->json($res, 200, ['Link' => $links]);
  1496. }
  1497. /**
  1498. * POST /api/v1/statuses
  1499. *
  1500. *
  1501. * @return StatusTransformer
  1502. */
  1503. public function statusCreate(Request $request)
  1504. {
  1505. abort_if(!$request->user(), 403);
  1506. $this->validate($request, [
  1507. 'status' => 'nullable|string',
  1508. 'in_reply_to_id' => 'nullable|integer',
  1509. 'media_ids' => 'array|max:' . config_cache('pixelfed.max_album_length'),
  1510. 'media_ids.*' => 'integer|min:1',
  1511. 'sensitive' => 'nullable|boolean',
  1512. 'visibility' => 'string|in:private,unlisted,public',
  1513. ]);
  1514. if(config('costar.enabled') == true) {
  1515. $blockedKeywords = config('costar.keyword.block');
  1516. if($blockedKeywords !== null && $request->status) {
  1517. $keywords = config('costar.keyword.block');
  1518. foreach($keywords as $kw) {
  1519. if(Str::contains($request->status, $kw) == true) {
  1520. abort(400, 'Invalid object. Contains banned keyword.');
  1521. }
  1522. }
  1523. }
  1524. }
  1525. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1526. abort(403, 'Empty statuses are not allowed');
  1527. }
  1528. $ids = $request->input('media_ids');
  1529. $in_reply_to_id = $request->input('in_reply_to_id');
  1530. $user = $request->user();
  1531. $profile = $user->profile;
  1532. $limitKey = 'compose:rate-limit:store:' . $user->id;
  1533. $limitTtl = now()->addMinutes(15);
  1534. $limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) {
  1535. $dailyLimit = Status::whereProfileId($user->profile_id)
  1536. ->whereNull('in_reply_to_id')
  1537. ->whereNull('reblog_of_id')
  1538. ->where('created_at', '>', now()->subDays(1))
  1539. ->count();
  1540. return $dailyLimit >= 100;
  1541. });
  1542. abort_if($limitReached == true, 429);
  1543. $visibility = $profile->is_private ? 'private' : (
  1544. $profile->unlisted == true &&
  1545. $request->input('visibility', 'public') == 'public' ?
  1546. 'unlisted' :
  1547. $request->input('visibility', 'public'));
  1548. if($user->last_active_at == null) {
  1549. return [];
  1550. }
  1551. if($in_reply_to_id) {
  1552. $parent = Status::findOrFail($in_reply_to_id);
  1553. $status = new Status;
  1554. $status->caption = strip_tags($request->input('status'));
  1555. $status->scope = $visibility;
  1556. $status->visibility = $visibility;
  1557. $status->profile_id = $user->profile_id;
  1558. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1559. $status->in_reply_to_id = $parent->id;
  1560. $status->in_reply_to_profile_id = $parent->profile_id;
  1561. $status->save();
  1562. StatusService::del($parent->id);
  1563. } else if($ids) {
  1564. if(Media::whereUserId($user->id)
  1565. ->whereNull('status_id')
  1566. ->find($ids)
  1567. ->count() == 0
  1568. ) {
  1569. abort(400, 'Invalid media_ids');
  1570. }
  1571. $status = new Status;
  1572. $status->caption = strip_tags($request->input('status'));
  1573. $status->profile_id = $user->profile_id;
  1574. $status->scope = 'draft';
  1575. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1576. $status->save();
  1577. $mimes = [];
  1578. foreach($ids as $k => $v) {
  1579. if($k + 1 > config_cache('pixelfed.max_album_length')) {
  1580. continue;
  1581. }
  1582. $m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v);
  1583. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1584. abort(403, 'Invalid media id');
  1585. }
  1586. $m->status_id = $status->id;
  1587. $m->save();
  1588. array_push($mimes, $m->mime);
  1589. }
  1590. if(empty($mimes)) {
  1591. $status->delete();
  1592. abort(400, 'Invalid media ids');
  1593. }
  1594. $status->scope = $visibility;
  1595. $status->visibility = $visibility;
  1596. $status->type = StatusController::mimeTypeCheck($mimes);
  1597. $status->save();
  1598. }
  1599. if(!$status) {
  1600. abort(500, 'An error occured.');
  1601. }
  1602. NewStatusPipeline::dispatch($status);
  1603. Cache::forget('user:account:id:'.$user->id);
  1604. Cache::forget('_api:statuses:recent_9:'.$user->profile_id);
  1605. Cache::forget('profile:status_count:'.$user->profile_id);
  1606. Cache::forget($user->storageUsedKey());
  1607. Cache::forget('profile:embed:' . $status->profile_id);
  1608. Cache::forget($limitKey);
  1609. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1610. $res = $this->fractal->createData($resource)->toArray();
  1611. return response()->json($res);
  1612. }
  1613. /**
  1614. * DELETE /api/v1/statuses
  1615. *
  1616. * @param integer $id
  1617. *
  1618. * @return null
  1619. */
  1620. public function statusDelete(Request $request, $id)
  1621. {
  1622. abort_if(!$request->user(), 403);
  1623. $status = Status::whereProfileId($request->user()->profile->id)
  1624. ->findOrFail($id);
  1625. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1626. Cache::forget('profile:status_count:'.$status->profile_id);
  1627. StatusDelete::dispatch($status);
  1628. $res = $this->fractal->createData($resource)->toArray();
  1629. $res['text'] = $res['content'];
  1630. unset($res['content']);
  1631. return response()->json($res);
  1632. }
  1633. /**
  1634. * POST /api/v1/statuses/{id}/reblog
  1635. *
  1636. * @param integer $id
  1637. *
  1638. * @return StatusTransformer
  1639. */
  1640. public function statusShare(Request $request, $id)
  1641. {
  1642. abort_if(!$request->user(), 403);
  1643. $user = $request->user();
  1644. $status = Status::findOrFail($id);
  1645. if($status->profile_id !== $user->profile_id) {
  1646. if($status->scope == 'private') {
  1647. abort_if(!$status->profile->followedBy($user->profile), 403);
  1648. } else {
  1649. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1650. }
  1651. }
  1652. $share = Status::firstOrCreate([
  1653. 'profile_id' => $user->profile_id,
  1654. 'reblog_of_id' => $status->id,
  1655. 'in_reply_to_profile_id' => $status->profile_id,
  1656. 'scope' => 'public',
  1657. 'visibility' => 'public'
  1658. ]);
  1659. if($share->wasRecentlyCreated == true) {
  1660. $status->reblogs_count = $status->shares()->count();
  1661. $status->save();
  1662. SharePipeline::dispatch($share);
  1663. }
  1664. StatusService::del($status->id);
  1665. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1666. $res = $this->fractal->createData($resource)->toArray();
  1667. return response()->json($res);
  1668. }
  1669. /**
  1670. * POST /api/v1/statuses/{id}/unreblog
  1671. *
  1672. * @param integer $id
  1673. *
  1674. * @return StatusTransformer
  1675. */
  1676. public function statusUnshare(Request $request, $id)
  1677. {
  1678. abort_if(!$request->user(), 403);
  1679. $user = $request->user();
  1680. $status = Status::findOrFail($id);
  1681. if($status->profile_id !== $user->profile_id) {
  1682. if($status->scope == 'private') {
  1683. abort_if(!$status->profile->followedBy($user->profile), 403);
  1684. } else {
  1685. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1686. }
  1687. }
  1688. Status::whereProfileId($user->profile_id)
  1689. ->whereReblogOfId($status->id)
  1690. ->delete();
  1691. $status->reblogs_count = $status->shares()->count();
  1692. $status->save();
  1693. StatusService::del($status->id);
  1694. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1695. $res = $this->fractal->createData($resource)->toArray();
  1696. return response()->json($res);
  1697. }
  1698. /**
  1699. * GET /api/v1/timelines/tag/{hashtag}
  1700. *
  1701. * @param string $hashtag
  1702. *
  1703. * @return StatusTransformer
  1704. */
  1705. public function timelineHashtag(Request $request, $hashtag)
  1706. {
  1707. $this->validate($request,[
  1708. 'page' => 'nullable|integer|max:40',
  1709. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1710. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1711. 'limit' => 'nullable|integer|max:40'
  1712. ]);
  1713. $tag = Hashtag::whereName($hashtag)
  1714. ->orWhere('slug', $hashtag)
  1715. ->first();
  1716. if(!$tag) {
  1717. return response()->json([]);
  1718. }
  1719. $min = $request->input('min_id');
  1720. $max = $request->input('max_id');
  1721. $limit = $request->input('limit', 20);
  1722. if(!$min && !$max) {
  1723. $id = 1;
  1724. $dir = '>';
  1725. } else {
  1726. $dir = $min ? '>' : '<';
  1727. $id = $min ?? $max;
  1728. }
  1729. $res = StatusHashtag::whereHashtagId($tag->id)
  1730. ->whereStatusVisibility('public')
  1731. ->whereHas('media')
  1732. ->where('status_id', $dir, $id)
  1733. ->latest()
  1734. ->limit($limit)
  1735. ->pluck('status_id')
  1736. ->map(function ($i) {
  1737. return StatusService::get($i);
  1738. })
  1739. ->all();
  1740. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  1741. }
  1742. /**
  1743. * GET /api/v1/bookmarks
  1744. *
  1745. *
  1746. *
  1747. * @return StatusTransformer
  1748. */
  1749. public function bookmarks(Request $request)
  1750. {
  1751. abort_if(!$request->user(), 403);
  1752. $this->validate($request, [
  1753. 'limit' => 'nullable|integer|min:1|max:40',
  1754. 'max_id' => 'nullable|integer|min:0',
  1755. 'since_id' => 'nullable|integer|min:0',
  1756. 'min_id' => 'nullable|integer|min:0'
  1757. ]);
  1758. $pid = $request->user()->profile_id;
  1759. $limit = $request->input('limit') ?? 20;
  1760. $max_id = $request->input('max_id');
  1761. $since_id = $request->input('since_id');
  1762. $min_id = $request->input('min_id');
  1763. $dir = $min_id ? '>' : '<';
  1764. $id = $min_id ?? $max_id;
  1765. if($id) {
  1766. $bookmarks = Bookmark::whereProfileId($pid)
  1767. ->where('status_id', $dir, $id)
  1768. ->limit($limit)
  1769. ->pluck('status_id');
  1770. } else {
  1771. $bookmarks = Bookmark::whereProfileId($pid)
  1772. ->latest()
  1773. ->limit($limit)
  1774. ->pluck('status_id');
  1775. }
  1776. $res = [];
  1777. foreach($bookmarks as $id) {
  1778. $res[] = \App\Services\StatusService::get($id);
  1779. }
  1780. return $res;
  1781. }
  1782. /**
  1783. * POST /api/v1/statuses/{id}/bookmark
  1784. *
  1785. *
  1786. *
  1787. * @return StatusTransformer
  1788. */
  1789. public function bookmarkStatus(Request $request, $id)
  1790. {
  1791. abort_if(!$request->user(), 403);
  1792. $status = Status::whereNull('uri')
  1793. ->whereScope('public')
  1794. ->findOrFail($id);
  1795. Bookmark::firstOrCreate([
  1796. 'status_id' => $status->id,
  1797. 'profile_id' => $request->user()->profile_id
  1798. ]);
  1799. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1800. $res = $this->fractal->createData($resource)->toArray();
  1801. return response()->json($res);
  1802. }
  1803. /**
  1804. * POST /api/v1/statuses/{id}/unbookmark
  1805. *
  1806. *
  1807. *
  1808. * @return StatusTransformer
  1809. */
  1810. public function unbookmarkStatus(Request $request, $id)
  1811. {
  1812. abort_if(!$request->user(), 403);
  1813. $status = Status::whereNull('uri')
  1814. ->whereScope('public')
  1815. ->findOrFail($id);
  1816. Bookmark::firstOrCreate([
  1817. 'status_id' => $status->id,
  1818. 'profile_id' => $request->user()->profile_id
  1819. ]);
  1820. $bookmark = Bookmark::whereStatusId($status->id)
  1821. ->whereProfileId($request->user()->profile_id)
  1822. ->firstOrFail();
  1823. $bookmark->delete();
  1824. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1825. $res = $this->fractal->createData($resource)->toArray();
  1826. return response()->json($res);
  1827. }
  1828. /**
  1829. * GET /api/v2/search
  1830. *
  1831. *
  1832. * @return array
  1833. */
  1834. public function searchV2(Request $request)
  1835. {
  1836. abort_if(!$request->user(), 403);
  1837. $this->validate($request, [
  1838. 'q' => 'required|string|min:1|max:80',
  1839. 'account_id' => 'nullable|string',
  1840. 'max_id' => 'nullable|string',
  1841. 'min_id' => 'nullable|string',
  1842. 'type' => 'nullable|in:accounts,hashtags,statuses',
  1843. 'exclude_unreviewed' => 'nullable',
  1844. 'resolve' => 'nullable',
  1845. 'limit' => 'nullable|integer|max:40',
  1846. 'offset' => 'nullable|integer',
  1847. 'following' => 'nullable'
  1848. ]);
  1849. return SearchApiV2Service::query($request);
  1850. }
  1851. }