StatusController.php 7.5 KB

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