StatusController.php 9.1 KB

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