1
0

StatusController.php 11 KB

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