StatusController.php 9.0 KB

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