StatusController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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($count >= 0) {
  219. $status->reblogs_count = $count;
  220. $status->save();
  221. }
  222. if ($request->ajax()) {
  223. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  224. } else {
  225. $response = redirect($status->url());
  226. }
  227. return $response;
  228. }
  229. public function showActivityPub(Request $request, $status)
  230. {
  231. $fractal = new Fractal\Manager();
  232. $resource = new Fractal\Resource\Item($status, new Note());
  233. $res = $fractal->createData($resource)->toArray();
  234. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  235. }
  236. public function edit(Request $request, $username, $id)
  237. {
  238. $this->authCheck();
  239. $user = Auth::user()->profile;
  240. $status = Status::whereProfileId($user->id)
  241. ->with(['media'])
  242. ->findOrFail($id);
  243. return view('status.edit', compact('user', 'status'));
  244. }
  245. public function editStore(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. $this->validate($request, [
  253. 'id' => 'required|integer|min:1',
  254. 'caption' => 'nullable',
  255. 'filter' => 'nullable|alpha_dash|max:30',
  256. ]);
  257. $id = $request->input('id');
  258. $caption = $request->input('caption');
  259. $filter = $request->input('filter');
  260. $media = Media::whereProfileId($user->id)
  261. ->whereStatusId($status->id)
  262. ->find($id);
  263. $changed = false;
  264. if ($media->caption != $caption) {
  265. $media->caption = $caption;
  266. $changed = true;
  267. }
  268. if ($media->filter_class != $filter) {
  269. $media->filter_class = $filter;
  270. $changed = true;
  271. }
  272. if ($changed === true) {
  273. $media->save();
  274. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  275. }
  276. return response()->json([], 200);
  277. }
  278. protected function authCheck()
  279. {
  280. if (Auth::check() == false) {
  281. abort(403);
  282. }
  283. }
  284. protected function validateVisibility($visibility)
  285. {
  286. $allowed = ['public', 'unlisted', 'private'];
  287. return in_array($visibility, $allowed) ? $visibility : 'public';
  288. }
  289. public static function mimeTypeCheck($mimes)
  290. {
  291. $allowed = explode(',', config('pixelfed.media_types'));
  292. $count = count($mimes);
  293. $photos = 0;
  294. $videos = 0;
  295. foreach($mimes as $mime) {
  296. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  297. continue;
  298. }
  299. if(str_contains($mime, 'image/')) {
  300. $photos++;
  301. }
  302. if(str_contains($mime, 'video/')) {
  303. $videos++;
  304. }
  305. }
  306. if($photos == 1 && $videos == 0) {
  307. return 'photo';
  308. }
  309. if($videos == 1 && $photos == 0) {
  310. return 'video';
  311. }
  312. if($photos > 1 && $videos == 0) {
  313. return 'photo:album';
  314. }
  315. if($videos > 1 && $photos == 0) {
  316. return 'video:album';
  317. }
  318. if($photos >= 1 && $videos >= 1) {
  319. return 'photo:video:album';
  320. }
  321. }
  322. public function toggleVisibility(Request $request) {
  323. $this->authCheck();
  324. $this->validate($request, [
  325. 'item' => 'required|string|min:1|max:20',
  326. 'disableComments' => 'required|boolean'
  327. ]);
  328. $user = Auth::user();
  329. $id = $request->input('item');
  330. $state = $request->input('disableComments');
  331. $status = Status::findOrFail($id);
  332. if($status->profile_id != $user->profile->id && $user->is_admin == false) {
  333. abort(403);
  334. }
  335. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  336. $status->save();
  337. return response()->json([200]);
  338. }
  339. }