From 01bc5af076c845b69de24efb7f63b53d70a12bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E6=A5=BD=E5=9D=82=20=E7=99=BD?= Date: Sun, 2 Jun 2024 12:50:33 +0800 Subject: [PATCH] mail read --- app/Console/Commands/MarkTimeoutMail.php | 38 +++++++++++++++++ app/Console/Kernel.php | 4 +- app/Http/Controllers/MailController.php | 41 +++++++++++-------- ..._02_043733_add_is_read_into_mail_table.php | 26 ++++++++++++ resources/views/welcome.blade.php | 10 ++--- 5 files changed, 95 insertions(+), 24 deletions(-) create mode 100644 app/Console/Commands/MarkTimeoutMail.php create mode 100644 database/migrations/2024_06_02_043733_add_is_read_into_mail_table.php diff --git a/app/Console/Commands/MarkTimeoutMail.php b/app/Console/Commands/MarkTimeoutMail.php new file mode 100644 index 0000000..6256aa7 --- /dev/null +++ b/app/Console/Commands/MarkTimeoutMail.php @@ -0,0 +1,38 @@ +subSeconds(300)], + ['is_read', '=', 0], + ])->update([ + 'is_read' => 1, + ]); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e6b9960..8457aa0 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -12,7 +12,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule): void { - // $schedule->command('inspire')->hourly(); + $schedule->command('app:mark-timeout-mail')->minutely(); } /** @@ -20,7 +20,7 @@ protected function schedule(Schedule $schedule): void */ protected function commands(): void { - $this->load(__DIR__.'/Commands'); + $this->load(__DIR__ . '/Commands'); require base_path('routes/console.php'); } diff --git a/app/Http/Controllers/MailController.php b/app/Http/Controllers/MailController.php index 359170e..9f7b6f3 100644 --- a/app/Http/Controllers/MailController.php +++ b/app/Http/Controllers/MailController.php @@ -2,13 +2,13 @@ namespace App\Http\Controllers; +use App\Http\Controllers\Controller; use App\Models\Mail; -use Illuminate\Support\Str; use Illuminate\Http\Request; use Illuminate\Support\Carbon; -use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Str; class MailController extends Controller { @@ -17,8 +17,8 @@ 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(json_encode(['id' => 0, 'time' => time()])); + $rand_prefix = strtolower(Str::random(8)); + $key = Crypt::encryptString(json_encode(['time' => time()])); return view('welcome', [ 'domain_list' => $domain_list, 'block_prefix' => $block_prefix, @@ -64,20 +64,26 @@ function ($attribute, $value, $fail) { return abort(400); } - $where = []; - $to_hash = hash('sha256', $email); - $where[] = ['to_hash', '=', $to_hash]; + $where = [ + 'to_hash' => hash('sha256', $email), + 'is_read' => 0, + ]; + // $to_hash = hash('sha256', $email); + // $where[] = ['to_hash', '=', $to_hash]; try { $key = Crypt::decryptString($key); $key = json_decode($key, true); - if (!empty($key['id'])) { - $where[] = ['id', '>', $key['id']]; - } elseif (time() - $key['time'] < 300) { - $where[] = ['received_at', '>', Carbon::parse($key['time'])]; - } else { - $where[] = ['received_at', '>', Carbon::now()->subSeconds(60)]; + if (time() - $key['time'] > 300) { + throw new Exception("The token has expired.", 400); } + // if (!empty($key['id'])) { + // $where[] = ['id', '>', $key['id']]; + // } elseif (time() - $key['time'] < 300) { + // $where[] = ['received_at', '>', Carbon::parse($key['time'])]; + // } else { + // $where[] = ['received_at', '>', Carbon::now()->subSeconds(60)]; + // } } catch (\Throwable $th) { return abort(400, $th->getMessage()); } @@ -94,10 +100,7 @@ function ($attribute, $value, $fail) { ->orderBy('received_at', 'asc') ->get(); - $key = ['id' => $key['id'], 'time' => time()]; - if ($new_email_list->count() > 0) { - $key['id'] = $new_email_list->last()->key; - } + $key = ['time' => time()]; foreach ($new_email_list as $value) { $value->key = Crypt::encryptString($value->key); @@ -142,6 +145,7 @@ public function put(Request $request) $mail->from_addr = $from_addr; $mail->from_protocol = $from_protocol; $mail->received_at = Carbon::parse($received_at); + $mail->is_read = 0; $mail->save(); return response()->json([ @@ -168,6 +172,9 @@ public function info(Request $request, $key) return abort(404); } + $mail_info->is_read = 1; + $mail_info->save(); + $title = str_replace(["\n", "\r", "\t"], '', strip_tags($mail_info->title)); $body = preg_replace('/]*>(.*?)<\/script>/is', "", $mail_info->body); diff --git a/database/migrations/2024_06_02_043733_add_is_read_into_mail_table.php b/database/migrations/2024_06_02_043733_add_is_read_into_mail_table.php new file mode 100644 index 0000000..37d7ebb --- /dev/null +++ b/database/migrations/2024_06_02_043733_add_is_read_into_mail_table.php @@ -0,0 +1,26 @@ +unsignedTinyInteger('is_read')->default(0)->index(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + $table->dropColumn('is_read'); + } +}; diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index e313d0d..1b8063c 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -7,7 +7,7 @@ {{config('app.name')}} - + @@ -192,7 +192,7 @@ - + @@ -209,7 +209,7 @@ function isValidDomain(domain) { console.log(domain); - var pattern = /^(?!:\/\/)([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.[a-zA-Z]{2,11}$/; + var pattern = /^(?!:\/\/)([a-z0-9-]+\.)*[a-z0-9-]+\.[a-z]{2,11}$/; return pattern.test(domain); } @@ -308,6 +308,7 @@ function fetchMail() { }); $('#inputText').on('change keyup', function () { + this.value = this.value.toLowerCase().replace(/[^a-z0-9]/g, ''); let textLength = $('#inputText').val().length; if (textLength < 5 || textLength > 32) { $('#inputText').addClass('is-invalid'); @@ -409,7 +410,6 @@ function fetchMail() { $('#myModal').modal('show'); - } catch (e) { console.error('Error parsing API response or decoding base64:', e); } @@ -424,4 +424,4 @@ function fetchMail() { - \ No newline at end of file +