StatusController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. ->withCount(['likes', 'comments', 'media'])
  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 = $this->detectTemplate($status);
  36. $replies = Status::whereInReplyToId($status->id)->simplePaginate(30);
  37. return view($template, compact('user', 'status', 'replies'));
  38. }
  39. protected function detectTemplate($status)
  40. {
  41. $template = Cache::rememberForever('template:status:type:'.$status->id, function () use ($status) {
  42. $template = 'status.show.photo';
  43. if (!$status->media_path && $status->in_reply_to_id) {
  44. $template = 'status.reply';
  45. }
  46. if ($status->media->count() > 1) {
  47. $template = 'status.show.album';
  48. }
  49. if ($status->viewType() == 'video') {
  50. $template = 'status.show.video';
  51. }
  52. return $template;
  53. });
  54. return $template;
  55. }
  56. public function compose()
  57. {
  58. $this->authCheck();
  59. return view('status.compose');
  60. }
  61. public function store(Request $request)
  62. {
  63. $this->authCheck();
  64. $user = Auth::user();
  65. $size = Media::whereUserId($user->id)->sum('size') / 1000;
  66. $limit = (int) config('pixelfed.max_account_size');
  67. if ($size >= $limit) {
  68. return redirect()->back()->with('error', 'You have exceeded your storage limit. Please click <a href="#">here</a> for more info.');
  69. }
  70. $this->validate($request, [
  71. 'photo.*' => function() {
  72. return [
  73. 'required',
  74. 'mimes:' . config('pixelfed.media_types'),
  75. 'max:' . config('pixelfed.max_photo_size'),
  76. ];
  77. },
  78. 'caption' => 'string|max:'.config('pixelfed.max_caption_length'),
  79. 'cw' => 'nullable|string',
  80. 'filter_class' => 'nullable|string',
  81. 'filter_name' => 'nullable|string',
  82. 'visibility' => 'required|string|min:5|max:10',
  83. ]);
  84. if (count($request->file('photo')) > config('pixelfed.max_album_length')) {
  85. return redirect()->back()->with('error', 'Too many files, max limit per post: '.config('pixelfed.max_album_length'));
  86. }
  87. $cw = $request->filled('cw') && $request->cw == 'on' ? true : false;
  88. $monthHash = hash('sha1', date('Y').date('m'));
  89. $userHash = hash('sha1', $user->id.(string) $user->created_at);
  90. $profile = $user->profile;
  91. $visibility = $this->validateVisibility($request->visibility);
  92. $status = new Status();
  93. $status->profile_id = $profile->id;
  94. $status->caption = strip_tags($request->caption);
  95. $status->is_nsfw = $cw;
  96. $status->visibility = $visibility;
  97. $status->save();
  98. $photos = $request->file('photo');
  99. $order = 1;
  100. foreach ($photos as $k => $v) {
  101. $storagePath = "public/m/{$monthHash}/{$userHash}";
  102. $path = $v->store($storagePath);
  103. $hash = \hash_file('sha256', $v);
  104. $media = new Media();
  105. $media->status_id = $status->id;
  106. $media->profile_id = $profile->id;
  107. $media->user_id = $user->id;
  108. $media->media_path = $path;
  109. $media->original_sha256 = $hash;
  110. $media->size = $v->getClientSize();
  111. $media->mime = $v->getClientMimeType();
  112. $media->filter_class = $request->input('filter_class');
  113. $media->filter_name = $request->input('filter_name');
  114. $media->order = $order;
  115. $media->save();
  116. ImageOptimize::dispatch($media);
  117. $order++;
  118. }
  119. NewStatusPipeline::dispatch($status);
  120. // TODO: Send to subscribers
  121. return redirect($status->url());
  122. }
  123. public function delete(Request $request)
  124. {
  125. if (!Auth::check()) {
  126. abort(403);
  127. }
  128. $this->validate($request, [
  129. 'type' => 'required|string',
  130. 'item' => 'required|integer|min:1',
  131. ]);
  132. $status = Status::findOrFail($request->input('item'));
  133. if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
  134. StatusDelete::dispatch($status);
  135. }
  136. return redirect(Auth::user()->url());
  137. }
  138. public function storeShare(Request $request)
  139. {
  140. $this->validate($request, [
  141. 'item' => 'required|integer',
  142. ]);
  143. $profile = Auth::user()->profile;
  144. $status = Status::withCount('shares')->findOrFail($request->input('item'));
  145. $count = $status->shares_count;
  146. $exists = Status::whereProfileId(Auth::user()->profile->id)
  147. ->whereReblogOfId($status->id)
  148. ->count();
  149. if ($exists !== 0) {
  150. $shares = Status::whereProfileId(Auth::user()->profile->id)
  151. ->whereReblogOfId($status->id)
  152. ->get();
  153. foreach ($shares as $share) {
  154. $share->delete();
  155. $count--;
  156. }
  157. } else {
  158. $share = new Status();
  159. $share->profile_id = $profile->id;
  160. $share->reblog_of_id = $status->id;
  161. $share->save();
  162. $count++;
  163. }
  164. if ($request->ajax()) {
  165. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  166. } else {
  167. $response = redirect($status->url());
  168. }
  169. return $response;
  170. }
  171. public function showActivityPub(Request $request, $status)
  172. {
  173. $fractal = new Fractal\Manager();
  174. $resource = new Fractal\Resource\Item($status, new StatusTransformer());
  175. $res = $fractal->createData($resource)->toArray();
  176. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  177. }
  178. public function edit(Request $request, $username, $id)
  179. {
  180. $this->authCheck();
  181. $user = Auth::user()->profile;
  182. $status = Status::whereProfileId($user->id)
  183. ->with(['media'])
  184. ->findOrFail($id);
  185. return view('status.edit', compact('user', 'status'));
  186. }
  187. public function editStore(Request $request, $username, $id)
  188. {
  189. $this->authCheck();
  190. $user = Auth::user()->profile;
  191. $status = Status::whereProfileId($user->id)
  192. ->with(['media'])
  193. ->findOrFail($id);
  194. $this->validate($request, [
  195. 'id' => 'required|integer|min:1',
  196. 'caption' => 'nullable',
  197. 'filter' => 'nullable|alpha_dash|max:30',
  198. ]);
  199. $id = $request->input('id');
  200. $caption = $request->input('caption');
  201. $filter = $request->input('filter');
  202. $media = Media::whereProfileId($user->id)
  203. ->whereStatusId($status->id)
  204. ->find($id);
  205. $changed = false;
  206. if ($media->caption != $caption) {
  207. $media->caption = $caption;
  208. $changed = true;
  209. }
  210. if ($media->filter_class != $filter) {
  211. $media->filter_class = $filter;
  212. $changed = true;
  213. }
  214. if ($changed === true) {
  215. $media->save();
  216. }
  217. return response()->json([], 200);
  218. }
  219. protected function authCheck()
  220. {
  221. if (Auth::check() == false) {
  222. abort(403);
  223. }
  224. }
  225. protected function validateVisibility($visibility)
  226. {
  227. $allowed = ['public', 'unlisted', 'private'];
  228. return in_array($visibility, $allowed) ? $visibility : 'public';
  229. }
  230. }