ApiV1Controller.php 62 KB

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