diff --git a/app/Http/Controllers/MailController.php b/app/Http/Controllers/MailController.php index 0fac348..b51500d 100644 --- a/app/Http/Controllers/MailController.php +++ b/app/Http/Controllers/MailController.php @@ -18,7 +18,7 @@ public function index(Request $request) $domain_list = config('mail.domain_list'); $block_prefix = config('mail.block_prefix'); $rand_prefix = Str::random(8); - $key = Crypt::encryptString(Carbon::now()->format('Y-m-d H:i:s')); + $key = Crypt::encryptString(json_encode(['id' => 0, 'time' => time()])); return view('welcome', [ 'domain_list' => $domain_list, 'block_prefix' => $block_prefix, @@ -63,32 +63,23 @@ function ($attribute, $value, $fail) { return abort(400); } + $where = []; + $to_hash = hash('sha256', $email); + $where[] = ['to_hash', '=', $to_hash]; try { $key = Crypt::decryptString($key); - $key = Carbon::parse($key); + $key = json_decode($key, true); + if (time() - $key['time'] < 300 && !empty($key['id'])) { + $where[] = ['id', '>', $key['id']]; + } else { + $where[] = ['received_at', '>', Carbon::now()]; + } } catch (\Throwable $th) { return abort(400); } - // $now_time = Carbon::now(); - // if ($key->diffInMinutes($now_time) > 5) { - // $key = $now_time; - // } - - $to_hash = hash('sha256', $email); - - $max_received_at = Mail::where([ - ['to_hash', '=', $to_hash], - ['received_at', '>', $key], - ]) - ->orderBy('received_at', 'desc') - ->value("received_at") ?? $key; - - $new_email_list = Mail::where([ - ['to_hash', '=', $to_hash], - ['received_at', '>', $key], - ]) + $new_email_list = Mail::where($where) ->select([ 'id as key', 'from', @@ -100,7 +91,12 @@ function ($attribute, $value, $fail) { ->orderBy('received_at', 'asc') ->get(); - foreach ($new_email_list as $key => $value) { + $key = ['id' => $key['id'], 'time' => time()]; + if ($new_email_list->count() > 0) { + $key['id'] = $new_email_list->last()->key; + } + + foreach ($new_email_list as $value) { $value->key = Crypt::encryptString($value->key); } @@ -108,8 +104,8 @@ function ($attribute, $value, $fail) { 'code' => 1, 'msg' => '', 'data' => [ - 'list' => $new_email_list, - 'new_key' => Crypt::encryptString(Carbon::parse($max_received_at)->format('Y-m-d H:i:s')), + 'list' => $new_email_list, + 'new_key' => Crypt::encryptString(json_encode($key)), ], ]); } @@ -157,6 +153,10 @@ public function info(Request $request, $key) return abort(400); } + if (empty(intval($id))) { + return abort(400); + } + $mail_info = Mail::find($id); if (empty($mail_info)) { return abort(404);