StatusController.php 9.7 KB

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