StatusController.php 14 KB

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