StatusController.php 9.2 KB

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