ApiV1Controller.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. $pid = $request->user()->profile_id;
  1123. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1124. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1125. return $following->push($pid)->toArray();
  1126. });
  1127. if($min || $max) {
  1128. $dir = $min ? '>' : '<';
  1129. $id = $min ?? $max;
  1130. $timeline = Status::select(
  1131. 'id',
  1132. 'uri',
  1133. 'caption',
  1134. 'rendered',
  1135. 'profile_id',
  1136. 'type',
  1137. 'in_reply_to_id',
  1138. 'reblog_of_id',
  1139. 'is_nsfw',
  1140. 'scope',
  1141. 'local',
  1142. 'reply_count',
  1143. 'likes_count',
  1144. 'reblogs_count',
  1145. 'comments_disabled',
  1146. 'place_id',
  1147. 'created_at',
  1148. 'updated_at'
  1149. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1150. ->with('profile', 'hashtags', 'mentions')
  1151. ->where('id', $dir, $id)
  1152. ->whereIn('profile_id', $following)
  1153. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1154. ->latest()
  1155. ->limit($limit)
  1156. ->get();
  1157. } else {
  1158. $timeline = Status::select(
  1159. 'id',
  1160. 'uri',
  1161. 'caption',
  1162. 'rendered',
  1163. 'profile_id',
  1164. 'type',
  1165. 'in_reply_to_id',
  1166. 'reblog_of_id',
  1167. 'is_nsfw',
  1168. 'scope',
  1169. 'local',
  1170. 'reply_count',
  1171. 'comments_disabled',
  1172. 'likes_count',
  1173. 'reblogs_count',
  1174. 'place_id',
  1175. 'created_at',
  1176. 'updated_at'
  1177. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1178. ->with('profile', 'hashtags', 'mentions')
  1179. ->whereIn('profile_id', $following)
  1180. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1181. ->latest()
  1182. ->simplePaginate($limit);
  1183. }
  1184. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1185. $res = $this->fractal->createData($fractal)->toArray();
  1186. return response()->json($res);
  1187. }
  1188. /**
  1189. * GET /api/v1/conversations
  1190. *
  1191. * Not implemented
  1192. *
  1193. * @return array
  1194. */
  1195. public function conversations(Request $request)
  1196. {
  1197. abort_if(!$request->user(), 403);
  1198. return response()->json([]);
  1199. }
  1200. /**
  1201. * GET /api/v1/timelines/public
  1202. *
  1203. *
  1204. * @return StatusTransformer
  1205. */
  1206. public function timelinePublic(Request $request)
  1207. {
  1208. $this->validate($request,[
  1209. 'page' => 'nullable|integer|max:40',
  1210. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1211. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1212. 'limit' => 'nullable|integer|max:80'
  1213. ]);
  1214. $page = $request->input('page');
  1215. $min = $request->input('min_id');
  1216. $max = $request->input('max_id');
  1217. $limit = $request->input('limit') ?? 3;
  1218. if($min || $max) {
  1219. $dir = $min ? '>' : '<';
  1220. $id = $min ?? $max;
  1221. $timeline = Status::select(
  1222. 'id',
  1223. 'uri',
  1224. 'caption',
  1225. 'rendered',
  1226. 'profile_id',
  1227. 'type',
  1228. 'in_reply_to_id',
  1229. 'reblog_of_id',
  1230. 'is_nsfw',
  1231. 'scope',
  1232. 'local',
  1233. 'reply_count',
  1234. 'comments_disabled',
  1235. 'place_id',
  1236. 'likes_count',
  1237. 'reblogs_count',
  1238. 'created_at',
  1239. 'updated_at'
  1240. )->whereNull('uri')
  1241. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1242. ->with('profile', 'hashtags', 'mentions')
  1243. ->where('id', $dir, $id)
  1244. ->whereScope('public')
  1245. ->latest()
  1246. ->limit($limit)
  1247. ->get();
  1248. } else {
  1249. $timeline = Status::select(
  1250. 'id',
  1251. 'uri',
  1252. 'caption',
  1253. 'rendered',
  1254. 'profile_id',
  1255. 'type',
  1256. 'in_reply_to_id',
  1257. 'reblog_of_id',
  1258. 'is_nsfw',
  1259. 'scope',
  1260. 'local',
  1261. 'reply_count',
  1262. 'comments_disabled',
  1263. 'place_id',
  1264. 'likes_count',
  1265. 'reblogs_count',
  1266. 'created_at',
  1267. 'updated_at'
  1268. )->whereNull('uri')
  1269. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1270. ->with('profile', 'hashtags', 'mentions')
  1271. ->whereScope('public')
  1272. ->latest()
  1273. ->simplePaginate($limit);
  1274. }
  1275. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1276. $res = $this->fractal->createData($fractal)->toArray();
  1277. return response()->json($res);
  1278. }
  1279. /**
  1280. * GET /api/v1/statuses/{id}
  1281. *
  1282. * @param integer $id
  1283. *
  1284. * @return StatusTransformer
  1285. */
  1286. public function statusById(Request $request, $id)
  1287. {
  1288. abort_if(!$request->user(), 403);
  1289. $user = $request->user();
  1290. $status = Status::findOrFail($id);
  1291. if($status->profile_id !== $user->profile_id) {
  1292. if($status->scope == 'private') {
  1293. abort_if(!$status->profile->followedBy($user->profile), 403);
  1294. } else {
  1295. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1296. }
  1297. }
  1298. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1299. $res = $this->fractal->createData($resource)->toArray();
  1300. return response()->json($res);
  1301. }
  1302. /**
  1303. * GET /api/v1/statuses/{id}/context
  1304. *
  1305. * @param integer $id
  1306. *
  1307. * @return StatusTransformer
  1308. */
  1309. public function statusContext(Request $request, $id)
  1310. {
  1311. abort_if(!$request->user(), 403);
  1312. $user = $request->user();
  1313. $status = Status::findOrFail($id);
  1314. if($status->profile_id !== $user->profile_id) {
  1315. if($status->scope == 'private') {
  1316. abort_if(!$status->profile->followedBy($user->profile), 403);
  1317. } else {
  1318. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1319. }
  1320. }
  1321. if($status->comments_disabled) {
  1322. $res = [
  1323. 'ancestors' => [],
  1324. 'descendants' => []
  1325. ];
  1326. } else {
  1327. $ancestors = $status->parent();
  1328. if($ancestors) {
  1329. $ares = new Fractal\Resource\Item($ancestors, new StatusTransformer());
  1330. $ancestors = [
  1331. $this->fractal->createData($ares)->toArray()
  1332. ];
  1333. } else {
  1334. $ancestors = [];
  1335. }
  1336. $descendants = Status::whereInReplyToId($id)->latest()->limit(20)->get();
  1337. $dres = new Fractal\Resource\Collection($descendants, new StatusTransformer());
  1338. $descendants = $this->fractal->createData($dres)->toArray();
  1339. $res = [
  1340. 'ancestors' => $ancestors,
  1341. 'descendants' => $descendants
  1342. ];
  1343. }
  1344. return response()->json($res);
  1345. }
  1346. /**
  1347. * GET /api/v1/statuses/{id}/card
  1348. *
  1349. * @param integer $id
  1350. *
  1351. * @return StatusTransformer
  1352. */
  1353. public function statusCard(Request $request, $id)
  1354. {
  1355. abort_if(!$request->user(), 403);
  1356. $user = $request->user();
  1357. $status = Status::findOrFail($id);
  1358. if($status->profile_id !== $user->profile_id) {
  1359. if($status->scope == 'private') {
  1360. abort_if(!$status->profile->followedBy($user->profile), 403);
  1361. } else {
  1362. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1363. }
  1364. }
  1365. // Return empty response since we don't handle support cards
  1366. $res = [];
  1367. return response()->json($res);
  1368. }
  1369. /**
  1370. * GET /api/v1/statuses/{id}/reblogged_by
  1371. *
  1372. * @param integer $id
  1373. *
  1374. * @return AccountTransformer
  1375. */
  1376. public function statusRebloggedBy(Request $request, $id)
  1377. {
  1378. abort_if(!$request->user(), 403);
  1379. $this->validate($request, [
  1380. 'page' => 'nullable|integer|min:1|max:40',
  1381. 'limit' => 'nullable|integer|min:1|max:80'
  1382. ]);
  1383. $limit = $request->input('limit') ?? 40;
  1384. $user = $request->user();
  1385. $status = Status::findOrFail($id);
  1386. if($status->profile_id !== $user->profile_id) {
  1387. if($status->scope == 'private') {
  1388. abort_if(!$status->profile->followedBy($user->profile), 403);
  1389. } else {
  1390. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1391. }
  1392. }
  1393. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1394. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1395. $res = $this->fractal->createData($resource)->toArray();
  1396. $url = $request->url();
  1397. $page = $request->input('page', 1);
  1398. $next = $page < 40 ? $page + 1 : 40;
  1399. $prev = $page > 1 ? $page - 1 : 1;
  1400. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1401. return response()->json($res, 200, ['Link' => $links]);
  1402. }
  1403. /**
  1404. * GET /api/v1/statuses/{id}/favourited_by
  1405. *
  1406. * @param integer $id
  1407. *
  1408. * @return AccountTransformer
  1409. */
  1410. public function statusFavouritedBy(Request $request, $id)
  1411. {
  1412. abort_if(!$request->user(), 403);
  1413. $this->validate($request, [
  1414. 'page' => 'nullable|integer|min:1|max:40',
  1415. 'limit' => 'nullable|integer|min:1|max:80'
  1416. ]);
  1417. $limit = $request->input('limit') ?? 40;
  1418. $user = $request->user();
  1419. $status = Status::findOrFail($id);
  1420. if($status->profile_id !== $user->profile_id) {
  1421. if($status->scope == 'private') {
  1422. abort_if(!$status->profile->followedBy($user->profile), 403);
  1423. } else {
  1424. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1425. }
  1426. }
  1427. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1428. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1429. $res = $this->fractal->createData($resource)->toArray();
  1430. $url = $request->url();
  1431. $page = $request->input('page', 1);
  1432. $next = $page < 40 ? $page + 1 : 40;
  1433. $prev = $page > 1 ? $page - 1 : 1;
  1434. $links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
  1435. return response()->json($res, 200, ['Link' => $links]);
  1436. }
  1437. /**
  1438. * POST /api/v1/statuses
  1439. *
  1440. *
  1441. * @return StatusTransformer
  1442. */
  1443. public function statusCreate(Request $request)
  1444. {
  1445. abort_if(!$request->user(), 403);
  1446. $this->validate($request, [
  1447. 'status' => 'nullable|string',
  1448. 'in_reply_to_id' => 'nullable|integer',
  1449. 'media_ids' => 'array|max:' . config('pixelfed.max_album_length'),
  1450. 'media_ids.*' => 'integer|min:1',
  1451. 'sensitive' => 'nullable|boolean',
  1452. 'visibility' => 'string|in:private,unlisted,public',
  1453. ]);
  1454. if(config('costar.enabled') == true) {
  1455. $blockedKeywords = config('costar.keyword.block');
  1456. if($blockedKeywords !== null && $request->status) {
  1457. $keywords = config('costar.keyword.block');
  1458. foreach($keywords as $kw) {
  1459. if(Str::contains($request->status, $kw) == true) {
  1460. abort(400, 'Invalid object. Contains banned keyword.');
  1461. }
  1462. }
  1463. }
  1464. }
  1465. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1466. abort(403, 'Empty statuses are not allowed');
  1467. }
  1468. $ids = $request->input('media_ids');
  1469. $in_reply_to_id = $request->input('in_reply_to_id');
  1470. $user = $request->user();
  1471. if($in_reply_to_id) {
  1472. $parent = Status::findOrFail($in_reply_to_id);
  1473. $status = new Status;
  1474. $status->caption = strip_tags($request->input('status'));
  1475. $status->scope = $request->input('visibility', 'public');
  1476. $status->visibility = $request->input('visibility', 'public');
  1477. $status->profile_id = $user->profile_id;
  1478. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1479. $status->in_reply_to_id = $parent->id;
  1480. $status->in_reply_to_profile_id = $parent->profile_id;
  1481. $status->save();
  1482. } else if($ids) {
  1483. $status = new Status;
  1484. $status->caption = strip_tags($request->input('status'));
  1485. $status->profile_id = $user->profile_id;
  1486. $status->scope = 'draft';
  1487. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1488. $status->save();
  1489. $mimes = [];
  1490. foreach($ids as $k => $v) {
  1491. if($k + 1 > config('pixelfed.max_album_length')) {
  1492. continue;
  1493. }
  1494. $m = Media::findOrFail($v);
  1495. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1496. abort(403, 'Invalid media id');
  1497. }
  1498. $m->status_id = $status->id;
  1499. $m->save();
  1500. array_push($mimes, $m->mime);
  1501. }
  1502. if(empty($mimes)) {
  1503. $status->delete();
  1504. abort(500, 'Invalid media ids');
  1505. }
  1506. $status->scope = $request->input('visibility', 'public');
  1507. $status->visibility = $request->input('visibility', 'public');
  1508. $status->type = StatusController::mimeTypeCheck($mimes);
  1509. $status->save();
  1510. }
  1511. if(!$status) {
  1512. $oops = 'An error occured. RefId: '.time().'-'.$user->profile_id.':'.Str::random(5).':'.Str::random(10);
  1513. abort(500, $oops);
  1514. }
  1515. NewStatusPipeline::dispatch($status);
  1516. Cache::forget('user:account:id:'.$user->id);
  1517. Cache::forget('_api:statuses:recent_9:'.$user->profile_id);
  1518. Cache::forget('profile:status_count:'.$user->profile_id);
  1519. Cache::forget($user->storageUsedKey());
  1520. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1521. $res = $this->fractal->createData($resource)->toArray();
  1522. return response()->json($res);
  1523. }
  1524. /**
  1525. * DELETE /api/v1/statuses
  1526. *
  1527. * @param integer $id
  1528. *
  1529. * @return null
  1530. */
  1531. public function statusDelete(Request $request, $id)
  1532. {
  1533. abort_if(!$request->user(), 403);
  1534. $status = Status::whereProfileId($request->user()->profile->id)
  1535. ->findOrFail($id);
  1536. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1537. Cache::forget('profile:status_count:'.$status->profile_id);
  1538. StatusDelete::dispatch($status);
  1539. $res = $this->fractal->createData($resource)->toArray();
  1540. $res['text'] = $res['content'];
  1541. unset($res['content']);
  1542. return response()->json($res);
  1543. }
  1544. /**
  1545. * POST /api/v1/statuses/{id}/reblog
  1546. *
  1547. * @param integer $id
  1548. *
  1549. * @return StatusTransformer
  1550. */
  1551. public function statusShare(Request $request, $id)
  1552. {
  1553. abort_if(!$request->user(), 403);
  1554. $user = $request->user();
  1555. $status = Status::findOrFail($id);
  1556. if($status->profile_id !== $user->profile_id) {
  1557. if($status->scope == 'private') {
  1558. abort_if(!$status->profile->followedBy($user->profile), 403);
  1559. } else {
  1560. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1561. }
  1562. }
  1563. $share = Status::firstOrCreate([
  1564. 'profile_id' => $user->profile_id,
  1565. 'reblog_of_id' => $status->id,
  1566. 'in_reply_to_profile_id' => $status->profile_id,
  1567. 'scope' => 'public',
  1568. 'visibility' => 'public'
  1569. ]);
  1570. if($share->wasRecentlyCreated == true) {
  1571. $status->reblogs_count = $status->shares()->count();
  1572. $status->save();
  1573. SharePipeline::dispatch($share);
  1574. }
  1575. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1576. $res = $this->fractal->createData($resource)->toArray();
  1577. return response()->json($res);
  1578. }
  1579. /**
  1580. * POST /api/v1/statuses/{id}/unreblog
  1581. *
  1582. * @param integer $id
  1583. *
  1584. * @return StatusTransformer
  1585. */
  1586. public function statusUnshare(Request $request, $id)
  1587. {
  1588. abort_if(!$request->user(), 403);
  1589. $user = $request->user();
  1590. $status = Status::findOrFail($id);
  1591. if($status->profile_id !== $user->profile_id) {
  1592. if($status->scope == 'private') {
  1593. abort_if(!$status->profile->followedBy($user->profile), 403);
  1594. } else {
  1595. abort_if(!in_array($status->scope, ['public','unlisted']), 403);
  1596. }
  1597. }
  1598. Status::whereProfileId($user->profile_id)
  1599. ->whereReblogOfId($status->id)
  1600. ->delete();
  1601. $status->reblogs_count = $status->shares()->count();
  1602. $status->save();
  1603. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1604. $res = $this->fractal->createData($resource)->toArray();
  1605. return response()->json($res);
  1606. }
  1607. /**
  1608. * GET /api/v1/timelines/tag/{hashtag}
  1609. *
  1610. * @param string $hashtag
  1611. *
  1612. * @return StatusTransformer
  1613. */
  1614. public function timelineHashtag(Request $request, $hashtag)
  1615. {
  1616. abort_if(!$request->user(), 403);
  1617. // todo
  1618. $res = [];
  1619. return response()->json($res);
  1620. }
  1621. /**
  1622. * GET /api/v1/bookmarks
  1623. *
  1624. *
  1625. *
  1626. * @return StatusTransformer
  1627. */
  1628. public function bookmarks(Request $request)
  1629. {
  1630. abort_if(!$request->user(), 403);
  1631. $this->validate($request, [
  1632. 'limit' => 'nullable|integer|min:1|max:40',
  1633. 'max_id' => 'nullable|integer|min:0',
  1634. 'since_id' => 'nullable|integer|min:0',
  1635. 'min_id' => 'nullable|integer|min:0'
  1636. ]);
  1637. $pid = $request->user()->profile_id;
  1638. $limit = $request->input('limit') ?? 20;
  1639. $max_id = $request->input('max_id');
  1640. $since_id = $request->input('since_id');
  1641. $min_id = $request->input('min_id');
  1642. $dir = $min_id ? '>' : '<';
  1643. $id = $min_id ?? $max_id;
  1644. if($id) {
  1645. $bookmarks = Bookmark::whereProfileId($pid)
  1646. ->where('status_id', $dir, $id)
  1647. ->limit($limit)
  1648. ->pluck('status_id');
  1649. } else {
  1650. $bookmarks = Bookmark::whereProfileId($pid)
  1651. ->latest()
  1652. ->limit($limit)
  1653. ->pluck('status_id');
  1654. }
  1655. $res = [];
  1656. foreach($bookmarks as $id) {
  1657. $res[] = \App\Services\StatusService::get($id);
  1658. }
  1659. return $res;
  1660. }
  1661. /**
  1662. * POST /api/v1/statuses/{id}/bookmark
  1663. *
  1664. *
  1665. *
  1666. * @return StatusTransformer
  1667. */
  1668. public function bookmarkStatus(Request $request, $id)
  1669. {
  1670. abort_if(!$request->user(), 403);
  1671. $status = Status::whereNull('uri')
  1672. ->whereScope('public')
  1673. ->findOrFail($id);
  1674. Bookmark::firstOrCreate([
  1675. 'status_id' => $status->id,
  1676. 'profile_id' => $request->user()->profile_id
  1677. ]);
  1678. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1679. $res = $this->fractal->createData($resource)->toArray();
  1680. return response()->json($res);
  1681. }
  1682. /**
  1683. * POST /api/v1/statuses/{id}/unbookmark
  1684. *
  1685. *
  1686. *
  1687. * @return StatusTransformer
  1688. */
  1689. public function unbookmarkStatus(Request $request, $id)
  1690. {
  1691. abort_if(!$request->user(), 403);
  1692. $status = Status::whereNull('uri')
  1693. ->whereScope('public')
  1694. ->findOrFail($id);
  1695. Bookmark::firstOrCreate([
  1696. 'status_id' => $status->id,
  1697. 'profile_id' => $request->user()->profile_id
  1698. ]);
  1699. $bookmark = Bookmark::whereStatusId($status->id)
  1700. ->whereProfileId($request->user()->profile_id)
  1701. ->firstOrFail();
  1702. $bookmark->delete();
  1703. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1704. $res = $this->fractal->createData($resource)->toArray();
  1705. return response()->json($res);
  1706. }
  1707. /**
  1708. * GET /api/v2/search
  1709. *
  1710. *
  1711. * @return array
  1712. */
  1713. public function searchV2(Request $request)
  1714. {
  1715. abort_if(!$request->user(), 403);
  1716. $this->validate($request, [
  1717. 'q' => 'required|string|min:1|max:80',
  1718. 'account_id' => 'nullable|string',
  1719. 'max_id' => 'nullable|string',
  1720. 'min_id' => 'nullable|string',
  1721. 'type' => 'nullable|in:accounts,hashtags,statuses',
  1722. 'exclude_unreviewed' => 'nullable',
  1723. 'resolve' => 'nullable',
  1724. 'limit' => 'nullable|integer|max:40',
  1725. 'offset' => 'nullable|integer',
  1726. 'following' => 'nullable'
  1727. ]);
  1728. return SearchApiV2Service::query($request);
  1729. }
  1730. }