StatusController.php 11 KB

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