StatusController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 3600, function () use ($profile) {
  112. $user = Profile::find($profile['id']);
  113. if (! $user) {
  114. return true;
  115. }
  116. $exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count();
  117. if ($exists) {
  118. return true;
  119. }
  120. return false;
  121. });
  122. if ($aiCheck) {
  123. $res = view('status.embed-removed');
  124. return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  125. }
  126. $status = StatusService::get($id);
  127. if (
  128. ! $status ||
  129. ! isset($status['account'], $status['account']['id']) ||
  130. intval($status['account']['id']) !== intval($profile['id']) ||
  131. $status['sensitive'] ||
  132. $status['visibility'] !== 'public' ||
  133. $status['pf_type'] !== 'photo'
  134. ) {
  135. $content = view('status.embed-removed');
  136. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  137. }
  138. $showLikes = $request->filled('likes') && $request->likes == true;
  139. $showCaption = $request->filled('caption') && $request->caption !== false;
  140. $layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
  141. $content = view('status.embed', compact('status', 'showLikes', 'showCaption', 'layout'));
  142. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  143. }
  144. public function showObject(Request $request, $username, int $id)
  145. {
  146. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  147. if ($user->status != null) {
  148. return ProfileController::accountCheck($user);
  149. }
  150. $status = Status::whereProfileId($user->id)
  151. ->whereNotIn('visibility', ['draft', 'direct'])
  152. ->findOrFail($id);
  153. abort_if($status->uri, 404);
  154. if ($status->visibility == 'private' || $user->is_private) {
  155. if (! Auth::check()) {
  156. abort(403);
  157. }
  158. $pid = Auth::user()->profile;
  159. if ($user->followedBy($pid) == false && $user->id !== $pid->id) {
  160. abort(403);
  161. }
  162. }
  163. return $this->showActivityPub($request, $status);
  164. }
  165. public function compose()
  166. {
  167. $this->authCheck();
  168. return view('status.compose');
  169. }
  170. public function store(Request $request)
  171. {
  172. }
  173. public function delete(Request $request)
  174. {
  175. $this->authCheck();
  176. $this->validate($request, [
  177. 'item' => 'required|integer|min:1',
  178. ]);
  179. $status = Status::findOrFail($request->input('item'));
  180. $user = Auth::user();
  181. if ($status->profile_id != $user->profile->id &&
  182. $user->is_admin == true &&
  183. $status->uri == null
  184. ) {
  185. $media = $status->media;
  186. $ai = new AccountInterstitial;
  187. $ai->user_id = $status->profile->user_id;
  188. $ai->type = 'post.removed';
  189. $ai->view = 'account.moderation.post.removed';
  190. $ai->item_type = 'App\Status';
  191. $ai->item_id = $status->id;
  192. $ai->has_media = (bool) $media->count();
  193. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  194. $ai->meta = json_encode([
  195. 'caption' => $status->caption,
  196. 'created_at' => $status->created_at,
  197. 'type' => $status->type,
  198. 'url' => $status->url(),
  199. 'is_nsfw' => $status->is_nsfw,
  200. 'scope' => $status->scope,
  201. 'reblog' => $status->reblog_of_id,
  202. 'likes_count' => $status->likes_count,
  203. 'reblogs_count' => $status->reblogs_count,
  204. ]);
  205. $ai->save();
  206. $u = $status->profile->user;
  207. $u->has_interstitial = true;
  208. $u->save();
  209. }
  210. if ($status->in_reply_to_id) {
  211. $parent = Status::find($status->in_reply_to_id);
  212. if ($parent && ($parent->profile_id == $user->profile_id) || ($status->profile_id == $user->profile_id) || $user->is_admin) {
  213. Cache::forget('_api:statuses:recent_9:'.$status->profile_id);
  214. Cache::forget('profile:status_count:'.$status->profile_id);
  215. Cache::forget('profile:embed:'.$status->profile_id);
  216. StatusService::del($status->id, true);
  217. Cache::forget('profile:status_count:'.$status->profile_id);
  218. $status->uri ? RemoteStatusDelete::dispatch($status) : StatusDelete::dispatch($status);
  219. }
  220. } elseif ($status->profile_id == $user->profile_id || $user->is_admin == true) {
  221. Cache::forget('_api:statuses:recent_9:'.$status->profile_id);
  222. Cache::forget('profile:status_count:'.$status->profile_id);
  223. Cache::forget('profile:embed:'.$status->profile_id);
  224. StatusService::del($status->id, true);
  225. Cache::forget('profile:status_count:'.$status->profile_id);
  226. $status->uri ? RemoteStatusDelete::dispatch($status) : StatusDelete::dispatch($status);
  227. }
  228. if ($request->wantsJson()) {
  229. return response()->json(['Status successfully deleted.']);
  230. } else {
  231. return redirect($user->url());
  232. }
  233. }
  234. public function storeShare(Request $request)
  235. {
  236. $this->authCheck();
  237. $this->validate($request, [
  238. 'item' => 'required|integer|min:1',
  239. ]);
  240. $user = Auth::user();
  241. $profile = $user->profile;
  242. $status = Status::whereScope('public')
  243. ->findOrFail($request->input('item'));
  244. $count = $status->reblogs_count;
  245. $exists = Status::whereProfileId(Auth::user()->profile->id)
  246. ->whereReblogOfId($status->id)
  247. ->exists();
  248. if ($exists == true) {
  249. $shares = Status::whereProfileId(Auth::user()->profile->id)
  250. ->whereReblogOfId($status->id)
  251. ->get();
  252. foreach ($shares as $share) {
  253. UndoSharePipeline::dispatch($share);
  254. ReblogService::del($profile->id, $status->id);
  255. $count--;
  256. }
  257. } else {
  258. $share = new Status();
  259. $share->profile_id = $profile->id;
  260. $share->reblog_of_id = $status->id;
  261. $share->in_reply_to_profile_id = $status->profile_id;
  262. $share->type = 'share';
  263. $share->save();
  264. $count++;
  265. SharePipeline::dispatch($share);
  266. ReblogService::add($profile->id, $status->id);
  267. }
  268. Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
  269. StatusService::del($status->id);
  270. if ($request->ajax()) {
  271. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  272. } else {
  273. $response = redirect($status->url());
  274. }
  275. return $response;
  276. }
  277. public function showActivityPub(Request $request, $status)
  278. {
  279. $object = $status->type == 'poll' ? new Question() : new Note();
  280. $fractal = new Fractal\Manager();
  281. $resource = new Fractal\Resource\Item($status, $object);
  282. $res = $fractal->createData($resource)->toArray();
  283. return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  284. }
  285. public function edit(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. $licenses = License::get();
  293. return view('status.edit', compact('user', 'status', 'licenses'));
  294. }
  295. public function editStore(Request $request, $username, $id)
  296. {
  297. $this->authCheck();
  298. $user = Auth::user()->profile;
  299. $status = Status::whereProfileId($user->id)
  300. ->with(['media'])
  301. ->findOrFail($id);
  302. $this->validate($request, [
  303. 'license' => 'nullable|integer|min:1|max:16',
  304. ]);
  305. $licenseId = $request->input('license');
  306. $status->media->each(function ($media) use ($licenseId) {
  307. $media->license = $licenseId;
  308. $media->save();
  309. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  310. });
  311. return redirect($status->url());
  312. }
  313. protected function authCheck()
  314. {
  315. if (Auth::check() == false) {
  316. abort(403);
  317. }
  318. }
  319. protected function validateVisibility($visibility)
  320. {
  321. $allowed = ['public', 'unlisted', 'private'];
  322. return in_array($visibility, $allowed) ? $visibility : 'public';
  323. }
  324. public static function mimeTypeCheck($mimes)
  325. {
  326. $allowed = explode(',', config_cache('pixelfed.media_types'));
  327. $count = count($mimes);
  328. $photos = 0;
  329. $videos = 0;
  330. foreach ($mimes as $mime) {
  331. if (in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  332. continue;
  333. }
  334. if (str_contains($mime, 'image/')) {
  335. $photos++;
  336. }
  337. if (str_contains($mime, 'video/')) {
  338. $videos++;
  339. }
  340. }
  341. if ($photos == 1 && $videos == 0) {
  342. return 'photo';
  343. }
  344. if ($videos == 1 && $photos == 0) {
  345. return 'video';
  346. }
  347. if ($photos > 1 && $videos == 0) {
  348. return 'photo:album';
  349. }
  350. if ($videos > 1 && $photos == 0) {
  351. return 'video:album';
  352. }
  353. if ($photos >= 1 && $videos >= 1) {
  354. return 'photo:video:album';
  355. }
  356. return 'text';
  357. }
  358. public function toggleVisibility(Request $request)
  359. {
  360. $this->authCheck();
  361. $this->validate($request, [
  362. 'item' => 'required|string|min:1|max:20',
  363. 'disableComments' => 'required|boolean',
  364. ]);
  365. $user = Auth::user();
  366. $id = $request->input('item');
  367. $state = $request->input('disableComments');
  368. $status = Status::findOrFail($id);
  369. if ($status->profile_id != $user->profile->id && $user->is_admin == false) {
  370. abort(403);
  371. }
  372. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  373. $status->save();
  374. return response()->json([200]);
  375. }
  376. public function storeView(Request $request)
  377. {
  378. abort_if(! $request->user(), 403);
  379. $views = $request->input('_v');
  380. $uid = $request->user()->profile_id;
  381. if (empty($views) || ! is_array($views)) {
  382. return response()->json(0);
  383. }
  384. Cache::forget('profile:home-timeline-cursor:'.$request->user()->id);
  385. foreach ($views as $view) {
  386. if (! isset($view['sid']) || ! isset($view['pid'])) {
  387. continue;
  388. }
  389. DB::transaction(function () use ($view, $uid) {
  390. StatusView::firstOrCreate([
  391. 'status_id' => $view['sid'],
  392. 'status_profile_id' => $view['pid'],
  393. 'profile_id' => $uid,
  394. ]);
  395. });
  396. }
  397. return response()->json(1);
  398. }
  399. }