StatusController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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\CreateNote;
  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. $status = new Status();
  107. $status->profile_id = $profile->id;
  108. $status->caption = strip_tags($request->caption);
  109. $status->is_nsfw = $cw;
  110. // TODO: remove deprecated visibility in favor of scope
  111. $status->visibility = $visibility;
  112. $status->scope = $visibility;
  113. $status->save();
  114. $photos = $request->file('photo');
  115. $order = 1;
  116. $mimes = [];
  117. $medias = 0;
  118. foreach ($photos as $k => $v) {
  119. $allowedMimes = explode(',', config('pixelfed.media_types'));
  120. if(in_array($v->getMimeType(), $allowedMimes) == false) {
  121. continue;
  122. }
  123. $storagePath = "public/m/{$monthHash}/{$userHash}";
  124. $path = $v->store($storagePath);
  125. $hash = \hash_file('sha256', $v);
  126. $media = new Media();
  127. $media->status_id = $status->id;
  128. $media->profile_id = $profile->id;
  129. $media->user_id = $user->id;
  130. $media->media_path = $path;
  131. $media->original_sha256 = $hash;
  132. $media->size = $v->getSize();
  133. $media->mime = $v->getMimeType();
  134. $media->filter_class = $request->input('filter_class');
  135. $media->filter_name = $request->input('filter_name');
  136. $media->order = $order;
  137. $media->save();
  138. array_push($mimes, $media->mime);
  139. ImageOptimize::dispatch($media);
  140. $order++;
  141. $medias++;
  142. }
  143. if($medias == 0) {
  144. $status->delete();
  145. return;
  146. }
  147. $status->type = (new self)::mimeTypeCheck($mimes);
  148. $status->save();
  149. NewStatusPipeline::dispatch($status);
  150. // TODO: Send to subscribers
  151. return redirect($status->url());
  152. }
  153. public function delete(Request $request)
  154. {
  155. $this->authCheck();
  156. $this->validate($request, [
  157. 'item' => 'required|integer|min:1',
  158. ]);
  159. $status = Status::findOrFail($request->input('item'));
  160. if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
  161. StatusDelete::dispatch($status);
  162. }
  163. if($request->wantsJson()) {
  164. return response()->json(['Status successfully deleted.']);
  165. } else {
  166. return redirect(Auth::user()->url());
  167. }
  168. }
  169. public function storeShare(Request $request)
  170. {
  171. $this->authCheck();
  172. $this->validate($request, [
  173. 'item' => 'required|integer',
  174. ]);
  175. $profile = Auth::user()->profile;
  176. $status = Status::withCount('shares')->findOrFail($request->input('item'));
  177. $count = $status->shares_count;
  178. $exists = Status::whereProfileId(Auth::user()->profile->id)
  179. ->whereReblogOfId($status->id)
  180. ->count();
  181. if ($exists !== 0) {
  182. $shares = Status::whereProfileId(Auth::user()->profile->id)
  183. ->whereReblogOfId($status->id)
  184. ->get();
  185. foreach ($shares as $share) {
  186. $share->delete();
  187. $count--;
  188. }
  189. } else {
  190. $share = new Status();
  191. $share->profile_id = $profile->id;
  192. $share->reblog_of_id = $status->id;
  193. $share->save();
  194. $count++;
  195. }
  196. if ($request->ajax()) {
  197. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  198. } else {
  199. $response = redirect($status->url());
  200. }
  201. return $response;
  202. }
  203. public function showActivityPub(Request $request, $status)
  204. {
  205. $fractal = new Fractal\Manager();
  206. $resource = new Fractal\Resource\Item($status, new CreateNote());
  207. $res = $fractal->createData($resource)->toArray();
  208. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  209. }
  210. public function edit(Request $request, $username, $id)
  211. {
  212. $this->authCheck();
  213. $user = Auth::user()->profile;
  214. $status = Status::whereProfileId($user->id)
  215. ->with(['media'])
  216. ->findOrFail($id);
  217. return view('status.edit', compact('user', 'status'));
  218. }
  219. public function editStore(Request $request, $username, $id)
  220. {
  221. $this->authCheck();
  222. $user = Auth::user()->profile;
  223. $status = Status::whereProfileId($user->id)
  224. ->with(['media'])
  225. ->findOrFail($id);
  226. $this->validate($request, [
  227. 'id' => 'required|integer|min:1',
  228. 'caption' => 'nullable',
  229. 'filter' => 'nullable|alpha_dash|max:30',
  230. ]);
  231. $id = $request->input('id');
  232. $caption = $request->input('caption');
  233. $filter = $request->input('filter');
  234. $media = Media::whereProfileId($user->id)
  235. ->whereStatusId($status->id)
  236. ->find($id);
  237. $changed = false;
  238. if ($media->caption != $caption) {
  239. $media->caption = $caption;
  240. $changed = true;
  241. }
  242. if ($media->filter_class != $filter) {
  243. $media->filter_class = $filter;
  244. $changed = true;
  245. }
  246. if ($changed === true) {
  247. $media->save();
  248. }
  249. return response()->json([], 200);
  250. }
  251. protected function authCheck()
  252. {
  253. if (Auth::check() == false) {
  254. abort(403);
  255. }
  256. }
  257. protected function validateVisibility($visibility)
  258. {
  259. $allowed = ['public', 'unlisted', 'private'];
  260. return in_array($visibility, $allowed) ? $visibility : 'public';
  261. }
  262. public static function mimeTypeCheck($mimes)
  263. {
  264. $allowed = explode(',', config('pixelfed.media_types'));
  265. $count = count($mimes);
  266. $photos = 0;
  267. $videos = 0;
  268. foreach($mimes as $mime) {
  269. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  270. continue;
  271. }
  272. if(str_contains($mime, 'image/')) {
  273. $photos++;
  274. }
  275. if(str_contains($mime, 'video/')) {
  276. $videos++;
  277. }
  278. }
  279. if($photos == 1 && $videos == 0) {
  280. return 'photo';
  281. }
  282. if($videos == 1 && $photos == 0) {
  283. return 'video';
  284. }
  285. if($photos > 1 && $videos == 0) {
  286. return 'photo:album';
  287. }
  288. if($videos > 1 && $photos == 0) {
  289. return 'video:album';
  290. }
  291. if($photos >= 1 && $videos >= 1) {
  292. return 'photo:video:album';
  293. }
  294. }
  295. }