ApiV1Controller.php 53 KB

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