ApiV1Controller.php 63 KB

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