AccountController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 followingActivity(Request $request)
  38. {
  39. $this->validate($request, [
  40. 'page' => 'nullable|min:1|max:3',
  41. 'a' => 'nullable|alpha_dash',
  42. ]);
  43. $profile = Auth::user()->profile;
  44. $action = $request->input('a');
  45. $timeago = Carbon::now()->subMonths(1);
  46. $following = $profile->following->pluck('id');
  47. $notifications = Notification::whereIn('actor_id', $following)
  48. ->where('profile_id', '!=', $profile->id)
  49. ->whereDate('created_at', '>', $timeago)
  50. ->orderBy('notifications.id','desc')
  51. ->simplePaginate(30);
  52. return view('account.following', compact('profile', 'notifications'));
  53. }
  54. public function verifyEmail(Request $request)
  55. {
  56. return view('account.verify_email');
  57. }
  58. public function sendVerifyEmail(Request $request)
  59. {
  60. $timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
  61. $recentAttempt = EmailVerification::whereUserId(Auth::id())
  62. ->where('created_at', '>', $timeLimit)->count();
  63. $exists = EmailVerification::whereUserId(Auth::id())->count();
  64. if($recentAttempt == 1 && $exists == 1) {
  65. return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
  66. } elseif ($recentAttempt == 0 && $exists !== 0) {
  67. // Delete old verification and send new one.
  68. EmailVerification::whereUserId(Auth::id())->delete();
  69. }
  70. $user = User::whereNull('email_verified_at')->find(Auth::id());
  71. $utoken = hash('sha512', $user->id);
  72. $rtoken = str_random(40);
  73. $verify = new EmailVerification;
  74. $verify->user_id = $user->id;
  75. $verify->email = $user->email;
  76. $verify->user_token = $utoken;
  77. $verify->random_token = $rtoken;
  78. $verify->save();
  79. Mail::to($user->email)->send(new ConfirmEmail($verify));
  80. return redirect()->back()->with('status', 'Email verification email sent!');
  81. }
  82. public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
  83. {
  84. $verify = EmailVerification::where('user_token', $userToken)
  85. ->where('random_token', $randomToken)
  86. ->firstOrFail();
  87. if(Auth::id() === $verify->user_id) {
  88. $user = User::find(Auth::id());
  89. $user->email_verified_at = Carbon::now();
  90. $user->save();
  91. return redirect('/');
  92. }
  93. }
  94. public function fetchNotifications($id)
  95. {
  96. $key = config('cache.prefix') . ":user.{$id}.notifications";
  97. $redis = Redis::connection();
  98. $notifications = $redis->lrange($key, 0, 30);
  99. if(empty($notifications)) {
  100. $notifications = Notification::whereProfileId($id)
  101. ->orderBy('id','desc')->take(30)->get();
  102. } else {
  103. $notifications = $this->hydrateNotifications($notifications);
  104. }
  105. return $notifications;
  106. }
  107. public function hydrateNotifications($keys)
  108. {
  109. $prefix = 'notification.';
  110. $notifications = collect([]);
  111. foreach($keys as $key) {
  112. $notifications->push(Cache::get("{$prefix}{$key}"));
  113. }
  114. return $notifications;
  115. }
  116. }