2018-05-26 23:00:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-09 23:30:54 +00:00
|
|
|
use Carbon\Carbon;
|
2018-06-14 05:30:43 +00:00
|
|
|
use App\Mail\ConfirmEmail;
|
|
|
|
use Auth, DB, Cache, Mail, Redis;
|
|
|
|
use App\{EmailVerification, Notification, Profile, User};
|
2018-05-26 23:00:07 +00:00
|
|
|
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notifications(Request $request)
|
|
|
|
{
|
2018-06-09 23:30:54 +00:00
|
|
|
$this->validate($request, [
|
2018-07-18 00:06:57 +00:00
|
|
|
'page' => 'nullable|min:1|max:3',
|
|
|
|
'a' => 'nullable|alpha_dash',
|
2018-06-09 23:30:54 +00:00
|
|
|
]);
|
2018-05-27 03:25:04 +00:00
|
|
|
$profile = Auth::user()->profile;
|
2018-07-18 00:06:57 +00:00
|
|
|
$action = $request->input('a');
|
2018-06-09 23:30:54 +00:00
|
|
|
$timeago = Carbon::now()->subMonths(6);
|
2018-07-18 00:06:57 +00:00
|
|
|
if($action && in_array($action, ['comment', 'follow', 'mention'])) {
|
|
|
|
$notifications = Notification::whereProfileId($profile->id)
|
|
|
|
->whereAction($action)
|
|
|
|
->whereDate('created_at', '>', $timeago)
|
|
|
|
->orderBy('id','desc')
|
|
|
|
->simplePaginate(30);
|
|
|
|
} else {
|
|
|
|
$notifications = Notification::whereProfileId($profile->id)
|
|
|
|
->whereDate('created_at', '>', $timeago)
|
|
|
|
->orderBy('id','desc')
|
|
|
|
->simplePaginate(30);
|
|
|
|
}
|
2018-06-09 23:30:54 +00:00
|
|
|
|
2018-05-27 03:25:04 +00:00
|
|
|
return view('account.activity', compact('profile', 'notifications'));
|
|
|
|
}
|
|
|
|
|
2018-08-10 01:56:50 +00:00
|
|
|
public function followingActivity(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'page' => 'nullable|min:1|max:3',
|
|
|
|
'a' => 'nullable|alpha_dash',
|
|
|
|
]);
|
|
|
|
$profile = Auth::user()->profile;
|
|
|
|
$action = $request->input('a');
|
|
|
|
$timeago = Carbon::now()->subMonths(1);
|
|
|
|
$following = $profile->following->pluck('id');
|
|
|
|
$notifications = Notification::whereIn('actor_id', $following)
|
|
|
|
->where('profile_id', '!=', $profile->id)
|
|
|
|
->whereDate('created_at', '>', $timeago)
|
|
|
|
->orderBy('notifications.id','desc')
|
|
|
|
->simplePaginate(30);
|
|
|
|
|
|
|
|
return view('account.following', compact('profile', 'notifications'));
|
|
|
|
}
|
|
|
|
|
2018-06-14 05:30:43 +00:00
|
|
|
public function verifyEmail(Request $request)
|
|
|
|
{
|
|
|
|
return view('account.verify_email');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendVerifyEmail(Request $request)
|
|
|
|
{
|
2018-07-12 16:34:33 +00:00
|
|
|
$timeLimit = Carbon::now()->subDays(1)->toDateTimeString();
|
|
|
|
$recentAttempt = EmailVerification::whereUserId(Auth::id())
|
|
|
|
->where('created_at', '>', $timeLimit)->count();
|
|
|
|
$exists = EmailVerification::whereUserId(Auth::id())->count();
|
|
|
|
|
|
|
|
if($recentAttempt == 1 && $exists == 1) {
|
|
|
|
return redirect()->back()->with('error', 'A verification email has already been sent recently. Please check your email, or try again later.');
|
|
|
|
} elseif ($recentAttempt == 0 && $exists !== 0) {
|
|
|
|
// Delete old verification and send new one.
|
|
|
|
EmailVerification::whereUserId(Auth::id())->delete();
|
2018-06-14 05:30:43 +00:00
|
|
|
}
|
2018-06-14 07:42:47 +00:00
|
|
|
|
2018-07-12 16:34:33 +00:00
|
|
|
|
2018-06-14 05:30:43 +00:00
|
|
|
$user = User::whereNull('email_verified_at')->find(Auth::id());
|
|
|
|
$utoken = hash('sha512', $user->id);
|
|
|
|
$rtoken = str_random(40);
|
|
|
|
|
|
|
|
$verify = new EmailVerification;
|
|
|
|
$verify->user_id = $user->id;
|
|
|
|
$verify->email = $user->email;
|
|
|
|
$verify->user_token = $utoken;
|
|
|
|
$verify->random_token = $rtoken;
|
|
|
|
$verify->save();
|
|
|
|
|
|
|
|
Mail::to($user->email)->send(new ConfirmEmail($verify));
|
|
|
|
|
|
|
|
return redirect()->back()->with('status', 'Email verification email sent!');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
|
|
|
|
{
|
2018-07-12 16:34:33 +00:00
|
|
|
$verify = EmailVerification::where('user_token', $userToken)
|
|
|
|
->where('random_token', $randomToken)
|
2018-06-14 05:30:43 +00:00
|
|
|
->firstOrFail();
|
2018-07-12 16:34:33 +00:00
|
|
|
|
2018-06-14 05:30:43 +00:00
|
|
|
if(Auth::id() === $verify->user_id) {
|
|
|
|
$user = User::find(Auth::id());
|
|
|
|
$user->email_verified_at = Carbon::now();
|
|
|
|
$user->save();
|
2018-07-12 16:34:33 +00:00
|
|
|
return redirect('/');
|
2018-06-14 05:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 03:25:04 +00:00
|
|
|
public function fetchNotifications($id)
|
|
|
|
{
|
|
|
|
$key = config('cache.prefix') . ":user.{$id}.notifications";
|
|
|
|
$redis = Redis::connection();
|
|
|
|
$notifications = $redis->lrange($key, 0, 30);
|
|
|
|
if(empty($notifications)) {
|
|
|
|
$notifications = Notification::whereProfileId($id)
|
|
|
|
->orderBy('id','desc')->take(30)->get();
|
|
|
|
} else {
|
|
|
|
$notifications = $this->hydrateNotifications($notifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hydrateNotifications($keys)
|
|
|
|
{
|
|
|
|
$prefix = 'notification.';
|
2018-05-30 03:03:11 +00:00
|
|
|
$notifications = collect([]);
|
2018-05-27 03:25:04 +00:00
|
|
|
foreach($keys as $key) {
|
2018-05-30 03:03:11 +00:00
|
|
|
$notifications->push(Cache::get("{$prefix}{$key}"));
|
2018-05-27 03:25:04 +00:00
|
|
|
}
|
|
|
|
return $notifications;
|
2018-05-26 23:00:07 +00:00
|
|
|
}
|
2018-07-18 00:06:57 +00:00
|
|
|
|
2018-05-26 23:00:07 +00:00
|
|
|
}
|