ApiV1Controller.php 54 KB

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