StatusController.php 11 KB

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