ApiV1Controller.php 58 KB

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