AccountController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\EmailVerification;
  4. use App\Follower;
  5. use App\FollowRequest;
  6. use App\Jobs\FollowPipeline\FollowPipeline;
  7. use App\Mail\ConfirmEmail;
  8. use App\Notification;
  9. use App\Profile;
  10. use App\User;
  11. use App\UserFilter;
  12. use Auth;
  13. use Cache;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\Request;
  16. use Mail;
  17. use Redis;
  18. use PragmaRX\Google2FA\Google2FA;
  19. class AccountController extends Controller
  20. {
  21. protected $filters = [
  22. 'user.mute',
  23. 'user.block',
  24. ];
  25. public function __construct()
  26. {
  27. $this->middleware('auth');
  28. }
  29. public function notifications(Request $request)
  30. {
  31. $this->validate($request, [
  32. 'page' => 'nullable|min:1|max:3',
  33. 'a' => 'nullable|alpha_dash',
  34. ]);
  35. $profile = Auth::user()->profile;
  36. $action = $request->input('a');
  37. $timeago = Carbon::now()->subMonths(6);
  38. if ($action && in_array($action, ['comment', 'follow', 'mention'])) {
  39. $notifications = Notification::whereProfileId($profile->id)
  40. ->whereAction($action)
  41. ->whereDate('created_at', '>', $timeago)
  42. ->orderBy('id', 'desc')
  43. ->simplePaginate(30);
  44. } else {
  45. $notifications = Notification::whereProfileId($profile->id)
  46. ->whereDate('created_at', '>', $timeago)
  47. ->orderBy('id', 'desc')
  48. ->simplePaginate(30);
  49. }
  50. return view('account.activity', compact('profile', 'notifications'));
  51. }
  52. public function followingActivity(Request $request)
  53. {
  54. $this->validate($request, [
  55. 'page' => 'nullable|min:1|max:3',
  56. 'a' => 'nullable|alpha_dash',
  57. ]);
  58. $profile = Auth::user()->profile;
  59. $action = $request->input('a');
  60. $timeago = Carbon::now()->subMonths(3);
  61. $following = $profile->following->pluck('id');
  62. $notifications = Notification::whereIn('actor_id', $following)
  63. ->where('profile_id', '!=', $profile->id)
  64. ->whereDate('created_at', '>', $timeago)
  65. ->orderBy('notifications.created_at', 'desc')
  66. ->simplePaginate(30);
  67. return view('account.following', compact('profile', 'notifications'));
  68. }
  69. public function verifyEmail(Request $request)
  70. {
  71. return view('account.verify_email');
  72. }
  73. public function sendVerifyEmail(Request $request)
  74. {
  75. $timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
  76. $recentAttempt = EmailVerification::whereUserId(Auth::id())
  77. ->where('created_at', '>', $timeLimit)->count();
  78. $exists = EmailVerification::whereUserId(Auth::id())->count();
  79. if ($recentAttempt == 1 && $exists == 1) {
  80. return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
  81. } elseif ($recentAttempt == 0 && $exists !== 0) {
  82. // Delete old verification and send new one.
  83. EmailVerification::whereUserId(Auth::id())->delete();
  84. }
  85. $user = User::whereNull('email_verified_at')->find(Auth::id());
  86. $utoken = hash('sha512', $user->id);
  87. $rtoken = str_random(40);
  88. $verify = new EmailVerification();
  89. $verify->user_id = $user->id;
  90. $verify->email = $user->email;
  91. $verify->user_token = $utoken;
  92. $verify->random_token = $rtoken;
  93. $verify->save();
  94. Mail::to($user->email)->send(new ConfirmEmail($verify));
  95. return redirect()->back()->with('status', 'Verification email sent!');
  96. }
  97. public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
  98. {
  99. $verify = EmailVerification::where('user_token', $userToken)
  100. ->where('random_token', $randomToken)
  101. ->firstOrFail();
  102. if (Auth::id() === $verify->user_id) {
  103. $user = User::find(Auth::id());
  104. $user->email_verified_at = Carbon::now();
  105. $user->save();
  106. return redirect('/');
  107. }
  108. }
  109. public function fetchNotifications($id)
  110. {
  111. $key = config('cache.prefix').":user.{$id}.notifications";
  112. $redis = Redis::connection();
  113. $notifications = $redis->lrange($key, 0, 30);
  114. if (empty($notifications)) {
  115. $notifications = Notification::whereProfileId($id)
  116. ->orderBy('id', 'desc')->take(30)->get();
  117. } else {
  118. $notifications = $this->hydrateNotifications($notifications);
  119. }
  120. return $notifications;
  121. }
  122. public function hydrateNotifications($keys)
  123. {
  124. $prefix = 'notification.';
  125. $notifications = collect([]);
  126. foreach ($keys as $key) {
  127. $notifications->push(Cache::get("{$prefix}{$key}"));
  128. }
  129. return $notifications;
  130. }
  131. public function messages()
  132. {
  133. return view('account.messages');
  134. }
  135. public function showMessage(Request $request, $id)
  136. {
  137. return view('account.message');
  138. }
  139. public function mute(Request $request)
  140. {
  141. $this->validate($request, [
  142. 'type' => 'required|string',
  143. 'item' => 'required|integer|min:1',
  144. ]);
  145. $user = Auth::user()->profile;
  146. $type = $request->input('type');
  147. $item = $request->input('item');
  148. $action = "{$type}.mute";
  149. if (!in_array($action, $this->filters)) {
  150. return abort(406);
  151. }
  152. $filterable = [];
  153. switch ($type) {
  154. case 'user':
  155. $profile = Profile::findOrFail($item);
  156. if ($profile->id == $user->id) {
  157. return abort(403);
  158. }
  159. $class = get_class($profile);
  160. $filterable['id'] = $profile->id;
  161. $filterable['type'] = $class;
  162. break;
  163. default:
  164. // code...
  165. break;
  166. }
  167. $filter = UserFilter::firstOrCreate([
  168. 'user_id' => $user->id,
  169. 'filterable_id' => $filterable['id'],
  170. 'filterable_type' => $filterable['type'],
  171. 'filter_type' => 'mute',
  172. ]);
  173. return redirect()->back();
  174. }
  175. public function block(Request $request)
  176. {
  177. $this->validate($request, [
  178. 'type' => 'required|string',
  179. 'item' => 'required|integer|min:1',
  180. ]);
  181. $user = Auth::user()->profile;
  182. $type = $request->input('type');
  183. $item = $request->input('item');
  184. $action = "{$type}.block";
  185. if (!in_array($action, $this->filters)) {
  186. return abort(406);
  187. }
  188. $filterable = [];
  189. switch ($type) {
  190. case 'user':
  191. $profile = Profile::findOrFail($item);
  192. $class = get_class($profile);
  193. $filterable['id'] = $profile->id;
  194. $filterable['type'] = $class;
  195. Follower::whereProfileId($profile->id)->whereFollowingId($user->id)->delete();
  196. Notification::whereProfileId($user->id)->whereActorId($profile->id)->delete();
  197. break;
  198. default:
  199. // code...
  200. break;
  201. }
  202. $filter = UserFilter::firstOrCreate([
  203. 'user_id' => $user->id,
  204. 'filterable_id' => $filterable['id'],
  205. 'filterable_type' => $filterable['type'],
  206. 'filter_type' => 'block',
  207. ]);
  208. return redirect()->back();
  209. }
  210. public function followRequests(Request $request)
  211. {
  212. $pid = Auth::user()->profile->id;
  213. $followers = FollowRequest::whereFollowingId($pid)->orderBy('id','desc')->whereIsRejected(0)->simplePaginate(10);
  214. return view('account.follow-requests', compact('followers'));
  215. }
  216. public function followRequestHandle(Request $request)
  217. {
  218. $this->validate($request, [
  219. 'action' => 'required|string|max:10',
  220. 'id' => 'required|integer|min:1'
  221. ]);
  222. $pid = Auth::user()->profile->id;
  223. $action = $request->input('action') === 'accept' ? 'accept' : 'reject';
  224. $id = $request->input('id');
  225. $followRequest = FollowRequest::whereFollowingId($pid)->findOrFail($id);
  226. $follower = $followRequest->follower;
  227. switch ($action) {
  228. case 'accept':
  229. $follow = new Follower();
  230. $follow->profile_id = $follower->id;
  231. $follow->following_id = $pid;
  232. $follow->save();
  233. FollowPipeline::dispatch($follow);
  234. $followRequest->delete();
  235. break;
  236. case 'reject':
  237. $followRequest->is_rejected = true;
  238. $followRequest->save();
  239. break;
  240. }
  241. return response()->json(['msg' => 'success'], 200);
  242. }
  243. public function sudoMode(Request $request)
  244. {
  245. return view('auth.sudo');
  246. }
  247. public function sudoModeVerify(Request $request)
  248. {
  249. $this->validate($request, [
  250. 'password' => 'required|string|max:500'
  251. ]);
  252. $user = Auth::user();
  253. $password = $request->input('password');
  254. $next = $request->session()->get('redirectNext', '/');
  255. if(password_verify($password, $user->password) === true) {
  256. $request->session()->put('sudoMode', time());
  257. return redirect($next);
  258. } else {
  259. return redirect()
  260. ->back()
  261. ->withErrors(['password' => __('auth.failed')]);
  262. }
  263. }
  264. public function twoFactorCheckpoint(Request $request)
  265. {
  266. return view('auth.checkpoint');
  267. }
  268. public function twoFactorVerify(Request $request)
  269. {
  270. $this->validate($request, [
  271. 'code' => 'required|string|max:32'
  272. ]);
  273. $user = Auth::user();
  274. $code = $request->input('code');
  275. $google2fa = new Google2FA();
  276. $verify = $google2fa->verifyKey($user->{'2fa_secret'}, $code);
  277. if($verify) {
  278. $request->session()->push('2fa.session.active', true);
  279. return redirect('/');
  280. } else {
  281. if($request->session()->has('2fa.attempts')) {
  282. $count = (int) $request->session()->has('2fa.attempts');
  283. $request->session()->push('2fa.attempts', $count + 1);
  284. } else {
  285. $request->session()->push('2fa.attempts', 1);
  286. }
  287. return redirect()->back()->withErrors([
  288. 'code' => 'Invalid code'
  289. ]);
  290. }
  291. }
  292. }