StatusController.php 7.7 KB

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