StatusController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  4. use App\Jobs\StatusPipeline\NewStatusPipeline;
  5. use App\Jobs\StatusPipeline\StatusDelete;
  6. use App\Jobs\SharePipeline\SharePipeline;
  7. use App\Jobs\SharePipeline\UndoSharePipeline;
  8. use App\AccountInterstitial;
  9. use App\Media;
  10. use App\Profile;
  11. use App\Status;
  12. use App\StatusArchived;
  13. use App\StatusView;
  14. use App\Transformer\ActivityPub\StatusTransformer;
  15. use App\Transformer\ActivityPub\Verb\Note;
  16. use App\Transformer\ActivityPub\Verb\Question;
  17. use App\User;
  18. use Auth, DB, Cache;
  19. use Illuminate\Http\Request;
  20. use League\Fractal;
  21. use App\Util\Media\Filter;
  22. use Illuminate\Support\Str;
  23. use App\Services\HashidService;
  24. use App\Services\StatusService;
  25. use App\Util\Media\License;
  26. use App\Services\ReblogService;
  27. class StatusController extends Controller
  28. {
  29. public function show(Request $request, $username, $id)
  30. {
  31. // redirect authed users to Metro 2.0
  32. if($request->user()) {
  33. // unless they force static view
  34. if(!$request->has('fs') || $request->input('fs') != '1') {
  35. return redirect('/i/web/post/' . $id);
  36. }
  37. }
  38. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  39. if($user->status != null) {
  40. return ProfileController::accountCheck($user);
  41. }
  42. $status = Status::whereProfileId($user->id)
  43. ->whereNull('reblog_of_id')
  44. ->whereIn('scope', ['public','unlisted', 'private'])
  45. ->findOrFail($id);
  46. if($status->uri || $status->url) {
  47. $url = $status->uri ?? $status->url;
  48. if(ends_with($url, '/activity')) {
  49. $url = str_replace('/activity', '', $url);
  50. }
  51. return redirect($url);
  52. }
  53. if($status->visibility == 'private' || $user->is_private) {
  54. if(!Auth::check()) {
  55. abort(404);
  56. }
  57. $pid = Auth::user()->profile;
  58. if($user->followedBy($pid) == false && $user->id !== $pid->id && Auth::user()->is_admin == false) {
  59. abort(404);
  60. }
  61. }
  62. if($status->type == 'archived') {
  63. if(Auth::user()->profile_id !== $status->profile_id) {
  64. abort(404);
  65. }
  66. }
  67. if($request->user() && $request->user()->profile_id != $status->profile_id) {
  68. StatusView::firstOrCreate([
  69. 'status_id' => $status->id,
  70. 'status_profile_id' => $status->profile_id,
  71. 'profile_id' => $request->user()->profile_id
  72. ]);
  73. }
  74. if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  75. return $this->showActivityPub($request, $status);
  76. }
  77. $template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
  78. return view($template, compact('user', 'status'));
  79. }
  80. public function shortcodeRedirect(Request $request, $id)
  81. {
  82. abort(404);
  83. }
  84. public function showId(int $id)
  85. {
  86. abort(404);
  87. $status = Status::whereNull('reblog_of_id')
  88. ->whereIn('scope', ['public', 'unlisted'])
  89. ->findOrFail($id);
  90. return redirect($status->url());
  91. }
  92. public function showEmbed(Request $request, $username, int $id)
  93. {
  94. if(!config('instance.embed.post')) {
  95. $res = view('status.embed-removed');
  96. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  97. }
  98. $profile = Profile::whereNull(['domain','status'])
  99. ->whereIsPrivate(false)
  100. ->whereUsername($username)
  101. ->first();
  102. if(!$profile) {
  103. $content = view('status.embed-removed');
  104. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  105. }
  106. $aiCheck = Cache::remember('profile:ai-check:spam-login:' . $profile->id, 86400, function() use($profile) {
  107. $exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
  108. if($exists) {
  109. return true;
  110. }
  111. return false;
  112. });
  113. if($aiCheck) {
  114. $res = view('status.embed-removed');
  115. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  116. }
  117. $status = Status::whereProfileId($profile->id)
  118. ->whereNull('uri')
  119. ->whereScope('public')
  120. ->whereIsNsfw(false)
  121. ->whereIn('type', ['photo', 'video','photo:album'])
  122. ->find($id);
  123. if(!$status) {
  124. $content = view('status.embed-removed');
  125. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  126. }
  127. $showLikes = $request->filled('likes') && $request->likes == true;
  128. $showCaption = $request->filled('caption') && $request->caption !== false;
  129. $layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
  130. $content = view('status.embed', compact('status', 'showLikes', 'showCaption', 'layout'));
  131. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  132. }
  133. public function showObject(Request $request, $username, int $id)
  134. {
  135. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  136. if($user->status != null) {
  137. return ProfileController::accountCheck($user);
  138. }
  139. $status = Status::whereProfileId($user->id)
  140. ->whereNotIn('visibility',['draft','direct'])
  141. ->findOrFail($id);
  142. abort_if($status->uri, 404);
  143. if($status->visibility == 'private' || $user->is_private) {
  144. if(!Auth::check()) {
  145. abort(403);
  146. }
  147. $pid = Auth::user()->profile;
  148. if($user->followedBy($pid) == false && $user->id !== $pid->id) {
  149. abort(403);
  150. }
  151. }
  152. return $this->showActivityPub($request, $status);
  153. }
  154. public function compose()
  155. {
  156. $this->authCheck();
  157. return view('status.compose');
  158. }
  159. public function store(Request $request)
  160. {
  161. return;
  162. }
  163. public function delete(Request $request)
  164. {
  165. $this->authCheck();
  166. $this->validate($request, [
  167. 'item' => 'required|integer|min:1',
  168. ]);
  169. $status = Status::findOrFail($request->input('item'));
  170. $user = Auth::user();
  171. if($status->profile_id != $user->profile->id &&
  172. $user->is_admin == true &&
  173. $status->uri == null
  174. ) {
  175. $media = $status->media;
  176. $ai = new AccountInterstitial;
  177. $ai->user_id = $status->profile->user_id;
  178. $ai->type = 'post.removed';
  179. $ai->view = 'account.moderation.post.removed';
  180. $ai->item_type = 'App\Status';
  181. $ai->item_id = $status->id;
  182. $ai->has_media = (bool) $media->count();
  183. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  184. $ai->meta = json_encode([
  185. 'caption' => $status->caption,
  186. 'created_at' => $status->created_at,
  187. 'type' => $status->type,
  188. 'url' => $status->url(),
  189. 'is_nsfw' => $status->is_nsfw,
  190. 'scope' => $status->scope,
  191. 'reblog' => $status->reblog_of_id,
  192. 'likes_count' => $status->likes_count,
  193. 'reblogs_count' => $status->reblogs_count,
  194. ]);
  195. $ai->save();
  196. $u = $status->profile->user;
  197. $u->has_interstitial = true;
  198. $u->save();
  199. }
  200. if($status->in_reply_to_id) {
  201. $parent = Status::find($status->in_reply_to_id);
  202. if($parent && ($parent->profile_id == $user->profile_id) || ($status->profile_id == $user->profile_id) || $user->is_admin) {
  203. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  204. Cache::forget('profile:status_count:' . $status->profile_id);
  205. Cache::forget('profile:embed:' . $status->profile_id);
  206. StatusService::del($status->id, true);
  207. Cache::forget('profile:status_count:'.$status->profile_id);
  208. StatusDelete::dispatch($status);
  209. }
  210. } else if ($status->profile_id == $user->profile_id || $user->is_admin == true) {
  211. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  212. Cache::forget('profile:status_count:' . $status->profile_id);
  213. Cache::forget('profile:embed:' . $status->profile_id);
  214. StatusService::del($status->id, true);
  215. Cache::forget('profile:status_count:'.$status->profile_id);
  216. StatusDelete::dispatch($status);
  217. }
  218. if($request->wantsJson()) {
  219. return response()->json(['Status successfully deleted.']);
  220. } else {
  221. return redirect($user->url());
  222. }
  223. }
  224. public function storeShare(Request $request)
  225. {
  226. $this->authCheck();
  227. $this->validate($request, [
  228. 'item' => 'required|integer|min:1',
  229. ]);
  230. $user = Auth::user();
  231. $profile = $user->profile;
  232. $status = Status::whereScope('public')
  233. ->findOrFail($request->input('item'));
  234. $count = $status->reblogs_count;
  235. $exists = Status::whereProfileId(Auth::user()->profile->id)
  236. ->whereReblogOfId($status->id)
  237. ->exists();
  238. if ($exists == true) {
  239. $shares = Status::whereProfileId(Auth::user()->profile->id)
  240. ->whereReblogOfId($status->id)
  241. ->get();
  242. foreach ($shares as $share) {
  243. UndoSharePipeline::dispatch($share);
  244. ReblogService::del($profile->id, $status->id);
  245. $count--;
  246. }
  247. } else {
  248. $share = new Status();
  249. $share->profile_id = $profile->id;
  250. $share->reblog_of_id = $status->id;
  251. $share->in_reply_to_profile_id = $status->profile_id;
  252. $share->type = 'share';
  253. $share->save();
  254. $count++;
  255. SharePipeline::dispatch($share);
  256. ReblogService::add($profile->id, $status->id);
  257. }
  258. Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
  259. StatusService::del($status->id);
  260. if ($request->ajax()) {
  261. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  262. } else {
  263. $response = redirect($status->url());
  264. }
  265. return $response;
  266. }
  267. public function showActivityPub(Request $request, $status)
  268. {
  269. $object = $status->type == 'poll' ? new Question() : new Note();
  270. $fractal = new Fractal\Manager();
  271. $resource = new Fractal\Resource\Item($status, $object);
  272. $res = $fractal->createData($resource)->toArray();
  273. return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  274. }
  275. public function edit(Request $request, $username, $id)
  276. {
  277. $this->authCheck();
  278. $user = Auth::user()->profile;
  279. $status = Status::whereProfileId($user->id)
  280. ->with(['media'])
  281. ->findOrFail($id);
  282. $licenses = License::get();
  283. return view('status.edit', compact('user', 'status', 'licenses'));
  284. }
  285. public function editStore(Request $request, $username, $id)
  286. {
  287. $this->authCheck();
  288. $user = Auth::user()->profile;
  289. $status = Status::whereProfileId($user->id)
  290. ->with(['media'])
  291. ->findOrFail($id);
  292. $this->validate($request, [
  293. 'license' => 'nullable|integer|min:1|max:16',
  294. ]);
  295. $licenseId = $request->input('license');
  296. $status->media->each(function($media) use($licenseId) {
  297. $media->license = $licenseId;
  298. $media->save();
  299. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  300. });
  301. return redirect($status->url());
  302. }
  303. protected function authCheck()
  304. {
  305. if (Auth::check() == false) {
  306. abort(403);
  307. }
  308. }
  309. protected function validateVisibility($visibility)
  310. {
  311. $allowed = ['public', 'unlisted', 'private'];
  312. return in_array($visibility, $allowed) ? $visibility : 'public';
  313. }
  314. public static function mimeTypeCheck($mimes)
  315. {
  316. $allowed = explode(',', config_cache('pixelfed.media_types'));
  317. $count = count($mimes);
  318. $photos = 0;
  319. $videos = 0;
  320. foreach($mimes as $mime) {
  321. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  322. continue;
  323. }
  324. if(str_contains($mime, 'image/')) {
  325. $photos++;
  326. }
  327. if(str_contains($mime, 'video/')) {
  328. $videos++;
  329. }
  330. }
  331. if($photos == 1 && $videos == 0) {
  332. return 'photo';
  333. }
  334. if($videos == 1 && $photos == 0) {
  335. return 'video';
  336. }
  337. if($photos > 1 && $videos == 0) {
  338. return 'photo:album';
  339. }
  340. if($videos > 1 && $photos == 0) {
  341. return 'video:album';
  342. }
  343. if($photos >= 1 && $videos >= 1) {
  344. return 'photo:video:album';
  345. }
  346. return 'text';
  347. }
  348. public function toggleVisibility(Request $request) {
  349. $this->authCheck();
  350. $this->validate($request, [
  351. 'item' => 'required|string|min:1|max:20',
  352. 'disableComments' => 'required|boolean'
  353. ]);
  354. $user = Auth::user();
  355. $id = $request->input('item');
  356. $state = $request->input('disableComments');
  357. $status = Status::findOrFail($id);
  358. if($status->profile_id != $user->profile->id && $user->is_admin == false) {
  359. abort(403);
  360. }
  361. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  362. $status->save();
  363. return response()->json([200]);
  364. }
  365. public function storeView(Request $request)
  366. {
  367. abort_if(!$request->user(), 403);
  368. $views = $request->input('_v');
  369. $uid = $request->user()->profile_id;
  370. if(empty($views) || !is_array($views)) {
  371. return response()->json(0);
  372. }
  373. Cache::forget('profile:home-timeline-cursor:' . $request->user()->id);
  374. foreach($views as $view) {
  375. if(!isset($view['sid']) || !isset($view['pid'])) {
  376. continue;
  377. }
  378. DB::transaction(function () use($view, $uid) {
  379. StatusView::firstOrCreate([
  380. 'status_id' => $view['sid'],
  381. 'status_profile_id' => $view['pid'],
  382. 'profile_id' => $uid
  383. ]);
  384. });
  385. }
  386. return response()->json(1);
  387. }
  388. }