StatusController.php 8.6 KB

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