ApiV1Controller.php 48 KB

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