ApiV1Controller.php 39 KB

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