1
0

ApiV1Controller.php 59 KB

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