StatusController.php 9.5 KB

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