StatusController.php 13 KB

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