ApiV1Controller.php 52 KB

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