ApiV1Controller.php 43 KB

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