StatusController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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\Media;
  8. use App\Profile;
  9. use App\Status;
  10. use App\Transformer\ActivityPub\StatusTransformer;
  11. use App\Transformer\ActivityPub\Verb\Note;
  12. use App\User;
  13. use Auth, Cache;
  14. use Illuminate\Http\Request;
  15. use League\Fractal;
  16. use App\Util\Media\Filter;
  17. use Illuminate\Support\Str;
  18. class StatusController extends Controller
  19. {
  20. public function show(Request $request, $username, int $id)
  21. {
  22. // $id = strlen($id) < 17 ? array_first(\Hashids::decode($id)) : $id;
  23. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  24. if($user->status != null) {
  25. return ProfileController::accountCheck($user);
  26. }
  27. $status = Status::whereProfileId($user->id)
  28. ->whereNotIn('visibility',['draft','direct'])
  29. ->findOrFail($id);
  30. if($status->uri) {
  31. $url = $status->uri;
  32. if(ends_with($url, '/activity')) {
  33. $url = str_replace('/activity', '', $url);
  34. }
  35. return redirect($url);
  36. }
  37. if($status->visibility == 'private' || $user->is_private) {
  38. if(!Auth::check()) {
  39. abort(404);
  40. }
  41. $pid = Auth::user()->profile;
  42. if($user->followedBy($pid) == false && $user->id !== $pid->id && Auth::user()->is_admin == false) {
  43. abort(404);
  44. }
  45. }
  46. if ($request->wantsJson() && config('federation.activitypub.enabled')) {
  47. return $this->showActivityPub($request, $status);
  48. }
  49. $template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
  50. return view($template, compact('user', 'status'));
  51. }
  52. public function showObject(Request $request, $username, int $id)
  53. {
  54. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  55. if($user->status != null) {
  56. return ProfileController::accountCheck($user);
  57. }
  58. $status = Status::whereProfileId($user->id)
  59. ->whereNotIn('visibility',['draft','direct'])
  60. ->findOrFail($id);
  61. if($status->uri) {
  62. $url = $status->uri;
  63. if(ends_with($url, '/activity')) {
  64. $url = str_replace('/activity', '', $url);
  65. }
  66. return redirect($url);
  67. }
  68. if($status->visibility == 'private' || $user->is_private) {
  69. if(!Auth::check()) {
  70. abort(403);
  71. }
  72. $pid = Auth::user()->profile;
  73. if($user->followedBy($pid) == false && $user->id !== $pid->id) {
  74. abort(403);
  75. }
  76. }
  77. return $this->showActivityPub($request, $status);
  78. }
  79. public function compose()
  80. {
  81. $this->authCheck();
  82. return view('status.compose');
  83. }
  84. public function store(Request $request)
  85. {
  86. $this->authCheck();
  87. $user = Auth::user();
  88. $size = Media::whereUserId($user->id)->sum('size') / 1000;
  89. $limit = (int) config('pixelfed.max_account_size');
  90. if ($size >= $limit) {
  91. return redirect()->back()->with('error', 'You have exceeded your storage limit. Please click <a href="#">here</a> for more info.');
  92. }
  93. $this->validate($request, [
  94. 'photo.*' => 'required|mimetypes:' . config('pixelfed.media_types').'|max:' . config('pixelfed.max_photo_size'),
  95. 'caption' => 'nullable|string|max:'.config('pixelfed.max_caption_length'),
  96. 'cw' => 'nullable|string',
  97. 'filter_class' => 'nullable|alpha_dash|max:30',
  98. 'filter_name' => 'nullable|string',
  99. 'visibility' => 'required|string|min:5|max:10',
  100. ]);
  101. if (count($request->file('photo')) > config('pixelfed.max_album_length')) {
  102. return redirect()->back()->with('error', 'Too many files, max limit per post: '.config('pixelfed.max_album_length'));
  103. }
  104. $cw = $request->filled('cw') && $request->cw == 'on' ? true : false;
  105. $monthHash = hash('sha1', date('Y').date('m'));
  106. $userHash = hash('sha1', $user->id.(string) $user->created_at);
  107. $profile = $user->profile;
  108. $visibility = $this->validateVisibility($request->visibility);
  109. $cw = $profile->cw == true ? true : $cw;
  110. $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
  111. if(config('costar.enabled') == true) {
  112. $blockedKeywords = config('costar.keyword.block');
  113. if($blockedKeywords !== null) {
  114. $keywords = config('costar.keyword.block');
  115. foreach($keywords as $kw) {
  116. if(Str::contains($request->caption, $kw) == true) {
  117. abort(400, 'Invalid object');
  118. }
  119. }
  120. }
  121. }
  122. $status = new Status();
  123. $status->profile_id = $profile->id;
  124. $status->caption = strip_tags($request->caption);
  125. $status->is_nsfw = $cw;
  126. // TODO: remove deprecated visibility in favor of scope
  127. $status->visibility = $visibility;
  128. $status->scope = $visibility;
  129. $status->save();
  130. $photos = $request->file('photo');
  131. $order = 1;
  132. $mimes = [];
  133. $medias = 0;
  134. foreach ($photos as $k => $v) {
  135. $allowedMimes = explode(',', config('pixelfed.media_types'));
  136. if(in_array($v->getMimeType(), $allowedMimes) == false) {
  137. continue;
  138. }
  139. $filter_class = $request->input('filter_class');
  140. $filter_name = $request->input('filter_name');
  141. $storagePath = "public/m/{$monthHash}/{$userHash}";
  142. $path = $v->store($storagePath);
  143. $hash = \hash_file('sha256', $v);
  144. $media = new Media();
  145. $media->status_id = $status->id;
  146. $media->profile_id = $profile->id;
  147. $media->user_id = $user->id;
  148. $media->media_path = $path;
  149. $media->original_sha256 = $hash;
  150. $media->size = $v->getSize();
  151. $media->mime = $v->getMimeType();
  152. $media->filter_class = in_array($filter_class, Filter::classes()) ? $filter_class : null;
  153. $media->filter_name = in_array($filter_name, Filter::names()) ? $filter_name : null;
  154. $media->order = $order;
  155. $media->save();
  156. array_push($mimes, $media->mime);
  157. ImageOptimize::dispatch($media);
  158. $order++;
  159. $medias++;
  160. }
  161. if($medias == 0) {
  162. $status->delete();
  163. return;
  164. }
  165. $status->type = (new self)::mimeTypeCheck($mimes);
  166. $status->save();
  167. Cache::forget('profile:status_count:'.$profile->id);
  168. NewStatusPipeline::dispatch($status);
  169. // TODO: Send to subscribers
  170. return redirect($status->url());
  171. }
  172. public function delete(Request $request)
  173. {
  174. $this->authCheck();
  175. $this->validate($request, [
  176. 'item' => 'required|integer|min:1',
  177. ]);
  178. $status = Status::findOrFail($request->input('item'));
  179. if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
  180. Cache::forget('profile:status_count:'.$status->profile_id);
  181. StatusDelete::dispatch($status);
  182. }
  183. if($request->wantsJson()) {
  184. return response()->json(['Status successfully deleted.']);
  185. } else {
  186. return redirect(Auth::user()->url());
  187. }
  188. }
  189. public function storeShare(Request $request)
  190. {
  191. $this->authCheck();
  192. $this->validate($request, [
  193. 'item' => 'required|integer|min:1',
  194. ]);
  195. $profile = Auth::user()->profile;
  196. $status = Status::withCount('shares')->findOrFail($request->input('item'));
  197. $count = $status->shares_count;
  198. $exists = Status::whereProfileId(Auth::user()->profile->id)
  199. ->whereReblogOfId($status->id)
  200. ->count();
  201. if ($exists !== 0) {
  202. $shares = Status::whereProfileId(Auth::user()->profile->id)
  203. ->whereReblogOfId($status->id)
  204. ->get();
  205. foreach ($shares as $share) {
  206. $share->delete();
  207. $count--;
  208. }
  209. } else {
  210. $share = new Status();
  211. $share->profile_id = $profile->id;
  212. $share->reblog_of_id = $status->id;
  213. $share->in_reply_to_profile_id = $status->profile_id;
  214. $share->save();
  215. $count++;
  216. SharePipeline::dispatch($share);
  217. }
  218. if ($request->ajax()) {
  219. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  220. } else {
  221. $response = redirect($status->url());
  222. }
  223. return $response;
  224. }
  225. public function showActivityPub(Request $request, $status)
  226. {
  227. $fractal = new Fractal\Manager();
  228. $resource = new Fractal\Resource\Item($status, new Note());
  229. $res = $fractal->createData($resource)->toArray();
  230. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  231. }
  232. public function edit(Request $request, $username, $id)
  233. {
  234. $this->authCheck();
  235. $user = Auth::user()->profile;
  236. $status = Status::whereProfileId($user->id)
  237. ->with(['media'])
  238. ->findOrFail($id);
  239. return view('status.edit', compact('user', 'status'));
  240. }
  241. public function editStore(Request $request, $username, $id)
  242. {
  243. $this->authCheck();
  244. $user = Auth::user()->profile;
  245. $status = Status::whereProfileId($user->id)
  246. ->with(['media'])
  247. ->findOrFail($id);
  248. $this->validate($request, [
  249. 'id' => 'required|integer|min:1',
  250. 'caption' => 'nullable',
  251. 'filter' => 'nullable|alpha_dash|max:30',
  252. ]);
  253. $id = $request->input('id');
  254. $caption = $request->input('caption');
  255. $filter = $request->input('filter');
  256. $media = Media::whereProfileId($user->id)
  257. ->whereStatusId($status->id)
  258. ->find($id);
  259. $changed = false;
  260. if ($media->caption != $caption) {
  261. $media->caption = $caption;
  262. $changed = true;
  263. }
  264. if ($media->filter_class != $filter) {
  265. $media->filter_class = $filter;
  266. $changed = true;
  267. }
  268. if ($changed === true) {
  269. $media->save();
  270. }
  271. return response()->json([], 200);
  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('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. }