StatusController.php 16 KB

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