AccountController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Carbon\Carbon;
  5. use App\Mail\ConfirmEmail;
  6. use Auth, DB, Cache, Mail, Redis;
  7. use App\{EmailVerification, Notification, Profile, User};
  8. class AccountController extends Controller
  9. {
  10. public function __construct()
  11. {
  12. $this->middleware('auth');
  13. }
  14. public function notifications(Request $request)
  15. {
  16. $this->validate($request, [
  17. 'page' => 'nullable|min:1|max:3',
  18. 'a' => 'nullable|alpha_dash',
  19. ]);
  20. $profile = Auth::user()->profile;
  21. $action = $request->input('a');
  22. $timeago = Carbon::now()->subMonths(6);
  23. if($action && in_array($action, ['comment', 'follow', 'mention'])) {
  24. $notifications = Notification::whereProfileId($profile->id)
  25. ->whereAction($action)
  26. ->whereDate('created_at', '>', $timeago)
  27. ->orderBy('id','desc')
  28. ->simplePaginate(30);
  29. } else {
  30. $notifications = Notification::whereProfileId($profile->id)
  31. ->whereDate('created_at', '>', $timeago)
  32. ->orderBy('id','desc')
  33. ->simplePaginate(30);
  34. }
  35. return view('account.activity', compact('profile', 'notifications'));
  36. }
  37. public function verifyEmail(Request $request)
  38. {
  39. return view('account.verify_email');
  40. }
  41. public function sendVerifyEmail(Request $request)
  42. {
  43. $timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
  44. $recentAttempt = EmailVerification::whereUserId(Auth::id())
  45. ->where('created_at', '>', $timeLimit)->count();
  46. $exists = EmailVerification::whereUserId(Auth::id())->count();
  47. if($recentAttempt == 1 && $exists == 1) {
  48. return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
  49. } elseif ($recentAttempt == 0 && $exists !== 0) {
  50. // Delete old verification and send new one.
  51. EmailVerification::whereUserId(Auth::id())->delete();
  52. }
  53. $user = User::whereNull('email_verified_at')->find(Auth::id());
  54. $utoken = hash('sha512', $user->id);
  55. $rtoken = str_random(40);
  56. $verify = new EmailVerification;
  57. $verify->user_id = $user->id;
  58. $verify->email = $user->email;
  59. $verify->user_token = $utoken;
  60. $verify->random_token = $rtoken;
  61. $verify->save();
  62. Mail::to($user->email)->send(new ConfirmEmail($verify));
  63. return redirect()->back()->with('status', 'Email verification email sent!');
  64. }
  65. public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
  66. {
  67. $verify = EmailVerification::where('user_token', $userToken)
  68. ->where('random_token', $randomToken)
  69. ->firstOrFail();
  70. if(Auth::id() === $verify->user_id) {
  71. $user = User::find(Auth::id());
  72. $user->email_verified_at = Carbon::now();
  73. $user->save();
  74. return redirect('/');
  75. }
  76. }
  77. public function fetchNotifications($id)
  78. {
  79. $key = config('cache.prefix') . ":user.{$id}.notifications";
  80. $redis = Redis::connection();
  81. $notifications = $redis->lrange($key, 0, 30);
  82. if(empty($notifications)) {
  83. $notifications = Notification::whereProfileId($id)
  84. ->orderBy('id','desc')->take(30)->get();
  85. } else {
  86. $notifications = $this->hydrateNotifications($notifications);
  87. }
  88. return $notifications;
  89. }
  90. public function hydrateNotifications($keys)
  91. {
  92. $prefix = 'notification.';
  93. $notifications = collect([]);
  94. foreach($keys as $key) {
  95. $notifications->push(Cache::get("{$prefix}{$key}"));
  96. }
  97. return $notifications;
  98. }
  99. }