StatusController.php 12 KB

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