StatusController.php 15 KB

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