ApiV1Controller.php 64 KB

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