ApiV1Controller.php 64 KB

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