ApiV1Controller.php 63 KB

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