ApiV1Controller.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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:40'
  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. LikePipeline::dispatch($like);
  645. }
  646. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  647. $res = $this->fractal->createData($resource)->toArray();
  648. return response()->json($res);
  649. }
  650. /**
  651. * POST /api/v1/statuses/{id}/unfavourite
  652. *
  653. * @param integer $id
  654. *
  655. * @return \App\Transformer\Api\StatusTransformer
  656. */
  657. public function statusUnfavouriteById(Request $request, $id)
  658. {
  659. abort_if(!$request->user(), 403);
  660. $user = $request->user();
  661. $status = Status::findOrFail($id);
  662. $like = Like::whereProfileId($user->profile_id)
  663. ->whereStatusId($status->id)
  664. ->first();
  665. if($like) {
  666. $like->delete();
  667. }
  668. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  669. $res = $this->fractal->createData($resource)->toArray();
  670. return response()->json($res);
  671. }
  672. /**
  673. * GET /api/v1/filters
  674. *
  675. * Return empty response since we filter server side
  676. *
  677. * @return array
  678. */
  679. public function accountFilters(Request $request)
  680. {
  681. abort_if(!$request->user(), 403);
  682. return response()->json([]);
  683. }
  684. /**
  685. * GET /api/v1/follow_requests
  686. *
  687. * Return array of Accounts that have sent follow requests
  688. *
  689. * @return \App\Transformer\Api\AccountTransformer
  690. */
  691. public function accountFollowRequests(Request $request)
  692. {
  693. abort_if(!$request->user(), 403);
  694. $user = $request->user();
  695. $followRequests = FollowRequest::whereFollowingId($user->profile->id)->pluck('follower_id');
  696. $profiles = Profile::find($followRequests);
  697. $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
  698. $res = $this->fractal->createData($resource)->toArray();
  699. return response()->json($res);
  700. }
  701. /**
  702. * POST /api/v1/follow_requests/{id}/authorize
  703. *
  704. * @param integer $id
  705. *
  706. * @return null
  707. */
  708. public function accountFollowRequestAccept(Request $request, $id)
  709. {
  710. abort_if(!$request->user(), 403);
  711. // todo
  712. return response()->json([]);
  713. }
  714. /**
  715. * POST /api/v1/follow_requests/{id}/reject
  716. *
  717. * @param integer $id
  718. *
  719. * @return null
  720. */
  721. public function accountFollowRequestReject(Request $request, $id)
  722. {
  723. abort_if(!$request->user(), 403);
  724. // todo
  725. return response()->json([]);
  726. }
  727. /**
  728. * GET /api/v1/suggestions
  729. *
  730. * Return empty array as we don't support suggestions
  731. *
  732. * @return null
  733. */
  734. public function accountSuggestions(Request $request)
  735. {
  736. abort_if(!$request->user(), 403);
  737. // todo
  738. return response()->json([]);
  739. }
  740. /**
  741. * GET /api/v1/instance
  742. *
  743. * Information about the server.
  744. *
  745. * @return Instance
  746. */
  747. public function instance(Request $request)
  748. {
  749. $res = [
  750. 'description' => 'Pixelfed - Photo sharing for everyone',
  751. 'email' => config('instance.email'),
  752. 'languages' => ['en'],
  753. 'max_toot_chars' => config('pixelfed.max_caption_length'),
  754. 'registrations' => config('pixelfed.open_registration'),
  755. 'stats' => [
  756. 'user_count' => 0,
  757. 'status_count' => 0,
  758. 'domain_count' => 0
  759. ],
  760. 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
  761. 'title' => 'Pixelfed (' . config('pixelfed.domain.app') . ')',
  762. 'uri' => config('app.url'),
  763. 'urls' => [],
  764. 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')'
  765. ];
  766. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  767. }
  768. /**
  769. * GET /api/v1/lists
  770. *
  771. * Return empty array as we don't support lists
  772. *
  773. * @return null
  774. */
  775. public function accountLists(Request $request)
  776. {
  777. abort_if(!$request->user(), 403);
  778. return response()->json([]);
  779. }
  780. /**
  781. * GET /api/v1/accounts/{id}/lists
  782. *
  783. * @param integer $id
  784. *
  785. * @return null
  786. */
  787. public function accountListsById(Request $request, $id)
  788. {
  789. abort_if(!$request->user(), 403);
  790. return response()->json([]);
  791. }
  792. /**
  793. * POST /api/v1/media
  794. *
  795. *
  796. * @return MediaTransformer
  797. */
  798. public function mediaUpload(Request $request)
  799. {
  800. abort_if(!$request->user(), 403);
  801. $this->validate($request, [
  802. 'file.*' => function() {
  803. return [
  804. 'required',
  805. 'mimes:' . config('pixelfed.media_types'),
  806. 'max:' . config('pixelfed.max_photo_size'),
  807. ];
  808. },
  809. 'filter_name' => 'nullable|string|max:24',
  810. 'filter_class' => 'nullable|alpha_dash|max:24',
  811. 'description' => 'nullable|string|max:420'
  812. ]);
  813. $user = $request->user();
  814. $profile = $user->profile;
  815. if(config('pixelfed.enforce_account_limit') == true) {
  816. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  817. return Media::whereUserId($user->id)->sum('size') / 1000;
  818. });
  819. $limit = (int) config('pixelfed.max_account_size');
  820. if ($size >= $limit) {
  821. abort(403, 'Account size limit reached.');
  822. }
  823. }
  824. $monthHash = hash('sha1', date('Y').date('m'));
  825. $userHash = hash('sha1', $user->id . (string) $user->created_at);
  826. $photo = $request->file('file');
  827. $mimes = explode(',', config('pixelfed.media_types'));
  828. if(in_array($photo->getMimeType(), $mimes) == false) {
  829. abort(403, 'Invalid or unsupported mime type.');
  830. }
  831. $storagePath = "public/m/{$monthHash}/{$userHash}";
  832. $path = $photo->store($storagePath);
  833. $hash = \hash_file('sha256', $photo);
  834. $media = new Media();
  835. $media->status_id = null;
  836. $media->profile_id = $profile->id;
  837. $media->user_id = $user->id;
  838. $media->media_path = $path;
  839. $media->original_sha256 = $hash;
  840. $media->size = $photo->getSize();
  841. $media->mime = $photo->getMimeType();
  842. $media->caption = $request->input('description');
  843. $media->filter_class = $request->input('filter_class');
  844. $media->filter_name = $request->input('filter_name');
  845. $media->save();
  846. switch ($media->mime) {
  847. case 'image/jpeg':
  848. case 'image/png':
  849. ImageOptimize::dispatch($media);
  850. break;
  851. case 'video/mp4':
  852. VideoThumbnail::dispatch($media);
  853. $preview_url = '/storage/no-preview.png';
  854. $url = '/storage/no-preview.png';
  855. break;
  856. }
  857. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  858. $res = $this->fractal->createData($resource)->toArray();
  859. $res['preview_url'] = url('/storage/no-preview.png');
  860. $res['url'] = url('/storage/no-preview.png');
  861. return response()->json($res);
  862. }
  863. /**
  864. * PUT /api/v1/media/{id}
  865. *
  866. * @param integer $id
  867. *
  868. * @return MediaTransformer
  869. */
  870. public function mediaUpdate(Request $request, $id)
  871. {
  872. abort_if(!$request->user(), 403);
  873. $this->validate($request, [
  874. 'description' => 'nullable|string|max:420'
  875. ]);
  876. $user = $request->user();
  877. $media = Media::whereUserId($user->id)
  878. ->whereNull('status_id')
  879. ->findOrFail($id);
  880. $media->caption = $request->input('description');
  881. $media->save();
  882. $resource = new Fractal\Resource\Item($media, new MediaTransformer());
  883. $res = $this->fractal->createData($resource)->toArray();
  884. $res['preview_url'] = url('/storage/no-preview.png');
  885. $res['url'] = url('/storage/no-preview.png');
  886. return response()->json($res);
  887. }
  888. /**
  889. * GET /api/v1/mutes
  890. *
  891. *
  892. * @return AccountTransformer
  893. */
  894. public function accountMutes(Request $request)
  895. {
  896. abort_if(!$request->user(), 403);
  897. $this->validate($request, [
  898. 'limit' => 'nullable|integer|min:1|max:40'
  899. ]);
  900. $user = $request->user();
  901. $limit = $request->input('limit') ?? 40;
  902. $mutes = UserFilter::whereUserId($user->profile_id)
  903. ->whereFilterableType('App\Profile')
  904. ->whereFilterType('mute')
  905. ->simplePaginate($limit)
  906. ->pluck('filterable_id');
  907. $accounts = Profile::find($mutes);
  908. $resource = new Fractal\Resource\Collection($accounts, new AccountTransformer());
  909. $res = $this->fractal->createData($resource)->toArray();
  910. return response()->json($res);
  911. }
  912. /**
  913. * POST /api/v1/accounts/{id}/mute
  914. *
  915. * @param integer $id
  916. *
  917. * @return RelationshipTransformer
  918. */
  919. public function accountMuteById(Request $request, $id)
  920. {
  921. abort_if(!$request->user(), 403);
  922. $user = $request->user();
  923. $pid = $user->profile_id;
  924. $account = Profile::findOrFail($id);
  925. $filter = UserFilter::firstOrCreate([
  926. 'user_id' => $pid,
  927. 'filterable_id' => $account->id,
  928. 'filterable_type' => 'App\Profile',
  929. 'filter_type' => 'mute',
  930. ]);
  931. Cache::forget("user:filter:list:$pid");
  932. Cache::forget("feature:discover:posts:$pid");
  933. Cache::forget("api:local:exp:rec:$pid");
  934. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  935. $res = $this->fractal->createData($resource)->toArray();
  936. return response()->json($res);
  937. }
  938. /**
  939. * POST /api/v1/accounts/{id}/unmute
  940. *
  941. * @param integer $id
  942. *
  943. * @return RelationshipTransformer
  944. */
  945. public function accountUnmuteById(Request $request, $id)
  946. {
  947. abort_if(!$request->user(), 403);
  948. $user = $request->user();
  949. $pid = $user->profile_id;
  950. $account = Profile::findOrFail($id);
  951. $filter = UserFilter::whereUserId($pid)
  952. ->whereFilterableId($account->id)
  953. ->whereFilterableType('App\Profile')
  954. ->whereFilterType('mute')
  955. ->first();
  956. if($filter) {
  957. $filter->delete();
  958. Cache::forget("user:filter:list:$pid");
  959. Cache::forget("feature:discover:posts:$pid");
  960. Cache::forget("api:local:exp:rec:$pid");
  961. }
  962. $resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
  963. $res = $this->fractal->createData($resource)->toArray();
  964. return response()->json($res);
  965. }
  966. /**
  967. * GET /api/v1/notifications
  968. *
  969. *
  970. * @return NotificationTransformer
  971. */
  972. public function accountNotifications(Request $request)
  973. {
  974. abort_if(!$request->user(), 403);
  975. $this->validate($request, [
  976. 'page' => 'nullable|integer|min:1|max:10',
  977. 'limit' => 'nullable|integer|min:1|max:80',
  978. 'max_id' => 'nullable|integer|min:1',
  979. 'min_id' => 'nullable|integer|min:0',
  980. ]);
  981. $pid = $request->user()->profile_id;
  982. $limit = $request->input('limit') ?? 20;
  983. $timeago = now()->subMonths(6);
  984. $min = $request->input('min_id');
  985. $max = $request->input('max_id');
  986. if($min || $max) {
  987. $dir = $min ? '>' : '<';
  988. $id = $min ?? $max;
  989. $notifications = Notification::whereProfileId($pid)
  990. ->whereDate('created_at', '>', $timeago)
  991. ->latest()
  992. ->where('id', $dir, $id)
  993. ->limit($limit)
  994. ->get();
  995. } else {
  996. $notifications = Notification::whereProfileId($pid)
  997. ->whereDate('created_at', '>', $timeago)
  998. ->latest()
  999. ->simplePaginate($limit);
  1000. }
  1001. $resource = new Fractal\Resource\Collection($notifications, new NotificationTransformer());
  1002. $res = $this->fractal->createData($resource)->toArray();
  1003. return response()->json($res);
  1004. }
  1005. /**
  1006. * GET /api/v1/timelines/home
  1007. *
  1008. *
  1009. * @return StatusTransformer
  1010. */
  1011. public function timelineHome(Request $request)
  1012. {
  1013. abort_if(!$request->user(), 403);
  1014. $this->validate($request,[
  1015. 'page' => 'nullable|integer|max:40',
  1016. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1017. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1018. 'limit' => 'nullable|integer|max:80'
  1019. ]);
  1020. $page = $request->input('page');
  1021. $min = $request->input('min_id');
  1022. $max = $request->input('max_id');
  1023. $limit = $request->input('limit') ?? 3;
  1024. $pid = $request->user()->profile_id;
  1025. $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
  1026. $following = Follower::whereProfileId($pid)->pluck('following_id');
  1027. return $following->push($pid)->toArray();
  1028. });
  1029. if($min || $max) {
  1030. $dir = $min ? '>' : '<';
  1031. $id = $min ?? $max;
  1032. $timeline = Status::select(
  1033. 'id',
  1034. 'uri',
  1035. 'caption',
  1036. 'rendered',
  1037. 'profile_id',
  1038. 'type',
  1039. 'in_reply_to_id',
  1040. 'reblog_of_id',
  1041. 'is_nsfw',
  1042. 'scope',
  1043. 'local',
  1044. 'reply_count',
  1045. 'comments_disabled',
  1046. 'place_id',
  1047. 'created_at',
  1048. 'updated_at'
  1049. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1050. ->with('profile', 'hashtags', 'mentions')
  1051. ->where('id', $dir, $id)
  1052. ->whereIn('profile_id', $following)
  1053. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1054. ->latest()
  1055. ->limit($limit)
  1056. ->get();
  1057. } else {
  1058. $timeline = Status::select(
  1059. 'id',
  1060. 'uri',
  1061. 'caption',
  1062. 'rendered',
  1063. 'profile_id',
  1064. 'type',
  1065. 'in_reply_to_id',
  1066. 'reblog_of_id',
  1067. 'is_nsfw',
  1068. 'scope',
  1069. 'local',
  1070. 'reply_count',
  1071. 'comments_disabled',
  1072. 'place_id',
  1073. 'created_at',
  1074. 'updated_at'
  1075. )->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1076. ->with('profile', 'hashtags', 'mentions')
  1077. ->whereIn('profile_id', $following)
  1078. ->whereIn('visibility',['public', 'unlisted', 'private'])
  1079. ->latest()
  1080. ->simplePaginate($limit);
  1081. }
  1082. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1083. $res = $this->fractal->createData($fractal)->toArray();
  1084. return response()->json($res);
  1085. }
  1086. /**
  1087. * GET /api/v1/conversations
  1088. *
  1089. * Not implemented
  1090. *
  1091. * @return array
  1092. */
  1093. public function conversations(Request $request)
  1094. {
  1095. abort_if(!$request->user(), 403);
  1096. return response()->json([]);
  1097. }
  1098. /**
  1099. * GET /api/v1/timelines/public
  1100. *
  1101. *
  1102. * @return StatusTransformer
  1103. */
  1104. public function timelinePublic(Request $request)
  1105. {
  1106. $this->validate($request,[
  1107. 'page' => 'nullable|integer|max:40',
  1108. 'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1109. 'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
  1110. 'limit' => 'nullable|integer|max:80'
  1111. ]);
  1112. $page = $request->input('page');
  1113. $min = $request->input('min_id');
  1114. $max = $request->input('max_id');
  1115. $limit = $request->input('limit') ?? 3;
  1116. if($min || $max) {
  1117. $dir = $min ? '>' : '<';
  1118. $id = $min ?? $max;
  1119. $timeline = Status::select(
  1120. 'id',
  1121. 'uri',
  1122. 'caption',
  1123. 'rendered',
  1124. 'profile_id',
  1125. 'type',
  1126. 'in_reply_to_id',
  1127. 'reblog_of_id',
  1128. 'is_nsfw',
  1129. 'scope',
  1130. 'local',
  1131. 'reply_count',
  1132. 'comments_disabled',
  1133. 'place_id',
  1134. 'created_at',
  1135. 'updated_at'
  1136. )->whereNull('uri')
  1137. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1138. ->with('profile', 'hashtags', 'mentions')
  1139. ->where('id', $dir, $id)
  1140. ->whereVisibility('public')
  1141. ->latest()
  1142. ->limit($limit)
  1143. ->get();
  1144. } else {
  1145. $timeline = Status::select(
  1146. 'id',
  1147. 'uri',
  1148. 'caption',
  1149. 'rendered',
  1150. 'profile_id',
  1151. 'type',
  1152. 'in_reply_to_id',
  1153. 'reblog_of_id',
  1154. 'is_nsfw',
  1155. 'scope',
  1156. 'local',
  1157. 'reply_count',
  1158. 'comments_disabled',
  1159. 'place_id',
  1160. 'created_at',
  1161. 'updated_at'
  1162. )->whereNull('uri')
  1163. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
  1164. ->with('profile', 'hashtags', 'mentions')
  1165. ->whereVisibility('public')
  1166. ->latest()
  1167. ->simplePaginate($limit);
  1168. }
  1169. $fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
  1170. $res = $this->fractal->createData($fractal)->toArray();
  1171. return response()->json($res);
  1172. }
  1173. /**
  1174. * GET /api/v1/statuses/{id}
  1175. *
  1176. * @param integer $id
  1177. *
  1178. * @return StatusTransformer
  1179. */
  1180. public function statusById(Request $request, $id)
  1181. {
  1182. abort_if(!$request->user(), 403);
  1183. $status = Status::whereVisibility('public')->findOrFail($id);
  1184. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1185. $res = $this->fractal->createData($resource)->toArray();
  1186. return response()->json($res);
  1187. }
  1188. /**
  1189. * GET /api/v1/statuses/{id}/context
  1190. *
  1191. * @param integer $id
  1192. *
  1193. * @return StatusTransformer
  1194. */
  1195. public function statusContext(Request $request, $id)
  1196. {
  1197. abort_if(!$request->user(), 403);
  1198. $status = Status::whereVisibility('public')->findOrFail($id);
  1199. // Return empty response since we don't handle threading like this
  1200. $res = [
  1201. 'ancestors' => [],
  1202. 'descendants' => []
  1203. ];
  1204. return response()->json($res);
  1205. }
  1206. /**
  1207. * GET /api/v1/statuses/{id}/card
  1208. *
  1209. * @param integer $id
  1210. *
  1211. * @return StatusTransformer
  1212. */
  1213. public function statusCard(Request $request, $id)
  1214. {
  1215. abort_if(!$request->user(), 403);
  1216. $status = Status::whereVisibility('public')->findOrFail($id);
  1217. // Return empty response since we don't handle support cards
  1218. $res = [];
  1219. return response()->json($res);
  1220. }
  1221. /**
  1222. * GET /api/v1/statuses/{id}/reblogged_by
  1223. *
  1224. * @param integer $id
  1225. *
  1226. * @return AccountTransformer
  1227. */
  1228. public function statusRebloggedBy(Request $request, $id)
  1229. {
  1230. abort_if(!$request->user(), 403);
  1231. $this->validate($request, [
  1232. 'limit' => 'nullable|integer|min:1|max:80'
  1233. ]);
  1234. $limit = $request->input('limit') ?? 40;
  1235. $status = Status::whereVisibility('public')->findOrFail($id);
  1236. $shared = $status->sharedBy()->latest()->simplePaginate($limit);
  1237. $resource = new Fractal\Resource\Collection($shared, new AccountTransformer());
  1238. $res = $this->fractal->createData($resource)->toArray();
  1239. return response()->json($res);
  1240. }
  1241. /**
  1242. * GET /api/v1/statuses/{id}/favourited_by
  1243. *
  1244. * @param integer $id
  1245. *
  1246. * @return AccountTransformer
  1247. */
  1248. public function statusFavouritedBy(Request $request, $id)
  1249. {
  1250. abort_if(!$request->user(), 403);
  1251. $this->validate($request, [
  1252. 'limit' => 'nullable|integer|min:1|max:80'
  1253. ]);
  1254. $limit = $request->input('limit') ?? 40;
  1255. $status = Status::whereVisibility('public')->findOrFail($id);
  1256. $liked = $status->likedBy()->latest()->simplePaginate($limit);
  1257. $resource = new Fractal\Resource\Collection($liked, new AccountTransformer());
  1258. $res = $this->fractal->createData($resource)->toArray();
  1259. return response()->json($res);
  1260. }
  1261. /**
  1262. * POST /api/v1/statuses
  1263. *
  1264. *
  1265. * @return StatusTransformer
  1266. */
  1267. public function statusCreate(Request $request)
  1268. {
  1269. abort_if(!$request->user(), 403);
  1270. $this->validate($request, [
  1271. 'status' => 'nullable|string',
  1272. 'in_reply_to_id' => 'nullable|integer',
  1273. 'media_ids' => 'array|max:' . config('pixelfed.max_album_length'),
  1274. 'media_ids.*' => 'integer|min:1',
  1275. 'sensitive' => 'nullable|boolean',
  1276. 'visibility' => 'string|in:private,unlisted,public',
  1277. ]);
  1278. if(config('costar.enabled') == true) {
  1279. $blockedKeywords = config('costar.keyword.block');
  1280. if($blockedKeywords !== null && $request->status) {
  1281. $keywords = config('costar.keyword.block');
  1282. foreach($keywords as $kw) {
  1283. if(Str::contains($request->status, $kw) == true) {
  1284. abort(400, 'Invalid object. Contains banned keyword.');
  1285. }
  1286. }
  1287. }
  1288. }
  1289. if(!$request->filled('media_ids') && !$request->filled('in_reply_to_id')) {
  1290. abort(403, 'Empty statuses are not allowed');
  1291. }
  1292. $ids = $request->input('media_ids');
  1293. $in_reply_to_id = $request->input('in_reply_to_id');
  1294. $user = $request->user();
  1295. if($in_reply_to_id) {
  1296. $parent = Status::findOrFail($in_reply_to_id);
  1297. $status = new Status;
  1298. $status->caption = strip_tags($request->input('status'));
  1299. $status->scope = $request->input('visibility');
  1300. $status->visibility = $request->input('visibility');
  1301. $status->profile_id = $user->profile_id;
  1302. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1303. $status->in_reply_to_id = $parent->id;
  1304. $status->in_reply_to_profile_id = $parent->profile_id;
  1305. $status->save();
  1306. } else if($ids) {
  1307. $status = new Status;
  1308. $status->caption = strip_tags($request->input('status'));
  1309. $status->profile_id = $user->profile_id;
  1310. $status->scope = 'draft';
  1311. $status->is_nsfw = $user->profile->cw == true ? true : $request->input('sensitive', false);
  1312. $status->save();
  1313. $mimes = [];
  1314. foreach($ids as $k => $v) {
  1315. if($k + 1 > config('pixelfed.max_album_length')) {
  1316. continue;
  1317. }
  1318. $m = Media::findOrFail($v);
  1319. if($m->profile_id !== $user->profile_id || $m->status_id) {
  1320. abort(403, 'Invalid media id');
  1321. }
  1322. $m->status_id = $status->id;
  1323. $m->save();
  1324. array_push($mimes, $m->mime);
  1325. }
  1326. if(empty($mimes)) {
  1327. $status->delete();
  1328. abort(500, 'Invalid media ids');
  1329. }
  1330. $status->scope = $request->input('visibility');
  1331. $status->visibility = $request->input('visibility');
  1332. $status->type = StatusController::mimeTypeCheck($mimes);
  1333. $status->save();
  1334. }
  1335. if(!$status) {
  1336. $oops = 'An error occured. RefId: '.time().'-'.$user->profile_id.':'.Str::random(5).':'.Str::random(10);
  1337. abort(500, $oops);
  1338. }
  1339. NewStatusPipeline::dispatch($status);
  1340. Cache::forget('user:account:id:'.$user->id);
  1341. Cache::forget('profile:status_count:'.$user->profile_id);
  1342. Cache::forget($user->storageUsedKey());
  1343. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1344. $res = $this->fractal->createData($resource)->toArray();
  1345. return response()->json($res);
  1346. }
  1347. /**
  1348. * DELETE /api/v1/statuses
  1349. *
  1350. * @param integer $id
  1351. *
  1352. * @return null
  1353. */
  1354. public function statusDelete(Request $request, $id)
  1355. {
  1356. abort_if(!$request->user(), 403);
  1357. $status = Status::whereProfileId($request->user()->profile->id)
  1358. ->findOrFail($id);
  1359. Cache::forget('profile:status_count:'.$status->profile_id);
  1360. StatusDelete::dispatch($status);
  1361. return response()->json(['Status successfully deleted.']);
  1362. }
  1363. /**
  1364. * POST /api/v1/statuses/{id}/reblog
  1365. *
  1366. * @param integer $id
  1367. *
  1368. * @return StatusTransformer
  1369. */
  1370. public function statusShare(Request $request, $id)
  1371. {
  1372. abort_if(!$request->user(), 403);
  1373. $user = $request->user();
  1374. $status = Status::findOrFail($id);
  1375. $share = Status::firstOrCreate([
  1376. 'profile_id' => $user->profile_id,
  1377. 'reblog_of_id' => $status->id,
  1378. 'in_reply_to_profile_id' => $status->profile_id
  1379. ]);
  1380. if($share->wasRecentlyCreated == true) {
  1381. SharePipeline::dispatch($share);
  1382. }
  1383. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1384. $res = $this->fractal->createData($resource)->toArray();
  1385. return response()->json($res);
  1386. }
  1387. /**
  1388. * POST /api/v1/statuses/{id}/unreblog
  1389. *
  1390. * @param integer $id
  1391. *
  1392. * @return StatusTransformer
  1393. */
  1394. public function statusUnshare(Request $request, $id)
  1395. {
  1396. abort_if(!$request->user(), 403);
  1397. $user = $request->user();
  1398. $status = Status::findOrFail($id);
  1399. Status::whereProfileId($user->profile_id)
  1400. ->whereReblogOfId($status->id)
  1401. ->delete();
  1402. $count = $status->reblogs_count;
  1403. $status->reblogs_count = $count > 0 ? $count - 1 : 0;
  1404. $status->save();
  1405. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  1406. $res = $this->fractal->createData($resource)->toArray();
  1407. return response()->json($res);
  1408. }
  1409. }