StatusController.php 12 KB

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