StatusController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. 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. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  190. Cache::forget('profile:status_count:' . $status->profile_id);
  191. Cache::forget('profile:embed:' . $status->profile_id);
  192. StatusService::del($status->id, true);
  193. if ($status->profile_id == $user->profile->id || $user->is_admin == true) {
  194. Cache::forget('profile:status_count:'.$status->profile_id);
  195. StatusDelete::dispatchNow($status);
  196. }
  197. if($request->wantsJson()) {
  198. return response()->json(['Status successfully deleted.']);
  199. } else {
  200. return redirect($user->url());
  201. }
  202. }
  203. public function storeShare(Request $request)
  204. {
  205. $this->authCheck();
  206. $this->validate($request, [
  207. 'item' => 'required|integer|min:1',
  208. ]);
  209. $user = Auth::user();
  210. $profile = $user->profile;
  211. $status = Status::whereScope('public')
  212. ->findOrFail($request->input('item'));
  213. $count = $status->reblogs_count;
  214. $exists = Status::whereProfileId(Auth::user()->profile->id)
  215. ->whereReblogOfId($status->id)
  216. ->exists();
  217. if ($exists == true) {
  218. $shares = Status::whereProfileId(Auth::user()->profile->id)
  219. ->whereReblogOfId($status->id)
  220. ->get();
  221. foreach ($shares as $share) {
  222. UndoSharePipeline::dispatch($share);
  223. ReblogService::del($profile->id, $status->id);
  224. $count--;
  225. }
  226. } else {
  227. $share = new Status();
  228. $share->profile_id = $profile->id;
  229. $share->reblog_of_id = $status->id;
  230. $share->in_reply_to_profile_id = $status->profile_id;
  231. $share->type = 'share';
  232. $share->save();
  233. $count++;
  234. SharePipeline::dispatch($share);
  235. ReblogService::add($profile->id, $status->id);
  236. }
  237. Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
  238. StatusService::del($status->id);
  239. if ($request->ajax()) {
  240. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  241. } else {
  242. $response = redirect($status->url());
  243. }
  244. return $response;
  245. }
  246. public function showActivityPub(Request $request, $status)
  247. {
  248. $object = $status->type == 'poll' ? new Question() : new Note();
  249. $fractal = new Fractal\Manager();
  250. $resource = new Fractal\Resource\Item($status, $object);
  251. $res = $fractal->createData($resource)->toArray();
  252. return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  253. }
  254. public function edit(Request $request, $username, $id)
  255. {
  256. $this->authCheck();
  257. $user = Auth::user()->profile;
  258. $status = Status::whereProfileId($user->id)
  259. ->with(['media'])
  260. ->findOrFail($id);
  261. $licenses = License::get();
  262. return view('status.edit', compact('user', 'status', 'licenses'));
  263. }
  264. public function editStore(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. $this->validate($request, [
  272. 'license' => 'nullable|integer|min:1|max:16',
  273. ]);
  274. $licenseId = $request->input('license');
  275. $status->media->each(function($media) use($licenseId) {
  276. $media->license = $licenseId;
  277. $media->save();
  278. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  279. });
  280. return redirect($status->url());
  281. }
  282. protected function authCheck()
  283. {
  284. if (Auth::check() == false) {
  285. abort(403);
  286. }
  287. }
  288. protected function validateVisibility($visibility)
  289. {
  290. $allowed = ['public', 'unlisted', 'private'];
  291. return in_array($visibility, $allowed) ? $visibility : 'public';
  292. }
  293. public static function mimeTypeCheck($mimes)
  294. {
  295. $allowed = explode(',', config_cache('pixelfed.media_types'));
  296. $count = count($mimes);
  297. $photos = 0;
  298. $videos = 0;
  299. foreach($mimes as $mime) {
  300. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  301. continue;
  302. }
  303. if(str_contains($mime, 'image/')) {
  304. $photos++;
  305. }
  306. if(str_contains($mime, 'video/')) {
  307. $videos++;
  308. }
  309. }
  310. if($photos == 1 && $videos == 0) {
  311. return 'photo';
  312. }
  313. if($videos == 1 && $photos == 0) {
  314. return 'video';
  315. }
  316. if($photos > 1 && $videos == 0) {
  317. return 'photo:album';
  318. }
  319. if($videos > 1 && $photos == 0) {
  320. return 'video:album';
  321. }
  322. if($photos >= 1 && $videos >= 1) {
  323. return 'photo:video:album';
  324. }
  325. return 'text';
  326. }
  327. public function toggleVisibility(Request $request) {
  328. $this->authCheck();
  329. $this->validate($request, [
  330. 'item' => 'required|string|min:1|max:20',
  331. 'disableComments' => 'required|boolean'
  332. ]);
  333. $user = Auth::user();
  334. $id = $request->input('item');
  335. $state = $request->input('disableComments');
  336. $status = Status::findOrFail($id);
  337. if($status->profile_id != $user->profile->id && $user->is_admin == false) {
  338. abort(403);
  339. }
  340. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  341. $status->save();
  342. return response()->json([200]);
  343. }
  344. public function storeView(Request $request)
  345. {
  346. abort_if(!$request->user(), 403);
  347. $views = $request->input('_v');
  348. $uid = $request->user()->profile_id;
  349. if(empty($views) || !is_array($views)) {
  350. return response()->json(0);
  351. }
  352. Cache::forget('profile:home-timeline-cursor:' . $request->user()->id);
  353. foreach($views as $view) {
  354. if(!isset($view['sid']) || !isset($view['pid'])) {
  355. continue;
  356. }
  357. DB::transaction(function () use($view, $uid) {
  358. StatusView::firstOrCreate([
  359. 'status_id' => $view['sid'],
  360. 'status_profile_id' => $view['pid'],
  361. 'profile_id' => $uid
  362. ]);
  363. });
  364. }
  365. return response()->json(1);
  366. }
  367. }