StatusController.php 10 KB

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