ApiV1Controller.php 58 KB

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