ApiV1Controller.php 61 KB

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