StatusController.php 11 KB

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