ApiV1Controller.php 64 KB

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