StatusController.php 7.7 KB

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