StatusController.php 11 KB

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