StatusController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\Jobs\SharePipeline\SharePipeline;
  7. use App\Media;
  8. use App\Profile;
  9. use App\Status;
  10. use App\Transformer\ActivityPub\StatusTransformer;
  11. use App\Transformer\ActivityPub\Verb\Note;
  12. use App\User;
  13. use Auth, Cache;
  14. use Illuminate\Http\Request;
  15. use League\Fractal;
  16. use App\Util\Media\Filter;
  17. use Illuminate\Support\Str;
  18. class StatusController extends Controller
  19. {
  20. public function show(Request $request, $username, int $id)
  21. {
  22. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  23. if($user->status != null) {
  24. return ProfileController::accountCheck($user);
  25. }
  26. $status = Status::whereProfileId($user->id)
  27. ->whereNull('reblog_of_id')
  28. ->whereNotIn('visibility',['draft','direct'])
  29. ->findOrFail($id);
  30. if($status->uri || $status->url) {
  31. $url = $status->uri ?? $status->url;
  32. if(ends_with($url, '/activity')) {
  33. $url = str_replace('/activity', '', $url);
  34. }
  35. return redirect($url);
  36. }
  37. if($status->visibility == 'private' || $user->is_private) {
  38. if(!Auth::check()) {
  39. abort(404);
  40. }
  41. $pid = Auth::user()->profile;
  42. if($user->followedBy($pid) == false && $user->id !== $pid->id && Auth::user()->is_admin == false) {
  43. abort(404);
  44. }
  45. }
  46. if($status->type == 'archived') {
  47. if(Auth::user()->profile_id !== $status->profile_id) {
  48. abort(404);
  49. }
  50. }
  51. if ($request->wantsJson() && config('federation.activitypub.enabled')) {
  52. return $this->showActivityPub($request, $status);
  53. }
  54. $template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
  55. return view($template, compact('user', 'status'));
  56. }
  57. public function showId(int $id)
  58. {
  59. abort(404);
  60. $status = Status::whereNull('reblog_of_id')
  61. ->whereIn('scope', ['public', 'unlisted'])
  62. ->findOrFail($id);
  63. return redirect($status->url());
  64. }
  65. public function showEmbed(Request $request, $username, int $id)
  66. {
  67. $profile = Profile::whereNull(['domain','status'])->whereUsername($username)->first();
  68. if(!$profile) {
  69. $content = view('status.embed-removed');
  70. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  71. }
  72. $status = Status::whereProfileId($profile->id)
  73. ->whereNull('uri')
  74. ->whereScope('public')
  75. ->whereIsNsfw(false)
  76. ->whereIn('type', ['photo', 'video'])
  77. ->find($id);
  78. if(!$status) {
  79. $content = view('status.embed-removed');
  80. return response($content)->header('X-Frame-Options', 'ALLOWALL');
  81. }
  82. $showLikes = $request->filled('likes') && $request->likes == true;
  83. $showCaption = $request->filled('caption') && $request->caption !== false;
  84. $layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
  85. $content = view('status.embed', compact('status', 'showLikes', 'showCaption', 'layout'));
  86. return response($content)->withHeaders(['x-frame-options' => 'ALLOWALL']);
  87. }
  88. public function showObject(Request $request, $username, int $id)
  89. {
  90. $user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  91. if($user->status != null) {
  92. return ProfileController::accountCheck($user);
  93. }
  94. $status = Status::whereProfileId($user->id)
  95. ->whereNotIn('visibility',['draft','direct'])
  96. ->findOrFail($id);
  97. abort_if($status->uri, 404);
  98. if($status->visibility == 'private' || $user->is_private) {
  99. if(!Auth::check()) {
  100. abort(403);
  101. }
  102. $pid = Auth::user()->profile;
  103. if($user->followedBy($pid) == false && $user->id !== $pid->id) {
  104. abort(403);
  105. }
  106. }
  107. return $this->showActivityPub($request, $status);
  108. }
  109. public function compose()
  110. {
  111. $this->authCheck();
  112. return view('status.compose');
  113. }
  114. public function store(Request $request)
  115. {
  116. return;
  117. }
  118. public function delete(Request $request)
  119. {
  120. $this->authCheck();
  121. $this->validate($request, [
  122. 'item' => 'required|integer|min:1',
  123. ]);
  124. $status = Status::findOrFail($request->input('item'));
  125. if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
  126. Cache::forget('profile:status_count:'.$status->profile_id);
  127. StatusDelete::dispatch($status);
  128. }
  129. if($request->wantsJson()) {
  130. return response()->json(['Status successfully deleted.']);
  131. } else {
  132. return redirect(Auth::user()->url());
  133. }
  134. }
  135. public function storeShare(Request $request)
  136. {
  137. $this->authCheck();
  138. $this->validate($request, [
  139. 'item' => 'required|integer|min:1',
  140. ]);
  141. $user = Auth::user();
  142. $profile = $user->profile;
  143. $status = Status::withCount('shares')
  144. ->whereIn('scope', ['public', 'unlisted'])
  145. ->findOrFail($request->input('item'));
  146. $count = $status->shares_count;
  147. $exists = Status::whereProfileId(Auth::user()->profile->id)
  148. ->whereReblogOfId($status->id)
  149. ->count();
  150. if ($exists !== 0) {
  151. $shares = Status::whereProfileId(Auth::user()->profile->id)
  152. ->whereReblogOfId($status->id)
  153. ->get();
  154. foreach ($shares as $share) {
  155. $share->delete();
  156. $count--;
  157. }
  158. } else {
  159. $share = new Status();
  160. $share->profile_id = $profile->id;
  161. $share->reblog_of_id = $status->id;
  162. $share->in_reply_to_profile_id = $status->profile_id;
  163. $share->save();
  164. $count++;
  165. SharePipeline::dispatch($share);
  166. }
  167. if($count >= 0) {
  168. $status->reblogs_count = $count;
  169. $status->save();
  170. }
  171. Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id);
  172. if ($request->ajax()) {
  173. $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count];
  174. } else {
  175. $response = redirect($status->url());
  176. }
  177. return $response;
  178. }
  179. public function showActivityPub(Request $request, $status)
  180. {
  181. $fractal = new Fractal\Manager();
  182. $resource = new Fractal\Resource\Item($status, new Note());
  183. $res = $fractal->createData($resource)->toArray();
  184. return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT);
  185. }
  186. public function edit(Request $request, $username, $id)
  187. {
  188. $this->authCheck();
  189. $user = Auth::user()->profile;
  190. $status = Status::whereProfileId($user->id)
  191. ->with(['media'])
  192. ->findOrFail($id);
  193. return view('status.edit', compact('user', 'status'));
  194. }
  195. public function editStore(Request $request, $username, $id)
  196. {
  197. $this->authCheck();
  198. $user = Auth::user()->profile;
  199. $status = Status::whereProfileId($user->id)
  200. ->with(['media'])
  201. ->findOrFail($id);
  202. $this->validate($request, [
  203. 'id' => 'required|integer|min:1',
  204. 'caption' => 'nullable',
  205. 'filter' => 'nullable|alpha_dash|max:30',
  206. ]);
  207. $id = $request->input('id');
  208. $caption = $request->input('caption');
  209. $filter = $request->input('filter');
  210. $media = Media::whereProfileId($user->id)
  211. ->whereStatusId($status->id)
  212. ->find($id);
  213. $changed = false;
  214. if ($media->caption != $caption) {
  215. $media->caption = $caption;
  216. $changed = true;
  217. }
  218. if ($media->filter_class != $filter) {
  219. $media->filter_class = $filter;
  220. $changed = true;
  221. }
  222. if ($changed === true) {
  223. $media->save();
  224. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  225. }
  226. return response()->json([], 200);
  227. }
  228. protected function authCheck()
  229. {
  230. if (Auth::check() == false) {
  231. abort(403);
  232. }
  233. }
  234. protected function validateVisibility($visibility)
  235. {
  236. $allowed = ['public', 'unlisted', 'private'];
  237. return in_array($visibility, $allowed) ? $visibility : 'public';
  238. }
  239. public static function mimeTypeCheck($mimes)
  240. {
  241. $allowed = explode(',', config('pixelfed.media_types'));
  242. $count = count($mimes);
  243. $photos = 0;
  244. $videos = 0;
  245. foreach($mimes as $mime) {
  246. if(in_array($mime, $allowed) == false && $mime !== 'video/mp4') {
  247. continue;
  248. }
  249. if(str_contains($mime, 'image/')) {
  250. $photos++;
  251. }
  252. if(str_contains($mime, 'video/')) {
  253. $videos++;
  254. }
  255. }
  256. if($photos == 1 && $videos == 0) {
  257. return 'photo';
  258. }
  259. if($videos == 1 && $photos == 0) {
  260. return 'video';
  261. }
  262. if($photos > 1 && $videos == 0) {
  263. return 'photo:album';
  264. }
  265. if($videos > 1 && $photos == 0) {
  266. return 'video:album';
  267. }
  268. if($photos >= 1 && $videos >= 1) {
  269. return 'photo:video:album';
  270. }
  271. }
  272. public function toggleVisibility(Request $request) {
  273. $this->authCheck();
  274. $this->validate($request, [
  275. 'item' => 'required|string|min:1|max:20',
  276. 'disableComments' => 'required|boolean'
  277. ]);
  278. $user = Auth::user();
  279. $id = $request->input('item');
  280. $state = $request->input('disableComments');
  281. $status = Status::findOrFail($id);
  282. if($status->profile_id != $user->profile->id && $user->is_admin == false) {
  283. abort(403);
  284. }
  285. $status->comments_disabled = $status->comments_disabled == true ? false : true;
  286. $status->save();
  287. return response()->json([200]);
  288. }
  289. }