ApiV1Controller.php 56 KB

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