Update AccountController

This commit is contained in:
Daniel Supernault 2019-07-10 23:10:00 -06:00
parent 506208f545
commit 213fba725e
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
2 changed files with 433 additions and 512 deletions

View file

@ -2,22 +2,21 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\EmailVerification;
use App\Follower;
use App\FollowRequest;
use App\Jobs\FollowPipeline\FollowPipeline;
use App\Mail\ConfirmEmail;
use App\Notification;
use App\Profile;
use App\User;
use App\UserFilter;
use Auth;
use Cache;
use Carbon\Carbon; use Carbon\Carbon;
use App\Mail\ConfirmEmail;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Mail; use {Auth, Cache, Mail, Redis};
use Redis;
use PragmaRX\Google2FA\Google2FA; use PragmaRX\Google2FA\Google2FA;
use App\Jobs\FollowPipeline\FollowPipeline;
use App\{
EmailVerification,
Follower,
FollowRequest,
Notification,
Profile,
User,
UserFilter
};
class AccountController extends Controller class AccountController extends Controller
{ {
@ -42,11 +41,14 @@ class AccountController extends Controller
'page' => 'nullable|min:1|max:3', 'page' => 'nullable|min:1|max:3',
'a' => 'nullable|alpha_dash', 'a' => 'nullable|alpha_dash',
]); ]);
$profile = Auth::user()->profile;
$action = $request->input('a'); $action = $request->input('a');
$allowed = ['like', 'follow']; $allowed = ['like', 'follow'];
$timeago = Carbon::now()->subMonths(3); $timeago = Carbon::now()->subMonths(3);
$profile = Auth::user()->profile;
$following = $profile->following->pluck('id'); $following = $profile->following->pluck('id');
$notifications = Notification::whereIn('actor_id', $following) $notifications = Notification::whereIn('actor_id', $following)
->whereIn('action', $allowed) ->whereIn('action', $allowed)
->where('actor_id', '<>', $profile->id) ->where('actor_id', '<>', $profile->id)
@ -75,7 +77,7 @@ class AccountController extends Controller
EmailVerification::whereUserId(Auth::id())->delete(); EmailVerification::whereUserId(Auth::id())->delete();
$user = User::whereNull('email_verified_at')->find(Auth::id()); $user = User::whereNull('email_verified_at')->find(Auth::id());
$utoken = str_random(40); $utoken = str_random(64);
$rtoken = str_random(128); $rtoken = str_random(128);
$verify = new EmailVerification(); $verify = new EmailVerification();
@ -93,12 +95,11 @@ class AccountController extends Controller
public function confirmVerifyEmail(Request $request, $userToken, $randomToken) public function confirmVerifyEmail(Request $request, $userToken, $randomToken)
{ {
$verify = EmailVerification::where('user_token', $userToken) $verify = EmailVerification::where('user_token', $userToken)
->where('created_at', '>', now()->subWeeks(2))
->where('random_token', $randomToken) ->where('random_token', $randomToken)
->firstOrFail(); ->firstOrFail();
if (Auth::id() === $verify->user_id && if (Auth::id() === $verify->user_id && $verify->user_token === $userToken && $verify->random_token === $randomToken) {
$verify->user_token === $userToken &&
$verify->random_token === $randomToken) {
$user = User::find(Auth::id()); $user = User::find(Auth::id());
$user->email_verified_at = Carbon::now(); $user->email_verified_at = Carbon::now();
$user->save(); $user->save();
@ -109,32 +110,6 @@ class AccountController extends Controller
} }
} }
public function fetchNotifications(int $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([]);
foreach ($keys as $key) {
$notifications->push(Cache::get("{$prefix}{$key}"));
}
return $notifications;
}
public function messages() public function messages()
{ {
return view('account.messages'); return view('account.messages');
@ -176,10 +151,6 @@ class AccountController extends Controller
$filterable['id'] = $profile->id; $filterable['id'] = $profile->id;
$filterable['type'] = $class; $filterable['type'] = $class;
break; break;
default:
// code...
break;
} }
$filter = UserFilter::firstOrCreate([ $filter = UserFilter::firstOrCreate([
@ -279,10 +250,6 @@ class AccountController extends Controller
Follower::whereProfileId($profile->id)->whereFollowingId($user->id)->delete(); Follower::whereProfileId($profile->id)->whereFollowingId($user->id)->delete();
Notification::whereProfileId($user->id)->whereActorId($profile->id)->delete(); Notification::whereProfileId($user->id)->whereActorId($profile->id)->delete();
break; break;
default:
// code...
break;
} }
$filter = UserFilter::firstOrCreate([ $filter = UserFilter::firstOrCreate([
@ -455,7 +422,6 @@ class AccountController extends Controller
$codes = json_decode($backupCodes, true); $codes = json_decode($backupCodes, true);
foreach ($codes as $c) { foreach ($codes as $c) {
if(hash_equals($c, $code)) { if(hash_equals($c, $code)) {
// remove code
$codes = array_flatten(array_diff($codes, [$code])); $codes = array_flatten(array_diff($codes, [$code]));
$user->{'2fa_backup_codes'} = json_encode($codes); $user->{'2fa_backup_codes'} = json_encode($codes);
$user->save(); $user->save();
@ -472,6 +438,5 @@ class AccountController extends Controller
public function accountRestored(Request $request) public function accountRestored(Request $request)
{ {
//
} }
} }

View file

@ -50,38 +50,7 @@ class InternalApiController extends Controller
// deprecated // deprecated
public function discover(Request $request) public function discover(Request $request)
{ {
$profile = Auth::user()->profile; return;
$pid = $profile->id;
$following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(60), function() use ($pid) {
return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
});
$filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(60), function() use($pid) {
return UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
});
$following = array_merge($following, $filters);
$posts = Status::select('id', 'caption', 'profile_id')
->whereHas('media')
->whereIsNsfw(false)
->whereVisibility('public')
->whereNotIn('profile_id', $following)
->with('media')
->orderBy('created_at', 'desc')
->take(21)
->get();
$res = [
'posts' => $posts->map(function($post) {
return [
'url' => $post->url(),
'thumb' => $post->thumb(),
];
})
];
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
} }
public function discoverPosts(Request $request) public function discoverPosts(Request $request)
@ -155,22 +124,9 @@ class InternalApiController extends Controller
return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT); return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
} }
public function notificationMarkAllRead(Request $request)
{
$profile = Auth::user()->profile;
$notifications = Notification::whereProfileId($profile->id)->get();
foreach($notifications as $n) {
$n->read_at = Carbon::now();
$n->save();
}
return;
}
public function statusReplies(Request $request, int $id) public function statusReplies(Request $request, int $id)
{ {
$parent = Status::findOrFail($id); $parent = Status::whereScope('public')->findOrFail($id);
$children = Status::whereInReplyToId($parent->id) $children = Status::whereInReplyToId($parent->id)
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')