StatusController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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\AccountInterstitial;
  8. use App\Media;
  9. use App\Profile;
  10. use App\Status;
  11. use App\StatusView;
  12. use App\Transformer\ActivityPub\StatusTransformer;
  13. use App\Transformer\ActivityPub\Verb\Note;
  14. use App\User;
  15. use Auth, Cache;
  16. use Illuminate\Http\Request;
  17. use League\Fractal;
  18. use App\Util\Media\Filter;
  19. use Illuminate\Support\Str;
  20. use App\Services\HashidService;
  21. use App\Services\StatusService;
  22. use App\Util\Media\License;
  23. class StatusController extends Controller
  24. {
  25. public function show(Request $request, $username, int $id)
  26. {
  27. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  28. if($user->status != null) {
  29. return ProfileController::accountCheck($user);
  30. }
  31. $status = Status::whereProfileId($user->id)
  32. ->whereNull('reblog_of_id')
  33. ->whereIn('scope', ['public','unlisted', 'private'])
  34. ->findOrFail($id);
  35. if($status->uri || $status->url) {
  36. $url = $status->uri ?? $status->url;
  37. if(ends_with($url, '/activity')) {
  38. $url = str_replace('/activity', '', $url);
  39. }
  40. return redirect($url);
  41. }
  42. if($status->visibility == 'private' || $user->is_private) {
  43. if(!Auth::check()) {
  44. abort(404);
  45. }
  46. $pid = Auth::user()->profile;
  47. if($user->followedBy($pid) == false && $user->id !== $pid->id && Auth::user()->is_admin == false) {
  48. abort(404);
  49. }
  50. }
  51. if($status->type == 'archived') {
  52. if(Auth::user()->profile_id !== $status->profile_id) {
  53. abort(404);
  54. }
  55. }
  56. if($request->user() && $request->user()->profile_id != $status->profile_id) {
  57. StatusView::firstOrCreate([
  58. 'status_id' => $status->id,
  59. 'status_profile_id' => $status->profile_id,
  60. 'profile_id' => $request->user()->profile_id
  61. ]);
  62. }
  63. if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
  64. return $this->showActivityPub($request, $status);
  65. }
  66. $template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
  67. // $template = $status->type === 'video' &&
  68. // $request->has('video_beta') &&
  69. // $request->video_beta == 1 &&
  70. // $request->user() ?
  71. // 'status.show_video' : 'status.show';
  72. return view($template, compact('user', 'status'));
  73. }
  74. public function shortcodeRedirect(Request $request, $id)
  75. {
  76. abort_if(strlen($id) < 5, 404);
  77. if(!Auth::check()) {
  78. return redirect('/login?next='.urlencode('/' . $request->path()));
  79. }
  80. $id = HashidService::decode($id);
  81. $status = Status::find($id);
  82. if(!$status) {
  83. return redirect('/404');
  84. }
  85. return redirect($status->url());
  86. }
  87. public function showId(int $id)
  88. {
  89. abort(404);
  90. $status = Status::whereNull('reblog_of_id')
  91. ->whereIn('scope', ['public', 'unlisted'])
  92. ->findOrFail($id);
  93. return redirect($status->url());
  94. }
  95. public function showEmbed(Request $request, $username, int $id)
  96. {
  97. $profile = Profile::whereNull(['domain','status'])
  98. ->whereIsPrivate(false)
  99. ->whereUsername($username)
  100. ->first();
  101. if(!$profile) {
  102. $content = view('status.embed-removed');
  103. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  104. }
  105. $status = Status::whereProfileId($profile->id)
  106. ->whereNull('uri')
  107. ->whereScope('public')
  108. ->whereIsNsfw(false)
  109. ->whereIn('type', ['photo', 'video','photo:album'])
  110. ->find($id);
  111. if(!$status) {
  112. $content = view('status.embed-removed');
  113. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  114. }
  115. $showLikes = $request->filled('likes') && $request->likes == true;
  116. $showCaption = $request->filled('caption') && $request->caption !== false;
  117. $layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
  118. $content = view('status.embed', compact('status', 'showLikes', 'showCaption', 'layout'));
  119. return response($content)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
  120. }
  121. public function showObject(Request $request, $username, int $id)
  122. {
  123. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  124. if($user->status != null) {
  125. return ProfileController::accountCheck($user);
  126. }
  127. $status = Status::whereProfileId($user->id)
  128. ->whereNotIn('visibility',['draft','direct'])
  129. ->findOrFail($id);
  130. abort_if($status->uri, 404);
  131. if($status->visibility == 'private' || $user->is_private) {
  132. if(!Auth::check()) {
  133. abort(403);
  134. }
  135. $pid = Auth::user()->profile;
  136. if($user->followedBy($pid) == false && $user->id !== $pid->id) {
  137. abort(403);
  138. }
  139. }
  140. return $this->showActivityPub($request, $status);
  141. }
  142. public function compose()
  143. {
  144. $this->authCheck();
  145. return view('status.compose');
  146. }
  147. public function store(Request $request)
  148. {
  149. return;
  150. }
  151. public function delete(Request $request)
  152. {
  153. $this->authCheck();
  154. $this->validate($request, [
  155. 'item' => 'required|integer|min:1',
  156. ]);
  157. $status = Status::findOrFail($request->input('item'));
  158. $user = Auth::user();
  159. if($status->profile_id != $user->profile->id &&
  160. $user->is_admin == true &&
  161. $status->uri == null
  162. ) {
  163. $media = $status->media;
  164. $ai = new AccountInterstitial;
  165. $ai->user_id = $status->profile->user_id;
  166. $ai->type = 'post.removed';
  167. $ai->view = 'account.moderation.post.removed';
  168. $ai->item_type = 'App\Status';
  169. $ai->item_id = $status->id;
  170. $ai->has_media = (bool) $media->count();
  171. $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
  172. $ai->meta = json_encode([
  173. 'caption' => $status->caption,
  174. 'created_at' => $status->created_at,
  175. 'type' => $status->type,
  176. 'url' => $status->url(),
  177. 'is_nsfw' => $status->is_nsfw,
  178. 'scope' => $status->scope,
  179. 'reblog' => $status->reblog_of_id,
  180. 'likes_count' => $status->likes_count,
  181. 'reblogs_count' => $status->reblogs_count,
  182. ]);
  183. $ai->save();
  184. $u = $status->profile->user;
  185. $u->has_interstitial = true;
  186. $u->save();
  187. }
  188. Cache::forget('_api:statuses:recent_9:' . $status->profile_id);
  189. Cache::forget('profile:status_count:' . $status->profile_id);
  190. Cache::forget('profile:embed:' . $status->profile_id);
  191. StatusService::del($status->id);
  192. if ($status->profile_id == $user->profile->id || $user->is_admin == true) {
  193. Cache::forget('profile:status_count:'.$status->profile_id);
  194. StatusDelete::dispatch($status);
  195. }
  196. if($request->wantsJson()) {
  197. return response()->json(['Status successfully deleted.']);
  198. } else {
  199. return redirect($user->url());
  200. }
  201. }
  202. public function storeShare(Request $request)
  203. {
  204. $this->authCheck();
  205. $this->validate($request, [
  206. 'item' => 'required|integer|min:1',
  207. ]);
  208. $user = Auth::user();
  209. $profile = $user->profile;
  210. $status = Status::withCount('shares')
  211. ->whereIn('scope', ['public', 'unlisted'])
  212. ->findOrFail($request->input('item'));
  213. $count = $status->shares()->count();
  214. $exists = Status::whereProfileId(Auth::user()->profile->id)
  215. ->whereReblogOfId($status->id)
  216. ->count();
  217. if ($exists !== 0) {
  218. $shares = Status::whereProfileId(Auth::user()->profile->id)
  219. ->whereReblogOfId($status->id)
  220. ->get();
  221. foreach ($shares as $share) {
  222. $share->delete();
  223. $count--;
  224. }
  225. } else {
  226. $share = new Status();
  227. $share->profile_id = $profile->id;
  228. $share->reblog_of_id = $status->id;
  229. $share->in_reply_to_profile_id = $status->profile_id;
  230. $share->save();
  231. $count++;
  232. SharePipeline::dispatch($share);
  233. }
  234. if($count >= 0) {
  235. $status->reblogs_count = $count;
  236. $status->save();
  237. }
  238. Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
  239. StatusService::del($status->id);
  240. if ($request->ajax()) {
  241. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  242. } else {
  243. $response = redirect($status->url());
  244. }
  245. return $response;
  246. }
  247. public function showActivityPub(Request $request, $status)
  248. {
  249. $fractal = new Fractal\Manager();
  250. $resource = new Fractal\Resource\Item($status, new Note());
  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. }
  326. public function toggleVisibility(Request $request) {
  327. $this->authCheck();
  328. $this->validate($request, [
  329. 'item' => 'required|string|min:1|max:20',
  330. 'disableComments' => 'required|boolean'
  331. ]);
  332. $user = Auth::user();
  333. $id = $request->input('item');
  334. $state = $request->input('disableComments');
  335. $status = Status::findOrFail($id);
  336. if($status->profile_id != $user->profile->id && $user->is_admin == false) {
  337. abort(403);
  338. }
  339. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  340. $status->save();
  341. return response()->json([200]);
  342. }
  343. public function storeView(Request $request)
  344. {
  345. abort_if(!$request->user(), 403);
  346. $this->validate($request, [
  347. 'status_id' => 'required|integer|exists:statuses,id',
  348. 'profile_id' => 'required|integer|exists:profiles,id'
  349. ]);
  350. $sid = (int) $request->input('status_id');
  351. $pid = (int) $request->input('profile_id');
  352. StatusView::firstOrCreate([
  353. 'status_id' => $sid,
  354. 'status_profile_id' => $pid,
  355. 'profile_id' => $request->user()->profile_id
  356. ]);
  357. return response()->json(1);
  358. }
  359. }