StatusController.php 11 KB

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