1
0

StatusController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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' => 'string|max:'.config('pixelfed.max_caption_length'),
  94. 'cw' => 'nullable|string',
  95. 'filter_class' => 'nullable|string',
  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. $count = $status->shares_count;
  181. $exists = Status::whereProfileId(Auth::user()->profile->id)
  182. ->whereReblogOfId($status->id)
  183. ->count();
  184. if ($exists !== 0) {
  185. $shares = Status::whereProfileId(Auth::user()->profile->id)
  186. ->whereReblogOfId($status->id)
  187. ->get();
  188. foreach ($shares as $share) {
  189. $share->delete();
  190. $count--;
  191. }
  192. } else {
  193. $share = new Status();
  194. $share->profile_id = $profile->id;
  195. $share->reblog_of_id = $status->id;
  196. $share->in_reply_to_profile_id = $status->profile_id;
  197. $share->save();
  198. $count++;
  199. SharePipeline::dispatch($share);
  200. }
  201. if ($request->ajax()) {
  202. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  203. } else {
  204. $response = redirect($status->url());
  205. }
  206. return $response;
  207. }
  208. public function showActivityPub(Request $request, $status)
  209. {
  210. $fractal = new Fractal\Manager();
  211. $resource = new Fractal\Resource\Item($status, new Note());
  212. $res = $fractal->createData($resource)->toArray();
  213. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  214. }
  215. public function edit(Request $request, $username, $id)
  216. {
  217. $this->authCheck();
  218. $user = Auth::user()->profile;
  219. $status = Status::whereProfileId($user->id)
  220. ->with(['media'])
  221. ->findOrFail($id);
  222. return view('status.edit', compact('user', 'status'));
  223. }
  224. public function editStore(Request $request, $username, $id)
  225. {
  226. $this->authCheck();
  227. $user = Auth::user()->profile;
  228. $status = Status::whereProfileId($user->id)
  229. ->with(['media'])
  230. ->findOrFail($id);
  231. $this->validate($request, [
  232. 'id' => 'required|integer|min:1',
  233. 'caption' => 'nullable',
  234. 'filter' => 'nullable|alpha_dash|max:30',
  235. ]);
  236. $id = $request->input('id');
  237. $caption = $request->input('caption');
  238. $filter = $request->input('filter');
  239. $media = Media::whereProfileId($user->id)
  240. ->whereStatusId($status->id)
  241. ->find($id);
  242. $changed = false;
  243. if ($media->caption != $caption) {
  244. $media->caption = $caption;
  245. $changed = true;
  246. }
  247. if ($media->filter_class != $filter) {
  248. $media->filter_class = $filter;
  249. $changed = true;
  250. }
  251. if ($changed === true) {
  252. $media->save();
  253. }
  254. return response()->json([], 200);
  255. }
  256. protected function authCheck()
  257. {
  258. if (Auth::check() == false) {
  259. abort(403);
  260. }
  261. }
  262. protected function validateVisibility($visibility)
  263. {
  264. $allowed = ['public', 'unlisted', 'private'];
  265. return in_array($visibility, $allowed) ? $visibility : 'public';
  266. }
  267. public static function mimeTypeCheck($mimes)
  268. {
  269. $allowed = explode(',', config('pixelfed.media_types'));
  270. $count = count($mimes);
  271. $photos = 0;
  272. $videos = 0;
  273. foreach($mimes as $mime) {
  274. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  275. continue;
  276. }
  277. if(str_contains($mime, 'image/')) {
  278. $photos++;
  279. }
  280. if(str_contains($mime, 'video/')) {
  281. $videos++;
  282. }
  283. }
  284. if($photos == 1 && $videos == 0) {
  285. return 'photo';
  286. }
  287. if($videos == 1 && $photos == 0) {
  288. return 'video';
  289. }
  290. if($photos > 1 && $videos == 0) {
  291. return 'photo:album';
  292. }
  293. if($videos > 1 && $photos == 0) {
  294. return 'video:album';
  295. }
  296. if($photos >= 1 && $videos >= 1) {
  297. return 'photo:video:album';
  298. }
  299. }
  300. }