pixelfed/app/Http/Controllers/AccountController.php

99 lines
2.9 KiB
PHP
Raw Normal View History

2018-05-26 23:00:07 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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)
{
$this->validate($request, [
'page' => 'nullable|min:1|max:3'
]);
2018-05-27 03:25:04 +00:00
$profile = Auth::user()->profile;
$timeago = Carbon::now()->subMonths(6);
2018-06-04 08:16:33 +00:00
$notifications = Notification::whereProfileId($profile->id)
->whereDate('created_at', '>', $timeago)
->orderBy('id','desc')
->take(30)
->simplePaginate();
2018-05-27 03:25:04 +00:00
return view('account.activity', 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)
{
if(EmailVerification::whereUserId(Auth::id())->count() !== 0) {
return redirect()->back()->with('status', 'A verification email has already been sent! Please check your email.');
}
$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)
{
$verify = EmailVerification::where(DB::raw('BINARY `user_token`'), $userToken)
->where(DB::raw('BINARY `random_token`'), $randomToken)
->firstOrFail();
if(Auth::id() === $verify->user_id) {
$user = User::find(Auth::id());
$user->email_verified_at = Carbon::now();
$user->save();
return redirect('/timeline');
}
}
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.';
$notifications = collect([]);
2018-05-27 03:25:04 +00:00
foreach($keys as $key) {
$notifications->push(Cache::get("{$prefix}{$key}"));
2018-05-27 03:25:04 +00:00
}
return $notifications;
2018-05-26 23:00:07 +00:00
}
}