ApiV1Controller.php 58 KB

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