StatusController.php 9.3 KB

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