StatusController.php 12 KB

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