StatusController.php 11 KB

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