StatusController.php 12 KB

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