StatusController.php 11 KB

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