ApiV1Controller.php 53 KB

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