ApiV1Controller.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  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. 'created_at',
  280. 'updated_at'
  281. )->whereProfileId($profile->id)
  282. ->whereIn('type', $scope)
  283. ->where('id', $dir, $id)
  284. ->whereIn('visibility', $visibility)
  285. ->latest()
  286. ->limit($limit)
  287. ->get();
  288. } else {
  289. $timeline = Status::select(
  290. 'id',
  291. 'uri',
  292. 'caption',
  293. 'rendered',
  294. 'profile_id',
  295. 'type',
  296. 'in_reply_to_id',
  297. 'reblog_of_id',
  298. 'is_nsfw',
  299. 'scope',
  300. 'local',
  301. 'place_id',
  302. 'created_at',
  303. 'updated_at'
  304. )->whereProfileId($profile->id)
  305. ->whereIn('type', $scope)
  306. ->whereIn('visibility', $visibility)
  307. ->latest()
  308. ->limit($limit)
  309. ->get();
  310. }
  311. $resource = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  312. $res = $this->fractal->createData($resource)->toArray();
  313. return response()->json($res);
  314. }
  315. /**
  316. * POST /api/v1/accounts/{id}/follow
  317. *
  318. * @param integer $id
  319. *
  320. * @return \App\Transformer\Api\RelationshipTransformer
  321. */
  322. public function accountFollowById(Request $request, $id)
  323. {
  324. abort_if(!$request->user(), 403);
  325. $user = $request->user();
  326. $target = Profile::where('id', '!=', $user->id)
  327. ->whereNull('status')
  328. ->findOrFail($item);
  329. $private = (bool) $target->is_private;
  330. $remote = (bool) $target->domain;
  331. $blocked = UserFilter::whereUserId($target->id)
  332. ->whereFilterType('block')
  333. ->whereFilterableId($user->id)
  334. ->whereFilterableType('App\Profile')
  335. ->exists();
  336. if($blocked == true) {
  337. abort(400, 'You cannot follow this user.');
  338. }
  339. $isFollowing = Follower::whereProfileId($user->id)
  340. ->whereFollowingId($target->id)
  341. ->exists();
  342. // Following already, return empty relationship
  343. if($isFollowing == true) {
  344. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  345. $res = $this->fractal->createData($resource)->toArray();
  346. return response()->json($res);
  347. }
  348. // Rate limits, max 7500 followers per account
  349. if($user->following()->count() >= Follower::MAX_FOLLOWING) {
  350. abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
  351. }
  352. // Rate limits, follow 30 accounts per hour max
  353. if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  354. abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  355. }
  356. if($private == true) {
  357. $follow = FollowRequest::firstOrCreate([
  358. 'follower_id' => $user->id,
  359. 'following_id' => $target->id
  360. ]);
  361. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  362. (new FollowerController())->sendFollow($user, $target);
  363. }
  364. } else {
  365. $follower = new Follower();
  366. $follower->profile_id = $user->id;
  367. $follower->following_id = $target->id;
  368. $follower->save();
  369. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  370. (new FollowerController())->sendFollow($user, $target);
  371. }
  372. FollowPipeline::dispatch($follower);
  373. }
  374. Cache::forget('profile:following:'.$target->id);
  375. Cache::forget('profile:followers:'.$target->id);
  376. Cache::forget('profile:following:'.$user->id);
  377. Cache::forget('profile:followers:'.$user->id);
  378. Cache::forget('api:local:exp:rec:'.$user->id);
  379. Cache::forget('user:account:id:'.$target->user_id);
  380. Cache::forget('user:account:id:'.$user->user_id);
  381. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  382. $res = $this->fractal->createData($resource)->toArray();
  383. return response()->json($res);
  384. }
  385. /**
  386. * POST /api/v1/accounts/{id}/unfollow
  387. *
  388. * @param integer $id
  389. *
  390. * @return \App\Transformer\Api\RelationshipTransformer
  391. */
  392. public function accountUnfollowById(Request $request, $id)
  393. {
  394. abort_if(!$request->user(), 403);
  395. $user = $request->user();
  396. $target = Profile::where('id', '!=', $user->id)
  397. ->whereNull('status')
  398. ->findOrFail($item);
  399. $private = (bool) $target->is_private;
  400. $remote = (bool) $target->domain;
  401. $isFollowing = Follower::whereProfileId($user->id)
  402. ->whereFollowingId($target->id)
  403. ->exists();
  404. if($isFollowing == false) {
  405. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  406. $res = $this->fractal->createData($resource)->toArray();
  407. return response()->json($res);
  408. }
  409. // Rate limits, follow 30 accounts per hour max
  410. if($user->following()->where('followers.updated_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
  411. abort(400, 'You can only follow or unfollow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
  412. }
  413. FollowRequest::whereFollowerId($user->id)
  414. ->whereFollowingId($target->id)
  415. ->delete();
  416. Follower::whereProfileId($user->id)
  417. ->whereFollowingId($target->id)
  418. ->delete();
  419. if($remote == true && config('federation.activitypub.remoteFollow') == true) {
  420. (new FollowerController())->sendUndoFollow($user, $target);
  421. }
  422. Cache::forget('profile:following:'.$target->id);
  423. Cache::forget('profile:followers:'.$target->id);
  424. Cache::forget('profile:following:'.$user->id);
  425. Cache::forget('profile:followers:'.$user->id);
  426. Cache::forget('api:local:exp:rec:'.$user->id);
  427. Cache::forget('user:account:id:'.$target->user_id);
  428. Cache::forget('user:account:id:'.$user->user_id);
  429. $resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
  430. $res = $this->fractal->createData($resource)->toArray();
  431. return response()->json($res);
  432. }
  433. /**
  434. * GET /api/v1/accounts/relationships
  435. *
  436. * @param array|integer $id
  437. *
  438. * @return \App\Transformer\Api\RelationshipTransformer
  439. */
  440. public function accountRelationshipsById(Request $request)
  441. {
  442. abort_if(!$request->user(), 403);
  443. $this->validate($request, [
  444. 'id' => 'required|array|min:1|max:20',
  445. 'id.*' => 'required|integer|min:1|max:' . PHP_INT_MAX
  446. ]);
  447. $pid = $request->user()->profile_id ?? $request->user()->profile->id;
  448. $ids = collect($request->input('id'));
  449. $filtered = $ids->filter(function($v) use($pid) {
  450. return $v != $pid;
  451. });
  452. $relations = Profile::whereNull('status')->findOrFail($filtered->values());
  453. $fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
  454. $res = $this->fractal->createData($fractal)->toArray();
  455. return response()->json($res);
  456. }
  457. /**
  458. * GET /api/v1/accounts/search
  459. *
  460. *
  461. *
  462. * @return \App\Transformer\Api\AccountTransformer
  463. */
  464. public function accountSearch(Request $request)
  465. {
  466. abort_if(!$request->user(), 403);
  467. $this->validate($request, [
  468. 'q' => 'required|string|min:1|max:255',
  469. 'limit' => 'nullable|integer|min:1|max:40',
  470. 'resolve' => 'nullable'
  471. ]);
  472. $user = $request->user();
  473. $query = $request->input('q');
  474. $limit = $request->input('limit') ?? 20;
  475. $resolve = (bool) $request->input('resolve', false);
  476. $q = '%' . $query . '%';
  477. $profiles = Profile::whereNull('status')
  478. ->where('username', 'like', $q)
  479. ->orWhere('name', 'like', $q)
  480. ->limit($limit)
  481. ->get();
  482. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  483. $res = $this->fractal->createData($resource)->toArray();
  484. return response()->json($res);
  485. }
  486. /**
  487. * GET /api/v1/blocks
  488. *
  489. *
  490. *
  491. * @return \App\Transformer\Api\AccountTransformer
  492. */
  493. public function accountBlocks(Request $request)
  494. {
  495. abort_if(!$request->user(), 403);
  496. $this->validate($request, [
  497. 'limit' => 'nullable|integer|min:1|max:40',
  498. 'page' => 'nullable|integer|min:1|max:10'
  499. ]);
  500. $user = $request->user();
  501. $limit = $request->input('limit') ?? 40;
  502. $blocked = UserFilter::select('filterable_id','filterable_type','filter_type','user_id')
  503. ->whereUserId($user->profile_id)
  504. ->whereFilterableType('App\Profile')
  505. ->whereFilterType('block')
  506. ->simplePaginate($limit)
  507. ->pluck('filterable_id');
  508. $profiles = Profile::findOrFail($blocked);
  509. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  510. $res = $this->fractal->createData($resource)->toArray();
  511. return response()->json($res);
  512. }
  513. /**
  514. * POST /api/v1/accounts/{id}/block
  515. *
  516. * @param integer $id
  517. *
  518. * @return \App\Transformer\Api\RelationshipTransformer
  519. */
  520. public function accountBlockById(Request $request, $id)
  521. {
  522. abort_if(!$request->user(), 403);
  523. $user = $request->user();
  524. $pid = $user->profile_id ?? $user->profile->id;
  525. if($id == $pid) {
  526. abort(400, 'You cannot block yourself');
  527. }
  528. $profile = Profile::findOrFail($id);
  529. Follower::whereProfileId($profile->id)->whereFollowingId($pid)->delete();
  530. Follower::whereProfileId($pid)->whereFollowingId($profile->id)->delete();
  531. Notification::whereProfileId($pid)->whereActorId($profile->id)->delete();
  532. $filter = UserFilter::firstOrCreate([
  533. 'user_id' => $pid,
  534. 'filterable_id' => $profile->id,
  535. 'filterable_type' => 'App\Profile',
  536. 'filter_type' => 'block',
  537. ]);
  538. Cache::forget("user:filter:list:$pid");
  539. Cache::forget("api:local:exp:rec:$pid");
  540. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  541. $res = $this->fractal->createData($resource)->toArray();
  542. return response()->json($res);
  543. }
  544. /**
  545. * POST /api/v1/accounts/{id}/unblock
  546. *
  547. * @param integer $id
  548. *
  549. * @return \App\Transformer\Api\RelationshipTransformer
  550. */
  551. public function accountUnblockById(Request $request, $id)
  552. {
  553. abort_if(!$request->user(), 403);
  554. $user = $request->user();
  555. $pid = $user->profile_id ?? $user->profile->id;
  556. if($id == $pid) {
  557. abort(400, 'You cannot unblock yourself');
  558. }
  559. $profile = Profile::findOrFail($id);
  560. UserFilter::whereUserId($pid)
  561. ->whereFilterableId($profile->id)
  562. ->whereFilterableType('App\Profile')
  563. ->whereFilterType('block')
  564. ->delete();
  565. Cache::forget("user:filter:list:$pid");
  566. Cache::forget("api:local:exp:rec:$pid");
  567. $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
  568. $res = $this->fractal->createData($resource)->toArray();
  569. return response()->json($res);
  570. }
  571. /**
  572. * GET /api/v1/custom_emojis
  573. *
  574. * Return empty array, we don't support custom emoji
  575. *
  576. * @return array
  577. */
  578. public function customEmojis()
  579. {
  580. return response()->json([]);
  581. }
  582. /**
  583. * GET /api/v1/domain_blocks
  584. *
  585. * Return empty array
  586. *
  587. * @return array
  588. */
  589. public function accountDomainBlocks(Request $request)
  590. {
  591. abort_if(!$request->user(), 403);
  592. return response()->json([]);
  593. }
  594. /**
  595. * GET /api/v1/endorsements
  596. *
  597. * Return empty array
  598. *
  599. * @return array
  600. */
  601. public function accountEndorsements(Request $request)
  602. {
  603. abort_if(!$request->user(), 403);
  604. return response()->json([]);
  605. }
  606. /**
  607. * GET /api/v1/favourites
  608. *
  609. * Returns collection of liked statuses
  610. *
  611. * @return \App\Transformer\Api\StatusTransformer
  612. */
  613. public function accountFavourites(Request $request)
  614. {
  615. abort_if(!$request->user(), 403);
  616. $user = $request->user();
  617. $limit = $request->input('limit') ?? 20;
  618. $favourites = Like::whereProfileId($user->profile_id)
  619. ->latest()
  620. ->simplePaginate($limit)
  621. ->pluck('status_id');
  622. $statuses = Status::findOrFail($favourites);
  623. $resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
  624. $res = $this->fractal->createData($resource)->toArray();
  625. return response()->json($res);
  626. }
  627. /**
  628. * POST /api/v1/statuses/{id}/favourite
  629. *
  630. * @param integer $id
  631. *
  632. * @return \App\Transformer\Api\StatusTransformer
  633. */
  634. public function statusFavouriteById(Request $request, $id)
  635. {
  636. abort_if(!$request->user(), 403);
  637. $user = $request->user();
  638. $status = Status::findOrFail($id);
  639. $like = Like::firstOrCreate([
  640. 'profile_id' => $user->profile_id,
  641. 'status_id' => $status->id
  642. ]);
  643. if($like->wasRecentlyCreated == true) {
  644. $status->likes_count = $status->likes()->count();
  645. $status->save();
  646. LikePipeline::dispatch($like);
  647. }
  648. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  649. $res = $this->fractal->createData($resource)->toArray();
  650. return response()->json($res);
  651. }
  652. /**
  653. * POST /api/v1/statuses/{id}/unfavourite
  654. *
  655. * @param integer $id
  656. *
  657. * @return \App\Transformer\Api\StatusTransformer
  658. */
  659. public function statusUnfavouriteById(Request $request, $id)
  660. {
  661. abort_if(!$request->user(), 403);
  662. $user = $request->user();
  663. $status = Status::findOrFail($id);
  664. $like = Like::whereProfileId($user->profile_id)
  665. ->whereStatusId($status->id)
  666. ->first();
  667. if($like) {
  668. $like->delete();
  669. $status->likes_count = $status->likes()->count();
  670. $status->save();
  671. }
  672. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  673. $res = $this->fractal->createData($resource)->toArray();
  674. return response()->json($res);
  675. }
  676. /**
  677. * GET /api/v1/filters
  678. *
  679. * Return empty response since we filter server side
  680. *
  681. * @return array
  682. */
  683. public function accountFilters(Request $request)
  684. {
  685. abort_if(!$request->user(), 403);
  686. return response()->json([]);
  687. }
  688. /**
  689. * GET /api/v1/follow_requests
  690. *
  691. * Return array of Accounts that have sent follow requests
  692. *
  693. * @return \App\Transformer\Api\AccountTransformer
  694. */
  695. public function accountFollowRequests(Request $request)
  696. {
  697. abort_if(!$request->user(), 403);
  698. $user = $request->user();
  699. $followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
  700. $profiles = Profile::find($followRequests);
  701. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  702. $res = $this->fractal->createData($resource)->toArray();
  703. return response()->json($res);
  704. }
  705. /**
  706. * POST /api/v1/follow_requests/{id}/authorize
  707. *
  708. * @param integer $id
  709. *
  710. * @return null
  711. */
  712. public function accountFollowRequestAccept(Request $request, $id)
  713. {
  714. abort_if(!$request->user(), 403);
  715. // todo
  716. return response()->json([]);
  717. }
  718. /**
  719. * POST /api/v1/follow_requests/{id}/reject
  720. *
  721. * @param integer $id
  722. *
  723. * @return null
  724. */
  725. public function accountFollowRequestReject(Request $request, $id)
  726. {
  727. abort_if(!$request->user(), 403);
  728. // todo
  729. return response()->json([]);
  730. }
  731. /**
  732. * GET /api/v1/suggestions
  733. *
  734. * Return empty array as we don't support suggestions
  735. *
  736. * @return null
  737. */
  738. public function accountSuggestions(Request $request)
  739. {
  740. abort_if(!$request->user(), 403);
  741. // todo
  742. return response()->json([]);
  743. }
  744. /**
  745. * GET /api/v1/instance
  746. *
  747. * Information about the server.
  748. *
  749. * @return Instance
  750. */
  751. public function instance(Request $request)
  752. {
  753. $res = [
  754. 'description' => 'Pixelfed - Photo sharing for everyone',
  755. 'email' => config('instance.email'),
  756. 'languages' => ['en'],
  757. 'max_toot_chars' => config('pixelfed.max_caption_length'),
  758. 'registrations' => config('pixelfed.open_registration'),
  759. 'stats' => [
  760. 'user_count' => 0,
  761. 'status_count' => 0,
  762. 'domain_count' => 0
  763. ],
  764. 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
  765. 'title' => 'Pixelfed (' . config('pixelfed.domain.app') . ')',
  766. 'uri' => config('app.url'),
  767. 'urls' => [],
  768. 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')'
  769. ];
  770. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  771. }
  772. /**
  773. * GET /api/v1/lists
  774. *
  775. * Return empty array as we don't support lists
  776. *
  777. * @return null
  778. */
  779. public function accountLists(Request $request)
  780. {
  781. abort_if(!$request->user(), 403);
  782. return response()->json([]);
  783. }
  784. /**
  785. * GET /api/v1/accounts/{id}/lists
  786. *
  787. * @param integer $id
  788. *
  789. * @return null
  790. */
  791. public function accountListsById(Request $request, $id)
  792. {
  793. abort_if(!$request->user(), 403);
  794. return response()->json([]);
  795. }
  796. /**
  797. * POST /api/v1/media
  798. *
  799. *
  800. * @return MediaTransformer
  801. */
  802. public function mediaUpload(Request $request)
  803. {
  804. abort_if(!$request->user(), 403);
  805. $this->validate($request, [
  806. 'file.*' => function() {
  807. return [
  808. 'required',
  809. 'mimes:' . config('pixelfed.media_types'),
  810. 'max:' . config('pixelfed.max_photo_size'),
  811. ];
  812. },
  813. 'filter_name' => 'nullable|string|max:24',
  814. 'filter_class' => 'nullable|alpha_dash|max:24',
  815. 'description' => 'nullable|string|max:420'
  816. ]);
  817. $user = $request->user();
  818. $profile = $user->profile;
  819. if(config('pixelfed.enforce_account_limit') == true) {
  820. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  821. return Media::whereUserId($user->id)->sum('size') / 1000;
  822. });
  823. $limit = (int) config('pixelfed.max_account_size');
  824. if ($size >= $limit) {
  825. abort(403, 'Account size limit reached.');
  826. }
  827. }
  828. $monthHash = hash('sha1', date('Y').date('m'));
  829. $userHash = hash('sha1', $user->id . (string) $user->created_at);
  830. $photo = $request->file('file');
  831. $mimes = explode(',', config('pixelfed.media_types'));
  832. if(in_array($photo->getMimeType(), $mimes) == false) {
  833. abort(403, 'Invalid or unsupported mime type.');
  834. }
  835. $storagePath = "public/m/{$monthHash}/{$userHash}";
  836. $path = $photo->store($storagePath);
  837. $hash = \hash_file('sha256', $photo);
  838. $media = new Media();
  839. $media->status_id = null;
  840. $media->profile_id = $profile->id;
  841. $media->user_id = $user->id;
  842. $media->media_path = $path;
  843. $media->original_sha256 = $hash;
  844. $media->size = $photo->getSize();
  845. $media->mime = $photo->getMimeType();
  846. $media->caption = $request->input('description');
  847. $media->filter_class = $request->input('filter_class');
  848. $media->filter_name = $request->input('filter_name');
  849. $media->save();
  850. switch ($media->mime) {
  851. case 'image/jpeg':
  852. case 'image/png':
  853. ImageOptimize::dispatch($media);
  854. break;
  855. case 'video/mp4':
  856. VideoThumbnail::dispatch($media);
  857. $preview_url = '/storage/no-preview.png';
  858. $url = '/storage/no-preview.png';
  859. break;
  860. }
  861. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  862. $res = $this->fractal->createData($resource)->toArray();
  863. $res['preview_url'] = url('/storage/no-preview.png');
  864. $res['url'] = url('/storage/no-preview.png');
  865. return response()->json($res);
  866. }
  867. /**
  868. * PUT /api/v1/media/{id}
  869. *
  870. * @param integer $id
  871. *
  872. * @return MediaTransformer
  873. */
  874. public function mediaUpdate(Request $request, $id)
  875. {
  876. abort_if(!$request->user(), 403);
  877. $this->validate($request, [
  878. 'description' => 'nullable|string|max:420'
  879. ]);
  880. $user = $request->user();
  881. $media = Media::whereUserId($user->id)
  882. ->whereNull('status_id')
  883. ->findOrFail($id);
  884. $media->caption = $request->input('description');
  885. $media->save();
  886. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  887. $res = $this->fractal->createData($resource)->toArray();
  888. $res['preview_url'] = url('/storage/no-preview.png');
  889. $res['url'] = url('/storage/no-preview.png');
  890. return response()->json($res);
  891. }
  892. /**
  893. * GET /api/v1/mutes
  894. *
  895. *
  896. * @return AccountTransformer
  897. */
  898. public function accountMutes(Request $request)
  899. {
  900. abort_if(!$request->user(), 403);
  901. $this->validate($request, [
  902. 'limit' => 'nullable|integer|min:1|max:40'
  903. ]);
  904. $user = $request->user();
  905. $limit = $request->input('limit') ?? 40;
  906. $mutes = UserFilter::whereUserId($user->profile_id)
  907. ->whereFilterableType('App\Profile')
  908. ->whereFilterType('mute')
  909. ->simplePaginate($limit)
  910. ->pluck('filterable_id');
  911. $accounts = Profile::find($mutes);
  912. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  913. $res = $this->fractal->createData($resource)->toArray();
  914. return response()->json($res);
  915. }
  916. /**
  917. * POST /api/v1/accounts/{id}/mute
  918. *
  919. * @param integer $id
  920. *
  921. * @return RelationshipTransformer
  922. */
  923. public function accountMuteById(Request $request, $id)
  924. {
  925. abort_if(!$request->user(), 403);
  926. $user = $request->user();
  927. $pid = $user->profile_id;
  928. $account = Profile::findOrFail($id);
  929. $filter = UserFilter::firstOrCreate([
  930. 'user_id' => $pid,
  931. 'filterable_id' => $account->id,
  932. 'filterable_type' => 'App\Profile',
  933. 'filter_type' => 'mute',
  934. ]);
  935. Cache::forget("user:filter:list:$pid");
  936. Cache::forget("feature:discover:posts:$pid");
  937. Cache::forget("api:local:exp:rec:$pid");
  938. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  939. $res = $this->fractal->createData($resource)->toArray();
  940. return response()->json($res);
  941. }
  942. /**
  943. * POST /api/v1/accounts/{id}/unmute
  944. *
  945. * @param integer $id
  946. *
  947. * @return RelationshipTransformer
  948. */
  949. public function accountUnmuteById(Request $request, $id)
  950. {
  951. abort_if(!$request->user(), 403);
  952. $user = $request->user();
  953. $pid = $user->profile_id;
  954. $account = Profile::findOrFail($id);
  955. $filter = UserFilter::whereUserId($pid)
  956. ->whereFilterableId($account->id)
  957. ->whereFilterableType('App\Profile')
  958. ->whereFilterType('mute')
  959. ->first();
  960. if($filter) {
  961. $filter->delete();
  962. Cache::forget("user:filter:list:$pid");
  963. Cache::forget("feature:discover:posts:$pid");
  964. Cache::forget("api:local:exp:rec:$pid");
  965. }
  966. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  967. $res = $this->fractal->createData($resource)->toArray();
  968. return response()->json($res);
  969. }
  970. /**
  971. * GET /api/v1/notifications
  972. *
  973. *
  974. * @return NotificationTransformer
  975. */
  976. public function accountNotifications(Request $request)
  977. {
  978. abort_if(!$request->user(), 403);
  979. $this->validate($request, [
  980. 'page' => 'nullable|integer|min:1|max:10',
  981. 'limit' => 'nullable|integer|min:1|max:80',
  982. 'max_id' => 'nullable|integer|min:1',
  983. 'min_id' => 'nullable|integer|min:0',
  984. ]);
  985. $pid = $request->user()->profile_id;
  986. $limit = $request->input('limit') ?? 20;
  987. $timeago = now()->subMonths(6);
  988. $min = $request->input('min_id');
  989. $max = $request->input('max_id');
  990. if($min || $max) {
  991. $dir = $min ? '>' : '<';
  992. $id = $min ?? $max;
  993. $notifications = Notification::whereProfileId($pid)
  994. ->whereDate('created_at', '>', $timeago)
  995. ->latest()
  996. ->where('id', $dir, $id)
  997. ->limit($limit)
  998. ->get();
  999. } else {
  1000. $notifications = Notification::whereProfileId($pid)
  1001. ->whereDate('created_at', '>', $timeago)
  1002. ->latest()
  1003. ->simplePaginate($limit);
  1004. }
  1005. $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
  1006. $res = $this->fractal->createData($resource)->toArray();
  1007. return response()->json($res);
  1008. }
  1009. /**
  1010. * GET /api/v1/timelines/home
  1011. *
  1012. *
  1013. * @return StatusTransformer
  1014. */
  1015. public function timelineHome(Request $request)
  1016. {
  1017. abort_if(!$request->user(), 403);
  1018. $this->validate($request,[
  1019. 'page' => 'nullable|integer|max:40',
  1020. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1021. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1022. 'limit' => 'nullable|integer|max:80'
  1023. ]);
  1024. $page = $request->input('page');
  1025. $min = $request->input('min_id');
  1026. $max = $request->input('max_id');
  1027. $limit = $request->input('limit') ?? 3;
  1028. $pid = $request->user()->profile_id;
  1029. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1030. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1031. return $following->push($pid)->toArray();
  1032. });
  1033. if($min || $max) {
  1034. $dir = $min ? '>' : '<';
  1035. $id = $min ?? $max;
  1036. $timeline = Status::select(
  1037. 'id',
  1038. 'uri',
  1039. 'caption',
  1040. 'rendered',
  1041. 'profile_id',
  1042. 'type',
  1043. 'in_reply_to_id',
  1044. 'reblog_of_id',
  1045. 'is_nsfw',
  1046. 'scope',
  1047. 'local',
  1048. 'reply_count',
  1049. 'comments_disabled',
  1050. 'place_id',
  1051. 'created_at',
  1052. 'updated_at'
  1053. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1054. ->with('profile', 'hashtags', 'mentions')
  1055. ->where('id', $dir, $id)
  1056. ->whereIn('profile_id', $following)
  1057. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1058. ->latest()
  1059. ->limit($limit)
  1060. ->get();
  1061. } else {
  1062. $timeline = Status::select(
  1063. 'id',
  1064. 'uri',
  1065. 'caption',
  1066. 'rendered',
  1067. 'profile_id',
  1068. 'type',
  1069. 'in_reply_to_id',
  1070. 'reblog_of_id',
  1071. 'is_nsfw',
  1072. 'scope',
  1073. 'local',
  1074. 'reply_count',
  1075. 'comments_disabled',
  1076. 'place_id',
  1077. 'created_at',
  1078. 'updated_at'
  1079. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1080. ->with('profile', 'hashtags', 'mentions')
  1081. ->whereIn('profile_id', $following)
  1082. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1083. ->latest()
  1084. ->simplePaginate($limit);
  1085. }
  1086. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1087. $res = $this->fractal->createData($fractal)->toArray();
  1088. return response()->json($res);
  1089. }
  1090. /**
  1091. * GET /api/v1/conversations
  1092. *
  1093. * Not implemented
  1094. *
  1095. * @return array
  1096. */
  1097. public function conversations(Request $request)
  1098. {
  1099. abort_if(!$request->user(), 403);
  1100. return response()->json([]);
  1101. }
  1102. /**
  1103. * GET /api/v1/timelines/public
  1104. *
  1105. *
  1106. * @return StatusTransformer
  1107. */
  1108. public function timelinePublic(Request $request)
  1109. {
  1110. $this->validate($request,[
  1111. 'page' => 'nullable|integer|max:40',
  1112. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1113. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1114. 'limit' => 'nullable|integer|max:80'
  1115. ]);
  1116. $page = $request->input('page');
  1117. $min = $request->input('min_id');
  1118. $max = $request->input('max_id');
  1119. $limit = $request->input('limit') ?? 3;
  1120. if($min || $max) {
  1121. $dir = $min ? '>' : '<';
  1122. $id = $min ?? $max;
  1123. $timeline = Status::select(
  1124. 'id',
  1125. 'uri',
  1126. 'caption',
  1127. 'rendered',
  1128. 'profile_id',
  1129. 'type',
  1130. 'in_reply_to_id',
  1131. 'reblog_of_id',
  1132. 'is_nsfw',
  1133. 'scope',
  1134. 'local',
  1135. 'reply_count',
  1136. 'comments_disabled',
  1137. 'place_id',
  1138. 'created_at',
  1139. 'updated_at'
  1140. )->whereNull('uri')
  1141. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1142. ->with('profile', 'hashtags', 'mentions')
  1143. ->where('id', $dir, $id)
  1144. ->whereVisibility('public')
  1145. ->latest()
  1146. ->limit($limit)
  1147. ->get();
  1148. } else {
  1149. $timeline = Status::select(
  1150. 'id',
  1151. 'uri',
  1152. 'caption',
  1153. 'rendered',
  1154. 'profile_id',
  1155. 'type',
  1156. 'in_reply_to_id',
  1157. 'reblog_of_id',
  1158. 'is_nsfw',
  1159. 'scope',
  1160. 'local',
  1161. 'reply_count',
  1162. 'comments_disabled',
  1163. 'place_id',
  1164. 'created_at',
  1165. 'updated_at'
  1166. )->whereNull('uri')
  1167. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1168. ->with('profile', 'hashtags', 'mentions')
  1169. ->whereVisibility('public')
  1170. ->latest()
  1171. ->simplePaginate($limit);
  1172. }
  1173. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1174. $res = $this->fractal->createData($fractal)->toArray();
  1175. return response()->json($res);
  1176. }
  1177. /**
  1178. * GET /api/v1/statuses/{id}
  1179. *
  1180. * @param integer $id
  1181. *
  1182. * @return StatusTransformer
  1183. */
  1184. public function statusById(Request $request, $id)
  1185. {
  1186. abort_if(!$request->user(), 403);
  1187. $status = Status::whereVisibility('public')->findOrFail($id);
  1188. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1189. $res = $this->fractal->createData($resource)->toArray();
  1190. return response()->json($res);
  1191. }
  1192. /**
  1193. * GET /api/v1/statuses/{id}/context
  1194. *
  1195. * @param integer $id
  1196. *
  1197. * @return StatusTransformer
  1198. */
  1199. public function statusContext(Request $request, $id)
  1200. {
  1201. abort_if(!$request->user(), 403);
  1202. $status = Status::whereVisibility('public')->findOrFail($id);
  1203. // Return empty response since we don't handle threading like this
  1204. $res = [
  1205. 'ancestors' => [],
  1206. 'descendants' => []
  1207. ];
  1208. return response()->json($res);
  1209. }
  1210. /**
  1211. * GET /api/v1/statuses/{id}/card
  1212. *
  1213. * @param integer $id
  1214. *
  1215. * @return StatusTransformer
  1216. */
  1217. public function statusCard(Request $request, $id)
  1218. {
  1219. abort_if(!$request->user(), 403);
  1220. $status = Status::whereVisibility('public')->findOrFail($id);
  1221. // Return empty response since we don't handle support cards
  1222. $res = [];
  1223. return response()->json($res);
  1224. }
  1225. /**
  1226. * GET /api/v1/statuses/{id}/reblogged_by
  1227. *
  1228. * @param integer $id
  1229. *
  1230. * @return AccountTransformer
  1231. */
  1232. public function statusRebloggedBy(Request $request, $id)
  1233. {
  1234. abort_if(!$request->user(), 403);
  1235. $this->validate($request, [
  1236. 'limit' => 'nullable|integer|min:1|max:80'
  1237. ]);
  1238. $limit = $request->input('limit') ?? 40;
  1239. $status = Status::whereVisibility('public')->findOrFail($id);
  1240. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1241. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1242. $res = $this->fractal->createData($resource)->toArray();
  1243. return response()->json($res);
  1244. }
  1245. /**
  1246. * GET /api/v1/statuses/{id}/favourited_by
  1247. *
  1248. * @param integer $id
  1249. *
  1250. * @return AccountTransformer
  1251. */
  1252. public function statusFavouritedBy(Request $request, $id)
  1253. {
  1254. abort_if(!$request->user(), 403);
  1255. $this->validate($request, [
  1256. 'limit' => 'nullable|integer|min:1|max:80'
  1257. ]);
  1258. $limit = $request->input('limit') ?? 40;
  1259. $status = Status::whereVisibility('public')->findOrFail($id);
  1260. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1261. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1262. $res = $this->fractal->createData($resource)->toArray();
  1263. return response()->json($res);
  1264. }
  1265. /**
  1266. * POST /api/v1/statuses
  1267. *
  1268. *
  1269. * @return StatusTransformer
  1270. */
  1271. public function statusCreate(Request $request)
  1272. {
  1273. abort_if(!$request->user(), 403);
  1274. $this->validate($request, [
  1275. 'status' => 'nullable|string',
  1276. 'in_reply_to_id' => 'nullable|integer',
  1277. 'media_ids' => 'array|max:' . config('pixelfed.max_album_length'),
  1278. 'media_ids.*' => 'integer|min:1',
  1279. 'sensitive' => 'nullable|boolean',
  1280. 'visibility' => 'string|in:private,unlisted,public',
  1281. ]);
  1282. if(config('costar.enabled') == true) {
  1283. $blockedKeywords = config('costar.keyword.block');
  1284. if($blockedKeywords !== null && $request->status) {
  1285. $keywords = config('costar.keyword.block');
  1286. foreach($keywords as $kw) {
  1287. if(Str::contains($request->status, $kw) == true) {
  1288. abort(400, 'Invalid object. Contains banned keyword.');
  1289. }
  1290. }
  1291. }
  1292. }
  1293. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1294. abort(403, 'Empty statuses are not allowed');
  1295. }
  1296. $ids = $request->input('media_ids');
  1297. $in_reply_to_id = $request->input('in_reply_to_id');
  1298. $user = $request->user();
  1299. if($in_reply_to_id) {
  1300. $parent = Status::findOrFail($in_reply_to_id);
  1301. $status = new Status;
  1302. $status->caption = strip_tags($request->input('status'));
  1303. $status->scope = $request->input('visibility');
  1304. $status->visibility = $request->input('visibility');
  1305. $status->profile_id = $user->profile_id;
  1306. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1307. $status->in_reply_to_id = $parent->id;
  1308. $status->in_reply_to_profile_id = $parent->profile_id;
  1309. $status->save();
  1310. } else if($ids) {
  1311. $status = new Status;
  1312. $status->caption = strip_tags($request->input('status'));
  1313. $status->profile_id = $user->profile_id;
  1314. $status->scope = 'draft';
  1315. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1316. $status->save();
  1317. $mimes = [];
  1318. foreach($ids as $k => $v) {
  1319. if($k + 1 > config('pixelfed.max_album_length')) {
  1320. continue;
  1321. }
  1322. $m = Media::findOrFail($v);
  1323. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1324. abort(403, 'Invalid media id');
  1325. }
  1326. $m->status_id = $status->id;
  1327. $m->save();
  1328. array_push($mimes, $m->mime);
  1329. }
  1330. if(empty($mimes)) {
  1331. $status->delete();
  1332. abort(500, 'Invalid media ids');
  1333. }
  1334. $status->scope = $request->input('visibility');
  1335. $status->visibility = $request->input('visibility');
  1336. $status->type = StatusController::mimeTypeCheck($mimes);
  1337. $status->save();
  1338. }
  1339. if(!$status) {
  1340. $oops = 'An error occured. RefId: '.time().'-'.$user->profile_id.':'.Str::random(5).':'.Str::random(10);
  1341. abort(500, $oops);
  1342. }
  1343. NewStatusPipeline::dispatch($status);
  1344. Cache::forget('user:account:id:'.$user->id);
  1345. Cache::forget('profile:status_count:'.$user->profile_id);
  1346. Cache::forget($user->storageUsedKey());
  1347. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1348. $res = $this->fractal->createData($resource)->toArray();
  1349. return response()->json($res);
  1350. }
  1351. /**
  1352. * DELETE /api/v1/statuses
  1353. *
  1354. * @param integer $id
  1355. *
  1356. * @return null
  1357. */
  1358. public function statusDelete(Request $request, $id)
  1359. {
  1360. abort_if(!$request->user(), 403);
  1361. $status = Status::whereProfileId($request->user()->profile->id)
  1362. ->findOrFail($id);
  1363. Cache::forget('profile:status_count:'.$status->profile_id);
  1364. StatusDelete::dispatch($status);
  1365. return response()->json(['Status successfully deleted.']);
  1366. }
  1367. /**
  1368. * POST /api/v1/statuses/{id}/reblog
  1369. *
  1370. * @param integer $id
  1371. *
  1372. * @return StatusTransformer
  1373. */
  1374. public function statusShare(Request $request, $id)
  1375. {
  1376. abort_if(!$request->user(), 403);
  1377. $user = $request->user();
  1378. $status = Status::findOrFail($id);
  1379. $share = Status::firstOrCreate([
  1380. 'profile_id' => $user->profile_id,
  1381. 'reblog_of_id' => $status->id,
  1382. 'in_reply_to_profile_id' => $status->profile_id
  1383. ]);
  1384. if($share->wasRecentlyCreated == true) {
  1385. SharePipeline::dispatch($share);
  1386. }
  1387. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1388. $res = $this->fractal->createData($resource)->toArray();
  1389. return response()->json($res);
  1390. }
  1391. /**
  1392. * POST /api/v1/statuses/{id}/unreblog
  1393. *
  1394. * @param integer $id
  1395. *
  1396. * @return StatusTransformer
  1397. */
  1398. public function statusUnshare(Request $request, $id)
  1399. {
  1400. abort_if(!$request->user(), 403);
  1401. $user = $request->user();
  1402. $status = Status::findOrFail($id);
  1403. Status::whereProfileId($user->profile_id)
  1404. ->whereReblogOfId($status->id)
  1405. ->delete();
  1406. $count = $status->reblogs_count;
  1407. $status->reblogs_count = $count > 0 ? $count - 1 : 0;
  1408. $status->save();
  1409. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1410. $res = $this->fractal->createData($resource)->toArray();
  1411. return response()->json($res);
  1412. }
  1413. /**
  1414. * GET /api/v1/timelines/tag/{hashtag}
  1415. *
  1416. * @param string $hashtag
  1417. *
  1418. * @return StatusTransformer
  1419. */
  1420. public function timelineHashtag(Request $request, $hashtag)
  1421. {
  1422. abort_if(!$request->user(), 403);
  1423. // todo
  1424. $res = [];
  1425. return response()->json($res);
  1426. }
  1427. }