ApiV1Controller.php 43 KB

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