ApiV1Controller.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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 Laravel\Passport\Passport;
  8. use Auth, Cache, DB, URL;
  9. use App\{
  10. Follower,
  11. FollowRequest,
  12. Like,
  13. Media,
  14. Notification,
  15. Profile,
  16. Status,
  17. UserFilter,
  18. };
  19. use League\Fractal;
  20. use App\Transformer\Api\Mastodon\v1\{
  21. AccountTransformer,
  22. MediaTransformer,
  23. NotificationTransformer,
  24. StatusTransformer,
  25. };
  26. use App\Transformer\Api\{
  27. RelationshipTransformer,
  28. };
  29. use App\Http\Controllers\FollowerController;
  30. use League\Fractal\Serializer\ArraySerializer;
  31. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  32. use App\Jobs\LikePipeline\LikePipeline;
  33. use App\Jobs\StatusPipeline\StatusDelete;
  34. use App\Jobs\FollowPipeline\FollowPipeline;
  35. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  36. use App\Jobs\VideoPipeline\{
  37. VideoOptimize,
  38. VideoPostProcess,
  39. VideoThumbnail
  40. };
  41. use App\Services\NotificationService;
  42. class ApiV1Controller extends Controller
  43. {
  44. protected $fractal;
  45. public function __construct()
  46. {
  47. $this->fractal = new Fractal\Manager();
  48. $this->fractal->setSerializer(new ArraySerializer());
  49. }
  50. public function apps(Request $request)
  51. {
  52. abort_if(!config('pixelfed.oauth_enabled'), 404);
  53. $this->validate($request, [
  54. 'client_name' => 'required',
  55. 'redirect_uris' => 'required',
  56. 'scopes' => 'nullable',
  57. 'website' => 'nullable'
  58. ]);
  59. $client = Passport::client()->forceFill([
  60. 'user_id' => null,
  61. 'name' => e($request->client_name),
  62. 'secret' => Str::random(40),
  63. 'redirect' => $request->redirect_uris,
  64. 'personal_access_client' => false,
  65. 'password_client' => false,
  66. 'revoked' => false,
  67. ]);
  68. $client->save();
  69. $res = [
  70. 'id' => $client->id,
  71. 'name' => $client->name,
  72. 'website' => null,
  73. 'redirect_uri' => $client->redirect,
  74. 'client_id' => $client->id,
  75. 'client_secret' => $client->secret,
  76. 'vapid_key' => null
  77. ];
  78. return $res;
  79. }
  80. /**
  81. * GET /api/v1/accounts/verify_credentials
  82. *
  83. *
  84. * @return \App\Transformer\Api\AccountTransformer
  85. */
  86. public function verifyCredentials(Request $request)
  87. {
  88. abort_if(!$request->user(), 403);
  89. $id = $request->user()->id;
  90. $res = Cache::remember('mastoapi:user:account:id:'.$id, now()->addHours(6), function() use($id) {
  91. $profile = Profile::whereNull('status')->whereUserId($id)->firstOrFail();
  92. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  93. $res = $this->fractal->createData($resource)->toArray();
  94. $res['source'] = [
  95. 'privacy' => $profile->is_private ? 'private' : 'public',
  96. 'sensitive' => $profile->cw ? true : false,
  97. 'language' => 'en',
  98. 'note' => '',
  99. 'fields' => []
  100. ];
  101. return $res;
  102. });
  103. return response()->json($res);
  104. }
  105. /**
  106. * GET /api/v1/accounts/{id}
  107. *
  108. * @param integer $id
  109. *
  110. * @return \App\Transformer\Api\AccountTransformer
  111. */
  112. public function accountById(Request $request, $id)
  113. {
  114. $profile = Profile::whereNull('status')->findOrFail($id);
  115. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  116. $res = $this->fractal->createData($resource)->toArray();
  117. return response()->json($res);
  118. }
  119. /**
  120. * PATCH /api/v1/accounts/update_credentials
  121. *
  122. * @return \App\Transformer\Api\AccountTransformer
  123. */
  124. public function accountUpdateCredentials(Request $request)
  125. {
  126. abort_if(!$request->user(), 403);
  127. $this->validate($request, [
  128. 'display_name' => 'nullable|string',
  129. 'note' => 'nullable|string',
  130. 'locked' => 'nullable|boolean',
  131. // 'source.privacy' => 'nullable|in:unlisted,public,private',
  132. // 'source.sensitive' => 'nullable|boolean'
  133. ]);
  134. $user = $request->user();
  135. $profile = $user->profile;
  136. $displayName = $request->input('display_name');
  137. $note = $request->input('note');
  138. $locked = $request->input('locked');
  139. // $privacy = $request->input('source.privacy');
  140. // $sensitive = $request->input('source.sensitive');
  141. $changes = false;
  142. if($displayName !== $user->name) {
  143. $user->name = $displayName;
  144. $profile->name = $displayName;
  145. $changes = true;
  146. }
  147. if($note !== $profile->bio) {
  148. $profile->bio = e($note);
  149. $changes = true;
  150. }
  151. if(!is_null($locked)) {
  152. $profile->is_private = $locked;
  153. $changes = true;
  154. }
  155. if($changes) {
  156. $user->save();
  157. $profile->save();
  158. }
  159. $resource = new Fractal\Resource\Item($profile, new AccountTransformer());
  160. $res = $this->fractal->createData($resource)->toArray();
  161. return response()->json($res);
  162. }
  163. /**
  164. * GET /api/v1/accounts/{id}/followers
  165. *
  166. * @param integer $id
  167. *
  168. * @return \App\Transformer\Api\AccountTransformer
  169. */
  170. public function accountFollowersById(Request $request, $id)
  171. {
  172. abort_if(!$request->user(), 403);
  173. $profile = Profile::whereNull('status')->findOrFail($id);
  174. if($profile->domain) {
  175. $res = [];
  176. } else {
  177. $settings = $profile->user->settings;
  178. if($settings->show_profile_followers == true) {
  179. $limit = $request->input('limit') ?? 40;
  180. $followers = $profile->followers()->paginate($limit);
  181. $resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
  182. $res = $this->fractal->createData($resource)->toArray();
  183. } else {
  184. $res = [];
  185. }
  186. }
  187. return response()->json($res);
  188. }
  189. /**
  190. * GET /api/v1/accounts/{id}/following
  191. *
  192. * @param integer $id
  193. *
  194. * @return \App\Transformer\Api\AccountTransformer
  195. */
  196. public function accountFollowingById(Request $request, $id)
  197. {
  198. abort_if(!$request->user(), 403);
  199. $profile = Profile::whereNull('status')->findOrFail($id);
  200. if($profile->domain) {
  201. $res = [];
  202. } else {
  203. $settings = $profile->user->settings;
  204. if($settings->show_profile_following == true) {
  205. $limit = $request->input('limit') ?? 40;
  206. $following = $profile->following()->paginate($limit);
  207. $resource = new Fractal\Resource\Collection($following, new AccountTransformer());
  208. $res = $this->fractal->createData($resource)->toArray();
  209. } else {
  210. $res = [];
  211. }
  212. }
  213. return response()->json($res);
  214. }
  215. /**
  216. * GET /api/v1/accounts/{id}/statuses
  217. *
  218. * @param integer $id
  219. *
  220. * @return \App\Transformer\Api\StatusTransformer
  221. */
  222. public function accountStatusesById(Request $request, $id)
  223. {
  224. abort_if(!$request->user(), 403);
  225. $this->validate($request, [
  226. 'only_media' => 'nullable',
  227. 'pinned' => 'nullable',
  228. 'exclude_replies' => 'nullable',
  229. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  230. 'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  231. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  232. 'limit' => 'nullable|integer|min:1|max:40'
  233. ]);
  234. $profile = Profile::whereNull('status')->findOrFail($id);
  235. $limit = $request->limit ?? 20;
  236. $max_id = $request->max_id;
  237. $min_id = $request->min_id;
  238. $pid = $request->user()->profile_id;
  239. $scope = $request->only_media == true ?
  240. ['photo', 'photo:album', 'video', 'video:album'] :
  241. ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
  242. if($pid == $profile->id) {
  243. $visibility = ['public', 'unlisted', 'private'];
  244. } else if($profile->is_private) {
  245. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  246. $following = Follower::whereProfileId($pid)->pluck('following_id');
  247. return $following->push($pid)->toArray();
  248. });
  249. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : [];
  250. } else {
  251. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  252. $following = Follower::whereProfileId($pid)->pluck('following_id');
  253. return $following->push($pid)->toArray();
  254. });
  255. $visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
  256. }
  257. if($min_id || $max_id) {
  258. $dir = $min_id ? '>' : '<';
  259. $id = $min_id ?? $max_id;
  260. $timeline = Status::select(
  261. 'id',
  262. 'uri',
  263. 'caption',
  264. 'rendered',
  265. 'profile_id',
  266. 'type',
  267. 'in_reply_to_id',
  268. 'reblog_of_id',
  269. 'is_nsfw',
  270. 'scope',
  271. 'local',
  272. 'place_id',
  273. 'created_at',
  274. 'updated_at'
  275. )->whereProfileId($profile->id)
  276. ->whereIn('type', $scope)
  277. ->where('id', $dir, $id)
  278. ->whereIn('visibility', $visibility)
  279. ->latest()
  280. ->limit($limit)
  281. ->get();
  282. } else {
  283. $timeline = Status::select(
  284. 'id',
  285. 'uri',
  286. 'caption',
  287. 'rendered',
  288. 'profile_id',
  289. 'type',
  290. 'in_reply_to_id',
  291. 'reblog_of_id',
  292. 'is_nsfw',
  293. 'scope',
  294. 'local',
  295. 'place_id',
  296. 'created_at',
  297. 'updated_at'
  298. )->whereProfileId($profile->id)
  299. ->whereIn('type', $scope)
  300. ->whereIn('visibility', $visibility)
  301. ->latest()
  302. ->limit($limit)
  303. ->get();
  304. }
  305. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  306. $res = $this->fractal->createData($resource)->toArray();
  307. return response()->json($res);
  308. }
  309. /**
  310. * POST /api/v1/accounts/{id}/follow
  311. *
  312. * @param integer $id
  313. *
  314. * @return \App\Transformer\Api\RelationshipTransformer
  315. */
  316. public function accountFollowById(Request $request, $id)
  317. {
  318. abort_if(!$request->user(), 403);
  319. $user = $request->user();
  320. $target = Profile::where('id', '!=', $user->id)
  321. ->whereNull('status')
  322. ->findOrFail($item);
  323. $private = (bool) $target->is_private;
  324. $remote = (bool) $target->domain;
  325. $blocked = UserFilter::whereUserId($target->id)
  326. ->whereFilterType('block')
  327. ->whereFilterableId($user->id)
  328. ->whereFilterableType('App\Profile')
  329. ->exists();
  330. if($blocked == true) {
  331. abort(400, 'You cannot follow this user.');
  332. }
  333. $isFollowing = Follower::whereProfileId($user->id)
  334. ->whereFollowingId($target->id)
  335. ->exists();
  336. // Following already, return empty relationship
  337. if($isFollowing == true) {
  338. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  339. $res = $this->fractal->createData($resource)->toArray();
  340. return response()->json($res);
  341. }
  342. // Rate limits, max 7500 followers per account
  343. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  344. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  345. }
  346. // Rate limits, follow 30 accounts per hour max
  347. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  348. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  349. }
  350. if($private == true) {
  351. $follow = FollowRequest::firstOrCreate([
  352. 'follower_id' => $user->id,
  353. 'following_id' => $target->id
  354. ]);
  355. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  356. (new FollowerController())->sendFollow($user, $target);
  357. }
  358. } else {
  359. $follower = new Follower();
  360. $follower->profile_id = $user->id;
  361. $follower->following_id = $target->id;
  362. $follower->save();
  363. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  364. (new FollowerController())->sendFollow($user, $target);
  365. }
  366. FollowPipeline::dispatch($follower);
  367. }
  368. Cache::forget('profile:following:'.$target->id);
  369. Cache::forget('profile:followers:'.$target->id);
  370. Cache::forget('profile:following:'.$user->id);
  371. Cache::forget('profile:followers:'.$user->id);
  372. Cache::forget('api:local:exp:rec:'.$user->id);
  373. Cache::forget('user:account:id:'.$target->user_id);
  374. Cache::forget('user:account:id:'.$user->user_id);
  375. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  376. $res = $this->fractal->createData($resource)->toArray();
  377. return response()->json($res);
  378. }
  379. /**
  380. * POST /api/v1/accounts/{id}/unfollow
  381. *
  382. * @param integer $id
  383. *
  384. * @return \App\Transformer\Api\RelationshipTransformer
  385. */
  386. public function accountUnfollowById(Request $request, $id)
  387. {
  388. abort_if(!$request->user(), 403);
  389. $user = $request->user();
  390. $target = Profile::where('id', '!=', $user->id)
  391. ->whereNull('status')
  392. ->findOrFail($item);
  393. $private = (bool) $target->is_private;
  394. $remote = (bool) $target->domain;
  395. $isFollowing = Follower::whereProfileId($user->id)
  396. ->whereFollowingId($target->id)
  397. ->exists();
  398. if($isFollowing == false) {
  399. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  400. $res = $this->fractal->createData($resource)->toArray();
  401. return response()->json($res);
  402. }
  403. // Rate limits, follow 30 accounts per hour max
  404. if($user->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  405. abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  406. }
  407. FollowRequest::whereFollowerId($user->id)
  408. ->whereFollowingId($target->id)
  409. ->delete();
  410. Follower::whereProfileId($user->id)
  411. ->whereFollowingId($target->id)
  412. ->delete();
  413. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  414. (new FollowerController())->sendUndoFollow($user, $target);
  415. }
  416. Cache::forget('profile:following:'.$target->id);
  417. Cache::forget('profile:followers:'.$target->id);
  418. Cache::forget('profile:following:'.$user->id);
  419. Cache::forget('profile:followers:'.$user->id);
  420. Cache::forget('api:local:exp:rec:'.$user->id);
  421. Cache::forget('user:account:id:'.$target->user_id);
  422. Cache::forget('user:account:id:'.$user->user_id);
  423. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  424. $res = $this->fractal->createData($resource)->toArray();
  425. return response()->json($res);
  426. }
  427. /**
  428. * GET /api/v1/accounts/relationships
  429. *
  430. * @param array|integer $id
  431. *
  432. * @return \App\Transformer\Api\RelationshipTransformer
  433. */
  434. public function accountRelationshipsById(Request $request)
  435. {
  436. abort_if(!$request->user(), 403);
  437. $this->validate($request, [
  438. 'id' => 'required|array|min:1|max:20',
  439. 'id.*' => 'required|integer|min:1|max:' . PHP_INT_MAX
  440. ]);
  441. $pid = $request->user()->profile_id ?? $request->user()->profile->id;
  442. $ids = collect($request->input('id'));
  443. $filtered = $ids->filter(function($v) use($pid) {
  444. return $v != $pid;
  445. });
  446. $relations = Profile::whereNull('status')->findOrFail($filtered->values());
  447. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  448. $res = $this->fractal->createData($fractal)->toArray();
  449. return response()->json($res);
  450. }
  451. /**
  452. * GET /api/v1/accounts/search
  453. *
  454. *
  455. *
  456. * @return \App\Transformer\Api\AccountTransformer
  457. */
  458. public function accountSearch(Request $request)
  459. {
  460. abort_if(!$request->user(), 403);
  461. $this->validate($request, [
  462. 'q' => 'required|string|min:1|max:255',
  463. 'limit' => 'nullable|integer|min:1|max:40',
  464. 'resolve' => 'nullable'
  465. ]);
  466. $user = $request->user();
  467. $query = $request->input('q');
  468. $limit = $request->input('limit') ?? 20;
  469. $resolve = (bool) $request->input('resolve', false);
  470. $q = '%' . $query . '%';
  471. $profiles = Profile::whereNull('status')
  472. ->where('username', 'like', $q)
  473. ->orWhere('name', 'like', $q)
  474. ->limit($limit)
  475. ->get();
  476. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  477. $res = $this->fractal->createData($resource)->toArray();
  478. return response()->json($res);
  479. }
  480. /**
  481. * GET /api/v1/blocks
  482. *
  483. *
  484. *
  485. * @return \App\Transformer\Api\AccountTransformer
  486. */
  487. public function accountBlocks(Request $request)
  488. {
  489. abort_if(!$request->user(), 403);
  490. $this->validate($request, [
  491. 'limit' => 'nullable|integer|min:1|max:40',
  492. 'page' => 'nullable|integer|min:1|max:10'
  493. ]);
  494. $user = $request->user();
  495. $limit = $request->input('limit') ?? 40;
  496. $blocked = UserFilter::select('filterable_id','filterable_type','filter_type','user_id')
  497. ->whereUserId($user->profile_id)
  498. ->whereFilterableType('App\Profile')
  499. ->whereFilterType('block')
  500. ->simplePaginate($limit)
  501. ->pluck('filterable_id');
  502. $profiles = Profile::findOrFail($blocked);
  503. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  504. $res = $this->fractal->createData($resource)->toArray();
  505. return response()->json($res);
  506. }
  507. /**
  508. * POST /api/v1/accounts/{id}/block
  509. *
  510. * @param integer $id
  511. *
  512. * @return \App\Transformer\Api\RelationshipTransformer
  513. */
  514. public function accountBlockById(Request $request, $id)
  515. {
  516. abort_if(!$request->user(), 403);
  517. $user = $request->user();
  518. $pid = $user->profile_id ?? $user->profile->id;
  519. if($id == $pid) {
  520. abort(400, 'You cannot block yourself');
  521. }
  522. $profile = Profile::findOrFail($id);
  523. Follower::whereProfileId($profile->id)->whereFollowingId($pid)->delete();
  524. Follower::whereProfileId($pid)->whereFollowingId($profile->id)->delete();
  525. Notification::whereProfileId($pid)->whereActorId($profile->id)->delete();
  526. $filter = UserFilter::firstOrCreate([
  527. 'user_id' => $pid,
  528. 'filterable_id' => $profile->id,
  529. 'filterable_type' => 'App\Profile',
  530. 'filter_type' => 'block',
  531. ]);
  532. Cache::forget("user:filter:list:$pid");
  533. Cache::forget("api:local:exp:rec:$pid");
  534. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  535. $res = $this->fractal->createData($resource)->toArray();
  536. return response()->json($res);
  537. }
  538. /**
  539. * POST /api/v1/accounts/{id}/unblock
  540. *
  541. * @param integer $id
  542. *
  543. * @return \App\Transformer\Api\RelationshipTransformer
  544. */
  545. public function accountUnblockById(Request $request, $id)
  546. {
  547. abort_if(!$request->user(), 403);
  548. $user = $request->user();
  549. $pid = $user->profile_id ?? $user->profile->id;
  550. if($id == $pid) {
  551. abort(400, 'You cannot unblock yourself');
  552. }
  553. $profile = Profile::findOrFail($id);
  554. UserFilter::whereUserId($pid)
  555. ->whereFilterableId($profile->id)
  556. ->whereFilterableType('App\Profile')
  557. ->whereFilterType('block')
  558. ->delete();
  559. Cache::forget("user:filter:list:$pid");
  560. Cache::forget("api:local:exp:rec:$pid");
  561. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  562. $res = $this->fractal->createData($resource)->toArray();
  563. return response()->json($res);
  564. }
  565. /**
  566. * GET /api/v1/custom_emojis
  567. *
  568. * Return empty array, we don't support custom emoji
  569. *
  570. * @return array
  571. */
  572. public function customEmojis()
  573. {
  574. return response()->json([]);
  575. }
  576. /**
  577. * GET /api/v1/domain_blocks
  578. *
  579. * Return empty array
  580. *
  581. * @return array
  582. */
  583. public function accountDomainBlocks(Request $request)
  584. {
  585. abort_if(!$request->user(), 403);
  586. return response()->json([]);
  587. }
  588. /**
  589. * GET /api/v1/endorsements
  590. *
  591. * Return empty array
  592. *
  593. * @return array
  594. */
  595. public function accountEndorsements(Request $request)
  596. {
  597. abort_if(!$request->user(), 403);
  598. return response()->json([]);
  599. }
  600. /**
  601. * GET /api/v1/favourites
  602. *
  603. * Returns collection of liked statuses
  604. *
  605. * @return \App\Transformer\Api\StatusTransformer
  606. */
  607. public function accountFavourites(Request $request)
  608. {
  609. abort_if(!$request->user(), 403);
  610. $user = $request->user();
  611. $limit = $request->input('limit') ?? 20;
  612. $favourites = Like::whereProfileId($user->profile_id)
  613. ->latest()
  614. ->simplePaginate($limit)
  615. ->pluck('status_id');
  616. $statuses = Status::findOrFail($favourites);
  617. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  618. $res = $this->fractal->createData($resource)->toArray();
  619. return response()->json($res);
  620. }
  621. /**
  622. * POST /api/v1/statuses/{id}/favourite
  623. *
  624. * @param integer $id
  625. *
  626. * @return \App\Transformer\Api\StatusTransformer
  627. */
  628. public function statusFavouriteById(Request $request, $id)
  629. {
  630. abort_if(!$request->user(), 403);
  631. $user = $request->user();
  632. $status = Status::findOrFail($id);
  633. $like = Like::firstOrCreate([
  634. 'profile_id' => $user->profile_id,
  635. 'status_id' => $status->id
  636. ]);
  637. if($like->wasRecentlyCreated == true) {
  638. LikePipeline::dispatch($like);
  639. }
  640. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  641. $res = $this->fractal->createData($resource)->toArray();
  642. return response()->json($res);
  643. }
  644. /**
  645. * POST /api/v1/statuses/{id}/unfavourite
  646. *
  647. * @param integer $id
  648. *
  649. * @return \App\Transformer\Api\StatusTransformer
  650. */
  651. public function statusUnfavouriteById(Request $request, $id)
  652. {
  653. abort_if(!$request->user(), 403);
  654. $user = $request->user();
  655. $status = Status::findOrFail($id);
  656. $like = Like::whereProfileId($user->profile_id)
  657. ->whereStatusId($status->id)
  658. ->first();
  659. if($like) {
  660. $like->delete();
  661. }
  662. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  663. $res = $this->fractal->createData($resource)->toArray();
  664. return response()->json($res);
  665. }
  666. /**
  667. * GET /api/v1/filters
  668. *
  669. * Return empty response since we filter server side
  670. *
  671. * @return array
  672. */
  673. public function accountFilters(Request $request)
  674. {
  675. abort_if(!$request->user(), 403);
  676. return response()->json([]);
  677. }
  678. /**
  679. * GET /api/v1/follow_requests
  680. *
  681. * Return array of Accounts that have sent follow requests
  682. *
  683. * @return \App\Transformer\Api\AccountTransformer
  684. */
  685. public function accountFollowRequests(Request $request)
  686. {
  687. abort_if(!$request->user(), 403);
  688. $user = $request->user();
  689. $followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
  690. $profiles = Profile::find($followRequests);
  691. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  692. $res = $this->fractal->createData($resource)->toArray();
  693. return response()->json($res);
  694. }
  695. /**
  696. * POST /api/v1/follow_requests/{id}/authorize
  697. *
  698. * @param integer $id
  699. *
  700. * @return null
  701. */
  702. public function accountFollowRequestAccept(Request $request, $id)
  703. {
  704. abort_if(!$request->user(), 403);
  705. // todo
  706. return response()->json([]);
  707. }
  708. /**
  709. * POST /api/v1/follow_requests/{id}/reject
  710. *
  711. * @param integer $id
  712. *
  713. * @return null
  714. */
  715. public function accountFollowRequestReject(Request $request, $id)
  716. {
  717. abort_if(!$request->user(), 403);
  718. // todo
  719. return response()->json([]);
  720. }
  721. /**
  722. * GET /api/v1/suggestions
  723. *
  724. * Return empty array as we don't support suggestions
  725. *
  726. * @return null
  727. */
  728. public function accountSuggestions(Request $request)
  729. {
  730. abort_if(!$request->user(), 403);
  731. // todo
  732. return response()->json([]);
  733. }
  734. /**
  735. * GET /api/v1/instance
  736. *
  737. * Information about the server.
  738. *
  739. * @return Instance
  740. */
  741. public function instance(Request $request)
  742. {
  743. $res = [
  744. 'description' => 'Pixelfed - Photo sharing for everyone',
  745. 'email' => config('instance.email'),
  746. 'languages' => ['en'],
  747. 'max_toot_chars' => config('pixelfed.max_caption_length'),
  748. 'registrations' => config('pixelfed.open_registration'),
  749. 'stats' => [
  750. 'user_count' => 0,
  751. 'status_count' => 0,
  752. 'domain_count' => 0
  753. ],
  754. 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
  755. 'title' => 'Pixelfed (' . config('pixelfed.domain.app') . ')',
  756. 'uri' => config('app.url'),
  757. 'urls' => [],
  758. 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')'
  759. ];
  760. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  761. }
  762. /**
  763. * GET /api/v1/lists
  764. *
  765. * Return empty array as we don't support lists
  766. *
  767. * @return null
  768. */
  769. public function accountLists(Request $request)
  770. {
  771. abort_if(!$request->user(), 403);
  772. return response()->json([]);
  773. }
  774. /**
  775. * GET /api/v1/accounts/{id}/lists
  776. *
  777. * @param integer $id
  778. *
  779. * @return null
  780. */
  781. public function accountListsById(Request $request, $id)
  782. {
  783. abort_if(!$request->user(), 403);
  784. return response()->json([]);
  785. }
  786. /**
  787. * POST /api/v1/media
  788. *
  789. *
  790. * @return App\Transformer\Api\MediaTransformer
  791. */
  792. public function mediaUpload(Request $request)
  793. {
  794. abort_if(!$request->user(), 403);
  795. $this->validate($request, [
  796. 'file.*' => function() {
  797. return [
  798. 'required',
  799. 'mimes:' . config('pixelfed.media_types'),
  800. 'max:' . config('pixelfed.max_photo_size'),
  801. ];
  802. },
  803. 'filter_name' => 'nullable|string|max:24',
  804. 'filter_class' => 'nullable|alpha_dash|max:24',
  805. 'description' => 'nullable|string|max:420'
  806. ]);
  807. $user = $request->user();
  808. $profile = $user->profile;
  809. if(config('pixelfed.enforce_account_limit') == true) {
  810. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  811. return Media::whereUserId($user->id)->sum('size') / 1000;
  812. });
  813. $limit = (int) config('pixelfed.max_account_size');
  814. if ($size >= $limit) {
  815. abort(403, 'Account size limit reached.');
  816. }
  817. }
  818. $monthHash = hash('sha1', date('Y').date('m'));
  819. $userHash = hash('sha1', $user->id . (string) $user->created_at);
  820. $photo = $request->file('file');
  821. $mimes = explode(',', config('pixelfed.media_types'));
  822. if(in_array($photo->getMimeType(), $mimes) == false) {
  823. abort(403, 'Invalid or unsupported mime type.');
  824. }
  825. $storagePath = "public/m/{$monthHash}/{$userHash}";
  826. $path = $photo->store($storagePath);
  827. $hash = \hash_file('sha256', $photo);
  828. $media = new Media();
  829. $media->status_id = null;
  830. $media->profile_id = $profile->id;
  831. $media->user_id = $user->id;
  832. $media->media_path = $path;
  833. $media->original_sha256 = $hash;
  834. $media->size = $photo->getSize();
  835. $media->mime = $photo->getMimeType();
  836. $media->caption = $request->input('description');
  837. $media->filter_class = $request->input('filter_class');
  838. $media->filter_name = $request->input('filter_name');
  839. $media->save();
  840. switch ($media->mime) {
  841. case 'image/jpeg':
  842. case 'image/png':
  843. ImageOptimize::dispatch($media);
  844. break;
  845. case 'video/mp4':
  846. VideoThumbnail::dispatch($media);
  847. $preview_url = '/storage/no-preview.png';
  848. $url = '/storage/no-preview.png';
  849. break;
  850. }
  851. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  852. $res = $this->fractal->createData($resource)->toArray();
  853. $res['preview_url'] = url('/storage/no-preview.png');
  854. $res['url'] = url('/storage/no-preview.png');
  855. return response()->json($res);
  856. }
  857. /**
  858. * PUT /api/v1/media/{id}
  859. *
  860. * @param integer $id
  861. *
  862. * @return App\Transformer\Api\MediaTransformer
  863. */
  864. public function mediaUpdate(Request $request, $id)
  865. {
  866. abort_if(!$request->user(), 403);
  867. $this->validate($request, [
  868. 'description' => 'nullable|string|max:420'
  869. ]);
  870. $user = $request->user();
  871. $media = Media::whereUserId($user->id)
  872. ->whereNull('status_id')
  873. ->findOrFail($id);
  874. $media->caption = $request->input('description');
  875. $media->save();
  876. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  877. $res = $this->fractal->createData($resource)->toArray();
  878. $res['preview_url'] = url('/storage/no-preview.png');
  879. $res['url'] = url('/storage/no-preview.png');
  880. return response()->json($res);
  881. }
  882. /**
  883. * GET /api/v1/mutes
  884. *
  885. *
  886. * @return App\Transformer\Api\AccountTransformer
  887. */
  888. public function accountMutes(Request $request)
  889. {
  890. abort_if(!$request->user(), 403);
  891. $this->validate($request, [
  892. 'limit' => 'nullable|integer|min:1|max:40'
  893. ]);
  894. $user = $request->user();
  895. $limit = $request->input('limit') ?? 40;
  896. $mutes = UserFilter::whereUserId($user->profile_id)
  897. ->whereFilterableType('App\Profile')
  898. ->whereFilterType('mute')
  899. ->simplePaginate($limit)
  900. ->pluck('filterable_id');
  901. $accounts = Profile::find($mutes);
  902. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  903. $res = $this->fractal->createData($resource)->toArray();
  904. return response()->json($res);
  905. }
  906. /**
  907. * POST /api/v1/accounts/{id}/mute
  908. *
  909. * @param integer $id
  910. *
  911. * @return App\Transformer\Api\RelationshipTransformer
  912. */
  913. public function accountMuteById(Request $request, $id)
  914. {
  915. abort_if(!$request->user(), 403);
  916. $user = $request->user();
  917. $pid = $user->profile_id;
  918. $account = Profile::findOrFail($id);
  919. $filter = UserFilter::firstOrCreate([
  920. 'user_id' => $pid,
  921. 'filterable_id' => $account->id,
  922. 'filterable_type' => 'App\Profile',
  923. 'filter_type' => 'mute',
  924. ]);
  925. Cache::forget("user:filter:list:$pid");
  926. Cache::forget("feature:discover:posts:$pid");
  927. Cache::forget("api:local:exp:rec:$pid");
  928. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  929. $res = $this->fractal->createData($resource)->toArray();
  930. return response()->json($res);
  931. }
  932. /**
  933. * POST /api/v1/accounts/{id}/unmute
  934. *
  935. * @param integer $id
  936. *
  937. * @return App\Transformer\Api\RelationshipTransformer
  938. */
  939. public function accountUnmuteById(Request $request, $id)
  940. {
  941. abort_if(!$request->user(), 403);
  942. $user = $request->user();
  943. $pid = $user->profile_id;
  944. $account = Profile::findOrFail($id);
  945. $filter = UserFilter::whereUserId($pid)
  946. ->whereFilterableId($account->id)
  947. ->whereFilterableType('App\Profile')
  948. ->whereFilterType('mute')
  949. ->first();
  950. if($filter) {
  951. $filter->delete();
  952. Cache::forget("user:filter:list:$pid");
  953. Cache::forget("feature:discover:posts:$pid");
  954. Cache::forget("api:local:exp:rec:$pid");
  955. }
  956. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  957. $res = $this->fractal->createData($resource)->toArray();
  958. return response()->json($res);
  959. }
  960. /**
  961. * GET /api/v1/notifications
  962. *
  963. *
  964. * @return App\Transformer\Api\NotificationTransformer
  965. */
  966. public function accountNotifications(Request $request)
  967. {
  968. abort_if(!$request->user(), 403);
  969. $this->validate($request, [
  970. 'page' => 'nullable|integer|min:1|max:10',
  971. 'limit' => 'nullable|integer|min:1|max:80',
  972. 'max_id' => 'nullable|integer|min:1',
  973. 'min_id' => 'nullable|integer|min:0',
  974. ]);
  975. $pid = $request->user()->profile_id;
  976. $limit = $request->input('limit') ?? 20;
  977. $timeago = now()->subMonths(6);
  978. $min = $request->input('min_id');
  979. $max = $request->input('max_id');
  980. if($min || $max) {
  981. $dir = $min ? '>' : '<';
  982. $id = $min ?? $max;
  983. $notifications = Notification::whereProfileId($pid)
  984. ->whereDate('created_at', '>', $timeago)
  985. ->latest()
  986. ->where('id', $dir, $id)
  987. ->limit($limit)
  988. ->get();
  989. } else {
  990. $notifications = Notification::whereProfileId($pid)
  991. ->whereDate('created_at', '>', $timeago)
  992. ->latest()
  993. ->simplePaginate($limit);
  994. }
  995. $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
  996. $res = $this->fractal->createData($resource)->toArray();
  997. return response()->json($res);
  998. }
  999. /**
  1000. * GET /api/v1/timelines/home
  1001. *
  1002. *
  1003. * @return App\Transformer\Api\StatusTransformer
  1004. */
  1005. public function timelineHome(Request $request)
  1006. {
  1007. abort_if(!$request->user(), 403);
  1008. $this->validate($request,[
  1009. 'page' => 'nullable|integer|max:40',
  1010. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1011. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1012. 'limit' => 'nullable|integer|max:80'
  1013. ]);
  1014. $page = $request->input('page');
  1015. $min = $request->input('min_id');
  1016. $max = $request->input('max_id');
  1017. $limit = $request->input('limit') ?? 3;
  1018. $pid = $request->user()->profile_id;
  1019. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1020. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1021. return $following->push($pid)->toArray();
  1022. });
  1023. if($min || $max) {
  1024. $dir = $min ? '>' : '<';
  1025. $id = $min ?? $max;
  1026. $timeline = Status::select(
  1027. 'id',
  1028. 'uri',
  1029. 'caption',
  1030. 'rendered',
  1031. 'profile_id',
  1032. 'type',
  1033. 'in_reply_to_id',
  1034. 'reblog_of_id',
  1035. 'is_nsfw',
  1036. 'scope',
  1037. 'local',
  1038. 'reply_count',
  1039. 'comments_disabled',
  1040. 'place_id',
  1041. 'created_at',
  1042. 'updated_at'
  1043. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1044. ->with('profile', 'hashtags', 'mentions')
  1045. ->where('id', $dir, $id)
  1046. ->whereIn('profile_id', $following)
  1047. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1048. ->latest()
  1049. ->limit($limit)
  1050. ->get();
  1051. } else {
  1052. $timeline = Status::select(
  1053. 'id',
  1054. 'uri',
  1055. 'caption',
  1056. 'rendered',
  1057. 'profile_id',
  1058. 'type',
  1059. 'in_reply_to_id',
  1060. 'reblog_of_id',
  1061. 'is_nsfw',
  1062. 'scope',
  1063. 'local',
  1064. 'reply_count',
  1065. 'comments_disabled',
  1066. 'place_id',
  1067. 'created_at',
  1068. 'updated_at'
  1069. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1070. ->with('profile', 'hashtags', 'mentions')
  1071. ->whereIn('profile_id', $following)
  1072. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1073. ->latest()
  1074. ->simplePaginate($limit);
  1075. }
  1076. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1077. $res = $this->fractal->createData($fractal)->toArray();
  1078. return response()->json($res);
  1079. }
  1080. /**
  1081. * GET /api/v1/conversations
  1082. *
  1083. * Not implemented
  1084. *
  1085. * @return array
  1086. */
  1087. public function conversations(Request $request)
  1088. {
  1089. abort_if(!$request->user(), 403);
  1090. return response()->json([]);
  1091. }
  1092. /**
  1093. * GET /api/v1/timelines/public
  1094. *
  1095. *
  1096. * @return App\Transformer\Api\StatusTransformer
  1097. */
  1098. public function timelinePublic(Request $request)
  1099. {
  1100. $this->validate($request,[
  1101. 'page' => 'nullable|integer|max:40',
  1102. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1103. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1104. 'limit' => 'nullable|integer|max:80'
  1105. ]);
  1106. $page = $request->input('page');
  1107. $min = $request->input('min_id');
  1108. $max = $request->input('max_id');
  1109. $limit = $request->input('limit') ?? 3;
  1110. if($min || $max) {
  1111. $dir = $min ? '>' : '<';
  1112. $id = $min ?? $max;
  1113. $timeline = Status::select(
  1114. 'id',
  1115. 'uri',
  1116. 'caption',
  1117. 'rendered',
  1118. 'profile_id',
  1119. 'type',
  1120. 'in_reply_to_id',
  1121. 'reblog_of_id',
  1122. 'is_nsfw',
  1123. 'scope',
  1124. 'local',
  1125. 'reply_count',
  1126. 'comments_disabled',
  1127. 'place_id',
  1128. 'created_at',
  1129. 'updated_at'
  1130. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1131. ->with('profile', 'hashtags', 'mentions')
  1132. ->where('id', $dir, $id)
  1133. ->whereVisibility('public')
  1134. ->latest()
  1135. ->limit($limit)
  1136. ->get();
  1137. } else {
  1138. $timeline = Status::select(
  1139. 'id',
  1140. 'uri',
  1141. 'caption',
  1142. 'rendered',
  1143. 'profile_id',
  1144. 'type',
  1145. 'in_reply_to_id',
  1146. 'reblog_of_id',
  1147. 'is_nsfw',
  1148. 'scope',
  1149. 'local',
  1150. 'reply_count',
  1151. 'comments_disabled',
  1152. 'place_id',
  1153. 'created_at',
  1154. 'updated_at'
  1155. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1156. ->with('profile', 'hashtags', 'mentions')
  1157. ->whereVisibility('public')
  1158. ->latest()
  1159. ->simplePaginate($limit);
  1160. }
  1161. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1162. $res = $this->fractal->createData($fractal)->toArray();
  1163. return response()->json($res);
  1164. }
  1165. public function statusById(Request $request, $id)
  1166. {
  1167. $status = Status::whereVisibility('public')->findOrFail($id);
  1168. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1169. $res = $this->fractal->createData($resource)->toArray();
  1170. return response()->json($res);
  1171. }
  1172. public function context(Request $request)
  1173. {
  1174. // todo
  1175. $res = [
  1176. 'ancestors' => [],
  1177. 'descendants' => []
  1178. ];
  1179. return response()->json($res);
  1180. }
  1181. public function createStatus(Request $request)
  1182. {
  1183. abort_if(!$request->user(), 403);
  1184. $this->validate($request, [
  1185. 'status' => 'string',
  1186. 'media_ids' => 'array',
  1187. 'media_ids.*' => 'integer|min:1',
  1188. 'sensitive' => 'nullable|boolean',
  1189. 'visibility' => 'string|in:private,unlisted,public',
  1190. 'in_reply_to_id' => 'integer'
  1191. ]);
  1192. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1193. abort(403, 'Empty statuses are not allowed');
  1194. }
  1195. }
  1196. }